[clang] [llvm] [clang][RISCV] Cache repeated target feature lookups (PR #240325)
Pengcheng Wang via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 9 02:43:12 PST 2026
https://github.com/wangpc-pp created https://github.com/llvm/llvm-project/pull/240325
Reuse the canonical default target-features attribute for functions
without per-function target settings. Keep target-specific overrides
separate from the default cache.
Cache repeated RISC-V subtarget queries by the complete function
attribute set, following the AArch64 implementation. Avoid temporary
copies of CPU and feature strings when constructing a new lookup key.
Three interleaved CTMark pairs reduce median compile time by 1.925%
and geomean by 1.893%.
Assisted-by: TRAE CLI (GPT-6)
>From b1750fe86d62a8bd75c022f20de387a4e3f6b966 Mon Sep 17 00:00:00 2001
From: Pengcheng Wang <wangpengcheng.pp at bytedance.com>
Date: Mon, 9 Nov 2026 18:40:38 +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/CodeGenModule.cpp | 21 +++++++++++++++++---
clang/lib/CodeGen/CodeGenModule.h | 2 ++
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp | 19 +++++++++++-------
llvm/lib/Target/RISCV/RISCVTargetMachine.h | 3 +++
4 files changed, 35 insertions(+), 10 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;
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index b5b3156930d4fb..40bd9d84265e99 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -127,16 +127,19 @@ RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
const RISCVSubtarget *
RISCVTargetMachine::getSubtargetImpl(const Function &F) const {
+ // Avoid rebuilding the subtarget key for repeated queries with the same
+ // function attributes.
+ AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
+ if (LastSubtarget && LastSubtargetAttrs == FnAttrs)
+ return LastSubtarget;
+
Attribute CPUAttr = F.getFnAttribute("target-cpu");
Attribute TuneAttr = F.getFnAttribute("tune-cpu");
Attribute FSAttr = F.getFnAttribute("target-features");
- std::string CPU =
- CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
- std::string TuneCPU =
- TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
- std::string FS =
- FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
+ StringRef CPU = CPUAttr.isValid() ? CPUAttr.getValueAsString() : TargetCPU;
+ StringRef TuneCPU = TuneAttr.isValid() ? TuneAttr.getValueAsString() : CPU;
+ StringRef FS = FSAttr.isValid() ? FSAttr.getValueAsString() : TargetFS;
unsigned RVVBitsMin = CLOpts.v_vector_bits_min.value_or(-1);
unsigned RVVBitsMax = CLOpts.v_vector_bits_max.value_or(0);
@@ -186,7 +189,9 @@ RISCVTargetMachine::getSubtargetImpl(const Function &F) const {
I = std::make_unique<RISCVSubtarget>(
TargetTriple, CPU, TuneCPU, FS, ABIName, RVVBitsMin, RVVBitsMax, *this);
}
- return I.get();
+ LastSubtargetAttrs = FnAttrs;
+ LastSubtarget = I.get();
+ return LastSubtarget;
}
MachineFunctionInfo *RISCVTargetMachine::createMachineFunctionInfo(
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.h b/llvm/lib/Target/RISCV/RISCVTargetMachine.h
index 03a4e0240b8c68..6ed56b70420b44 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.h
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.h
@@ -16,6 +16,7 @@
#include "MCTargetDesc/RISCVMCTargetDesc.h"
#include "RISCVSubtarget.h"
#include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
+#include "llvm/IR/Attributes.h"
#include "llvm/IR/DataLayout.h"
#include <optional>
@@ -24,6 +25,8 @@ class RISCVTargetMachine : public CodeGenTargetMachineImpl {
const RISCVOptions &CLOpts;
std::unique_ptr<TargetLoweringObjectFile> TLOF;
mutable StringMap<std::unique_ptr<RISCVSubtarget>> SubtargetMap;
+ mutable AttributeSet LastSubtargetAttrs;
+ mutable const RISCVSubtarget *LastSubtarget = nullptr;
public:
RISCVTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
More information about the cfe-commits
mailing list