Repository navigation
selfhost: textual MLIR emission for the core language in flowc (FLOWC_EMIT=mlir) - #1020
Merged
Merged
Conversation
godofecht
force-pushed
the
selfhost/mlir-emit
branch
2 times, most recently
from
September 29, 2026 09:40
9f8bc60 to
d2e72b1
Compare
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
force-pushed
the
selfhost/mlir-emit
branch
from
September 29, 2026 10:23
d2e72b1 to
f6edbf6
Compare
This was referenced Sep 29, 2026
Merged
9 of 11 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/mainhas since gained #1014 (algebraic effects), which conflicts with #1015 incgen.flowandtypecheck.flow. This branch stops atb380c42cand 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 withFLOWC_EMIT=mlir(orFLOWC_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,printfand 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.flowc mlir: unsupported: match statement at line 8.compiler/scripts/mlir_lower.sh: the mlir-opt and mlir-translate step frommlir_jit.py, with the same pass list, as bash.flow-driver:--backend=mlirandFLOW_CPU_BACKEND=mliron the default flowc host now emit with flowc, lower withmlir_lower.shand link with clang, with no Python. A refused program falls back to the Python generator, and the reason is printed.FLOW_HOST=pythonkeeps the old path.compiler/scripts/parity_mlir.shpluscompiler/scripts/mlir_normalize.awk: the parity gate (details below).flowc-selfhostjob installs the MLIR tools and runs the golden gate plusflow run --backend=mlirwith python stubbed out.main.flowanddriver.flowemit buffers grow from 1 MiB to 4 MiB, because the compiler bundle now passes 1 MiB of C.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
%Nvalues and^bbNlabels by first appearance and drops indentation. Nothing else is normalized. Goldens were recorded from the Python generator ateec7463f.--python eec7463f, over the 2,400.flowfiles underexamples/,tests/,lib/,benchmarks/andcompiler/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_from_c.sh --verifyandself_host_full.shpass.roundtrip.shandparse_coverage.sh --checkpass.parity_field_dsl,parity_lowering,parity_dynamics_dsl,parity_flow_blocks,parity_math_prose_expr,parity_proof_parse,parity_know_indexandparity_mlir(golden and--python) all pass.parity_mlir.shgolden mode andflow run --backend=mlirpass with python and python3 replaced by stubs that fail.Found along the way
!(x < 0)as(!x < 0)..llfortests/benchmarks/publish/flow/matmul.flowfrom either MLIR path. The gate now puts a 120 s limit on each link.