[llvm] [NFC][AMDGPU] Add test showing caller/callee SGPR mismatch for inreg args (PR #182753)
Shilei Tian via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 23 07:03:51 PST 2026
https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/182753
>From ec077d02d34f3d782c266cc6927e824cd2c21157 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Sun, 22 Feb 2026 11:05:07 -0500
Subject: [PATCH] [NFC][AMDGPU] Add test showing caller/callee SGPR mismatch
for inreg args
Add a test demonstrating a bug where the caller and callee disagree on which
SGPRs hold user inreg arguments when there are enough to reach the SGPR0-3
range.
On the callee side, `LowerFormalArguments` marks SGPR0-3 as allocated in
`CCState` before the CC analysis runs. On the caller side, `LowerCall` adds the
scratch resource to `RegsToPass` without marking SGPR0-3 in `CCState`. This
causes `CC_AMDGPU_Func` to assign user inreg args to SGPR0-3 on the caller side
(they appear free) while the callee skips them.
In the test, the caller writes arg 0 (value 42) to s0, but the callee reads arg
0 from s16.
---
.../AMDGPU/cc-inreg-sgpr0-3-mismatch.ll | 105 ++++++++++++++++++
1 file changed, 105 insertions(+)
create mode 100644 llvm/test/CodeGen/AMDGPU/cc-inreg-sgpr0-3-mismatch.ll
diff --git a/llvm/test/CodeGen/AMDGPU/cc-inreg-sgpr0-3-mismatch.ll b/llvm/test/CodeGen/AMDGPU/cc-inreg-sgpr0-3-mismatch.ll
new file mode 100644
index 0000000000000..d0d2d65f7edf8
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/cc-inreg-sgpr0-3-mismatch.ll
@@ -0,0 +1,105 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 < %s | FileCheck %s
+
+; Test for a caller/callee mismatch in SGPR assignment for inreg args.
+;
+; The following test case demonstrates the bug with a function that takes 32
+; inreg i32 arguments and returns the first one. In the callee, arg 0 is read
+; from s16 (v_mov_b32_e32 v0, s16). In the caller, arg 0 (value 42) is written
+; to s0 (s_mov_b32 s0, 42). Since s0 != s16, the callee does not see the value
+; the caller intended to pass.
+;
+; The root cause is that the scratch resource descriptor occupies SGPR0-3. On
+; the callee side, LowerFormalArguments marks SGPR0-3 as allocated in CCState
+; before running the CC analysis, so CC_AMDGPU_Func skips them. On the caller
+; side, LowerCall adds the scratch resource to RegsToPass without marking
+; SGPR0-3 in CCState, so CC_AMDGPU_Func treats them as available and assigns
+; user inreg args there.
+
+define i32 @callee_returns_arg0(
+; CHECK-LABEL: callee_returns_arg0:
+; CHECK: ; %bb.0:
+; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; CHECK-NEXT: v_mov_b32_e32 v0, s16
+; CHECK-NEXT: s_setpc_b64 s[30:31]
+ i32 inreg %a0, i32 inreg %a1, i32 inreg %a2, i32 inreg %a3,
+ i32 inreg %a4, i32 inreg %a5, i32 inreg %a6, i32 inreg %a7,
+ i32 inreg %a8, i32 inreg %a9, i32 inreg %a10, i32 inreg %a11,
+ i32 inreg %a12, i32 inreg %a13, i32 inreg %a14, i32 inreg %a15,
+ i32 inreg %a16, i32 inreg %a17, i32 inreg %a18, i32 inreg %a19,
+ i32 inreg %a20, i32 inreg %a21, i32 inreg %a22, i32 inreg %a23,
+ i32 inreg %a24, i32 inreg %a25, i32 inreg %a26, i32 inreg %a27,
+ i32 inreg %a28, i32 inreg %a29, i32 inreg %a30, i32 inreg %a31) {
+ ret i32 %a0
+}
+
+define i32 @caller_passes_42() {
+; CHECK-LABEL: caller_passes_42:
+; CHECK: ; %bb.0:
+; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; CHECK-NEXT: s_mov_b32 s42, s33
+; CHECK-NEXT: s_mov_b32 s33, s32
+; CHECK-NEXT: s_xor_saveexec_b64 s[16:17], -1
+; CHECK-NEXT: buffer_store_dword v14, off, s[0:3], s33 ; 4-byte Folded Spill
+; CHECK-NEXT: s_mov_b64 exec, s[16:17]
+; CHECK-NEXT: s_addk_i32 s32, 0x400
+; CHECK-NEXT: s_getpc_b64 s[16:17]
+; CHECK-NEXT: s_add_u32 s16, s16, callee_returns_arg0 at gotpcrel32@lo+4
+; CHECK-NEXT: s_addc_u32 s17, s17, callee_returns_arg0 at gotpcrel32@hi+12
+; CHECK-NEXT: s_load_dwordx2 s[40:41], s[16:17], 0x0
+; CHECK-NEXT: v_writelane_b32 v14, s30, 0
+; CHECK-NEXT: s_mov_b32 s0, 42
+; CHECK-NEXT: s_mov_b32 s1, 1
+; CHECK-NEXT: s_mov_b32 s2, 2
+; CHECK-NEXT: s_mov_b32 s3, 3
+; CHECK-NEXT: s_mov_b32 s16, 4
+; CHECK-NEXT: s_mov_b32 s17, 5
+; CHECK-NEXT: s_mov_b32 s18, 6
+; CHECK-NEXT: s_mov_b32 s19, 7
+; CHECK-NEXT: s_mov_b32 s20, 8
+; CHECK-NEXT: s_mov_b32 s21, 9
+; CHECK-NEXT: s_mov_b32 s22, 10
+; CHECK-NEXT: s_mov_b32 s23, 11
+; CHECK-NEXT: s_mov_b32 s24, 12
+; CHECK-NEXT: s_mov_b32 s25, 13
+; CHECK-NEXT: s_mov_b32 s26, 14
+; CHECK-NEXT: s_mov_b32 s27, 15
+; CHECK-NEXT: s_mov_b32 s28, 16
+; CHECK-NEXT: s_mov_b32 s29, 17
+; CHECK-NEXT: v_mov_b32_e32 v0, 18
+; CHECK-NEXT: v_mov_b32_e32 v1, 19
+; CHECK-NEXT: v_mov_b32_e32 v2, 20
+; CHECK-NEXT: v_mov_b32_e32 v3, 21
+; CHECK-NEXT: v_mov_b32_e32 v4, 22
+; CHECK-NEXT: v_mov_b32_e32 v5, 23
+; CHECK-NEXT: v_mov_b32_e32 v6, 24
+; CHECK-NEXT: v_mov_b32_e32 v7, 25
+; CHECK-NEXT: v_mov_b32_e32 v8, 26
+; CHECK-NEXT: v_mov_b32_e32 v9, 27
+; CHECK-NEXT: v_mov_b32_e32 v10, 28
+; CHECK-NEXT: v_mov_b32_e32 v11, 29
+; CHECK-NEXT: v_mov_b32_e32 v12, 30
+; CHECK-NEXT: v_mov_b32_e32 v13, 31
+; CHECK-NEXT: v_writelane_b32 v14, s31, 1
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: s_swappc_b64 s[30:31], s[40:41]
+; CHECK-NEXT: v_readlane_b32 s31, v14, 1
+; CHECK-NEXT: v_readlane_b32 s30, v14, 0
+; CHECK-NEXT: s_mov_b32 s32, s33
+; CHECK-NEXT: s_xor_saveexec_b64 s[4:5], -1
+; CHECK-NEXT: buffer_load_dword v14, off, s[0:3], s33 ; 4-byte Folded Reload
+; CHECK-NEXT: s_mov_b64 exec, s[4:5]
+; CHECK-NEXT: s_mov_b32 s33, s42
+; CHECK-NEXT: s_waitcnt vmcnt(0)
+; CHECK-NEXT: s_setpc_b64 s[30:31]
+ %r = call i32 @callee_returns_arg0(
+ i32 inreg 42, i32 inreg 1, i32 inreg 2, i32 inreg 3,
+ i32 inreg 4, i32 inreg 5, i32 inreg 6, i32 inreg 7,
+ i32 inreg 8, i32 inreg 9, i32 inreg 10, i32 inreg 11,
+ i32 inreg 12, i32 inreg 13, i32 inreg 14, i32 inreg 15,
+ i32 inreg 16, i32 inreg 17, i32 inreg 18, i32 inreg 19,
+ i32 inreg 20, i32 inreg 21, i32 inreg 22, i32 inreg 23,
+ i32 inreg 24, i32 inreg 25, i32 inreg 26, i32 inreg 27,
+ i32 inreg 28, i32 inreg 29, i32 inreg 30, i32 inreg 31)
+ ret i32 %r
+}
More information about the llvm-commits
mailing list