Skip to content

fix(mlir): declare each runtime helper symbol once - #1563

Merged
godofecht merged 6 commits into
fix/mlir-import-resolutionfrom
fix/mlir-bundle-dup-symbols
Oct 16, 2026
Merged

godofecht merged 6 commits into
fix/mlir-import-resolutionfrom
fix/mlir-bundle-dup-symbols

Conversation

@godofecht

@godofecht godofecht commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1557 (fix/mlir-import-resolution). Merge that one first. This PR's own commits are the last three.

Cause

The duplicate symbols do not come from a module being bundled twice. mlir_bundle.flow already loads each module once, by canonical path. They come from helpers that mlirgen.flow writes next to a program's own extern declarations of the same names.

mlg_put_mem_profile, the #740 memory profiler, is pulled in by any malloc. It did two things that clash:

  • It declared getenv and atexit every time. A program with extern { function getenv(...) } then had @getenv twice. That accounts for 45 of the 54 failures, including tools/lsp, tools/run, tools/repl and tools/dap.
  • It defined flow_mem_note_arena, flow_mem_note_stack and its sibling hooks. lib/stdlib/memory.flow declares flow_mem_note_arena as an extern, so it was emitted twice. That accounts for the other 9.

The C backend has neither problem. cgen.flow skips externs that its prelude provides, and that list includes flow_mem_note_arena and flow_mem_note_stack.

mlir-opt rejected the module with redefinition of symbol named 'getenv' or 'flow_mem_note_arena'. With #1557, 54 programs that the C backend builds failed this way: 37 at baseline and 17 more that #1557 exposed.

Fix

In compiler/src/mlirgen.flow:

  • The profiler declares getenv and atexit only when the program does not already declare them (mlg_lc_declared). That is the rule it already used for calloc and realloc.
  • An extern of one of the profiler's hooks (flow_mem_note_alloc, _copy, _temp, _stack or _arena) emits nothing and pulls the profiler in (mlg_mem_use). The profiler then defines the hook, as the C prelude does.

Regression test

compiler/fixtures/mlir/extern_runtime_names.flow declares getenv, malloc, free and flow_mem_note_arena as externs. On the old backend its MLIR fails mlir-opt with redefinition of symbol named 'getenv'. Now it lowers and runs with 7 / 1, exit 0.

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

baseline (main 7bbb4b3) #1557 this PR
programs 1864 1865 1866
MLIR builds 1269 1275 1310
same 1163 1169 1203

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

  • examples/evolution/bouncing_ball_energy.flow, lotka_volterra.flow and thermostat_hysteresis.flow were "same" when re-run with --only. They are load flakes; the machine's load average was about 100.
  • examples/concurrency/work_steal.flow showed as nondet.

examples/circuits/lc_tank.flow was run-stdout at baseline and "same" on #1557. It is run-stdout again here. Its MLIR is byte-identical to #1557's, so the change comes from the run.

On the 54 affected programs (--only), all 54 now lower: 33 same, 1 nondet and 20 mlir-link. The 20 that fail to link are a separate cause in the C-embed side file:

  • 16: variable has incomplete type, in programs that embed C from std.process and similar modules.
  • 4: expected expression.

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 26 of 26 text and run; corpus 2716, 0 failures)
  • tests/cgen: PASS. test-lang: PASS.
  • test-scripts: mlir_math_override failed inside the full run (on its C-backend step) and passed when run alone. It did the same on fix(mlir): resolve bundle imports with the C bundle's resolver #1557. 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 goldens and 33 corpus digests that moved. Every moved digest is a program or library whose MLIR held a duplicate @getenv, @atexit or @flow_mem_note_* declaration, for example examples/systems/manual_memory.flow, examples/audio/*, tests/lang/test_attributes.flow and lib/stdlib/memory.flow. At baseline, each of the programs among them either failed with the redefinition or did not build with C. No digest of another program moved. 11 unrelated additions from main drift are left out.

Risk

  • A program that declares getenv or atexit with a signature other than (ptr) -> ptr or (ptr) -> i32 now gets its own declaration, and the profiler's calls use the profiler's types. No corpus program does this.
  • A program that defines (rather than declares) a function named flow_mem_note_* is unaffected, because only externs take the new path.

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

The #740 memory profiler (mlg_put_mem_profile) declared getenv and atexit
unconditionally and defined flow_mem_note_arena and its siblings, while a
program's own extern of the same name was declared as well. mlir-opt
rejected the module with "redefinition of symbol named 'getenv'" (or
'flow_mem_note_arena', which lib/stdlib/memory.flow declares as an extern).
54 programs the C backend builds failed in mlir-lower this way.

The profiler now declares getenv and atexit only when the program does
not (mlg_lc_declared, as for calloc and realloc), and an extern of a
profiler hook declares nothing and pulls the profiler in, as the C prelude
provides those hooks.

The fixture compiler/fixtures/mlir/extern_runtime_names.flow declares both;
the old backend's MLIR does not lower.
parity_mlir --record, kept to the fixture and the 33 corpus digests whose
MLIR held a duplicate getenv, atexit or flow_mem_note_* declaration. No
other digest moved.
* fix(mlir): lower a field through a module-level ptr<Struct>

mlg_struct_addr0 refused `s.f` when s is a module static of type
ptr<Struct> ("field through a pointer static"). tools/flow_cli/sys.flow
keeps its string lists in such statics, so 40 programs that import it,
and tests/lang/test_static_ptr_field.flow, stopped there.

The address of the field's struct is the static's current value: load
the pointer from @s and use it, as the pointer-local case does. A static
pointer to a non-struct is still refused.

The fixture compiler/fixtures/mlir/static_ptr_field.flow reads and stores
fields through such a static; the old backend refuses it.

* test(mlir): record static_ptr_field goldens and test_static_ptr_field's digest

parity_mlir --record, kept to the fixture and tests/lang/test_static_ptr_field.flow,
which the previous flowc refused. No digest moved.

* fix: regenerate bootstrap C after the pointer static field change
@godofecht
godofecht merged commit 99dd3aa into fix/mlir-import-resolution Oct 16, 2026
3 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