https://github.com/BeMg created https://github.com/llvm/llvm-project/pull/106495
…isValidFeatureName This patch makes unsupported target attributes emit a warning and ignore the target attribute during semantic checks. The changes include: 1. Adding the RISCVTargetInfo::isValidFeatureName function. 2. Adding a '+' sign to __RISCV_TargetAttrNeedOverride because checkTargetAttr will remove the sign. 3. Rejecting non-full-arch strings in the handleFullArchString function. 4. Adding test cases to demonstrate the warning behavior. >From 64557cf6950c17a92b6d85980530abe1e193e111 Mon Sep 17 00:00:00 2001 From: Piyou Chen <piyou.c...@sifive.com> Date: Wed, 28 Aug 2024 21:15:57 -0700 Subject: [PATCH] [Clang][RISCV] Recognize unsupport feature by supporting isValidFeatureName --- clang/lib/Basic/Targets/RISCV.cpp | 18 ++++++++++++++++-- clang/lib/Basic/Targets/RISCV.h | 1 + clang/lib/Sema/SemaDeclAttr.cpp | 15 +++++++++++---- clang/test/Sema/attr-target-riscv.c | 9 +++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/clang/lib/Basic/Targets/RISCV.cpp b/clang/lib/Basic/Targets/RISCV.cpp index 1f8a8cd1462c9d..1d6f49ca232692 100644 --- a/clang/lib/Basic/Targets/RISCV.cpp +++ b/clang/lib/Basic/Targets/RISCV.cpp @@ -257,7 +257,7 @@ bool RISCVTargetInfo::initFeatureMap( // If a target attribute specified a full arch string, override all the ISA // extension target features. - const auto I = llvm::find(FeaturesVec, "__RISCV_TargetAttrNeedOverride"); + const auto I = llvm::find(FeaturesVec, "+__RISCV_TargetAttrNeedOverride"); if (I != FeaturesVec.end()) { std::vector<std::string> OverrideFeatures(std::next(I), FeaturesVec.end()); @@ -391,7 +391,14 @@ void RISCVTargetInfo::fillValidTuneCPUList( static void handleFullArchString(StringRef FullArchStr, std::vector<std::string> &Features) { - Features.push_back("__RISCV_TargetAttrNeedOverride"); + + // Should be full arch string. + if (!FullArchStr.starts_with("rv")) { + Features.push_back(FullArchStr.str()); + return; + } + + Features.push_back("+__RISCV_TargetAttrNeedOverride"); auto RII = llvm::RISCVISAInfo::parseArchString( FullArchStr, /* EnableExperimentalExtension */ true); if (llvm::errorToBool(RII.takeError())) { @@ -485,3 +492,10 @@ bool RISCVTargetInfo::validateCpuSupports(StringRef Feature) const { // __riscv_feature_bits structure. return -1 != llvm::RISCVISAInfo::getRISCVFeaturesBitsInfo(Feature).second; } + +bool RISCVTargetInfo::isValidFeatureName(StringRef Name) const { + if (Name == "__RISCV_TargetAttrNeedOverride") + return true; + + return llvm::RISCVISAInfo::isSupportedExtensionFeature(Name); +} diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h index 626274b8fc437c..b808ccc8e9cfe9 100644 --- a/clang/lib/Basic/Targets/RISCV.h +++ b/clang/lib/Basic/Targets/RISCV.h @@ -130,6 +130,7 @@ class RISCVTargetInfo : public TargetInfo { bool supportsCpuSupports() const override { return getTriple().isOSLinux(); } bool supportsCpuInit() const override { return getTriple().isOSLinux(); } bool validateCpuSupports(StringRef Feature) const override; + bool isValidFeatureName(StringRef Name) const override; }; class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo { public: diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 1e074298ac5289..81cff8d7362ad5 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2993,10 +2993,17 @@ bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Unknown << Tune << ParsedAttrs.Tune << Target; - if (Context.getTargetInfo().getTriple().isRISCV() && - ParsedAttrs.Duplicate != "") - return Diag(LiteralLoc, diag::err_duplicate_target_attribute) - << Duplicate << None << ParsedAttrs.Duplicate << Target; + if (Context.getTargetInfo().getTriple().isRISCV()) { + if (ParsedAttrs.Duplicate != "") + return Diag(LiteralLoc, diag::err_duplicate_target_attribute) + << Duplicate << None << ParsedAttrs.Duplicate << Target; + for (const auto &Feature : ParsedAttrs.Features) { + auto CurFeature = StringRef(Feature); + if (!CurFeature.starts_with("+") && !CurFeature.starts_with("-")) + return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) + << Unsupported << None << AttrStr << Target; + } + } if (ParsedAttrs.Duplicate != "") return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) diff --git a/clang/test/Sema/attr-target-riscv.c b/clang/test/Sema/attr-target-riscv.c index ed4e2915d6c6ef..01d928c1d78e48 100644 --- a/clang/test/Sema/attr-target-riscv.c +++ b/clang/test/Sema/attr-target-riscv.c @@ -4,3 +4,12 @@ int __attribute__((target("arch=rv64g"))) foo(void) { return 0; } //expected-error@+1 {{redefinition of 'foo'}} int __attribute__((target("arch=rv64gc"))) foo(void) { return 0; } + +//expected-warning@+1 {{unsupported 'notafeature' in the 'target' attribute string; 'target' attribute ignored}} +int __attribute__((target("arch=+notafeature"))) UnsupportFeature(void) { return 0; } + +//expected-warning@+1 {{unsupported 'arch=+zba,zbb' in the 'target' attribute string; 'target' attribute ignored}} +int __attribute__((target("arch=+zba,zbb"))) WithoutAddSigned(void) { return 0; } + +//expected-warning@+1 {{unsupported 'arch=zba' in the 'target' attribute string; 'target' attribute ignored}} +int __attribute__((target("arch=zba"))) WithoutAddSigned2(void) { return 0; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits