| Issue |
233612
|
| Summary |
[NVPTX][LSR] Search-space narrowing keeps a solution worse than baseline and drops shared-load immediate offsets
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
praveenperera
|
On NVPTX, LoopStrengthReduce rewrites a loop of shared-memory loads at `base + (i % 3) * 16384 + C` so that every load gets its own `add` and none keeps its immediate offset. LSR's own cost model rates the result as worse than the original loop, but it keeps the result. With `-disable-lsr`, every load is `ld.shared [reg+imm]`.
Compiler Explorer (trunk, with and without `-disable-lsr`): https://godbolt.org/z/o4qjds5M6
```llvm
target datalayout = "e-p3:32:32-p4:32:32-p5:32:32-p6:32:32-p7:32:32-p101:32:32-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64"
target triple = "nvptx64-nvidia-cuda"
; 3-slot shared-memory ring: each iteration stores to i << 14 and loads
; 8 floats from slot (i % 3) at constant offsets from a thread base x + y
define ptx_kernel void @ring(i32 %x, i32 %y, i32 %n, ptr addrspace(1) %out) {
entry:
%base = add i32 %x, %y
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
%acc = phi float [ 0.0, %entry ], [ %s7, %loop ]
%w = shl i32 %i, 14
%wp = inttoptr i32 %w to ptr addrspace(3)
store float %acc, ptr addrspace(3) %wp, align 4
%slot = urem i32 %i, 3
%slot.off = shl i32 %slot, 14
%a = add i32 %slot.off, %base
%a0 = add i32 %a, 4
%p0 = inttoptr i32 %a0 to ptr addrspace(3)
%v0 = load float, ptr addrspace(3) %p0, align 4
%s0 = fadd float %acc, %v0
%a1 = add i32 %a, 8
%p1 = inttoptr i32 %a1 to ptr addrspace(3)
%v1 = load float, ptr addrspace(3) %p1, align 4
%s1 = fadd float %s0, %v1
%a2 = add i32 %a, 12
%p2 = inttoptr i32 %a2 to ptr addrspace(3)
%v2 = load float, ptr addrspace(3) %p2, align 4
%s2 = fadd float %s1, %v2
%a3 = add i32 %a, 16
%p3 = inttoptr i32 %a3 to ptr addrspace(3)
%v3 = load float, ptr addrspace(3) %p3, align 4
%s3 = fadd float %s2, %v3
%a4 = add i32 %a, 20
%p4 = inttoptr i32 %a4 to ptr addrspace(3)
%v4 = load float, ptr addrspace(3) %p4, align 4
%s4 = fadd float %s3, %v4
%a5 = add i32 %a, 24
%p5 = inttoptr i32 %a5 to ptr addrspace(3)
%v5 = load float, ptr addrspace(3) %p5, align 4
%s5 = fadd float %s4, %v5
%a6 = add i32 %a, 28
%p6 = inttoptr i32 %a6 to ptr addrspace(3)
%v6 = load float, ptr addrspace(3) %p6, align 4
%s6 = fadd float %s5, %v6
%a7 = add i32 %a, 32
%p7 = inttoptr i32 %a7 to ptr addrspace(3)
%v7 = load float, ptr addrspace(3) %p7, align 4
%s7 = fadd float %s6, %v7
%i.next = add nuw i32 %i, 1
%done = icmp eq i32 %i.next, %n
br i1 %done, label %exit, label %loop
exit:
store float %s7, ptr addrspace(1) %out, align 4
ret void
}
```
```sh
llc -march=nvptx64 -mcpu=sm_80 -target-abi=shortptr repro.ll -o -
```
Releases before `-target-abi=shortptr` existed need `-nvptx-short-ptr` instead.
### Output
Trunk:
```
$L__BB0_1:
mul.hi.u32 %r11, %r48, -1431655765;
shr.u32 %r12, %r11, 1;
mul.lo.s32 %r13, %r12, 49152;
sub.s32 %r14, %r1, %r13;
sub.s32 %r15, %r2, %r13;
sub.s32 %r16, %r3, %r13;
sub.s32 %r17, %r4, %r13;
sub.s32 %r18, %r5, %r13;
sub.s32 %r19, %r6, %r13;
sub.s32 %r20, %r7, %r13;
sub.s32 %r21, %r8, %r13;
st.shared.b32 [%r46], %r49;
add.s32 %r22, %r9, %r46;
add.s32 %r23, %r22, %r21;
ld.shared.b32 %r24, [%r23];
add.rn.f32 %r25, %r49, %r24;
add.s32 %r26, %r22, %r20;
ld.shared.b32 %r27, [%r26];
...
add.s32 %r44, %r22, %r14;
ld.shared.b32 %r45, [%r44];
add.rn.f32 %r49, %r43, %r45;
add.s32 %r48, %r48, 1;
add.s32 %r47, %r47, -1;
add.s32 %r46, %r46, 16384;
setp.ne.b32 %p1, %r47, 0;
@%p1 bra $L__BB0_1;
```
With `-disable-lsr`:
```
$L__BB0_1:
shl.b32 %r5, %r27, 14;
st.shared.b32 [%r5], %r28;
mul.hi.u32 %r6, %r27, -1431655765;
shr.u32 %r7, %r6, 1;
mul.lo.s32 %r8, %r7, 3;
sub.s32 %r9, %r27, %r8;
shl.b32 %r10, %r9, 14;
add.s32 %r11, %r10, %r1;
ld.shared.b32 %r12, [%r11+4];
add.rn.f32 %r13, %r28, %r12;
ld.shared.b32 %r14, [%r11+8];
...
ld.shared.b32 %r26, [%r11+32];
add.rn.f32 %r28, %r25, %r26;
add.s32 %r27, %r27, 1;
setp.ne.b32 %p1, %r27, %r2;
@%p1 bra $L__BB0_1;
```
| `llc` flags (trunk) | loads with `[reg+imm]` | loop instructions | `.b32` registers |
| --- | --- | --- | --- |
| default | 0 of 8 | 42 | 50 |
| `-disable-lsr` | 8 of 8 | 27 | 29 |
| `-lsr-complexity-limit=1000000` | 8 of 8 | 27 | 28 |
| `-lsr-exp-narrow` | 8 of 8 | 27 | 28 |
| `-lsr-filter-same-scaled-reg=false` | 8 of 8 | 26 | 27 |
| `-lsr-drop-solution` | 8 of 8 | 27 | 29 |
The default result is the same with LLVM 20.1.0, 21.1.0, 21.1.8, 22.1.0, 23.1.3 and trunk.
### LSR debug output
`-debug-_only_=loop-reduce` on an assertions build of trunk:
```
The baseline solution requires 12 instructions 10 regs, with addrec cost 2, plus 50 setup cost
The search space is too complex.
Narrowing the search space by eliminating formulae which use a superset of registers used by other formulae.
...
Narrowing the search space by assuming {0,+,16384}<%loop> will yield profitable reuse.
The search space is too complex.
Narrowing the search space by assuming %x will yield profitable reuse.
Deleting reg(((-49152 * ({0,+,1}<nuw><%loop> /u 3)) + %x + %y)) + 1*reg({0,+,16384}<%loop>) + imm(32)
Deleting reg(%y) + reg({0,+,16384}<%loop>) + 1*reg((32 + (-49152 * ({0,+,1}<nuw><%loop> /u 3)) + %x))
...
The chosen solution requires 29 instructions 11 regs, with addrec cost 2, plus 16 base adds, plus 43 setup cost:
Baseline is more profitable than chosen solution, add option 'lsr-drop-solution' to drop LSR solution.
```
The search space goes over `lsr-complexity-limit`, and `NarrowSearchSpaceByPickingWinnerRegs` keeps only the formulae that contain `%x`. That deletes the `reg(base) + reg({0,+,16384}) + imm(N)` formulae, which let all eight loads share one base register. With `-lsr-complexity-limit=1000000` the narrowing stops after the `{0,+,16384}` step, and LSR chooses a solution of 21 instructions and 3 registers.
### Conditions
Each of these changes makes the problem go away:
- 7 loads instead of 8: the search space stays under the limit
- a power-of-two slot count (`urem i32 %i, 2` or `4`); 5 and 6 slots still reproduce
- removing the store at `i << 14`, which has the same stride as the slot term
- a base from one argument instead of `%x + %y`
The loads get their addresses through `inttoptr i32 ... to ptr addrspace(3)`, so LSR records them as `Kind=Basic` uses, not `Kind=Address`. If the same addresses are written as `getelementptr i8, ptr addrspace(3) null, i32 %aN`, LSR records address uses and keeps 7 of the 8 immediate offsets. Code generators that keep shared addresses as 32-bit integers, and inline-asm operands, produce the `inttoptr` form.
NVPTX does not override `TTI::shouldDropLSRSolutionIfLessProfitable`, so LSR keeps its solution even after reporting that the baseline is cheaper.
### Background
This came from the main loop of a 3-stage `cp.async` tensor-core GEMM generated by cuda-oxide (NVIDIA/cuda-rust#1465). In that kernel, `-disable-lsr` takes the sm_120 SASS from 569 to 523 instructions and from 115 to 106 registers, and raises the shared loads that keep an immediate offset from 30 to 37 of 58.
Related: #31895 (LSR picks offsets that cannot be folded, on SystemZ) and #186135 (LSR keeps a solution its cost model rates worse than the baseline, on RISC-V).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs