Skip to content

fir: --apply passes for dead_elim, const_fold and dce - #1156

Merged
godofecht merged 3 commits into
mainfrom
devin/issues
Oct 1, 2026
Merged

godofecht merged 3 commits into
mainfrom
devin/issues

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Summary

flow fir-g can now apply optimisation passes, not only report candidates (#686).

  • --apply PASSES takes a comma list from {dead_elim, const_fold, dce, all}. Reports, --opts candidates, routing and benchmarks all run on the rewritten graph.
  • FirG ops carry real operand value edges, operator codes and CONST literal text; the lowerer records them at each site. op_operand_count keeps its declared arity so existing report counts are unchanged.
  • dead_elim compacts the graph to reachable functions, remapping function, block, op and value ids. Compaction buckets ops by op_block rather than slicing: the lowerer interleaves ops across blocks, which had silently dropped the ping -> pong edge of a recursive pair.
  • const_fold rewrites BINOP/UNOP over integer and boolean literals to a CONST in place (i64 arithmetic, division by zero left alone).
  • dce drops pure result ops whose value is never an operand, iterated to a fixpoint through a generalised op-level compaction. It collects the CONSTs stranded by const_fold.
  • all runs const_fold, then dce, then dead_elim.

Baseline: fir gate: 60 passed before this branch; 68 passed, 0 failed after, with the ten new --apply cases plus tests/fir/programs/fold.flow (foldable literals, a discarded expression, and a non-foldable variable operand).

Not in scope: inlining and pipeline fusion. FIR-G has no variable or loop structure to splice or fuse over; that is a larger IR change than a pass.

Test plan

  • ./flow tool tests/fir/run.flow — 68 passed, 0 failed
  • analysis.flow --apply dead_elim keeps both edges of the recursive ping/pong pair and drops dead
  • fold.flow --apply all folds 4 ops, dce removes 8 stranded CONSTs
  • --apply wat and bare --apply produce argparse-style errors with exit code 2

fir-g now accepts --apply PASSES (dead_elim, all). The pass rewrites the
FirG: unreachable functions are dropped and function, block, callee and
operand metadata are remapped. Ops are bucketed by op_block during
compaction because the lowerer interleaves them across blocks. Reports
and --opts run on the rewritten graph and show what was removed.

Towards #686.
FirG ops now carry real operand value edges, an operator code, and the literal text of CONST ops. The lowerer records them at each site, and compaction remaps operand values through the value map.

--apply accepts const_fold (and all): BINOP/UNOP ops over integer and boolean literals fold to a CONST in place. The report shows the folded count next to the dead_elim result. Towards #686.
const_fold leaves folded operands stranded and expression statements produce unused results. --apply dce drops CONST/BINOP/UNOP ops whose result is never an operand, iterating to a fixpoint through a generalized op-level compaction. all now runs const_fold, dce, then dead_elim.

Towards #686.
@godofecht
godofecht merged commit 47af187 into main Oct 1, 2026
28 checks passed
@godofecht
godofecht deleted the devin/issues branch October 12, 2026 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant