[llvm] [AArch64] Prevent debug info from changing stack tag slot selection (PR #230935)
Yongqiang Tian via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 03:08:14 PDT 2026
https://github.com/yqtian-se created https://github.com/llvm/llvm-project/pull/230935
`AArch64StackTaggingPreRA::findFirstSlotCandidate` ranks tagged stack slots by uses of their `TAGPstack` result. Its `use_instructions` walk also counts `DBG_VALUE`, so adding one debug record can change which slot is pinned to offset zero. The reduced, verifier-valid IR has two tagged allocas and a `#dbg_value` of the first `llvm.aarch64.tagp` result. With that record, `irg` uses `x0` and `addg` uses `x1`; without it, those registers are reversed.
I update the `use_instructions` by `use_nodbg_instructions`. The new `llc` test compiles the same IR both with its debug record and after `opt -strip-debug`, checking that the slot choice stays the same. On the unmodified base, the run with the debug record fails its `irg` check; both runs pass with the fix. The new test and nearby stack-tagging CodeGen tests pass on LLVM main at `52246606581f`.
The trigger uses a direct `llvm.aarch64.tagp` call in IR. The IR has been verified by `llc -verify-machineinstrs`.
>From 7840fc4d8081e1753b04b071adeb020faf7e4d7b Mon Sep 17 00:00:00 2001
From: Yongqiang Tian <yqtian668 at gmail.com>
Date: Sun, 11 Oct 2026 09:59:39 +0000
Subject: [PATCH] [AArch64] Prevent debug info from changing stack tag slot
selection
AArch64StackTaggingPreRA ranks tagged stack slots by uses of the
TAGPstack result. Counting DBG_VALUE uses can change which slot is
pinned to offset zero, so adding a debug record changes the IRG/ADDG
selection for otherwise identical ordinary input.
Use use_nodbg_instructions for the score. Add a regression test that
compiles direct llvm.aarch64.tagp IR with and without debug information
and checks that both choose the same slot.
Assisted-by: OpenAI Codex 5.6 Sol, co-reviewed by Claude and Yongqiang Tian.
---
.../AArch64/AArch64StackTaggingPreRA.cpp | 2 +-
.../AArch64/stack-tagging-first-slot-debug.ll | 39 +++++++++++++++++++
2 files changed, 40 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/AArch64/stack-tagging-first-slot-debug.ll
diff --git a/llvm/lib/Target/AArch64/AArch64StackTaggingPreRA.cpp b/llvm/lib/Target/AArch64/AArch64StackTaggingPreRA.cpp
index c4bf8d5bd57ae..28e547341d8b1 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTaggingPreRA.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTaggingPreRA.cpp
@@ -275,7 +275,7 @@ std::optional<int> AArch64StackTaggingPreRAImpl::findFirstSlotCandidate() {
while (!WorkList.empty()) {
Register UseReg = WorkList.pop_back_val();
- for (auto &UseI : MRI->use_instructions(UseReg)) {
+ for (auto &UseI : MRI->use_nodbg_instructions(UseReg)) {
unsigned Opcode = UseI.getOpcode();
if (Opcode == AArch64::STGi || Opcode == AArch64::ST2Gi ||
Opcode == AArch64::STZGi || Opcode == AArch64::STZ2Gi ||
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-first-slot-debug.ll b/llvm/test/CodeGen/AArch64/stack-tagging-first-slot-debug.ll
new file mode 100644
index 0000000000000..8ff59bf2182d4
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-first-slot-debug.ll
@@ -0,0 +1,39 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --filter "irg|addg" --version 6
+; RUN: llc -mtriple=aarch64 -mattr=+mte -verify-machineinstrs %s -o - | FileCheck %s
+; RUN: opt -strip-debug -S %s | llc -mtriple=aarch64 -mattr=+mte -verify-machineinstrs -o - | FileCheck %s
+
+; The DBG_VALUE of %a_t must not affect which stack slot is pinned to the
+; unoffset IRG result. The second RUN line removes only the debug information.
+source_filename = "first-slot-debug.ll"
+
+declare ptr @llvm.aarch64.irg.sp(i64)
+declare ptr @llvm.aarch64.tagp.p0(ptr, ptr, i64 immarg)
+declare void @use2(ptr, ptr)
+
+define void @f() !dbg !4 {
+; CHECK-LABEL: f:
+; CHECK: irg x1, sp
+; CHECK: addg x0, x1, #16, #1
+entry:
+ %a = alloca i8, align 16
+ %b = alloca i8, align 16
+ %base = call ptr @llvm.aarch64.irg.sp(i64 0)
+ %a_t = call ptr @llvm.aarch64.tagp.p0(ptr %a, ptr %base, i64 1)
+ #dbg_value(ptr %a_t, !7, !DIExpression(), !8)
+ %b_t = call ptr @llvm.aarch64.tagp.p0(ptr %b, ptr %base, i64 0)
+ call void @use2(ptr %a_t, ptr %b_t)
+ ret void
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3}
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "reduced", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "reduced.c", directory: "/")
+!2 = !DISubroutineType(types: !5)
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !2, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !6)
+!5 = !{}
+!6 = !{!7}
+!7 = !DILocalVariable(name: "a_tagged", scope: !4, file: !1, line: 1, type: !9)
+!8 = !DILocation(line: 1, column: 1, scope: !4)
+!9 = !DIBasicType(name: "pointer", size: 64, encoding: DW_ATE_unsigned)
More information about the llvm-commits
mailing list