Skip to content

fix(mlir): resolve bundle imports with the C bundle's resolver - #1557

Merged
godofecht merged 3 commits into
mainfrom
fix/mlir-import-resolution
Oct 14, 2026
Merged

godofecht merged 3 commits into
mainfrom
fix/mlir-import-resolution

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Cause

compiler/src/mlir_bundle.flow had its own copy of the retired Python module resolver. It differed from the C bundle in two ways:

  • It refused every string import with a .. part. lib/stdlib/bytebuf.flow reaches the compiler's byte buffer as import "../../compiler/src/bytebuf.flow". The C resolver allows that inside lib/stdlib and lib/runtime (flowc_resolve_unsafe_import). So every program that imports std.bytebuf, std.regex or std.process failed.
  • It never searched FLOWC_DIR. flow compile sets FLOWC_DIR to the distribution root, so import "tools/flow_cli/sys.flow" resolves for C. The MLIR path only tried the module's directory, the stdlib, packages/ and the project root.

64 programs that the C backend builds stopped at flowc mlir: unsupported: import that does not resolve.

Fix

  • resolve.flow exports flowc_resolve_module_import: the unsafe-path check, then flowc_resolve_import_at. flowc_resolve_gather now calls it, with the same behaviour as before.
  • mlir_bundle.flow resolves every import through that function. It uses the C bundle's search directory (FLOWC_DIR, else FLOWC_ROOT, which the MLIR driver sets to the same root, else .) and project root. Its own resolver is gone, along with its flow.toml reader, its stdlib-root logic and the refusal of programs that have no flow.toml. That is about 330 lines.
  • The module walk order (depth first, imports before the module) does not change.

Regression test

compiler/fixtures/mlir/imports_resolve.flow imports "compiler/fixtures/mlir/mlir_dep.flow" (rooted at the repository) and std.bytebuf. The old flowc refuses it with import that does not resolve at line 6. The new one emits it, and it runs with 12 / 8 / 1, exit 0.

Baseline and after (mlir_vs_c, macOS arm64, LLVM 22.1.7, --jobs 8)

baseline (main 7bbb4b3) this branch
programs 1864 1865 (the new fixture)
C builds 1467 1468
MLIR builds 1269 1275
same 1163 1169

All 64 programs now resolve. 5 of them are now "same", plus the new fixture.

The other 59 now stop at later, separate causes:

  • 40: field through a pointer static. These are mostly tools that import tools/flow_cli/sys.flow.
  • 17: redefinition of symbol named 'X'. This is the follow-up fix on fix/mlir-bundle-dup-symbols.
  • 1: top-level let.
  • 1: extern type.

No program that was "same" at baseline is a real regression. The full run showed 4 programs that were not "same":

  • examples/concurrency/http_middleware.flow and tests/stdlib/test_gpu_model.flow were "same" when re-run with --only. They are load flakes at --jobs 8.
  • examples/concurrency/work_steal.flow and tests/lang/test_c_import_string_time.flow showed as nondet (concurrency and clock).

The floor in compiler/mlir_vs_c/floor.txt is not raised.

Gates

  • selftest of the edited compiler: flowc: PASS
  • bootstrap_from_c.sh --regen (fixed point at gen2), then --verify: ALL PASS
  • self_host_full: ALL PASS. roundtrip: PASS
  • parity_mlir: PASS. Fixtures 25 of 25 for text and for run, corpus 2716 with 0 failures.
  • tests/cgen: PASS. test-lang: 309 passed.
  • test-scripts: mlir_math_override failed once inside the full run (on its C-backend step) and passed when run alone. Everything else passed.

Regenerated state

  • compiler/bootstrap/flowc_stage_a.c and flowc_stage_a, in their own last commit.
  • parity_mlir --record, trimmed by hand to the new fixture's .mlir/.out and 8 new corpus.txt digests. Those 8 are lib/stdlib/bytebuf.flow, lib/stdlib/regex.flow, tests/lang/test_stdlib_bytebuf.flow, tests/lang/test_stdlib_regex.flow, tests/lang/test_import_normalized_path.flow, tests/fixtures/modules/dotpath/m/a.flow and the two cross_harness benchmarks. The old flowc refused each of them. No existing digest moved. The record also added 11 programs that the old flowc already accepted (main drift). I left those out.

Risk

  • The MLIR path now resolves exactly as the C bundle does. A program whose imports resolved under the old Python-style rules, but not under the C rules, would now be refused. The full gate shows none.
  • The [paths] stdlib override and the compiler packages/ fallback for string imports had no C counterpart, and no corpus program uses them. packages/ does not exist in the tree.
  • lib/stdlib in the C resolver is relative to the working directory, as it is for flow compile.

This touches compiler/, so it needs review by a human CODEOWNER. Please do not enqueue it.

mlir_bundle.flow carried its own copy of the Python module resolver. It
refused the `..` string import that lib/stdlib/bytebuf.flow uses to reach
compiler/src/bytebuf.flow, and it did not search FLOWC_DIR, so a string
import rooted at the repository (tools/flow_cli/sys.flow) did not resolve.
64 programs the C backend builds stopped at "import that does not resolve".

resolve.flow now exports flowc_resolve_module_import, the unsafe-path
check plus flowc_resolve_import_at, and flowc_resolve_gather calls it. The
MLIR bundle resolves every import through it with the C bundle's search
directory (FLOWC_DIR, else FLOWC_ROOT, else the working directory) and
project root, and drops its own resolver.

The fixture compiler/fixtures/mlir/imports_resolve.flow imports both
forms; the old flowc refuses it.
… unblocks

parity_mlir --record, kept to the fixture and the 8 corpus programs the
old flowc refused with "import that does not resolve" (std.bytebuf,
std.regex and their importers, and a dotted import). No recorded digest
moved.
@godofecht
godofecht force-pushed the fix/mlir-import-resolution branch from a799841 to 27e9c34 Compare October 14, 2026 11:48
@godofecht
godofecht added this pull request to the merge queue Oct 14, 2026
Merged via the queue into main with commit 845efd9 Oct 14, 2026
30 checks passed
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