Skip to content

Commit 3ab64c5

Browse files
authored
[NFC][Clang][FMV] Make FMV priority data type future proof. (#150079)
FMV priority is the returned value of a polymorphic function. On RISC-V and X86 targets a 32-bit value is enough. On AArch64 we currently need 64 bits and we will soon exceed that. APInt seems to be a suitable replacement for uint64_t, presumably with minimal compile time overhead. It allows bit manipulation, comparison and variable bit width.
1 parent 36c37b0 commit 3ab64c5

File tree

18 files changed

+45
-40
lines changed

18 files changed

+45
-40
lines changed

clang/include/clang/Basic/TargetInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,8 +1551,8 @@ class TargetInfo : public TransferrableTargetInfo,
15511551

15521552
// Return the target-specific priority for features/cpus/vendors so
15531553
// that they can be properly sorted for checking.
1554-
virtual uint64_t getFMVPriority(ArrayRef<StringRef> Features) const {
1555-
return 0;
1554+
virtual llvm::APInt getFMVPriority(ArrayRef<StringRef> Features) const {
1555+
return llvm::APInt::getZero(32);
15561556
}
15571557

15581558
// Validate the contents of the __builtin_cpu_is(const char*)

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,8 @@ AArch64TargetInfo::getVScaleRange(const LangOptions &LangOpts,
786786
return std::nullopt;
787787
}
788788

789-
uint64_t AArch64TargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
789+
llvm::APInt
790+
AArch64TargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
790791
return llvm::AArch64::getFMVPriority(Features);
791792
}
792793

clang/lib/Basic/Targets/AArch64.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class LLVM_LIBRARY_VISIBILITY AArch64TargetInfo : public TargetInfo {
152152
void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
153153
bool setCPU(const std::string &Name) override;
154154

155-
uint64_t getFMVPriority(ArrayRef<StringRef> Features) const override;
155+
llvm::APInt getFMVPriority(ArrayRef<StringRef> Features) const override;
156156

157157
bool useFP16ConversionIntrinsics() const override {
158158
return false;

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ ParsedTargetAttr RISCVTargetInfo::parseTargetAttr(StringRef Features) const {
568568
return Ret;
569569
}
570570

571-
uint64_t RISCVTargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
571+
llvm::APInt
572+
RISCVTargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
572573
// Priority is explicitly specified on RISC-V unlike on other targets, where
573574
// it is derived by all the features of a specific version. Therefore if a
574575
// feature contains the priority string, then return it immediately.
@@ -580,12 +581,12 @@ uint64_t RISCVTargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
580581
Feature = RHS;
581582
else
582583
continue;
583-
uint64_t Priority;
584+
unsigned Priority;
584585
if (!Feature.getAsInteger(0, Priority))
585-
return Priority;
586+
return llvm::APInt(32, Priority);
586587
}
587588
// Default Priority is zero.
588-
return 0;
589+
return llvm::APInt::getZero(32);
589590
}
590591

591592
TargetInfo::CallingConvCheckResult

clang/lib/Basic/Targets/RISCV.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class RISCVTargetInfo : public TargetInfo {
123123
void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const override;
124124
bool supportsTargetAttributeTune() const override { return true; }
125125
ParsedTargetAttr parseTargetAttr(StringRef Str) const override;
126-
uint64_t getFMVPriority(ArrayRef<StringRef> Features) const override;
126+
llvm::APInt getFMVPriority(ArrayRef<StringRef> Features) const override;
127127

128128
std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
129129
return std::make_pair(32, 32);

clang/lib/Basic/Targets/X86.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,8 @@ static llvm::X86::ProcessorFeatures getFeature(StringRef Name) {
13901390
// correct, so it asserts if the value is out of range.
13911391
}
13921392

1393-
uint64_t X86TargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
1394-
auto getPriority = [](StringRef Feature) -> uint64_t {
1393+
llvm::APInt X86TargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
1394+
auto getPriority = [](StringRef Feature) -> unsigned {
13951395
// Valid CPUs have a 'key feature' that compares just better than its key
13961396
// feature.
13971397
using namespace llvm::X86;
@@ -1405,11 +1405,11 @@ uint64_t X86TargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
14051405
return getFeaturePriority(getFeature(Feature)) << 1;
14061406
};
14071407

1408-
uint64_t Priority = 0;
1408+
unsigned Priority = 0;
14091409
for (StringRef Feature : Features)
14101410
if (!Feature.empty())
14111411
Priority = std::max(Priority, getPriority(Feature));
1412-
return Priority;
1412+
return llvm::APInt(32, Priority);
14131413
}
14141414

14151415
bool X86TargetInfo::validateCPUSpecificCPUDispatch(StringRef Name) const {

clang/lib/Basic/Targets/X86.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
388388
return CPU != llvm::X86::CK_None;
389389
}
390390

391-
uint64_t getFMVPriority(ArrayRef<StringRef> Features) const override;
391+
llvm::APInt getFMVPriority(ArrayRef<StringRef> Features) const override;
392392

393393
bool setFPMath(StringRef Name) override;
394394

clang/lib/CodeGen/ABIInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ void ABIInfo::appendAttributeMangling(StringRef AttrStr,
218218
// only have "+" prefixes here.
219219
assert(LHS.starts_with("+") && RHS.starts_with("+") &&
220220
"Features should always have a prefix.");
221-
return TI.getFMVPriority({LHS.substr(1)}) >
222-
TI.getFMVPriority({RHS.substr(1)});
221+
return TI.getFMVPriority({LHS.substr(1)})
222+
.ugt(TI.getFMVPriority({RHS.substr(1)}));
223223
});
224224

225225
bool IsFirst = true;

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,8 +4418,9 @@ void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
44184418
static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
44194419
llvm::Function *NewFn);
44204420

