[AMDGPU] LLC pipeline fails to simplify trivial loops that OPT pipeline optimizes away
Summary
The LLC pipeline is unable to simplify trivial loops (loops that exit in the first iteration or exit without any iteration) that are successfully optimized away in the OPT pipeline. This results in suboptimal codegen with unnecessary loop structures in the final output.
Problem Description
When loops have statically determinable exit conditions that cause them to:
- Exit immediately without executing the loop body (exit on first iteration)
- Exit after exactly one iteration
The OPT pipeline successfully removes these trivial loops, but the LLC pipeline retains the loop structure, generating unnecessary branches and basic blocks.
Case 1: Loop exits immediately (no iterations)
Input IR: define protected amdgpu_kernel void @trivial_zero_itr(ptr addrspace(1) %out) { entry: br label %while
while: %done = phi i1 [ 1, %entry ], [ 0, %if ] br i1 %done, label %exit, label %if
if: store i32 5, ptr addrspace(1) %out br label %while
exit: ret void }
Expected behavior:
The loop should be eliminated entirely since %done is 1 (true) on entry from %entry, meaning the loop exits immediately without executing the %if block.
OPT pipeline solution:
IPSCCPPassdetermines that the%ifblock is unreachable and removes body instructions while keeping structure intactSimplifyCFGPassremoves the empty loop structure
LLC behavior: The loop structure is retained in the final codegen.
Case 2: Loop exits after one iteration
Input IR: define protected amdgpu_kernel void @trivial_one_itr(ptr addrspace(1) %out) { entry: br label %while
while: %done = phi i1 [ 0, %entry ], [ 1, %if ] br i1 %done, label %exit, label %if
if: store i32 5, ptr addrspace(1) %out br label %while
exit: ret void }
Expected behavior:
The loop should be unrolled/simplified since it only executes once (%done is 0 on entry, executes %if, then %done becomes 1 causing exit).
OPT pipeline solution:
LoopRotatePasstransforms the loopLICMPassoptimizes loop-invariant codeSimplifyCFGPasssimplifies the control flow
LLC behavior: The loop structure is retained in the final codegen.
Impact
This leads to:
- Suboptimal machine code with unnecessary branches
- Increased code size
- Potential performance degradation
- Missed optimization opportunities in backend
Expected Solution
The LLC pipeline should include passes or analyses that can:
- Detect trivial loops with statically determinable iteration counts (0 or 1 iterations)
- Either unroll these loops completely or eliminate them when they're dead code
- Simplify the resulting CFG to remove unnecessary basic blocks
Environment
- Target: AMDGPU (amdgpu_kernel calling convention)
- Likely affects other targets as well
Questions
- Should LLC incorporate similar constant propagation and CFG simplification passes?
- Is this intentional (separation of concerns between OPT and LLC)?
- Should there be a late-stage loop simplification pass in the LLC pipeline for these trivial cases?
Cc @arsenm @jayfoad @cdevadas @ssahasra
@llvm/issue-subscribers-backend-amdgpu
Author: Pankaj Dwivedi (PankajDwivedi-25)
The LLC pipeline is unable to simplify trivial loops (loops that exit in the first iteration or exit without any iteration) that are successfully optimized away in the OPT pipeline. This results in suboptimal codegen with unnecessary loop structures in the final output.
Problem Description
When loops have statically determinable exit conditions that cause them to:
- Exit immediately without executing the loop body (exit on first iteration)
- Exit after exactly one iteration
The OPT pipeline successfully removes these trivial loops, but the LLC pipeline retains the loop structure, generating unnecessary branches and basic blocks.
Case 1: Loop exits immediately (no iterations)
Input IR: define protected amdgpu_kernel void @trivial_waterfall_eq_zero(ptr addrspace(1) %out) { entry: br label %while
while: %done = phi i1 [ 1, %entry ], [ 0, %if ] br i1 %done, label %exit, label %if
if: store i32 5, ptr addrspace(1) %out br label %while
exit:
ret void
}Expected behavior:
The loop should be eliminated entirely since %done is 1 (true) on entry from %entry, meaning the loop exits immediately without executing the %if block.
OPT pipeline solution:
IPSCCPPassdetermines that the%ifblock is unreachable and removes body instructions while keeping structure intactSimplifyCFGPassremoves the empty loop structure
LLC behavior: The loop structure is retained in the final codegen.
Case 2: Loop exits after one iteration
Input IR: define protected amdgpu_kernel void @trivial_waterfall_eq_zero(ptr addrspace(1) %out) { entry: br label %while
while: %done = phi i1 [ 0, %entry ], [ 1, %if ] br i1 %done, label %exit, label %if
if: store i32 5, ptr addrspace(1) %out br label %while
exit:
ret void
}Expected behavior:
The loop should be unrolled/simplified since it only executes once (%done is 0 on entry, executes %if, then %done becomes 1 causing exit).
OPT pipeline solution:
LoopRotatePasstransforms the loopLICMPassoptimizes loop-invariant codeSimplifyCFGPasssimplifies the control flow
LLC behavior: The loop structure is retained in the final codegen.
Impact
This leads to:
- Suboptimal machine code with unnecessary branches
- Increased code size
- Potential performance degradation
- Missed optimization opportunities in backend
Expected Solution
The LLC pipeline should include passes or analyses that can:
- Detect trivial loops with statically determinable iteration counts (0 or 1 iterations)
- Either unroll these loops completely or eliminate them when they're dead code
- Simplify the resulting CFG to remove unnecessary basic blocks
Environment
- Target: AMDGPU (amdgpu_kernel calling convention)
- Likely affects other targets as well
Questions
- Should LLC incorporate similar constant propagation and CFG simplification passes?
- Is this intentional (separation of concerns between OPT and LLC)?
- Should there be a late-stage loop simplification pass in the LLC pipeline for these trivial cases?
The LLC pipeline is unable to simplify trivial loops (loops that exit in the first iteration or exit without any iteration) that are successfully optimized away in the OPT pipeline.
The CodeGen pass pipeline is not intended to be a complete optimization solution. The expectation is most of the target independent optimizations like this should have been handled by the middle end already. The codegen pass pipeline should not be going out of its way to take care of cases that the middle end should have already handled
I agree, but is it possible that the IR optimizations in llc pipeline pre-isel could generate such an optimization scope?
I can see we already have many IR passes pre-isel running in llc pipeline; they are also present in the IR pipeline.
I see it is worth doing this, as one of the use cases for this will be just after the AMDGPUUniformIntrinsicCombine if enabled. We had an offline discussion where it was mentioned by @ssahasra that some applications generate trivial loops significantly, that was also motivation behind the pass AMDGPUUniformIntrinsicCombine but the issue here is, this pass leaves the trivial loop structure mentioned above unoptimized.
I am sure that having this optimization given AMDGPUUniformIntrinsicCombine enabled will surely do less code gen and also it will boost the performance since we will not have those branch instructions.
The codegen IR pipeline should avoid adding more optimizations. It makes more sense to move AMDGPUUniformIntrinsicCombine to the middle end then try to clean up after it in codegen
It makes more sense to move AMDGPUUniformIntrinsicCombine to the middle
by middle end, you mean here to move in opt pipeline? this pass is already present in opt.
It makes more sense to move AMDGPUUniformIntrinsicCombine to the middle
by middle end, you mean here to move in opt pipeline? this pass is already present in opt.
Yes
It's not codegen's responsibility to re-run every possible optimization. In fact, it's better to reduce the number of high level optimizations in the backend pipeline. If there's a concrete optimization issue in a complete compilation pipeline (i.e clang -O2 run to the end), should open a new issue for a phase ordering problem