llvm-project
llvm-project copied to clipboard
[AMDGPU] scale_offset miscompiles wrapping offsets on gfx1250
With scale_offset, the hardware multiplies the offset register by the access size. Measured on gfx1250:
- Global and flat with an SGPR base:
base + sext(off) * size, in 64 bits. - SMEM:
base + zext((off * size) mod 2^32). Both instruction selectors setscale_offsetwhether the program scales the index in 32 or in 64 bits, so each kind of instruction miscompiles one of the two:
define amdgpu_ps <4 x float> @vmem(ptr addrspace(1) inreg %p, i32 %i) {
%s = shl i32 %i, 4
%e = sext i32 %s to i64
%a = getelementptr i8, ptr addrspace(1) %p, i64 %e
%v = load <4 x float>, ptr addrspace(1) %a, align 16
ret <4 x float> %v
}
define amdgpu_ps <4 x i32> @smem(ptr addrspace(4) inreg %p, i32 inreg %i) {
%e = zext i32 %i to i64
%a = getelementptr <4 x i32>, ptr addrspace(4) %p, i64 %e
%v = load <4 x i32>, ptr addrspace(4) %a, align 16
ret <4 x i32> %v
}
llc -mtriple=amdgpu12.50, with either selector:
global_load_b128 v[0:3], v0, s[0:1] scale_offset ; @vmem
s_load_b128 s[0:3], s[0:1], s2 offset:0x0 scale_offset nv ; @smem
@vmemwithi = 0x10000000: the shift wraps to 0, so it should load fromp, but it loads fromp + 4 GiB.@smemwithi = 0x10000001: it should load fromp + 4 GiB + 16, but it loads fromp + 16.scale_offsetis only correct when the hardware computes the same address as the program. For global and flat, the 32-bit shift or multiply must not wrap (nswor known bits). For SMEM,i * sizemust be known to fit in 32 bits.
@llvm/issue-subscribers-backend-amdgpu
Author: Petar Jovanovic (petar-jovanovic)
With `scale_offset`, the hardware multiplies the offset register by the access size. Measured on gfx1250:
- Global and flat with an SGPR base: `base + sext(off) * size`, in 64 bits.
- SMEM: `base + zext((off * size) mod 2^32)`.
Both instruction selectors set `scale_offset` whether the program scales the index in 32 or in 64 bits, so each kind of instruction miscompiles one of the two:
```llvm
define amdgpu_ps <4 x float> @vmem(ptr addrspace(1) inreg %p, i32 %i) {
%s = shl i32 %i, 4
%e = sext i32 %s to i64
%a = getelementptr i8, ptr addrspace(1) %p, i64 %e
%v = load <4 x float>, ptr addrspace(1) %a, align 16
ret <4 x float> %v
}
define amdgpu_ps <4 x i32> @smem(ptr addrspace(4) inreg %p, i32 inreg %i) {
%e = zext i32 %i to i64
%a = getelementptr <4 x i32>, ptr addrspace(4) %p, i64 %e
%v = load <4 x i32>, ptr addrspace(4) %a, align 16
ret <4 x i32> %v
}
```
`llc -mtriple=amdgpu12.50`, with either selector:
```
global_load_b128 v[0:3], v0, s[0:1] scale_offset ; @vmem
s_load_b128 s[0:3], s[0:1], s2 offset:0x0 scale_offset nv ; @smem
```
- `@vmem` with `i = 0x10000000`: the shift wraps to 0, so it should load from `p`, but it loads from `p + 4 GiB`.
- `@smem` with `i = 0x10000001`: it should load from `p + 4 GiB + 16`, but it loads from `p + 16`.
`scale_offset` is only correct when the hardware computes the same address as the program. For global and flat, the 32-bit shift or multiply must not wrap (`nsw` or known bits). For SMEM, `i * size` must be known to fit in 32 bits.