Skip to content

[RISCV][NFC] Reimplementation of target attribute override mechanism #106680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions clang/lib/Basic/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,25 +255,6 @@ bool RISCVTargetInfo::initFeatureMap(
Features["32bit"] = true;
}

// If a target attribute specified a full arch string, override all the ISA
// extension target features.
const auto I = llvm::find(FeaturesVec, "__RISCV_TargetAttrNeedOverride");
if (I != FeaturesVec.end()) {
std::vector<std::string> OverrideFeatures(std::next(I), FeaturesVec.end());

// Add back any non ISA extension features, e.g. +relax.
auto IsNonISAExtFeature = [](StringRef Feature) {
assert(Feature.size() > 1 && (Feature[0] == '+' || Feature[0] == '-'));
StringRef Ext = Feature.substr(1); // drop the +/-
return !llvm::RISCVISAInfo::isSupportedExtensionFeature(Ext);
};
llvm::copy_if(llvm::make_range(FeaturesVec.begin(), I),
std::back_inserter(OverrideFeatures), IsNonISAExtFeature);

return TargetInfo::initFeatureMap(Features, Diags, CPU, OverrideFeatures);
}

// Otherwise, parse the features and add any implied extensions.
std::vector<std::string> AllFeatures = FeaturesVec;
auto ParseResult = llvm::RISCVISAInfo::parseFeatures(XLen, FeaturesVec);
if (!ParseResult) {
Expand Down Expand Up @@ -389,9 +370,20 @@ void RISCVTargetInfo::fillValidTuneCPUList(
llvm::RISCV::fillValidTuneCPUArchList(Values, Is64Bit);
}

static void populateNegativeRISCVFeatures(std::vector<std::string> &Features) {
auto RII = llvm::RISCVISAInfo::parseArchString(
"rv64i", /* EnableExperimentalExtension */ true);

if (llvm::errorToBool(RII.takeError()))
llvm_unreachable("unsupport rv64i");

std::vector<std::string> FeatStrings =
(*RII)->toFeatures(/* AddAllExtensions */ true);
Features.insert(Features.end(), FeatStrings.begin(), FeatStrings.end());
}

static void handleFullArchString(StringRef FullArchStr,
std::vector<std::string> &Features) {
Features.push_back("__RISCV_TargetAttrNeedOverride");
auto RII = llvm::RISCVISAInfo::parseArchString(
FullArchStr, /* EnableExperimentalExtension */ true);
if (llvm::errorToBool(RII.takeError())) {
Expand All @@ -400,6 +392,7 @@ static void handleFullArchString(StringRef FullArchStr,
} else {
// Append a full list of features, including any negative extensions so that
// we override the CPU's features.
populateNegativeRISCVFeatures(Features);
std::vector<std::string> FeatStrings =
(*RII)->toFeatures(/* AddAllExtensions */ true);
Features.insert(Features.end(), FeatStrings.begin(), FeatStrings.end());
Expand Down
Loading