| Issue |
237718
|
| Summary |
[AMDGPU] performMulhsCombine has no width check: `mulhs i24` (also i7/i8/i16/i64) becomes MULHI_I24 (bits 32..47), so the `_BitInt(24)` high multiply returns the wrong bits
|
| Labels |
backend:AMDGPU,
miscompilation,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
/* [AMDGPU] performMulhsCombine turns mulhs of types other than i32 into
* MULHI_I24 + sign_extend/truncate. MULHI_I24 (v_mul_hi_i32_i24) returns bits
* 32..47 of the 48-bit product, but mulhs iN needs bits N..2N-1. The combine
* has no width check (performMulhuCombine at least rejects types wider than 32).
* InstCombine turns ((_BitInt(48))a * b) >> 24 on _BitInt(24) into llvm.smulh.i24.
* Affected: every GPU with 24-bit multiplies (tahiti, gfx900, gfx1100, ...);
* llvm.smulh.i64 of 24-bit sign-extended values (IR) is wrong too.
*
* Godbolt: compiler "x86-64 clang (trunk)" with flags:
* --target=amdgcn-amd-amdhsa -mcpu=gfx900 -O2 -nogpulib
* Godbolt cannot execute AMDGPU code (main() only illustrates the check; a GPU
* program would run h24 in a kernel). View the assembly of `h24`:
* buggy: v_mul_hi_i32_i24 v0, v0, v1 ; v_bfe_i32 v0, v0, 0, 24 (bits 32..55)
* correct: a 48-bit product (v_mul_i32_i24 + v_mul_hi_i32_i24) shifted right by 24
*
* EXPECTED (correct compiler): h24(2^22, 2^22) = 1048576 (expected 1048576)
* BUGGY (vanilla trunk): h24(2^22, 2^22) = 4096 (expected 1048576)
* MISMATCH (exit 1)
*/
#include <stdio.h>
typedef _BitInt(24) s24;
__attribute__((noinline)) s24 h24(s24 a, s24 b) {
return (s24)(((_BitInt(48))a * b) >> 24); /* high 24 bits of the 48-bit product */
}
volatile int ga = 1 << 22, gb = 1 << 22;
int main(void) {
int r = (int)h24((s24)ga, (s24)gb);
printf("h24(2^22, 2^22) = %d (expected 1048576)\n", r);
if (r != 1048576) {
printf("MISMATCH\n");
return 1;
}
return 0;
}
```
Resulted in this output:
```
; h24, clang --target=amdgcn-amd-amdhsa -mcpu=gfx900 -O2 -nogpulib
h24:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_mul_hi_i32_i24_e32 v0, v0, v1 ; bits 32..55 of a*b, but bits 24..47 are needed
v_bfe_i32 v0, v0, 0, 24
s_setpc_b64 s[30:31]
```
| a, b | expected h24(a, b) | actual (from the machine code) |
|------|--------------------|--------------------------------|
| 2^22, 2^22 | 2^44 >> 24 = 1048576 | 2^44 >> 32 = 4096 |
(The expected value comes from the same program built with clang -O2 on x86-64 and run, which prints `h24(2^22, 2^22) = 1048576`. No AMDGPU hardware was available, so the actual value is read from the machine code.) Correct code would be, e.g., `v_lshlrev_b32 v0, 8, v0; v_mul_hi_i32 v0, v0, v1; v_bfe_i32 v0, v0, 0, 24`.
Trunk: https://godbolt.org/z/xhaodz9TT (x86-64 clang trunk, `--target=amdgcn-amd-amdhsa -mcpu=gfx900 -O2 -nogpulib`, assembly view; reproduces as of 2026-10-23)
Root cause: `AMDGPUTargetLowering::performMulhsCombine` in llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp rewrites any scalar `mulhs` whose operands fit in 24 bits to `MULHI_I24` followed by a sign-extend or truncate to the original type. It never checks that the type is i32. `MULHI_I24` (`v_mul_hi_i32_i24`) returns bits 32..47 of the product, which is the high half of an i32 multiply, while `mulhs iN` needs bits N..2N-1. For i24 it returns `(a*b) >> 32` instead of `>> 24`. For i64 with 24-bit sign-extended operands it returns bits 32..47 instead of the sign of the product. InstCombine forms `llvm.smulh.i24` from the `_BitInt(24)` source above, and every subtarget with 24-bit multiplies is affected.
Suggested fix: Return early from `performMulhsCombine` unless `VT` is exactly i32. `performMulhuCombine`, which only rejects types wider than 32 bits, needs the same restriction for narrow types. Validated locally (a local tree carries the `performMulhsCombine` fix, and `h24` then computes `(a*b) >> 24`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs