[llvm-branch-commits] [llvm] [AMDGPU] Track GlobalISel known bits for mul24 intrinsics (PR #238437)
Alan Li via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 4 05:05:12 PST 2026
https://github.com/lialan updated https://github.com/llvm/llvm-project/pull/238437
>From 5b63e40cbe34db19941d086e6bd47d2a4f6c6425 Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Wed, 4 Nov 2026 05:04:21 -0800
Subject: [PATCH] [AMDGPU] Infer mul24 known bits with unknown operands
Do not stop GlobalISel known-bits analysis when a mul24 operand has no known bits. Truncating to 24 bits and extending to the result width can still establish useful product bounds through the shared helper added by #236738.
Cover both operand orders, ignored high bits, zero and overflow boundaries, and i64 results without limiting known bits to the i32 operand width.
---
llvm/lib/Target/AMDGPU/SIISelLowering.cpp | 4 -
.../amdgpu-mul24-knownbits-intrinsics.ll | 250 ++++++++++++++++++
2 files changed, 250 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/CodeGen/AMDGPU/amdgpu-mul24-knownbits-intrinsics.ll
diff --git a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
index 5846bb7c53d9d..4271f66cee14e 100644
--- a/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/SIISelLowering.cpp
@@ -20724,13 +20724,9 @@ void SITargetLowering::computeKnownBitsForTargetInstr(
KnownBits RHSKnown;
VT.computeKnownBitsImpl(RHS, RHSKnown, DemandedElts, Depth + 1);
- if (RHSKnown.isUnknown())
- break;
KnownBits LHSKnown;
VT.computeKnownBitsImpl(LHS, LHSKnown, DemandedElts, Depth + 1);
- if (LHSKnown.isUnknown())
- break;
bool SelfMultiply =
LHS == RHS && isGuaranteedNotToBeUndef(LHS, MRI, Depth + 1);
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-mul24-knownbits-intrinsics.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-mul24-knownbits-intrinsics.ll
new file mode 100644
index 0000000000000..b359b132cc3dd
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-mul24-knownbits-intrinsics.ll
@@ -0,0 +1,250 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=amdgpu9.00-amd-amdhsa < %s | FileCheck --check-prefix=GCN %s
+; RUN: llc -mtriple=amdgpu9.00-amd-amdhsa -global-isel -global-isel-abort=1 -verify-machineinstrs < %s | FileCheck --check-prefix=GISEL %s
+
+; Both operands are negative after sign extending their low 24 bits, even
+; though their i32 sign bits are unknown.
+define i32 @mul_i24_negative(i32 %x, i32 %y) {
+; GCN-LABEL: mul_i24_negative:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mov_b32_e32 v0, 0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_i24_negative:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mov_b32_e32 v0, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %a = or i32 %x, 16777088
+ %b = or i32 %y, 16777088
+ %mul = call i32 @llvm.amdgcn.mul.i24(i32 %a, i32 %b)
+ %r = lshr i32 %mul, 15
+ ret i32 %r
+}
+
+; The positive operand is nonzero, so the product is strictly negative.
+define i32 @mul_i24_mixed(i32 %x, i32 %y) {
+; GCN-LABEL: mul_i24_mixed:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_or_b32_e32 v0, 0xfffff0, v0
+; GCN-NEXT: v_and_or_b32 v1, v1, 6, 1
+; GCN-NEXT: v_mul_i32_i24_e32 v0, v0, v1
+; GCN-NEXT: v_ashrrev_i32_e32 v0, 7, v0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_i24_mixed:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mov_b32_e32 v0, -1
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %a = or i32 %x, 16777200
+ %mask = and i32 %y, 7
+ %b = or i32 %mask, 1
+ %mul = call i32 @llvm.amdgcn.mul.i24(i32 %a, i32 %b)
+ %r = ashr i32 %mul, 7
+ ret i32 %r
+}
+
+define i32 @mul_i24_may_be_zero(i32 %x, i32 %y) {
+; GCN-LABEL: mul_i24_may_be_zero:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_or_b32_e32 v0, 0xfffff0, v0
+; GCN-NEXT: v_and_b32_e32 v1, 7, v1
+; GCN-NEXT: v_mul_i32_i24_e32 v0, v0, v1
+; GCN-NEXT: v_ashrrev_i32_e32 v0, 7, v0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_i24_may_be_zero:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_or_b32_e32 v0, 0xfffff0, v0
+; GISEL-NEXT: v_and_b32_e32 v1, 7, v1
+; GISEL-NEXT: v_mul_i32_i24_e32 v0, v0, v1
+; GISEL-NEXT: v_ashrrev_i32_e32 v0, 7, v0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %a = or i32 %x, 16777200
+ %b = and i32 %y, 7
+ %mul = call i32 @llvm.amdgcn.mul.i24(i32 %a, i32 %b)
+ %r = ashr i32 %mul, 7
+ ret i32 %r
+}
+
+; A product of two negative i24 values can overflow i32.
+define i32 @mul_i24_overflow(i32 %x, i32 %y) {
+; GCN-LABEL: mul_i24_overflow:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_or_b32_e32 v0, 0x800000, v0
+; GCN-NEXT: v_or_b32_e32 v1, 0x800000, v1
+; GCN-NEXT: v_mul_i32_i24_e32 v0, v0, v1
+; GCN-NEXT: v_lshrrev_b32_e32 v0, 31, v0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_i24_overflow:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_or_b32_e32 v0, 0x800000, v0
+; GISEL-NEXT: v_or_b32_e32 v1, 0x800000, v1
+; GISEL-NEXT: v_mul_i32_i24_e32 v0, v0, v1
+; GISEL-NEXT: v_lshrrev_b32_e32 v0, 31, v0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %a = or i32 %x, 8388608
+ %b = or i32 %y, 8388608
+ %mul = call i32 @llvm.amdgcn.mul.i24(i32 %a, i32 %b)
+ %r = lshr i32 %mul, 31
+ ret i32 %r
+}
+
+; Bits 24-31 are ignored, including the i32 sign bit.
+define i32 @mul_u24_high_bits(i32 %x, i32 %y) {
+; GCN-LABEL: mul_u24_high_bits:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mov_b32_e32 v0, 0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_u24_high_bits:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mov_b32_e32 v0, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %mask = and i32 %x, -16777209
+ %a = or i32 %mask, -2147483648
+ %mul = call i32 @llvm.amdgcn.mul.u24(i32 %a, i32 %y)
+ %r = lshr i32 %mul, 27
+ ret i32 %r
+}
+
+; Bits 24-31 are ignored, including the i32 sign bit.
+define i32 @mul_u24_high_bits_swapped(i32 %x, i32 %y) {
+; GCN-LABEL: mul_u24_high_bits_swapped:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mov_b32_e32 v0, 0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_u24_high_bits_swapped:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mov_b32_e32 v0, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %mask = and i32 %x, -16777209
+ %a = or i32 %mask, -2147483648
+ %mul = call i32 @llvm.amdgcn.mul.u24(i32 %y, i32 %a)
+ %r = lshr i32 %mul, 27
+ ret i32 %r
+}
+
+define i32 @mul_u24_overflow(i32 %x, i32 %y) {
+; GCN-LABEL: mul_u24_overflow:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mul_u32_u24_e32 v0, v0, v1
+; GCN-NEXT: v_lshrrev_b32_e32 v0, 31, v0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_u24_overflow:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mul_u32_u24_e32 v0, v0, v1
+; GISEL-NEXT: v_lshrrev_b32_e32 v0, 31, v0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %mul = call i32 @llvm.amdgcn.mul.u24(i32 %x, i32 %y)
+ %r = lshr i32 %mul, 31
+ ret i32 %r
+}
+
+declare i32 @llvm.amdgcn.mul.i24(i32, i32)
+declare i32 @llvm.amdgcn.mul.u24(i32, i32)
+
+; These products can have nonzero upper words. Operand widths must not limit
+; the width of the known bits returned for the i64 result.
+define i64 @mul_u24_i64_high(i32 %x, i32 %y) {
+; GCN-LABEL: mul_u24_i64_high:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mul_hi_u32_u24_e32 v0, v0, v1
+; GCN-NEXT: v_mov_b32_e32 v1, 0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_u24_i64_high:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mul_hi_u32_u24_e32 v0, v0, v1
+; GISEL-NEXT: v_mov_b32_e32 v1, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %mul = call i64 @llvm.amdgcn.mul.u24.i64(i32 %x, i32 %y)
+ %r = lshr i64 %mul, 32
+ ret i64 %r
+}
+
+define i64 @mul_i24_i64_high(i32 %x, i32 %y) {
+; GCN-LABEL: mul_i24_i64_high:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mul_hi_i32_i24_e32 v0, v0, v1
+; GCN-NEXT: v_mov_b32_e32 v1, 0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_i24_i64_high:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mul_hi_i32_i24_e32 v0, v0, v1
+; GISEL-NEXT: v_mov_b32_e32 v1, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %mul = call i64 @llvm.amdgcn.mul.i24.i64(i32 %x, i32 %y)
+ %r = lshr i64 %mul, 32
+ ret i64 %r
+}
+
+; The product is strictly negative after sign extending the low 24 bits.
+define i64 @mul_i24_i64_negative(i32 %x, i32 %y) {
+; GCN-LABEL: mul_i24_i64_negative:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mov_b32_e32 v0, 1
+; GCN-NEXT: v_mov_b32_e32 v1, 0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_i24_i64_negative:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_or_b32_e32 v0, 0x800000, v0
+; GISEL-NEXT: v_and_or_b32 v1, v1, 7, 1
+; GISEL-NEXT: v_mul_hi_i32_i24_e32 v0, v0, v1
+; GISEL-NEXT: v_lshrrev_b32_e32 v0, 31, v0
+; GISEL-NEXT: v_mov_b32_e32 v1, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %a = or i32 %x, 8388608
+ %mask = and i32 %y, 7
+ %b = or i32 %mask, 1
+ %mul = call i64 @llvm.amdgcn.mul.i24.i64(i32 %a, i32 %b)
+ %r = lshr i64 %mul, 63
+ ret i64 %r
+}
+
+; Unsigned i24 products fit in 48 bits, independently of operand bits 24-31.
+define i64 @mul_u24_i64_known_zero(i32 %x, i32 %y) {
+; GCN-LABEL: mul_u24_i64_known_zero:
+; GCN: ; %bb.0:
+; GCN-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GCN-NEXT: v_mov_b32_e32 v0, 0
+; GCN-NEXT: v_mov_b32_e32 v1, 0
+; GCN-NEXT: s_setpc_b64 s[30:31]
+;
+; GISEL-LABEL: mul_u24_i64_known_zero:
+; GISEL: ; %bb.0:
+; GISEL-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
+; GISEL-NEXT: v_mov_b32_e32 v0, 0
+; GISEL-NEXT: v_mov_b32_e32 v1, 0
+; GISEL-NEXT: s_setpc_b64 s[30:31]
+ %mul = call i64 @llvm.amdgcn.mul.u24.i64(i32 %x, i32 %y)
+ %r = lshr i64 %mul, 48
+ ret i64 %r
+}
+
+declare i64 @llvm.amdgcn.mul.i24.i64(i32, i32)
+declare i64 @llvm.amdgcn.mul.u24.i64(i32, i32)
More information about the llvm-branch-commits
mailing list