Issue 238129
Summary [AMDGPU] optimizeEndCf drops a join block's EXEC restore; RA rematerializes a VGPR constant there and inactive lanes keep a stale value
Labels new issue
Assignees
Reporter AviralGoelAMD
    Wrong code on AMDGPU (gfx950): a VGPR constant is rematerialized in a join block whose EXEC restore was removed by `SILowerControlFlow::optimizeEndCf`. Lanes that are inactive at that point keep a stale value from another live range, which is then read after EXEC is restored.

**Reproduced on:** llvm/llvm-project main nightly `9f081a5d7475` (2026-11-02, apt.llvm.org snapshot), main `fbe7392bdf` (2026-06-04), and ROCm/llvm-project `8f497e0992fb` (clang 23.0.0git). The open PR #225433 does not change the result.

### Reproducer
`l1_repro_opt.ll` (165 lines, below; no `undef`/`poison`):
```
llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx950 -O3 -filetype=obj l1_repro_opt.ll -o k.o
llvm-objdump -d --mcpu=gfx950 k.o
```
With `-amdgpu-remove-redundant-endcf=false` the problem goes away (the restore stays before the remat). Needs a recent `llc` (tested 2026-06-04 to 2026-11-02 main); LLVM 20 rejects one of the inline-asm strings.

### What happens (main `9f081a5d7475`)
```
0x0f8  v_mov_b32 v114, 0x10000                    ; first def of the constant
0x390 s_and_saveexec_b64 s[14:15], vcc            ; divergent region begins
0x488 v_mov_b32 v114, 0x44000                     ; v114 reused for another live range
0x500  s_and_b64 exec, exec, vcc                   ; tid == 0 only
0x504  s_cbranch_execz -> 0x16c
...
0x16c  (join block / loop latch, reached with the narrowed EXEC)
0x17c  v_mov_b32 v114, 0x10000 ; remat under narrowed EXEC
0x19c  s_waitcnt vmcnt(0) lgkmcnt(0)
0x1a0  s_or_b64 exec, exec, s[14:15]               ; restore comes AFTER the remat
...
0x2d4  buffer_load_dwordx4 v[74:77], v114, s[8:11], 0 offen   ; full EXEC: inactive lanes read 0x44000
```
Pass dumps (on the 2026-06-04 build): the join block still has `SI_END_CF` after `phi-node-elimination`; it is gone after `si-lower-control-flow` (`optimizeEndCf` removes it because the successor starts with another END_CF); the extra `V_MOV_B32` is inserted by greedy RA's live-range split into the end of that block. A control where the branch predicate is made uniform with `readfirstlane` compiles without the EXEC narrowing and is correct.

### Notes
- The reproducer contains one empty, clobber-only inline asm (`~{v185}`...`~{v243}`) to create register pressure. The original, unreduced kernel (a large gfx950 kernel) hit this up to 2026-06-04 and stopped hitting after an unrelated codegen change (`5515188560`, uniform `S_CSELECT` selection), which only shifted register allocation; the END_CF removal path is unchanged on main, so the defect looks hidden, not fixed.
- On hardware, the original kernel showed this as wrong data and, in a variant where the constant was an LDS address, as a memory aperture violation. The reduced file has only been compiled, not run.
- Expected: either keep the END_CF when the join block can still receive VGPR definitions, or avoid inserting VGPR writes into a block whose EXEC restore was elided.

<details><summary>l1_repro_opt.ll</summary>

