https://github.com/wangpc-pp updated https://github.com/llvm/llvm-project/pull/240338
>From 221cc5803088496dc09830fdc682857bc94379ee Mon Sep 17 00:00:00 2001 From: Pengcheng Wang <[email protected]> Date: Mon, 9 Nov 2026 19:35:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?= =?UTF-8?q?itial=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.6-beta.1 --- clang/lib/CodeGen/CodeGenModule.cpp | 21 ++++++++++++++++++--- clang/lib/CodeGen/CodeGenModule.h | 2 ++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 88129acfe2dabc..8428203a0b6f1c 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -3508,6 +3508,7 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr; const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr; bool AddedAttr = false; + bool CacheDefaultFeatures = false; if (TD || TV || SD || TC) { llvm::StringMap<bool> FeatureMap; getContext().getFunctionFeatureMap(FeatureMap, GD); @@ -3566,8 +3567,16 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, // The AMDGCN-flavored SPIR-V target unions every GPU's features so it can // report all builtins as supported, but that union is meaningless in the // emitted IR. - } else { - Features = getTarget().getTargetOpts().Features; + } else if (SetTargetFeatures) { + if (DefaultTargetFeatures) { + if (DefaultTargetFeatures->isValid()) { + Attrs.addAttribute(*DefaultTargetFeatures); + AddedAttr = true; + } + } else { + Features = getTarget().getTargetOpts().Features; + CacheDefaultFeatures = true; + } } } @@ -3579,13 +3588,19 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, Attrs.addAttribute("tune-cpu", TuneCPU); AddedAttr = true; } + if (CacheDefaultFeatures) + DefaultTargetFeatures = llvm::Attribute(); if (!Features.empty() && SetTargetFeatures) { llvm::erase_if(Features, [&](const std::string& F) { return getTarget().isReadOnlyFeature(F.substr(1)); }); if (!Features.empty()) { llvm::sort(Features); - Attrs.addAttribute("target-features", llvm::join(Features, ",")); + llvm::Attribute FeatureAttr = llvm::Attribute::get( + getLLVMContext(), "target-features", llvm::join(Features, ",")); + Attrs.addAttribute(FeatureAttr); + if (CacheDefaultFeatures) + DefaultTargetFeatures = FeatureAttr; AddedAttr = true; } } diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index e2bd6bc4701fda..9116c52e1aa080 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -362,6 +362,8 @@ class CodeGenModule : public CodeGenTypeCache { llvm::Module &TheModule; DiagnosticsEngine &Diags; const TargetInfo &Target; + /// Canonical target features for functions without a target attribute. + std::optional<llvm::Attribute> DefaultTargetFeatures; std::unique_ptr<CGCXXABI> ABI; llvm::LLVMContext &VMContext; std::string ModuleNameHash; >From 9522deb914a99be00803debcdf42b83f60b43ac6 Mon Sep 17 00:00:00 2001 From: Pengcheng Wang <[email protected]> Date: Tue, 10 Nov 2026 13:30:04 +0800 Subject: [PATCH 2/2] Address comments and remove CacheDefaultFeatures flag Created using spr 1.3.6-beta.1 --- clang/lib/CodeGen/CodeGenModule.cpp | 38 ++++++++++++++--------------- clang/lib/CodeGen/CodeGenModule.h | 4 ++- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 8428203a0b6f1c..0f5a994774c3fb 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -3508,7 +3508,16 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr; const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr; bool AddedAttr = false; - bool CacheDefaultFeatures = false; + auto GetTargetFeaturesAttr = [&](std::vector<std::string> Features) { + llvm::erase_if(Features, [&](const std::string &F) { + return getTarget().isReadOnlyFeature(F.substr(1)); + }); + if (Features.empty()) + return llvm::Attribute(); + llvm::sort(Features); + return llvm::Attribute::get(getLLVMContext(), "target-features", + llvm::join(Features, ",")); + }; if (TD || TV || SD || TC) { llvm::StringMap<bool> FeatureMap; getContext().getFunctionFeatureMap(FeatureMap, GD); @@ -3568,14 +3577,12 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, // report all builtins as supported, but that union is meaningless in the // emitted IR. } else if (SetTargetFeatures) { - if (DefaultTargetFeatures) { - if (DefaultTargetFeatures->isValid()) { - Attrs.addAttribute(*DefaultTargetFeatures); - AddedAttr = true; - } - } else { - Features = getTarget().getTargetOpts().Features; - CacheDefaultFeatures = true; + if (!DefaultTargetFeatures) + DefaultTargetFeatures = + GetTargetFeaturesAttr(getTarget().getTargetOpts().Features); + if (DefaultTargetFeatures->isValid()) { + Attrs.addAttribute(*DefaultTargetFeatures); + AddedAttr = true; } } } @@ -3588,19 +3595,10 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, Attrs.addAttribute("tune-cpu", TuneCPU); AddedAttr = true; } - if (CacheDefaultFeatures) - DefaultTargetFeatures = llvm::Attribute(); if (!Features.empty() && SetTargetFeatures) { - llvm::erase_if(Features, [&](const std::string& F) { - return getTarget().isReadOnlyFeature(F.substr(1)); - }); - if (!Features.empty()) { - llvm::sort(Features); - llvm::Attribute FeatureAttr = llvm::Attribute::get( - getLLVMContext(), "target-features", llvm::join(Features, ",")); + llvm::Attribute FeatureAttr = GetTargetFeaturesAttr(std::move(Features)); + if (FeatureAttr.isValid()) { Attrs.addAttribute(FeatureAttr); - if (CacheDefaultFeatures) - DefaultTargetFeatures = FeatureAttr; AddedAttr = true; } } diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 9116c52e1aa080..92afa8e708e2aa 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -362,7 +362,9 @@ class CodeGenModule : public CodeGenTypeCache { llvm::Module &TheModule; DiagnosticsEngine &Diags; const TargetInfo &Target; - /// Canonical target features for functions without a target attribute. + /// Lazily cached target features for functions without a target attribute. + /// An invalid attribute means that the defaults were computed but no + /// target-features attribute needs to be emitted. std::optional<llvm::Attribute> DefaultTargetFeatures; std::unique_ptr<CGCXXABI> ABI; llvm::LLVMContext &VMContext; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
