Skip to content

flat lowering: a block merge should not keep a returned Result in a stack slot #4570

Backlog

Description

@minzyfox

What happens

Each return Result::Err(e) in a function builds its Result in a stack slot of its own. When two such returns end in blocks that differ only in which slot they use, MLIR's greedy rewrite driver merges the two blocks. The merged block takes the slot as a block argument, which becomes a phi of pointers in LLVM IR. LLVM's SROA cannot move a slot that is written through such a phi into registers. So the function keeps its stack slots, grows larger, and LLVM does not inline it.

PR #4485 (for #4484) avoids the merge: four lowering passes no longer merge blocks in functions where they have no work. But the speedup depends on that. A new lowering pass that merges blocks would bring the stack slots back.

Fix

Make the flat lowering build the returned value so that a block merge does no harm. Two possible ways:

  • Build the returned value as an SSA value, not in a stack slot.
  • Use one return slot for each function, which every return writes.

With either one, the merged blocks have no slot operand that differs, and SROA can remove the slot.

How to check

tests/optimizations/pass/a_function_with_several_question_marks_keeps_its_result_in_registers.vx checks that Reader$header keeps no alloca after opt. After this fix, that check should still pass with the block merging of the greedy driver turned back on in the four passes.

Found in the review of #4485.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions