[llvm] [AMDGPU] Fix a crash when a bool variable is used in inline asm (PR #171004)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 8 08:01:56 PST 2025


https://github.com/shiltian updated https://github.com/llvm/llvm-project/pull/171004

>From 4044fc59dab7057fbd226b8949e8def41bbf8484 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Sat, 6 Dec 2025 20:18:06 -0500
Subject: [PATCH 1/4] [AMDGPU] Fix a crash when a bool variable is used in
 inline asm

---
 llvm/lib/Target/AMDGPU/SIISelLowering.cpp       |  5 +++++
 llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll | 15 +++++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll

diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index f206cee0222f9..159c16a2d80a6 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -17684,6 +17684,11 @@ SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI_,
       break;
     case 'v':
       switch (BitWidth) {
+      case 1:
+        RC = Subtarget->has1024AddressableVGPRs()
+                 ? &AMDGPU::VGPR_32_Lo256RegClass
+                 : &AMDGPU::VGPR_32RegClass;
+        break;
       case 16:
         RC = Subtarget->useRealTrue16Insts() ? &AMDGPU::VGPR_16RegClass
                                              : &AMDGPU::VGPR_32_Lo256RegClass;
diff --git a/llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll b/llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll
new file mode 100644
index 0000000000000..f26032656a2e9
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll
@@ -0,0 +1,15 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx942 %s -o - | FileCheck %s
+; RUN: llc -global-isel=1 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx942 %s -o - | FileCheck %s
+
+define void @test(ptr %p, i1 %b) {
+; CHECK-LABEL: test:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; CHECK-NEXT:    ;;#ASMSTART
+; CHECK-NEXT:    global_store_byte v[0:1], v2, off glc slc
+; CHECK-NEXT:    ;;#ASMEND
+; CHECK-NEXT:    s_setpc_b64 s[30:31]
+  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,v"(ptr %p, i1 %b)
+  ret void
+}

>From 88a9db43e67b72fbbcd1c2e7fa355a7b3ac874e0 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Sun, 7 Dec 2025 19:24:46 -0500
Subject: [PATCH 2/4] move test

---
 llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll | 15 ---------------
 llvm/test/CodeGen/AMDGPU/inline-asm.ll          | 12 ++++++++++++
 2 files changed, 12 insertions(+), 15 deletions(-)
 delete mode 100644 llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll

diff --git a/llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll b/llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll
deleted file mode 100644
index f26032656a2e9..0000000000000
--- a/llvm/test/CodeGen/AMDGPU/inline-asm-use-bool.ll
+++ /dev/null
@@ -1,15 +0,0 @@
-; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
-; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx942 %s -o - | FileCheck %s
-; RUN: llc -global-isel=1 -mtriple=amdgcn-amd-amdhsa -mcpu=gfx942 %s -o - | FileCheck %s
-
-define void @test(ptr %p, i1 %b) {
-; CHECK-LABEL: test:
-; CHECK:       ; %bb.0:
-; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    ;;#ASMSTART
-; CHECK-NEXT:    global_store_byte v[0:1], v2, off glc slc
-; CHECK-NEXT:    ;;#ASMEND
-; CHECK-NEXT:    s_setpc_b64 s[30:31]
-  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,v"(ptr %p, i1 %b)
-  ret void
-}
diff --git a/llvm/test/CodeGen/AMDGPU/inline-asm.ll b/llvm/test/CodeGen/AMDGPU/inline-asm.ll
index 54e7d0e6b08f3..8794c91f9ab40 100644
--- a/llvm/test/CodeGen/AMDGPU/inline-asm.ll
+++ b/llvm/test/CodeGen/AMDGPU/inline-asm.ll
@@ -363,3 +363,15 @@ define void @mixed_def_sgpr_vgpr_def_asm() {
   call void asm sideeffect "; use $0 ", "{s[4:5]}"(i64 %sgpr.add)
   ret void
 }
+
+define void @i1_used_as_vgpr_operand(ptr %p, i1 %b) {
+; CHECK-LABEL: i1_used_as_vgpr_operand:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; CHECK-NEXT:    ;;#ASMSTART
+; CHECK-NEXT:    global_store_byte v[0:1], v2, off glc slc
+; CHECK-NEXT:    ;;#ASMEND
+; CHECK-NEXT:    s_setpc_b64 s[30:31]
+  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,v"(ptr %p, i1 %b)
+  ret void
+}

>From 4846cd282fa911cc5d12769946c68fba684b4a0c Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Mon, 8 Dec 2025 09:59:48 -0500
Subject: [PATCH 3/4] review comments

---
 llvm/lib/Target/AMDGPU/SIISelLowering.cpp            |  5 +----
 llvm/test/CodeGen/AMDGPU/inline-asm.ll               | 12 ------------
 .../AMDGPU/inlineasm-mismatched-size-error.ll        |  6 ++++++
 3 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 159c16a2d80a6..12bba6aa45493 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -17685,10 +17685,7 @@ SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI_,
     case 'v':
       switch (BitWidth) {
       case 1:
-        RC = Subtarget->has1024AddressableVGPRs()
-                 ? &AMDGPU::VGPR_32_Lo256RegClass
-                 : &AMDGPU::VGPR_32RegClass;
-        break;
+        return std::pair(0U, nullptr);
       case 16:
         RC = Subtarget->useRealTrue16Insts() ? &AMDGPU::VGPR_16RegClass
                                              : &AMDGPU::VGPR_32_Lo256RegClass;
diff --git a/llvm/test/CodeGen/AMDGPU/inline-asm.ll b/llvm/test/CodeGen/AMDGPU/inline-asm.ll
index 8794c91f9ab40..54e7d0e6b08f3 100644
--- a/llvm/test/CodeGen/AMDGPU/inline-asm.ll
+++ b/llvm/test/CodeGen/AMDGPU/inline-asm.ll
@@ -363,15 +363,3 @@ define void @mixed_def_sgpr_vgpr_def_asm() {
   call void asm sideeffect "; use $0 ", "{s[4:5]}"(i64 %sgpr.add)
   ret void
 }
-
-define void @i1_used_as_vgpr_operand(ptr %p, i1 %b) {
-; CHECK-LABEL: i1_used_as_vgpr_operand:
-; CHECK:       ; %bb.0:
-; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    ;;#ASMSTART
-; CHECK-NEXT:    global_store_byte v[0:1], v2, off glc slc
-; CHECK-NEXT:    ;;#ASMEND
-; CHECK-NEXT:    s_setpc_b64 s[30:31]
-  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,v"(ptr %p, i1 %b)
-  ret void
-}
diff --git a/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll b/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
index e8accc1c8a0f3..3c7cf01bdbb9d 100644
--- a/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
+++ b/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
@@ -153,3 +153,9 @@ define <4 x i32> @misaligned_sgpr_4xi32_out_2() {
   %asm = call <4 x i32> asm sideeffect "; def $0", "={s[2:5]}"()
   ret <4 x i32> %asm
 }
+
+; ERR: error: couldn't allocate input reg for constraint 'v'
+define void @i1_used_as_vgpr_operand(ptr %p, i1 %b) {
+  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,v"(ptr %p, i1 %b)
+  ret void
+}

>From e65d0e7b02e920fbf421975e04a4a7d66dec7843 Mon Sep 17 00:00:00 2001
From: Shilei Tian <i at tianshilei.me>
Date: Mon, 8 Dec 2025 11:01:18 -0500
Subject: [PATCH 4/4] add more tests

---
 llvm/lib/Target/AMDGPU/SIISelLowering.cpp      |  2 ++
 .../AMDGPU/inlineasm-mismatched-size-error.ll  | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 12bba6aa45493..a871d978dfbc8 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -17703,6 +17703,8 @@ SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI_,
       if (!Subtarget->hasMAIInsts())
         break;
       switch (BitWidth) {
+      case 1:
+        return std::pair(0U, nullptr);
       case 16:
         RC = &AMDGPU::AGPR_32RegClass;
         break;
diff --git a/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll b/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
index 3c7cf01bdbb9d..292bb7383a551 100644
--- a/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
+++ b/llvm/test/CodeGen/AMDGPU/inlineasm-mismatched-size-error.ll
@@ -159,3 +159,21 @@ define void @i1_used_as_vgpr_operand(ptr %p, i1 %b) {
   tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,v"(ptr %p, i1 %b)
   ret void
 }
+
+; ERR: error: couldn't allocate input reg for constraint 'a'
+define void @i1_used_as_agpr_operand(ptr %p, i1 %b) {
+  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,a"(ptr %p, i1 %b)
+  ret void
+}
+
+; ERR: error: couldn't allocate input reg for constraint 's'
+define void @i1_used_as_sgpr_operand_s(ptr %p, i1 %b) {
+  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,s"(ptr %p, i1 %b)
+  ret void
+}
+
+; ERR: error: couldn't allocate input reg for constraint 'r'
+define void @i1_used_as_sgpr_operand_r(ptr %p, i1 %b) {
+  tail call void asm sideeffect "global_store_byte $0, $1, off glc slc", "v,r"(ptr %p, i1 %b)
+  ret void
+}



More information about the llvm-commits mailing list