Repository navigation
selfhost: flowc MLIR emitter for match, merges, defer, imports, statics and memref arrays - #1041
Merged
Merged
Conversation
…lized lets Slice 2 of docs/design/mlir-in-flow.md. The emitter now lowers match (literal, wildcard, binding, struct, enum variant and guarded or-pattern arms, default), enums as the tagged structs Python synthesizes, defer (including the return ordering), address-of on locals, consts, fields and elements, lets without an annotation (Python's memref<16xi8> auto type) and lets without an initializer. Locals held as SSA values are merged at joins the way the Python generator does it: scf.if yields, cf block arguments on if, match and loop exits, loop-carried block arguments and scf.for iter_args, and carried values on break and continue.
Slice 3 of docs/design/mlir-in-flow.md. compiler/src/mlir_bundle.flow walks the imports the way src/flow/module_resolver.py does (legacy string, relative and dotted paths, flow.toml [paths], the stdlib fallbacks, depth-first with each module's imports before its own declarations), joins the expanded modules in that order, typechecks them dependencies first and parses them once; the emitter drops imports, export lists and an imported main, as the resolver does. FLOWC_EMIT=mlir takes this path for any program with an import, and for FLOWC_BUNDLE=1; the driver passes FLOWC_ROOT for the packages and stdlib fallbacks. Also: top-level let mut statics (scalar, string, pointer, struct and array globals with LLVM-dialect initializers), accessors that return a static or its address, Python's placeholder for string +, the module-wide !llvm.array tag cache by variable name, 1 KiB float text for huge literals. Programs with erased proof-layer nodes, structs holding a Tensor-shaped field, and Tensor arguments to struct-returning calls are refused, since Python either rejects them or copies them its own way. parity_mlir.sh gives the Python reference the bootstrap C and this checkout's lib and packages.
Slice 4 of docs/design/mlir-in-flow.md, the parts that match Python today: uninitialized fixed arrays as memref.alloca (spelled with the element's own type name, as Python does), memref.load and memref.store with the index_cast Python writes, unsized array<T> as memref<?xT> for parameters and results, a memref decaying to a pointer, array fields of a struct literal built inline with insertvalue, and the element type of a memref local when Python reads it. The one-statement pointer loop out[i] = a[i] op b[i] over 0 to N becomes Python's linalg.generic; every other single-store pointer loop now lowers normally instead of being refused. The sprintf, snprintf, fprintf and scanf externs, which Python always turns into llvm.func varargs, are refused.
…data Six new fixtures (match_enums, merges, defer_addr, imports_statics with its module mlir_dep, and data) and the corpus digests, recorded from the Python generator at 84806b0: 13 fixtures and 781 corpus programs. The design doc records what slices 2 and 3 and part of slice 4 cover, the Python behaviours the emitter copies rather than fixes, and the numbers per slice.
getcwd pulled unistd.h into the C the Python host builds for flowc, where proof_doc.flow's write extern clashes with it, and roundtrip.sh failed.
godofecht
force-pushed
the
selfhost/mlir-emit-2
branch
from
September 29, 2026 18:18
ee2251f to
47e6886
Compare
godofecht
added a commit
that referenced
this pull request
Sep 29, 2026
This was referenced Sep 29, 2026
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.
Builds on #1020 (merged): the next slices of
docs/design/mlir-in-flow.md, soFLOWC_EMIT=mlirandflow run --backend=mlircover far more of the corpus with no Python.What changes
compiler/src/mlirgen.flow):match(literal, negative literal, bool, string,_, binding, struct, enum variant, guarded or-patterns,default, with thellvm.unreachablejoin frommlir_match_termination.py), enums as the tagged struct Python synthesizes,defer(block exit,break, andreturnwith its value computed first), address-of on locals, consts, statics, fields and elements,letwith no annotation (Python'sauto, lowered tomemref<16xi8>) andletwith no initializer.scf.ifyields, cf block arguments onif/elif/matchjoins, loop-carried block arguments onwhileand cffor,scf.foriter_args, and the carried values onbreakandcontinue.compiler/src/mlir_bundle.flow, new): the declaration order ofmodule_resolver.py(legacy string, relative and dotted imports,flow.toml[paths], the stdlib fallbacks, depth first with a module's imports before its own declarations). The expanded modules are joined in that order, typechecked dependencies first, and parsed once; imports,export a, blists and an importedmainare dropped.FLOWC_EMIT=mlirtakes this path for any program with an import. The driver passesFLOWC_ROOT.let mutscalars, strings, pointers, structs and fixed arrays asllvm.mlir.globals with LLVM-dialect initializers; accessors returning a static or its address are inlined (MLIR generator should inline trivial return-by-reference accessor functions at IR gen time #474).memref.alloca,memref.load/memref.store,array<T>asmemref<?xT>for parameters and results, memref-to-pointer decay, arrays of structs, array fields built inline in struct literals, and Python'slinalg.genericforout[i] = a[i] op b[i]over pointers. Other single-store pointer loops now lower normally instead of being refused.+gets Python's placeholder comment. Programs with erased proof-layer nodes are refused (the typecheck erased them, so they were accepted by mistake after the merge), as are Tensor-shaped struct fields, Tensor arguments to struct-returning calls and the sprintf family, where Python does its own thing.parity_mlir.sh: six new fixtures with goldens (match_enums,merges,defer_addr,imports_staticsplus its modulemlir_dep,data), corpus digests re-recorded from84806b0d, and the Python reference gets the bootstrap C and this checkout'slibandpackages, which its resolver and flow-block expansion need.Parity
Against the Python generator (its MLIR code is unchanged since
eec7463f), every.flowunderexamples/,tests/,lib/,benchmarks/andcompiler/fixtures/, macOS, Homebrew LLVM 22. Slice numbers follow the design doc.tests/langThe slice 3 commit had four text mismatches (a memref passed as a pointer, and the sprintf family); the next commit fixes them, and the final state has 0 text and 0 run failures. One more accepted program crashes the Python parser and is not counted. Most programs that build on neither path use
let x = e, which Python typesmemref<16xi8>, or need the Flow runtime or a C header.parity_mlir.shgolden mode: 13 of 13 fixtures (text and run), 781 corpus programs, withpythonandpython3stubbed out.flow run --backend=mlironimports_statics.flow,match_enums.flowanddefer_addr.flowbuilds and runs with Python stubbed out.Verification (local, macOS)
bootstrap_from_c.sh --verify,self_host_full.sh,roundtrip.sh,parse_coverage.sh --check,fmt_check.sh --checkandcorpus_parity.sh --checkpass.parity_mlir,parity_lowering,parity_effects,parity_proofs,parity_field_dsl,parity_dynamics_dsl,parity_flow_blockspass.parity_shader_dslpasses except the Metal compile step: this Mac hasxcrunbut no Metal toolchain.Found along the way
The emitter now reproduces these Python MLIR behaviours rather than fixing them; the doc lists them:
let x = etypesxasmemref<16xi8>, so arithmetic on it does not verify.elifconditions of a cfifread the then-arm's bindings, which fails dominance when the then-arm assigned the local.-1starts at 0.!llvm.arraytag of a local is cached under its name for the rest of the module, so a laterptrof the same name in another function is indexed as that array.linalg.genericgoes throughbuiltin.unrealized_conversion_cast, which mlir-translate cannot lower.The Python host cannot build
compiler/src/main.flowwhen any module usesgetcwd(unistd.h clashes with thewriteextern inproof_doc.flow), so the bootstrap was regenerated through flowc, andmlir_bundle.flowreadsPWDinstead.I could not add
imports_statics.flowto the no-Python--backend=mlirCI step: a local hook blocks edits to workflow files. The golden gate in CI already covers it.What is next
Slice 4's remaining parts (spans and slices, list patterns,
array<T>(n), record update), string and cast-wrapped consts, then slice 5 (lambdas and closures). Leaving aside proof programs (855, which Python rejects too) and files flowc cannot parse (169), the largest refusals left are effects and capabilities (51),@gpu, mode-guard and C-interop attributes (47), imports that do not resolve the Python way (36), span and function types (27) and forward declarations (27).