[clang] [clang] Avoid repeated per-function target setup (PR #241192)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 01:54:26 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Pengcheng Wang (wangpc-pp)

<details>
<summary>Changes</summary>

Only AArch64 consumes the per-function feature map when computing a
vscale range. Skip constructing and parsing the map for other targets,
including RISC-V.

Reuse the canonical vscale_range attribute on targets whose range does
not depend on per-function state. Keep AArch64 target features and
streaming modes on the existing dynamic path.

Three interleaved CTMark pairs reduce median compile time by 0.506%
and geomean by 0.691%.

Assisted-by: TRAE CLI (GPT-6)


---
Full diff: https://github.com/llvm/llvm-project/pull/241192.diff


3 Files Affected:

- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+23-18) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+12) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+3) 


``````````diff
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 33434526d8632..9adfcfb35b304 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1130,25 +1130,30 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
     Fn->removeFnAttr("zero-call-used-regs");
 
   // Add vscale_range attribute if appropriate.
-  llvm::StringMap<bool> FeatureMap;
-  auto IsArmStreaming = TargetInfo::ArmStreamingKind::NotStreaming;
-  if (FD) {
-    getContext().getFunctionFeatureMap(FeatureMap, FD);
-    if (const auto *T = FD->getType()->getAs<FunctionProtoType>())
-      if (T->getAArch64SMEAttributes() &
-          FunctionType::SME_PStateSMCompatibleMask)
-        IsArmStreaming = TargetInfo::ArmStreamingKind::StreamingCompatible;
-
-    if (IsArmStreamingFunction(FD, true))
-      IsArmStreaming = TargetInfo::ArmStreamingKind::Streaming;
-  }
-  std::optional<std::pair<unsigned, unsigned>> VScaleRange =
-      getContext().getTargetInfo().getVScaleRange(getLangOpts(), IsArmStreaming,
-                                                  &FeatureMap);
-  if (VScaleRange) {
-    CurFn->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
-        getLLVMContext(), VScaleRange->first, VScaleRange->second));
+  llvm::Attribute VScaleRangeAttr;
+  if (CGM.getTriple().isAArch64()) {
+    llvm::StringMap<bool> FeatureMap;
+    auto IsArmStreaming = TargetInfo::ArmStreamingKind::NotStreaming;
+    if (FD) {
+      getContext().getFunctionFeatureMap(FeatureMap, FD);
+      if (const auto *T = FD->getType()->getAs<FunctionProtoType>())
+        if (T->getAArch64SMEAttributes() &
+            FunctionType::SME_PStateSMCompatibleMask)
+          IsArmStreaming = TargetInfo::ArmStreamingKind::StreamingCompatible;
+
+      if (IsArmStreamingFunction(FD, true))
+        IsArmStreaming = TargetInfo::ArmStreamingKind::Streaming;
+    }
+    auto VScaleRange = getContext().getTargetInfo().getVScaleRange(
+        getLangOpts(), IsArmStreaming, FD ? &FeatureMap : nullptr);
+    if (VScaleRange)
+      VScaleRangeAttr = llvm::Attribute::getWithVScaleRangeArgs(
+          getLLVMContext(), VScaleRange->first, VScaleRange->second);
+  } else {
+    VScaleRangeAttr = CGM.getDefaultVScaleRange();
   }
+  if (VScaleRangeAttr.isValid())
+    CurFn->addFnAttr(VScaleRangeAttr);
 
   llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
 
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 3b8ec02a94e54..303e56f91cc24 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -722,6 +722,18 @@ CodeGenModule::CodeGenModule(ASTContext &C,
 
 CodeGenModule::~CodeGenModule() {}
 
+llvm::Attribute CodeGenModule::getDefaultVScaleRange() {
+  if (!DefaultVScaleRange) {
+    auto Range = Target.getVScaleRange(
+        LangOpts, TargetInfo::ArmStreamingKind::NotStreaming);
+    DefaultVScaleRange.emplace(
+        Range ? llvm::Attribute::getWithVScaleRangeArgs(
+                    getLLVMContext(), Range->first, Range->second)
+              : llvm::Attribute());
+  }
+  return *DefaultVScaleRange;
+}
+
 void CodeGenModule::createObjCRuntime() {
   // This is just isGNUFamily(), but we want to force implementors of
   // new ABIs to decide how best to do this.
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index 92afa8e708e2a..0bbe76c03b2c8 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -366,6 +366,8 @@ class CodeGenModule : public CodeGenTypeCache {
   /// An invalid attribute means that the defaults were computed but no
   /// target-features attribute needs to be emitted.
   std::optional<llvm::Attribute> DefaultTargetFeatures;
+  /// Canonical vscale range for targets without per-function ranges.
+  std::optional<llvm::Attribute> DefaultVScaleRange;
   std::unique_ptr<CGCXXABI> ABI;
   llvm::LLVMContext &VMContext;
   std::string ModuleNameHash;
@@ -915,6 +917,7 @@ class CodeGenModule : public CodeGenTypeCache {
   }
   const TargetInfo &getTarget() const { return Target; }
   const llvm::Triple &getTriple() const { return Target.getTriple(); }
+  llvm::Attribute getDefaultVScaleRange();
   bool supportsCOMDAT() const;
   void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/241192


More information about the cfe-commits mailing list