Skip to content

Commit b0276ec

Browse files
authored
[RISCV][NFC] Reimplementation of target attribute override mechanism (#106680)
This patch aims to replace the target attribute override mechanism based on `__RISCV_TargetAttrNeedOverride` with the insertion of several negative target features When the target attribute uses the full architecture string ("arch=rv64gc") or specifies the CPU ("cpu=rocket-rv64") as the version, it will override the module-level target feature. Currently, this mechanism is implemented by inserting `__RISCV_TargetAttrNeedOverride` as a dummy target feature immediately before the target attribute's feature. ``` module target features + __RISCV_TargetAttrNeedOverride + target attribute's feature ``` The RISCVTargetInfo::initFeatureMap function will remove the "module target features" and use only the "target attribute's features". This patch changes the process as follows: ``` module target features + negative target feature for all supported extension + target attribute's feature ``` The `module target features` will be disable by `negative target feature for all supported extension` in `TargetInfo::initFeatureMap`
1 parent 9e86d4f commit b0276ec

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,6 @@ bool RISCVTargetInfo::initFeatureMap(
255255
Features["32bit"] = true;
256256
}
257257

258-
// If a target attribute specified a full arch string, override all the ISA
259-
// extension target features.
260-
const auto I = llvm::find(FeaturesVec, "__RISCV_TargetAttrNeedOverride");
261-
if (I != FeaturesVec.end()) {
262-
std::vector<std::string> OverrideFeatures(std::next(I), FeaturesVec.end());
263-
264-
// Add back any non ISA extension features, e.g. +relax.
265-
auto IsNonISAExtFeature = [](StringRef Feature) {
266-
assert(Feature.size() > 1 && (Feature[0] == '+' || Feature[0] == '-'));
267-
StringRef Ext = Feature.substr(1); // drop the +/-
268-
return !llvm::RISCVISAInfo::isSupportedExtensionFeature(Ext);
269-
};
270-
llvm::copy_if(llvm::make_range(FeaturesVec.begin(), I),
271-
std::back_inserter(OverrideFeatures), IsNonISAExtFeature);
272-
273-
return TargetInfo::initFeatureMap(Features, Diags, CPU, OverrideFeatures);
274-
}
275-
276-
// Otherwise, parse the features and add any implied extensions.
277258
std::vector<std::string> AllFeatures = FeaturesVec;
278259
auto ParseResult = llvm::RISCVISAInfo::parseFeatures(XLen, FeaturesVec);
279260
if (!ParseResult) {
@@ -389,9 +370,20 @@ void RISCVTargetInfo::fillValidTuneCPUList(
389370
llvm::RISCV::fillValidTuneCPUArchList(Values, Is64Bit);
390371
}
391372

373+
static void populateNegativeRISCVFeatures(std::vector<std::string> &Features) {
374+
auto RII = llvm::RISCVISAInfo::parseArchString(
375+
"rv64i", /* EnableExperimentalExtension */ true);
376+
377+
if (llvm::errorToBool(RII.takeError()))
378+
llvm_unreachable("unsupport rv64i");
379+
380+
std::vector<std::string> FeatStrings =
381+
(*RII)->toFeatures(/* AddAllExtensions */ true);
382+
Features.insert(Features.end(), FeatStrings.begin(), FeatStrings.end());
383+
}
384+
392385
static void handleFullArchString(StringRef FullArchStr,
393386
std::vector<std::string> &Features) {
394-
Features.push_back("__RISCV_TargetAttrNeedOverride");
395387
auto RII = llvm::RISCVISAInfo::parseArchString(
396388
FullArchStr, /* EnableExperimentalExtension */ true);
397389
if (llvm::errorToBool(RII.takeError())) {
@@ -400,6 +392,7 @@ static void handleFullArchString(StringRef FullArchStr,
400392
} else {
401393
// Append a full list of features, including any negative extensions so that
402394
// we override the CPU's features.
395+
populateNegativeRISCVFeatures(Features);
403396
std::vector<std::string> FeatStrings =
404397
(*RII)->toFeatures(/* AddAllExtensions */ true);
405398
Features.insert(Features.end(), FeatStrings.begin(), FeatStrings.end());

0 commit comments

Comments
 (0)