Skip to content

selfhost: textual MLIR emission for the core language in flowc (FLOWC_EMIT=mlir) - #1020

Merged
godofecht merged 12 commits into
mainfrom
selfhost/mlir-emit
Sep 29, 2026
Merged

godofecht merged 12 commits into
mainfrom
selfhost/mlir-emit

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

First slice of moving the MLIR backend out of src/flow. Plan, inventory and slice order: docs/design/mlir-in-flow.md.

Depends on #1015 (Stage-A batch 3), which is merged into this branch for the current Stage-A AST. Merge #1015 first. origin/main has since gained #1014 (algebraic effects), which conflicts with #1015 in cgen.flow and typecheck.flow. This branch stops at b380c42c and needs one more merge plus a bootstrap regeneration once those two are reconciled.

What changes

  • compiler/src/mlirgen.flow (new, about 4800 lines): textual MLIR for the core language, selected with FLOWC_EMIT=mlir (or FLOWC_BACKEND=mlir) on the single-file emit path. It covers functions and externs, integer, float, bool and string scalars, ptr<T>, array<T, N>, structs (nested, as parameters and return values), module consts with literal values, all arithmetic, bitwise, comparison and short-circuit operators, casts, if/elif/else, if-expressions, while, for (all step forms), break, continue, print, println, printf and the float math intrinsics. It follows the Python generator's lowering decisions, including its quirks (listed in the doc), so the two outputs can be compared as text.
  • Anything outside the slice is refused with a located reason, for example flowc mlir: unsupported: match statement at line 8.
  • compiler/scripts/mlir_lower.sh: the mlir-opt and mlir-translate step from mlir_jit.py, with the same pass list, as bash.
  • flow-driver: --backend=mlir and FLOW_CPU_BACKEND=mlir on the default flowc host now emit with flowc, lower with mlir_lower.sh and link with clang, with no Python. A refused program falls back to the Python generator, and the reason is printed. FLOW_HOST=python keeps the old path.
  • compiler/scripts/parity_mlir.sh plus compiler/scripts/mlir_normalize.awk: the parity gate (details below).
  • CI: the no-Python flowc-selfhost job installs the MLIR tools and runs the golden gate plus flow run --backend=mlir with python stubbed out.
  • main.flow and driver.flow emit buffers grow from 1 MiB to 4 MiB, because the compiler bundle now passes 1 MiB of C.
  • Bootstrap C regenerated (fixed point).

No Python is deleted yet. Every Python MLIR module still has callers: the fallback path, flow mlir, flow jit, wasm, BPF, GPU and the unit tests. The doc lists which slice retires which module.

Parity

Normalization renames %N values and ^bbN labels by first appearance and drops indentation. Nothing else is normalized. Goldens were recorded from the Python generator at eec7463f.

  • Golden mode (no Python): 7/7 fixtures give the same MLIR text and the same run output. 250 recorded corpus programs still give the same MLIR.
  • --python eec7463f, over the 2,400 .flow files under examples/, tests/, lib/, benchmarks/ and compiler/fixtures/: flowc accepts 291. For 290 of them the MLIR text matches Python; the remaining one crashes the Python parser. Built through both MLIR paths and run: 184 give the same output, 16 print timings or addresses that change from run to run, and 90 build on neither path (they need the Flow runtime or a C header). 0 failures.
  • tests/lang: 16 of 166 programs are covered.

Verification (run locally, macOS, Homebrew LLVM 22)

  • Bootstrap: fixed point; bootstrap_from_c.sh --verify and self_host_full.sh pass.
  • roundtrip.sh and parse_coverage.sh --check pass.
  • Parity gates: parity_field_dsl, parity_lowering, parity_dynamics_dsl, parity_flow_blocks, parity_math_prose_expr, parity_proof_parse, parity_know_index and parity_mlir (golden and --python) all pass.
  • parity_mlir.sh golden mode and flow run --backend=mlir pass with python and python3 replaced by stubs that fail.

Found along the way

@godofecht
godofecht force-pushed the selfhost/mlir-emit branch 2 times, most recently from 9f8bc60 to d2e72b1 Compare September 29, 2026 09:40
compiler/src/mlirgen.flow ports the core of src/flow/mlir_generator.py:
functions, scalar arithmetic, casts, if/elif/else, while, counted for loops
with break/continue, structs, fixed arrays, pointers and print/printf. It
follows the Python lowering decisions so the two outputs match after SSA
and label renaming (compiler/scripts/mlir_normalize.awk). Programs outside
the slice are refused with a located reason.

compiler/scripts/mlir_lower.sh runs the mlir-opt and mlir-translate steps
that src/flow/mlir_jit.py drives, with the same pass list.
mlirgen.flow now covers module consts with literal values (loaded through
their llvm globals, with Python's trivial-accessor inlining), the printf
intrinsic, and names what it refuses (match, defer, address-of, C interop,
@gpu and mode-guard attributes).

compiler/fixtures/mlir holds seven fixtures with goldens recorded from the
Python generator at e74c99a: normalized MLIR (.mlir) and the output of the
program built through the Python MLIR path (.out). corpus.txt lists the 247
repository programs whose flowc MLIR already equals Python's.

flow-driver: --backend=mlir (and FLOW_CPU_BACKEND=mlir) on the flowc host
emits with flowc and lowers with compiler/scripts/mlir_lower.sh, with no
Python. A program outside the slice falls back to the Python generator
with the refusal reason printed.
compiler/scripts/parity_mlir.sh compares flowc's MLIR with the Python
generator: goldens with no Python by default, the Python generator at a
pinned revision with --python <rev>, and the run output of every program
built through both MLIR paths when mlir-opt is installed. Programs whose
Python build disagrees with itself (timings, addresses) are counted as
nondeterministic rather than compared.

CI runs the golden mode in the no-Python flowc-selfhost job with the MLIR
tools installed.
ensure_flowc may pick a driver.flow build, which has no FLOWC_EMIT=mlir.
The MLIR path now uses FLOWC_BIN or compiler/build/flowc_bootstrap built
from the checked-in bootstrap C, and treats output that is not an MLIR
module as a refusal. CI runs --backend=mlir with python stubbed out.
The .mlir and .out goldens matched the *.mlir and *.out ignore rules, so a
clean checkout had none and golden mode failed every fixture. Recorded
with --write-golden eec7463 and unignored. corpus.txt now lists the 290
programs whose flowc MLIR equals the Python generator's on this tree.
@godofecht
godofecht merged commit 25e26e4 into main Sep 29, 2026
11 checks passed
@godofecht
godofecht deleted the selfhost/mlir-emit branch October 1, 2026 12:50
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