Skip to content

MLIR generator should inline trivial return-by-reference accessor functions at IR gen time #474

Closed

Description

@godofecht

Problem

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:

%1 = call ptr @drawshim_dc_yh_addr()
%2 = load i32, ptr %1, align 4

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:

  1. Function body is a single return &<global> or return <global>
  2. No parameters
  3. No side effects
  4. 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.

Activity

  1. added
    enhancement
    on Aug 16, 2026
  2. godofecht commented on Aug 17, 2026

    @godofecht
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions