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

Pengcheng Wang via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 01:53:50 PST 2026


https://github.com/wangpc-pp created https://github.com/llvm/llvm-project/pull/241192

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)


>From dd77d26ff91b7fb6b956bdaea4bbc9802984a7e5 Mon Sep 17 00:00:00 2001
From: Pengcheng Wang <wangpengcheng.pp at bytedance.com>
Date: Wed, 11 Nov 2026 17:53:36 +0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=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/CodeGenFunction.cpp | 41 +++++++++++++++------------
 clang/lib/CodeGen/CodeGenModule.cpp   | 12 ++++++++
 clang/lib/CodeGen/CodeGenModule.h     |  3 ++
 3 files changed, 38 insertions(+), 18 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 33434526d86325..9adfcfb35b3049 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 3b8ec02a94e542..303e56f91cc242 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 92afa8e708e2aa..0bbe76c03b2c86 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);
 



More information about the cfe-commits mailing list