[llvm] [AArch64] Cache repeated subtarget queries (NFC) (PR #222637)
Cullen Rhodes via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 05:59:44 PDT 2026
https://github.com/c-rhodes created https://github.com/llvm/llvm-project/pull/222637
Cache the last subtarget along with the function attributes used to select it. This allows reuse across both repeated queries for the same function and across functions with identical attributes.
Improves CTMark geomean -0.10% on aarch64-O3 and -0.17 on aarch64-O0-g.
https://llvm-compile-time-tracker.com/compare.php?from=87d9c3c24ff5941ac08799850a6549b561c98a81&to=544bb3d9ac28bc1a28248f6b03bcd95fa85b20e0&stat=instructions:u
Assisted-by: codex
>From 544bb3d9ac28bc1a28248f6b03bcd95fa85b20e0 Mon Sep 17 00:00:00 2001
From: Cullen Rhodes <cullen.rhodes at arm.com>
Date: Mon, 7 Sep 2026 21:39:49 +0000
Subject: [PATCH] [AArch64] Cache repeated subtarget queries (NFC)
CodeGen frequently queries the subtarget for the same function. Cache
the last result while invalidating it when function attributes change.
Assisted-by: codex
---
llvm/lib/Target/AArch64/AArch64TargetMachine.cpp | 13 +++++++++++--
llvm/lib/Target/AArch64/AArch64TargetMachine.h | 3 +++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
index 8195ad04c3556..09a80980819c7 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
@@ -296,7 +296,10 @@ bool AArch64TargetMachine::isGlobalISelOptNone() const {
!GlobalISelFlag);
}
-void AArch64TargetMachine::reset() { SubtargetMap.clear(); }
+void AArch64TargetMachine::reset() {
+ SubtargetMap.clear();
+ LastSubtarget = nullptr;
+}
//===----------------------------------------------------------------------===//
// AArch64 Lowering public interface.
@@ -436,6 +439,10 @@ AArch64TargetMachine::~AArch64TargetMachine() = default;
const AArch64Subtarget *
AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
+ 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");
@@ -503,7 +510,9 @@ AArch64TargetMachine::getSubtargetImpl(const Function &F) const {
if (IsStreaming && !I->hasSME())
reportFatalUsageError("streaming SVE functions require SME");
- return I.get();
+ LastSubtargetAttrs = FnAttrs;
+ LastSubtarget = I.get();
+ return LastSubtarget;
}
// Encourage placing FORM_TRANSPOSED_REG immediately before the instruction that
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.h b/llvm/lib/Target/AArch64/AArch64TargetMachine.h
index 98eaecbb6d2af..28b9a0144abfe 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.h
@@ -16,6 +16,7 @@
#include "AArch64InstrInfo.h"
#include "AArch64Subtarget.h"
#include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
+#include "llvm/IR/Attributes.h"
#include "llvm/IR/DataLayout.h"
#include <optional>
@@ -25,6 +26,8 @@ class AArch64TargetMachine : public CodeGenTargetMachineImpl {
protected:
std::unique_ptr<TargetLoweringObjectFile> TLOF;
mutable StringMap<std::unique_ptr<AArch64Subtarget>> SubtargetMap;
+ mutable AttributeSet LastSubtargetAttrs;
+ mutable const AArch64Subtarget *LastSubtarget = nullptr;
/// Reset internal state.
void reset() override;
More information about the llvm-commits
mailing list