Author: Piyou Chen Date: 2024-09-09T15:07:39+08:00 New Revision: 022b3c27e27832f27c61683095899227c26e0cca
URL: https://github.com/llvm/llvm-project/commit/022b3c27e27832f27c61683095899227c26e0cca DIFF: https://github.com/llvm/llvm-project/commit/022b3c27e27832f27c61683095899227c26e0cca.diff LOG: [Clang][RISCV] Recognize unsupport target feature by supporting isValidFeatureName (#106495) 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. Rejecting non-full-arch strings in the handleFullArchString function. 3. Adding test cases to demonstrate the warning behavior. Added: Modified: clang/lib/Basic/Targets/RISCV.cpp clang/lib/Basic/Targets/RISCV.h clang/lib/Sema/SemaDeclAttr.cpp clang/test/Sema/attr-target-riscv.c Removed: ################################################################################ diff --git a/clang/lib/Basic/Targets/RISCV.cpp b/clang/lib/Basic/Targets/RISCV.cpp index b89109e7725d44..6f9d050fc71a90 100644 --- a/clang/lib/Basic/Targets/RISCV.cpp +++ b/clang/lib/Basic/Targets/RISCV.cpp @@ -388,7 +388,7 @@ static void handleFullArchString(StringRef FullArchStr, FullArchStr, /* EnableExperimentalExtension */ true); if (llvm::errorToBool(RII.takeError())) { // Forward the invalid FullArchStr. - Features.push_back("+" + FullArchStr.str()); + Features.push_back(FullArchStr.str()); } else { // Append a full list of features, including any negative extensions so that // we override the CPU's features. @@ -478,3 +478,7 @@ bool RISCVTargetInfo::validateCpuSupports(StringRef Feature) const { // __riscv_feature_bits structure. return -1 != llvm::RISCVISAInfo::getRISCVFeaturesBitsInfo(Feature).second; } + +bool RISCVTargetInfo::isValidFeatureName(StringRef Name) const { + 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 d068cb6a78f266..72d82b424c26c8 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) { + StringRef CurFeature = 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..35e2ec3986ada3 100644 --- a/clang/test/Sema/attr-target-riscv.c +++ b/clang/test/Sema/attr-target-riscv.c @@ -4,3 +4,15 @@ 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 'notafeature' in the 'target' attribute string; 'target' attribute ignored}} +int __attribute__((target("arch=-notafeature"))) UnsupportNegativeFeature(void) { return 0; } + +//expected-warning@+1 {{unsupported 'arch=+zba,zbb' in the 'target' attribute string; 'target' attribute ignored}} +int __attribute__((target("arch=+zba,zbb"))) WithoutPlus(void) { return 0; } + +//expected-warning@+1 {{unsupported 'arch=zba' in the 'target' attribute string; 'target' attribute ignored}} +int __attribute__((target("arch=zba"))) WithoutPlus2(void) { return 0; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits