[llvm] [AMDGPU][GIsel] Fix constrainRegAttrs to retain whichever of the two types is more specific instead of always adopting ConstrainingRegTy (PR #227226)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 29 02:04:50 PDT 2026
https://github.com/adeshcom14 created https://github.com/llvm/llvm-project/pull/227226
On extended-LLT targets, a scalar type can be either an untyped s32 or a specifically typed i32/f32. When a combine merges two equivalent registers, code that unifies their types treated these as interchangeable and overwrote the specifically typed registers type with the untyped one, even though its defining instruction never changed. Since GlobalISel's compiled matcher tables only match the specific type, which makes the register as unselectable and crashes.
This patch fixes it to keep the more specific type instead of always taking whichever side happens to be the replacement register.
Fixes: LCOMPILER-2715
Assisted by: Claude Sonnet
>From d5cb9eb3017ffedb0113593b7965bc800e75dde8 Mon Sep 17 00:00:00 2001
From: Adesh Adikane <aadikane at amd.com>
Date: Tue, 29 Sep 2026 06:53:05 +0000
Subject: [PATCH] Keep more specific type register
---
llvm/lib/CodeGen/MachineRegisterInfo.cpp | 11 +-
.../regbank-combiner-constrain-reg-attrs.ll | 174 ++++++++++++++++++
2 files changed, 184 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/GlobalISel/regbank-combiner-constrain-reg-attrs.ll
diff --git a/llvm/lib/CodeGen/MachineRegisterInfo.cpp b/llvm/lib/CodeGen/MachineRegisterInfo.cpp
index 035ccaac06a5a..95512c3369bf6 100644
--- a/llvm/lib/CodeGen/MachineRegisterInfo.cpp
+++ b/llvm/lib/CodeGen/MachineRegisterInfo.cpp
@@ -113,8 +113,17 @@ MachineRegisterInfo::constrainRegAttrs(Register Reg,
} else if (RegCB != ConstrainingRegCB)
return false;
}
- if (ConstrainingRegTy.isValid())
+ if (ConstrainingRegTy.isValid()) {
+ // RegTy and ConstrainingRegTy can be "equal" here while having different
+ // LLT::Kind, e.g. an untyped ANY_SCALAR vs a typed INTEGER/FLOAT of the
+ // same size (LLT::operator== treats these as equal). Don't lose the more
+ // specific type by blindly copying over ConstrainingRegTy in that case.
+ if (RegTy.isValid() &&
+ RegTy.isAnyScalar() != ConstrainingRegTy.isAnyScalar() &&
+ ConstrainingRegTy.isAnyScalar())
+ return true;
setType(Reg, ConstrainingRegTy);
+ }
return true;
}
diff --git a/llvm/test/CodeGen/AMDGPU/GlobalISel/regbank-combiner-constrain-reg-attrs.ll b/llvm/test/CodeGen/AMDGPU/GlobalISel/regbank-combiner-constrain-reg-attrs.ll
new file mode 100644
index 0000000000000..c8effcf7534fd
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/GlobalISel/regbank-combiner-constrain-reg-attrs.ll
@@ -0,0 +1,174 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc -global-isel -mtriple=amdgpu9.50-amd-amdhsa < %s | FileCheck %s
+
+; These all legalize a 64-bit op on a uniform value down to 32-bit scalar
+; instructions, whose results later get unmerged/re-merged by the regbank
+; combiner. Unifying the resulting untyped register with the typed result
+; via MachineRegisterInfo::constrainRegAttrs used to downgrade the latter's
+; type and make it unselectable.
+
+define amdgpu_kernel void @ctpop_uniform_i64(i64 %x) {
+; CHECK-LABEL: ctpop_uniform_i64:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: s_load_dwordx2 s[0:1], s[4:5], 0x0
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: s_bcnt1_i32_b64 s2, s[0:1]
+; CHECK-NEXT: s_and_b32 s3, s2, 1
+; CHECK-NEXT: s_cmp_lg_u32 s3, 0
+; CHECK-NEXT: s_cbranch_scc1 .LBB0_2
+; CHECK-NEXT: ; %bb.1: ; %if
+; CHECK-NEXT: s_mov_b32 s3, 0
+; CHECK-NEXT: s_xor_b64 s[0:1], s[0:1], s[2:3]
+; CHECK-NEXT: v_mov_b64_e32 v[0:1], s[0:1]
+; CHECK-NEXT: v_mov_b64_e32 v[2:3], 0
+; CHECK-NEXT: global_store_dwordx2 v[2:3], v[0:1], off
+; CHECK-NEXT: .LBB0_2: ; %else
+; CHECK-NEXT: s_endpgm
+entry:
+ %pop = call i64 @llvm.ctpop.i64(i64 %x)
+ %bit = and i64 %pop, 1
+ %cond = icmp eq i64 %bit, 0
+ br i1 %cond, label %if, label %else
+
+if:
+ %mix = xor i64 %x, %pop
+ store i64 %mix, ptr addrspace(1) null
+ ret void
+
+else:
+ ret void
+}
+
+define amdgpu_kernel void @cttz_uniform_i64(i64 %x) {
+; CHECK-LABEL: cttz_uniform_i64:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: s_load_dwordx2 s[0:1], s[4:5], 0x0
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: s_ff1_i32_b64 s2, s[0:1]
+; CHECK-NEXT: s_min_u32 s2, s2, 64
+; CHECK-NEXT: s_and_b32 s3, s2, 1
+; CHECK-NEXT: s_cmp_lg_u32 s3, 0
+; CHECK-NEXT: s_cbranch_scc1 .LBB1_2
+; CHECK-NEXT: ; %bb.1: ; %if
+; CHECK-NEXT: s_mov_b32 s3, 0
+; CHECK-NEXT: s_xor_b64 s[0:1], s[0:1], s[2:3]
+; CHECK-NEXT: v_mov_b64_e32 v[0:1], s[0:1]
+; CHECK-NEXT: v_mov_b64_e32 v[2:3], 0
+; CHECK-NEXT: global_store_dwordx2 v[2:3], v[0:1], off
+; CHECK-NEXT: .LBB1_2: ; %else
+; CHECK-NEXT: s_endpgm
+entry:
+ %ctz = call i64 @llvm.cttz.i64(i64 %x, i1 false)
+ %bit = and i64 %ctz, 1
+ %cond = icmp eq i64 %bit, 0
+ br i1 %cond, label %if, label %else
+
+if:
+ %mix = xor i64 %x, %ctz
+ store i64 %mix, ptr addrspace(1) null
+ ret void
+
+else:
+ ret void
+}
+
+define amdgpu_kernel void @ctlz_uniform_i64(i64 %x) {
+; CHECK-LABEL: ctlz_uniform_i64:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: s_load_dwordx2 s[0:1], s[4:5], 0x0
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: s_flbit_i32_b64 s2, s[0:1]
+; CHECK-NEXT: s_min_u32 s2, s2, 64
+; CHECK-NEXT: s_and_b32 s3, s2, 1
+; CHECK-NEXT: s_cmp_lg_u32 s3, 0
+; CHECK-NEXT: s_cbranch_scc1 .LBB2_2
+; CHECK-NEXT: ; %bb.1: ; %if
+; CHECK-NEXT: s_mov_b32 s3, 0
+; CHECK-NEXT: s_xor_b64 s[0:1], s[0:1], s[2:3]
+; CHECK-NEXT: v_mov_b64_e32 v[0:1], s[0:1]
+; CHECK-NEXT: v_mov_b64_e32 v[2:3], 0
+; CHECK-NEXT: global_store_dwordx2 v[2:3], v[0:1], off
+; CHECK-NEXT: .LBB2_2: ; %else
+; CHECK-NEXT: s_endpgm
+entry:
+ %clz = call i64 @llvm.ctlz.i64(i64 %x, i1 false)
+ %bit = and i64 %clz, 1
+ %cond = icmp eq i64 %bit, 0
+ br i1 %cond, label %if, label %else
+
+if:
+ %mix = xor i64 %x, %clz
+ store i64 %mix, ptr addrspace(1) null
+ ret void
+
+else:
+ ret void
+}
+
+define amdgpu_kernel void @lshr_uniform_i64(i64 %x) {
+; CHECK-LABEL: lshr_uniform_i64:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: s_load_dwordx2 s[0:1], s[4:5], 0x0
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: s_lshr_b32 s2, s1, 15
+; CHECK-NEXT: s_and_b32 s3, s2, 1
+; CHECK-NEXT: s_cmp_lg_u32 s3, 0
+; CHECK-NEXT: s_cbranch_scc1 .LBB3_2
+; CHECK-NEXT: ; %bb.1: ; %if
+; CHECK-NEXT: s_mov_b32 s3, 0
+; CHECK-NEXT: s_xor_b64 s[0:1], s[0:1], s[2:3]
+; CHECK-NEXT: v_mov_b64_e32 v[0:1], s[0:1]
+; CHECK-NEXT: v_mov_b64_e32 v[2:3], 0
+; CHECK-NEXT: global_store_dwordx2 v[2:3], v[0:1], off
+; CHECK-NEXT: .LBB3_2: ; %else
+; CHECK-NEXT: s_endpgm
+entry:
+ %shr = lshr i64 %x, 47
+ %bit = and i64 %shr, 1
+ %cond = icmp eq i64 %bit, 0
+ br i1 %cond, label %if, label %else
+
+if:
+ %mix = xor i64 %x, %shr
+ store i64 %mix, ptr addrspace(1) null
+ ret void
+
+else:
+ ret void
+}
+
+define amdgpu_kernel void @ashr_uniform_i64(i64 %x) {
+; CHECK-LABEL: ashr_uniform_i64:
+; CHECK: ; %bb.0: ; %entry
+; CHECK-NEXT: s_load_dwordx2 s[0:1], s[4:5], 0x0
+; CHECK-NEXT: s_waitcnt lgkmcnt(0)
+; CHECK-NEXT: s_and_b32 s2, s1, 1
+; CHECK-NEXT: s_cmp_lg_u32 s2, 0
+; CHECK-NEXT: s_cbranch_scc1 .LBB4_2
+; CHECK-NEXT: ; %bb.1: ; %if
+; CHECK-NEXT: s_ashr_i32 s3, s1, 31
+; CHECK-NEXT: s_mov_b32 s2, s1
+; CHECK-NEXT: s_xor_b64 s[0:1], s[0:1], s[2:3]
+; CHECK-NEXT: v_mov_b64_e32 v[0:1], s[0:1]
+; CHECK-NEXT: v_mov_b64_e32 v[2:3], 0
+; CHECK-NEXT: global_store_dwordx2 v[2:3], v[0:1], off
+; CHECK-NEXT: .LBB4_2: ; %else
+; CHECK-NEXT: s_endpgm
+entry:
+ %shr = ashr i64 %x, 32
+ %bit = and i64 %shr, 1
+ %cond = icmp eq i64 %bit, 0
+ br i1 %cond, label %if, label %else
+
+if:
+ %mix = xor i64 %x, %shr
+ store i64 %mix, ptr addrspace(1) null
+ ret void
+
+else:
+ ret void
+}
+
+declare i64 @llvm.ctpop.i64(i64)
+declare i64 @llvm.cttz.i64(i64, i1)
+declare i64 @llvm.ctlz.i64(i64, i1)
More information about the llvm-commits
mailing list