[llvm] [CodeGen][AArch64] Avoid duplicate hints in register allocation (PR #219007)
Nick Desaulniers via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 16:52:48 PDT 2026
https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/219007
>From 96bd7942a169c0b75b3c624d849e3703dbb3a0d6 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Wed, 29 Jul 2026 14:33:45 -0700
Subject: [PATCH 1/2] [CodeGen][AArch64] Avoid duplicate hints in register
allocation
Duplicate register allocation hints cause AllocationOrder to yield
duplicate physical register candidates. For fixed-capacity eviction
advisors like MLEvictAdvisor (MaxInterferences = 32), this leads to
out-of-bounds array indexing and assertion failures.
In commit 0756e5985f0c, TargetRegisterInfo::getRegAllocationHints was
hoisted to the top of AArch64RegisterInfo::getRegAllocationHints, but
fallthrough paths invoked it a second time on an already-populated Hints
vector, duplicating copy hints for standard GPRs.
This patch:
1. Returns ConsiderOnlyHints on fallthrough in
AArch64RegisterInfo::getRegAllocationHints instead of calling
TargetRegisterInfo::getRegAllocationHints a second time.
2. In TargetRegisterInfo::getRegAllocationHints, deduplicates directly
against Hints instead of using a transient local set.
3. Adds regression test coverage for AArch64 MLRegAlloc eviction
advisor.
There are other issues lurking across LLVM where duplicate Hints are
created; it was decided in review of this PR to follow up with changing
the interface so that Hints is a SmallSetVector.
Assisted-by: Gemini
Fixes: 0756e5985f0c ("[AArch64] Hint regalloc to choose distinct predicate for MATCH/CMP (#190139)")
Fixes: https://github.com/llvm/llvm-project/issues/193011
---
llvm/lib/CodeGen/TargetRegisterInfo.cpp | 12 ++---
.../Target/AArch64/AArch64RegisterInfo.cpp | 9 ++--
.../aarch64-evict-advisor-duplicate-hints.ll | 54 +++++++++++++++++++
3 files changed, 63 insertions(+), 12 deletions(-)
create mode 100644 llvm/test/CodeGen/MLRegAlloc/aarch64-evict-advisor-duplicate-hints.ll
diff --git a/llvm/lib/CodeGen/TargetRegisterInfo.cpp b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
index 3b29b0863dd6c..fd14fe48c33a0 100644
--- a/llvm/lib/CodeGen/TargetRegisterInfo.cpp
+++ b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
@@ -14,7 +14,6 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/CodeGen/LiveInterval.h"
@@ -408,7 +407,6 @@ bool TargetRegisterInfo::getRegAllocationHints(
if (!Hints_MRI)
return false;
- SmallSet<Register, 32> HintedRegs;
// First hint may be a target hint.
bool Skip = (Hints_MRI->first != 0);
for (auto Reg : Hints_MRI->second) {
@@ -422,10 +420,6 @@ bool TargetRegisterInfo::getRegAllocationHints(
if (VRM && Phys.isVirtual())
Phys = VRM->getPhys(Phys);
- // Don't add the same reg twice (Hints_MRI may contain multiple virtual
- // registers allocated to the same physreg).
- if (!HintedRegs.insert(Phys).second)
- continue;
// Check that Phys is a valid hint in VirtReg's register class.
if (!Phys.isPhysical())
continue;
@@ -437,6 +431,12 @@ bool TargetRegisterInfo::getRegAllocationHints(
if (!is_contained(Order, Phys))
continue;
+ // Don't add the same reg twice (Hints_MRI may contain multiple virtual
+ // registers allocated to the same physreg, or Hints may already contain
+ // it).
+ if (is_contained(Hints, Phys.id()))
+ continue;
+
// All clear, tell the register allocator to prefer this register.
Hints.push_back(Phys.id());
}
diff --git a/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp b/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
index c8e0cb289abdf..84eab98cd9598 100644
--- a/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64RegisterInfo.cpp
@@ -1265,8 +1265,7 @@ bool AArch64RegisterInfo::getRegAllocationHints(
return ConsiderOnlyHints;
if (!ST.hasSME() || !ST.isStreaming())
- return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF,
- VRM);
+ return ConsiderOnlyHints;
// The SVE calling convention preserves registers Z8-Z23. As a result, there
// are no ZPR2Strided or ZPR4Strided registers that do not overlap with the
@@ -1408,8 +1407,7 @@ bool AArch64RegisterInfo::getRegAllocationHints(
}
if (!Hints.empty())
- return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints,
- MF, VRM);
+ return ConsiderOnlyHints;
}
}
@@ -1440,8 +1438,7 @@ bool AArch64RegisterInfo::getRegAllocationHints(
}
}
- return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF,
- VRM);
+ return ConsiderOnlyHints;
}
unsigned AArch64RegisterInfo::getLocalAddressRegister(
diff --git a/llvm/test/CodeGen/MLRegAlloc/aarch64-evict-advisor-duplicate-hints.ll b/llvm/test/CodeGen/MLRegAlloc/aarch64-evict-advisor-duplicate-hints.ll
new file mode 100644
index 0000000000000..67895b3d199cf
--- /dev/null
+++ b/llvm/test/CodeGen/MLRegAlloc/aarch64-evict-advisor-duplicate-hints.ll
@@ -0,0 +1,54 @@
+; REQUIRES: have_tf_aot
+; REQUIRES: aarch64-registered-target
+;
+; Check that AArch64 virtual registers with copy hints do not get duplicate
+; physical register hints added to AllocationOrder, which would otherwise lead
+; to out-of-bounds indexing in MLEvictAdvisor.
+;
+; RUN: llc -mtriple=aarch64-linux-gnu -regalloc=greedy \
+; RUN: -regalloc-enable-advisor=release < %s -o /dev/null
+
+%struct.Flags = type { i8, i32, i8, i8, i8, i8, i8, i32, i8 }
+
+ at asan_flags_dont_use_directly = external global %struct.Flags
+
+define void @_Z15InitializeFlagsv(ptr %0, ptr %1) {
+ store i32 1, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 4), align 4
+ %3 = load i8, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 9), align 1
+ store i8 %3, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 8), align 4
+ %4 = load i8, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 12), align 4
+ store i8 %4, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 11), align 1
+ store i8 1, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 10), align 2
+ %5 = load i8, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 20), align 4
+ %6 = zext i8 %5 to i32
+ store i32 %6, ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 16), align 4
+ %7 = tail call ptr @_Znwmi(i64 16, i32 0)
+ store ptr null, ptr %0, align 8
+ store ptr null, ptr %7, align 8
+ store ptr null, ptr @_Z26GetGlobalLowLevelAllocatorv, align 8
+ %8 = tail call ptr @_Znwmi(i64 0, i32 0)
+ store ptr @asan_flags_dont_use_directly, ptr %0, align 8
+ %9 = tail call ptr @_Znwmi(i64 0, i32 0)
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 4), ptr %9, align 8
+ %10 = load i32, ptr null, align 4
+ %11 = tail call ptr @_Znwmi(i64 0, i32 %10)
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 8), ptr %0, align 8
+ %12 = load i32, ptr null, align 4
+ %13 = tail call ptr @_Znwmi(i64 0, i32 %12)
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 9), ptr @_Znwmi, align 8
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 10), ptr %1, align 8
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 11), ptr %0, align 8
+ %14 = tail call ptr @_Znwmi(i64 0, i32 0)
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 12), ptr %0, align 8
+ %15 = tail call ptr @_Znwmi(i64 0, i32 0)
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 16), ptr %1, align 8
+ store ptr getelementptr inbounds nuw (i8, ptr @asan_flags_dont_use_directly, i64 20), ptr %0, align 8
+ ret void
+}
+
+declare i32 @_Z26GetGlobalLowLevelAllocatorv()
+
+declare ptr @_Znwmi(i64, i32)
+
+; uselistorder directives
+uselistorder ptr @_Znwmi, { 7, 6, 5, 4, 3, 2, 1, 0 }
>From e1bdbff0e930f07a98eb6120f811978690c6a806 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Wed, 16 Sep 2026 16:45:51 -0700
Subject: [PATCH 2/2] [AArch64][CodeGen] Address code review feedback on
regalloc hints
- Use `is_contained(Hints, Phys)` instead of `Phys.id()` in `TargetRegisterInfo.cpp`.
- Add `AArch64RegAllocationHints.NoDuplicates` unit test in `AArch64RegisterInfoTest.cpp`.
To run the unit test:
$ ninja AArch64Tests
$ ./unittests/Target/AArch64/AArch64Tests --gtest_filter="AArch64RegAllocationHints.NoDuplicates"
---
llvm/lib/CodeGen/TargetRegisterInfo.cpp | 2 +-
.../AArch64/AArch64RegisterInfoTest.cpp | 46 +++++++++++++++++++
2 files changed, 47 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/TargetRegisterInfo.cpp b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
index fd14fe48c33a0..b85805c68829d 100644
--- a/llvm/lib/CodeGen/TargetRegisterInfo.cpp
+++ b/llvm/lib/CodeGen/TargetRegisterInfo.cpp
@@ -434,7 +434,7 @@ bool TargetRegisterInfo::getRegAllocationHints(
// Don't add the same reg twice (Hints_MRI may contain multiple virtual
// registers allocated to the same physreg, or Hints may already contain
// it).
- if (is_contained(Hints, Phys.id()))
+ if (is_contained(Hints, Phys))
continue;
// All clear, tell the register allocator to prefer this register.
diff --git a/llvm/unittests/Target/AArch64/AArch64RegisterInfoTest.cpp b/llvm/unittests/Target/AArch64/AArch64RegisterInfoTest.cpp
index 0906219ce0a4f..79d39c593e144 100644
--- a/llvm/unittests/Target/AArch64/AArch64RegisterInfoTest.cpp
+++ b/llvm/unittests/Target/AArch64/AArch64RegisterInfoTest.cpp
@@ -4,6 +4,7 @@
#include "AArch64TargetMachine.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCSubtargetInfo.h"
@@ -187,4 +188,49 @@ TEST(AArch64ReservedRegs, ArtificialHIRegistersAreReserved) {
EXPECT_TRUE(Reserved.test(AArch64::Q31_HI));
}
+TEST(AArch64RegAllocationHints, NoDuplicates) {
+ std::unique_ptr<TargetMachine> TM = createTargetMachine("");
+ ASSERT_TRUE(TM);
+
+ std::unique_ptr<AArch64InstrInfo> II = createInstrInfo(TM.get());
+ ASSERT_TRUE(II);
+
+ const AArch64RegisterInfo &TRI = II->getRegisterInfo();
+
+ LLVMContext Context;
+ Module M("", Context);
+ M.setDataLayout(TM->createDataLayout());
+ Function *F = Function::Create(
+ FunctionType::get(Type::getVoidTy(Context), /*isVarArg=*/false),
+ GlobalValue::ExternalLinkage, "f", &M);
+
+ MachineModuleInfo MMI(TM.get());
+ const TargetSubtargetInfo *STI = TM->getSubtargetImpl(*F);
+ MachineFunction MF(*F, *TM, *STI, MMI.getContext(), /*FunctionNum=*/0);
+ MF.initTargetMachineFunctionInfo(*STI);
+
+ MachineRegisterInfo &MRI = MF.getRegInfo();
+ MRI.freezeReservedRegs();
+
+ Register VirtReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
+ MRI.addRegAllocationHint(VirtReg, AArch64::X0);
+
+ ArrayRef<MCPhysReg> Order = AArch64::GPR64RegClass.getRegisters();
+ SmallVector<MCPhysReg, 4> Hints;
+
+ // Calling getRegAllocationHints once should not produce duplicate hints
+ // from fallthrough to TargetRegisterInfo::getRegAllocationHints.
+ TRI.getRegAllocationHints(VirtReg, Order, Hints, MF, /*VRM=*/nullptr,
+ /*Matrix=*/nullptr);
+ EXPECT_EQ(Hints.size(), 1u);
+ EXPECT_EQ(Hints[0], MCPhysReg(AArch64::X0));
+
+ // Calling getRegAllocationHints a second time with Hints already populated
+ // should not append duplicate hints.
+ TRI.getRegAllocationHints(VirtReg, Order, Hints, MF, /*VRM=*/nullptr,
+ /*Matrix=*/nullptr);
+ EXPECT_EQ(Hints.size(), 1u);
+ EXPECT_EQ(Hints[0], MCPhysReg(AArch64::X0));
+}
+
} // namespace
More information about the llvm-commits
mailing list