You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
MLIR generator should inline trivial return-by-reference accessor functions at IR gen time #474
The Flow MLIR generator emits accessor functions like function drawshim_dc_yh_addr() -> ptr<i32> { return &dc_yh } as full func.func definitions. These become call+load pairs in the generated LLVM IR:
LLVM can inline these at -O2/-O3, but the inlining happens late in the pipeline. By the time it runs, the loop body has already been analyzed with the call boundaries intact, which blocks early LICM and vectorization decisions.
Context
Discovered while compiling Doom through the Flow MLIR backend. The Doom source uses accessor functions for every global variable that the renderer touches:
function drawshim_dc_yh_addr() -> ptr<i32> { return &dc_yh }
function drawshim_dc_yl_addr() -> ptr<i32> { return &dc_yl }
function drawshim_dc_x_addr() -> ptr<i32> { return &dc_x }
function drawshim_dc_iscale_addr() -> ptr<i32> { return &dc_iscale }
function drawshim_dc_texturemid_addr() -> ptr<i32> { return &dc_texturemid }
function drawshim_dc_source_addr() -> ptr<ptr<u8>> { return &dc_source }
The hottest loop, R_DrawColumn, makes 8 such accessor calls per iteration. Each call is a single return &global. The generated loop body has 8 call instructions that could be eliminated entirely at generation time.
Proposed fix
Add an early inlining pass in the MLIR generator that recognizes functions matching the pattern:
Function body is a single return &<global> or return <global>
No parameters
No side effects
Returns a pointer or value
When detected, substitute the function body directly at every call site during IR generation. The accessor function definition can still be emitted for external linkage, but internal call sites should use the global address directly.
This reduces the hot loop from 8 call+load pairs to 8 loads, which LLVM can then hoist, vectorize, or eliminate more effectively.
Impact
This is the second biggest blocker for -O3 producing useful code on Flow-generated MLIR. Combined with loop canonicalization (issue #473), this would allow -O3 to produce smaller and faster binaries for the Doom rendering loops.
The doom-flow project has 1005 CRC32 parity samples that can validate this change.
Implementation note from cleanup pass: this is better expressed as a trivial-function canonicalization on FIR/early IR, before backend lowering, rather than hard-coding accessor recognition only in the MLIR generator. Recognize zero-arg, side-effect-free single-return functions whose result is a global address/value; substitute internal calls with the corresponding symbol/address while preserving externally visible definitions where required. This makes the optimization available to both C and MLIR and gives one place to prove linkage/visibility/alias semantics. Add negative tests for mutable locals, volatile/extern globals, calls/effects in the body, parameters, and address-taking of the accessor function itself; use doom-flow CRC parity as the end-to-end guard.
Activity
godofecht commentedon Aug 17, 2026
Implementation note from cleanup pass: this is better expressed as a trivial-function canonicalization on FIR/early IR, before backend lowering, rather than hard-coding accessor recognition only in the MLIR generator. Recognize zero-arg, side-effect-free single-return functions whose result is a global address/value; substitute internal calls with the corresponding symbol/address while preserving externally visible definitions where required. This makes the optimization available to both C and MLIR and gives one place to prove linkage/visibility/alias semantics. Add negative tests for mutable locals, volatile/extern globals, calls/effects in the body, parameters, and address-taking of the accessor function itself; use doom-flow CRC parity as the end-to-end guard.