Skip to content

Commit 069e9b4

Browse files
authored
[RISCV] Overwrite cpu target features for full arch string in target attribute (llvm#77426)
This patch reworks RISCVTargetInfo::initFeatureMap to fix the issue described in llvm#74889 (review) (and is an alternative to llvm#75804) When a full arch string is specified, a "full" list of extensions is now passed after the __RISCV_TargetAttrNeedOverride marker feature, which includes any negative features that disable ISA extensions. In initFeatureMap, there are now two code paths: 1. If the arch string was overriden, use the "full" list of override features, only adding back any non-isa features that were specified. Using the full list of positive and negative features will mean that the target-cpu will have no effect on the final arch, e.g. __attribute__((target("arch=rv64i"))) with -mcpu=sifive-x280 will have the features for rv64i, not a mix of both. 2. Otherwise, parse and *append* the list of implied features. By appending, we turn back on any features that might have been disabled by a negative extension, i.e. this handles the case fixed in llvm#74889.
1 parent 00d6232 commit 069e9b4

File tree

2 files changed

+31
-56
lines changed

2 files changed

+31
-56
lines changed

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -241,39 +241,6 @@ ArrayRef<Builtin::Info> RISCVTargetInfo::getTargetBuiltins() const {
241241
clang::RISCV::LastTSBuiltin - Builtin::FirstTSBuiltin);
242242
}
243243

244-
static std::vector<std::string>
245-
collectNonISAExtFeature(ArrayRef<std::string> FeaturesNeedOverride, int XLen) {
246-
std::vector<std::string> NonISAExtFeatureVec;
247-
248-
auto IsNonISAExtFeature = [](const std::string &Feature) {
249-
assert(Feature.size() > 1 && (Feature[0] == '+' || Feature[0] == '-'));
250-
StringRef Ext = StringRef(Feature).drop_front(); // drop the +/-
251-
return !llvm::RISCVISAInfo::isSupportedExtensionFeature(Ext);
252-
};
253-
llvm::copy_if(FeaturesNeedOverride, std::back_inserter(NonISAExtFeatureVec),
254-
IsNonISAExtFeature);
255-
256-
return NonISAExtFeatureVec;
257-
}
258-
259-
static std::vector<std::string>
260-
resolveTargetAttrOverride(const std::vector<std::string> &FeaturesVec,
261-
int XLen) {
262-
auto I = llvm::find(FeaturesVec, "__RISCV_TargetAttrNeedOverride");
263-
if (I == FeaturesVec.end())
264-
return FeaturesVec;
265-
266-
ArrayRef<std::string> FeaturesNeedOverride(&*FeaturesVec.begin(), &*I);
267-
std::vector<std::string> NonISAExtFeature =
268-
collectNonISAExtFeature(FeaturesNeedOverride, XLen);
269-
270-
std::vector<std::string> ResolvedFeature(++I, FeaturesVec.end());
271-
ResolvedFeature.insert(ResolvedFeature.end(), NonISAExtFeature.begin(),
272-
NonISAExtFeature.end());
273-
274-
return ResolvedFeature;
275-
}
276-
277244
bool RISCVTargetInfo::initFeatureMap(
278245
llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
279246
const std::vector<std::string> &FeaturesVec) const {
@@ -287,10 +254,27 @@ bool RISCVTargetInfo::initFeatureMap(
287254
Features["32bit"] = true;
288255
}
289256

290-
std::vector<std::string> NewFeaturesVec =
291-
resolveTargetAttrOverride(FeaturesVec, XLen);
257+
// If a target attribute specified a full arch string, override all the ISA
258+
// extension target features.
259+
const auto I = llvm::find(FeaturesVec, "__RISCV_TargetAttrNeedOverride");
260+
if (I != FeaturesVec.end()) {
261+
std::vector<std::string> OverrideFeatures(std::next(I), FeaturesVec.end());
262+
263+
// Add back any non ISA extension features, e.g. +relax.
264+
auto IsNonISAExtFeature = [](StringRef Feature) {
265+
assert(Feature.size() > 1 && (Feature[0] == '+' || Feature[0] == '-'));
266+
StringRef Ext = Feature.substr(1); // drop the +/-
267+
return !llvm::RISCVISAInfo::isSupportedExtensionFeature(Ext);
268+
};
269+
llvm::copy_if(llvm::make_range(FeaturesVec.begin(), I),
270+
std::back_inserter(OverrideFeatures), IsNonISAExtFeature);
292271

293-
auto ParseResult = llvm::RISCVISAInfo::parseFeatures(XLen, NewFeaturesVec);
272+
return TargetInfo::initFeatureMap(Features, Diags, CPU, OverrideFeatures);
273+
}
274+
275+
// Otherwise, parse the features and add any implied extensions.
276+
std::vector<std::string> AllFeatures = FeaturesVec;
277+
auto ParseResult = llvm::RISCVISAInfo::parseFeatures(XLen, FeaturesVec);
294278
if (!ParseResult) {
295279
std::string Buffer;
296280
llvm::raw_string_ostream OutputErrMsg(Buffer);
@@ -301,21 +285,9 @@ bool RISCVTargetInfo::initFeatureMap(
301285
return false;
302286
}
303287

304-
// RISCVISAInfo makes implications for ISA features
305-
std::vector<std::string> ImpliedFeatures = (*ParseResult)->toFeatures();
306-
307-
// parseFeatures normalizes the feature set by dropping any explicit
308-
// negatives, and non-extension features. We need to preserve the later
309-
// for correctness and want to preserve the former for consistency.
310-
for (auto &Feature : NewFeaturesVec) {
311-
StringRef ExtName = Feature;
312-
assert(ExtName.size() > 1 && (ExtName[0] == '+' || ExtName[0] == '-'));
313-
ExtName = ExtName.drop_front(1); // Drop '+' or '-'
314-
if (!llvm::is_contained(ImpliedFeatures, ("+" + ExtName).str()) &&
315-
!llvm::is_contained(ImpliedFeatures, ("-" + ExtName).str()))
316-
ImpliedFeatures.push_back(Feature);
317-
}
318-
return TargetInfo::initFeatureMap(Features, Diags, CPU, ImpliedFeatures);
288+
// Append all features, not just new ones, so we override any negatives.
289+
llvm::append_range(AllFeatures, (*ParseResult)->toFeatures());
290+
return TargetInfo::initFeatureMap(Features, Diags, CPU, AllFeatures);
319291
}
320292

321293
std::optional<std::pair<unsigned, unsigned>>
@@ -424,7 +396,10 @@ static void handleFullArchString(StringRef FullArchStr,
424396
// Forward the invalid FullArchStr.
425397
Features.push_back("+" + FullArchStr.str());
426398
} else {
427-
std::vector<std::string> FeatStrings = (*RII)->toFeatures();
399+
// Append a full list of features, including any negative extensions so that
400+
// we override the CPU's features.
401+
std::vector<std::string> FeatStrings =
402+
(*RII)->toFeatures(/* AddAllExtensions */ true);
428403
Features.insert(Features.end(), FeatStrings.begin(), FeatStrings.end());
429404
}
430405
}