4421-
static uint64_t getFMVPriority(const TargetInfo &TI,
4422-
const CodeGenFunction::FMVResolverOption &RO) {
4421+
static llvm::APInt
4422+
getFMVPriority(const TargetInfo &TI,
4423+
const CodeGenFunction::FMVResolverOption &RO) {
44234424
llvm::SmallVector<StringRef, 8> Features{RO.Features};
44244425
if (RO.Architecture)
44254426
Features.push_back(*RO.Architecture);
@@ -4544,7 +4545,7 @@ void CodeGenModule::emitMultiVersionFunctions() {
45444545
llvm::stable_sort(
45454546
Options, [&TI](const CodeGenFunction::FMVResolverOption &LHS,
45464547
const CodeGenFunction::FMVResolverOption &RHS) {
4547-
return getFMVPriority(TI, LHS) > getFMVPriority(TI, RHS);
4548+
return getFMVPriority(TI, LHS).ugt(getFMVPriority(TI, RHS));
45484549
});
45494550
CodeGenFunction CGF(*this);
45504551
CGF.EmitMultiVersionResolver(ResolverFunc, Options);

clang/lib/CodeGen/TargetBuiltins/ARM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8112,7 +8112,7 @@ Value *CodeGenFunction::EmitAArch64CpuSupports(const CallExpr *E) {
81128112

81138113
llvm::Value *
81148114
CodeGenFunction::EmitAArch64CpuSupports(ArrayRef<StringRef> FeaturesStrs) {
8115-
uint64_t FeaturesMask = llvm::AArch64::getCpuSupportsMask(FeaturesStrs);
8115+
llvm::APInt FeaturesMask = llvm::AArch64::getCpuSupportsMask(FeaturesStrs);
81168116
Value *Result = Builder.getTrue();
81178117
if (FeaturesMask != 0) {
81188118
// Get features from structure in runtime library
@@ -8128,7 +8128,7 @@ CodeGenFunction::EmitAArch64CpuSupports(ArrayRef<StringRef> FeaturesStrs) {
81288128
{ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 0)});
81298129
Value *Features = Builder.CreateAlignedLoad(Int64Ty, CpuFeatures,
81308130
CharUnits::fromQuantity(8));
8131-
Value *Mask = Builder.getInt64(FeaturesMask);
8131+
Value *Mask = Builder.getInt(FeaturesMask.trunc(64));
81328132
Value *Bitset = Builder.CreateAnd(Features, Mask);
81338133
Value *Cmp = Builder.CreateICmpEQ(Bitset, Mask);
81348134
Result = Builder.CreateAnd(Result, Cmp);

0 commit comments

Comments
 (0)