```llvm
target triple = "amdgcn-amd-amdhsa"

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(read)
declare <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32>, i32, i32, i32 immarg) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(read)
declare i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32>, i32, i32, i32 immarg) #0

; Function Attrs: convergent mustprogress nocallback nofree nosync nounwind willreturn memory(none)
declare <4 x float> @llvm.amdgcn.mfma.scale.f32.16x16x128.f8f6f4.v8i32.v8i32(<8 x i32>, <8 x i32>, <4 x float>, i32 immarg, i32 immarg, i32 immarg, i32, i32 immarg, i32) #1

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare noundef range(i32 0, 1024) i32 @llvm.amdgcn.workitem.id.x() #2

; Function Attrs: nounwind
define amdgpu_kernel void @l1_repro(i32 %lane4, i32 %iv.next.for.header.468, i32 %tid3, i32 %iblk25207, i32 %lay, ptr addrspace(3) readnone captures(none) %0, i32 %sel10, i32 %div4623512, <4 x i8> %av447437.pre) local_unnamed_addr #3 {
entry:
  %tid.x = tail call i32 @llvm.amdgcn.workitem.id.x()
  %and34014 = and i32 %tid.x, 1
  %eq47391 = icmp eq i32 %and34014, 0
  %and34017 = and i32 %lane4, 1
  %cmp.38391.not = icmp eq i32 %and34017, 0
  %cmp.1988 = icmp slt i32 %lane4, 1
  br label %for.body.469

for.body.469: ; preds = %entry, %for.header.468.backedge
  tail call void asm sideeffect "ds_add_u32 $0, $1", "v,v"(i32 158208, i32 %sel10) #4
  %ldsw33300 = tail call i32 asm sideeffect "ds_read_b32 $0, $1\0A s_waitcnt lgkmcnt(0)", "=v,v"(i32 158220) #4
  %eq33301 = icmp eq i32 %ldsw33300, 0
  %sel33303 = select i1 %eq33301, i32 4194304, i32 0
 %asmcl.2900 = tail call { i32, i32, i32 } asm sideeffect "s_mov_b32 $0, $4\0A1:\0A ds_read_b32 $1, $3\0A s_waitcnt lgkmcnt(0)\0A v_readfirstlane_b32 $2, $1\0A s_cmp_ge_i32 $2, $5\0A s_cbranch_scc1 2f\0A s_sleep 1\0A s_sub_u32 $0, $0, 1\0A s_cmp_lg_u32 $0, 0\0A s_cbranch_scc1 1b\0A2:", "=&s,=&v,=&s,v,s,s,~{scc}"(i32 0, i32 %sel33303, i32 0) #4
 %asm33619 = tail call i32 asm sideeffect "global_load_dword $0, $1, off sc0 sc1\0A s_waitcnt vmcnt(0)", "=v,v"(i64 0) #4
  tail call void asm sideeffect "ds_add_u32 $0, $1", "v,v"(i32 158228, i32 0) #4
  tail call void asm sideeffect "global_atomic_add $0, $1, off sc1", "v,v,~{memory}"(i64 0, i32 0) #4
  %umf41053 = tail call <4 x float> asm sideeffect "v_mfma_scale_f32_16x16x128_f8f6f4 $0, $1, $2, $0, $3, $4 op_sel:[0,1,0] op_sel_hi:[0,0,0] cbsz:0 blgp:4", "=a,v,v,v,v,0"(<8 x i32> zeroinitializer, <4 x i32> zeroinitializer, i32 0, i32 0, <4 x float> zeroinitializer) #4
  %umf41054 = tail call <4 x float> asm sideeffect "v_mfma_scale_f32_16x16x128_f8f6f4 $0, $1, $2, $0, $3, $4 op_sel:[0,0,0] op_sel_hi:[0,1,0] cbsz:0 blgp:4", "=a,v,v,v,v,0"(<8 x i32> zeroinitializer, <4 x i32> zeroinitializer, i32 0, i32 0, <4 x float> zeroinitializer) #4
  %bl1646927 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 139264, i32 0, i32 0)
  %bl1646951 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 278528, i32 0, i32 0)
  %bl446962 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 %lane4, i32 0, i32 0)
  %bl1646975 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 417792, i32 0, i32 0)
  %bl1647004 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %lay, i32 0, i32 0)
  %bl447015 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 320, i32 0, i32 0)
  %bl1647028 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 136, i32 0, i32 0)
  %bl447039 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 0, i32 0, i32 0)
  %bl1647052 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 255, i32 0, i32 0)
  %bl447063 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 64, i32 0, i32 0)
  %bl1647076 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 65536, i32 0, i32 0)
  %bl1647105 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %lane4, i32 0, i32 0)
  %bl447116 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 255, i32 0, i32 0)
  %bl1647129 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %tid3, i32 0, i32 0)
  %bl1647153 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 64, i32 0, i32 0)
  %bl1647177 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 6, i32 0, i32 0)
  %bl447188 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 1, i32 0, i32 0)
  %bl1647206 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %iblk25207, i32 0, i32 0)
  %bl1647230 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %div4623512, i32 0, i32 0)
  %bl1647254 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 261120, i32 0, i32 0)
  %bl1647278 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 0, i32 0, i32 1)
  %bl1647307 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 1, i32 0, i32 0)
  %bl1647331 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 4, i32 0, i32 0)
  %bl447342 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 4096, i32 0, i32 0)
  %bl1647355 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %iv.next.for.header.468, i32 0, i32 0)
  %bl1647379 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 0, i32 0, i32 0)
  %itid340125 = tail call i32 asm sideeffect "", "=v,0"(i32 %tid.x) #4
  br i1 %eq47391, label %if.then.894, label %if.end.897

common.ret:                                       ; preds = %for.header.468.backedge
  ret void

if.then.894: ; preds = %for.body.469
  store <4 x i8> zeroinitializer, ptr addrspace(3) null, align 2147483648
  br label %if.end.897

if.end.897: ; preds = %if.then.894, %for.body.469
 %av447437 = phi <4 x i8> [ zeroinitializer, %if.then.894 ], [ %av447437.pre, %for.body.469 ]
  %bc47438 = bitcast <4 x i8> %av447437 to <1 x i32>
 %e47439 = extractelement <1 x i32> %bc47438, i64 0
  %eq47440 = icmp eq i32 %e47439, 0
  br i1 %eq47440, label %if.then.898, label %for.header.468.backedge

for.header.468.backedge: ; preds = %if.end.897, %if.end.942
  br i1 %cmp.1988, label %for.body.469, label %common.ret

if.then.898:                                      ; preds = %if.end.897
  %bl1647610 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %tid3, i32 0, i32 0)
  %bl1647639 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %iv.next.for.header.468, i32 0, i32 0)
  %bl1647668 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 0, i32 0, i32 0)
  %bl447672 = tail call i32 @llvm.amdgcn.raw.buffer.load.i32(<4 x i32> zeroinitializer, i32 0, i32 0, i32 0)
  %bl1647697 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 %lane4, i32 0, i32 0)
  %bl1647726 = tail call <4 x i32> @llvm.amdgcn.raw.buffer.load.v4i32(<4 x i32> zeroinitializer, i32 1, i32 0, i32 0)
  %pin48301 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> splat (i32 1)) #4
  %mfs48362 = tail call <4 x float> @llvm.amdgcn.mfma.scale.f32.16x16x128.f8f6f4.v8i32.v8i32(<8 x i32> splat (i32 1), <8 x i32> zeroinitializer, <4 x float> zeroinitializer, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0)
  %asmcl.3587 = tail call { i32, i32, i32, i32 } asm sideeffect "s_nop 1\0A v_mov_b32_dpp $0, $4 row_ror:8 row_mask:0xf bank_mask:0x3\0A v_mov_b32_dpp $1, $5 row_ror:8 row_mask:0xf bank_mask:0x3\0A v_mov_b32_dpp $2, $6 row_ror:8 row_mask:0xf bank_mask:0x3\0A v_mov_b32_dpp $3, $7 row_ror:8 row_mask:0xf bank_mask:0x3\0A", "=v,=v,=v,=v,v,v,v,v,0,1,2,3"(i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0) #4
  %pin48392 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1646927) #4
  tail call void asm sideeffect "", "~{v185},~{v186},~{v187},~{v188},~{v189},~{v190},~{v191},~{v192},~{v193},~{v194},~{v195},~{v196},~{v197},~{v198},~{v199},~{v200},~{v201},~{v202},~{v203},~{v204},~{v205},~{v206},~{v207},~{v208},~{v209},~{v210},~{v211},~{v212},~{v213},~{v214},~{v215},~{v216},~{v217},~{v218},~{v219},~{v220},~{v221},~{v222},~{v223},~{v224},~{v225},~{v226},~{v227},~{v228},~{v229},~{v230},~{v231},~{v232},~{v233},~{v234},~{v235},~{v236},~{v237},~{v238},~{v239},~{v240},~{v241},~{v242},~{v243}"() #4
  %pin48403 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1646951) #4
  %pin48409 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl446962) #4
  %pin48414 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1646975) #4
  %pin48425 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647610) #4
  %pin48426 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 0) #4
  %pin48443 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647004) #4
 %pin48449 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl447015) #4
  %pin48454 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647028) #4
  %pin48460 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl447039) #4
  %pin48465 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647052) #4
  %pin48471 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl447063) #4
  %pin48476 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647076) #4
  %pin48482 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 0) #4
  %pin48487 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647639) #4
 %pin48505 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647105) #4
  %pin48511 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl447116) #4
  %pin48516 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647129) #4
  %pin48527 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647153) #4
  %pin48538 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647177) #4
  %pin48544 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl447188) #4
  %pin48549 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647668) #4
  %pin48550 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl447672) #4
  %pin48567 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647206) #4
  %pin48578 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647230) #4
 %pin48584 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 0) #4
  %pin48589 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647254) #4
  %pin48600 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647278) #4
 %pin48611 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647697) #4
  %pin48612 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 0) #4
  %pin48629 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647307) #4
  %pin48640 = tail call <4 x i32> asm sideeffect "", "=&v,0,~{memory}"(<4 x i32> %bl1647331) #4
  %pin48646 = tail call i32 asm sideeffect "", "=&v,0,~{memory}"(i32 %bl447342) #4<truncated>Please see the issue for the entire body.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to