clang/test/CodeGen/RISCV/riscv-func-attr-target.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ __attribute__((target("cpu=sifive-u54"))) void testAttrCpuOnly() {}
4040
// CHECK: attributes #1 = { {{.*}}"target-cpu"="rocket-rv64" "target-features"="+64bit,+a,+d,+f,+m,+save-restore,+v,+zicsr,+zifencei,+zve32f,+zve32x,+zve64d,+zve64f,+zve64x,+zvl128b,+zvl32b,+zvl64b,-relax,-zbb,-zfa" "tune-cpu"="generic-rv64" }
4141
// CHECK: attributes #2 = { {{.*}}"target-features"="+64bit,+a,+m,+save-restore,+zbb,+zifencei,-relax,-zfa" }
4242
// CHECK: attributes #3 = { {{.*}}"target-features"="+64bit,+a,+d,+experimental-zicond,+f,+m,+save-restore,+v,+zbb,+zicsr,+zifencei,+zve32f,+zve32x,+zve64d,+zve64f,+zve64x,+zvl128b,+zvl32b,+zvl64b,-relax,-zfa" }
43-
// CHECK: attributes #4 = { {{.*}}"target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zbb,+zicsr,+zifencei,-relax" }
44-
// CHECK: attributes #5 = { {{.*}}"target-features"="+64bit,+m,+save-restore,-relax" }
43+
// CHECK: attributes #4 = { {{.*}}"target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zbb,+zicsr,+zifencei,-e,-experimental-zacas,-experimental-zcmop,-experimental-zfbfmin,-experimental-zicfilp,-experimental-zicfiss,-experimental-zicond,-experimental-zimop,-experimental-ztso,-experimental-zvfbfmin,-experimental-zvfbfwma,-h,-relax,-smaia,-ssaia,-svinval,-svnapot,-svpbmt,-v,-xcvalu,-xcvbi,-xcvbitmanip,-xcvelw,-xcvmac,-xcvmem,-xcvsimd,-xsfvcp,-xsfvfnrclipxfqf,-xsfvfwmaccqqq,-xsfvqmaccdod,-xsfvqmaccqoq,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-zawrs,-zba,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmp,-zcmt,-zdinx,-zfa,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zicbom,-zicbop,-zicboz,-zicntr,-zihintntl,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-zmmul,-zvbb,-zvbc,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfh,-zvfhmin,-zvkb,-zvkg,-zvkn,-zvknc,-zvkned,-zvkng,-zvknha,-zvknhb,-zvks,-zvksc,-zvksed,-zvksg,-zvksh,-zvkt,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
44+
// CHECK: attributes #5 = { {{.*}}"target-features"="+64bit,+m,+save-restore,-a,-c,-d,-e,-experimental-zacas,-experimental-zcmop,-experimental-zfbfmin,-experimental-zicfilp,-experimental-zicfiss,-experimental-zicond,-experimental-zimop,-experimental-ztso,-experimental-zvfbfmin,-experimental-zvfbfwma,-f,-h,-relax,-smaia,-ssaia,-svinval,-svnapot,-svpbmt,-v,-xcvalu,-xcvbi,-xcvbitmanip,-xcvelw,-xcvmac,-xcvmem,-xcvsimd,-xsfvcp,-xsfvfnrclipxfqf,-xsfvfwmaccqqq,-xsfvqmaccdod,-xsfvqmaccqoq,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmp,-zcmt,-zdinx,-zfa,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zicbom,-zicbop,-zicboz,-zicntr,-zicsr,-zifencei,-zihintntl,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-zmmul,-zvbb,-zvbc,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfh,-zvfhmin,-zvkb,-zvkg,-zvkn,-zvknc,-zvkned,-zvkng,-zvknha,-zvknhb,-zvks,-zvksc,-zvksed,-zvksg,-zvksh,-zvkt,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
4545
// CHECK: attributes #6 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+a,+m,+save-restore,+zbb,+zifencei,-relax,-zfa" }
46-
// CHECK: attributes #7 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+m,+save-restore,-relax" }
47-
// CHECK: attributes #8 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zicsr,+zifencei,-relax" }
46+
// CHECK: attributes #7 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+m,+save-restore,-a,-c,-d,-e,-experimental-zacas,-experimental-zcmop,-experimental-zfbfmin,-experimental-zicfilp,-experimental-zicfiss,-experimental-zicond,-experimental-zimop,-experimental-ztso,-experimental-zvfbfmin,-experimental-zvfbfwma,-f,-h,-relax,-smaia,-ssaia,-svinval,-svnapot,-svpbmt,-v,-xcvalu,-xcvbi,-xcvbitmanip,-xcvelw,-xcvmac,-xcvmem,-xcvsimd,-xsfvcp,-xsfvfnrclipxfqf,-xsfvfwmaccqqq,-xsfvqmaccdod,-xsfvqmaccqoq,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmp,-zcmt,-zdinx,-zfa,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zicbom,-zicbop,-zicboz,-zicntr,-zicsr,-zifencei,-zihintntl,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-zmmul,-zvbb,-zvbc,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfh,-zvfhmin,-zvkb,-zvkg,-zvkn,-zvknc,-zvkned,-zvkng,-zvknha,-zvknhb,-zvks,-zvksc,-zvksed,-zvksg,-zvksh,-zvkt,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
47+
// CHECK: attributes #8 = { {{.*}}"target-cpu"="sifive-u54" "target-features"="+64bit,+a,+c,+d,+f,+m,+save-restore,+zicsr,+zifencei,-e,-experimental-zacas,-experimental-zcmop,-experimental-zfbfmin,-experimental-zicfilp,-experimental-zicfiss,-experimental-zicond,-experimental-zimop,-experimental-ztso,-experimental-zvfbfmin,-experimental-zvfbfwma,-h,-relax,-smaia,-ssaia,-svinval,-svnapot,-svpbmt,-v,-xcvalu,-xcvbi,-xcvbitmanip,-xcvelw,-xcvmac,-xcvmem,-xcvsimd,-xsfvcp,-xsfvfnrclipxfqf,-xsfvfwmaccqqq,-xsfvqmaccdod,-xsfvqmaccqoq,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmp,-zcmt,-zdinx,-zfa,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zicbom,-zicbop,-zicboz,-zicntr,-zihintntl,-zihintpause,-zihpm,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-zmmul,-zvbb,-zvbc,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfh,-zvfhmin,-zvkb,-zvkg,-zvkn,-zvknc,-zvkned,-zvkng,-zvknha,-zvknhb,-zvks,-zvksc,-zvksed,-zvksg,-zvksh,-zvkt,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }

0 commit comments

Comments
 (0)