Repository navigation
fix(mlir): declare each runtime helper symbol once - #1563
Merged
godofecht merged 6 commits intoOct 16, 2026
Merged
Conversation
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
Open
19 of 37 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.
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.flowalready loads each module once, by canonical path. They come from helpers thatmlirgen.flowwrites next to a program's own extern declarations of the same names.mlg_put_mem_profile, the #740 memory profiler, is pulled in by anymalloc. It did two things that clash:getenvandatexitevery time. A program withextern { function getenv(...) }then had@getenvtwice. That accounts for 45 of the 54 failures, including tools/lsp, tools/run, tools/repl and tools/dap.flow_mem_note_arena,flow_mem_note_stackand its sibling hooks.lib/stdlib/memory.flowdeclaresflow_mem_note_arenaas an extern, so it was emitted twice. That accounts for the other 9.The C backend has neither problem.
cgen.flowskips externs that its prelude provides, and that list includesflow_mem_note_arenaandflow_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:getenvandatexitonly when the program does not already declare them (mlg_lc_declared). That is the rule it already used forcallocandrealloc.flow_mem_note_alloc,_copy,_temp,_stackor_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.flowdeclaresgetenv,malloc,freeandflow_mem_note_arenaas externs. On the old backend its MLIR fails mlir-opt withredefinition of symbol named 'getenv'. Now it lowers and runs with7 / 1, exit 0.Baseline and after (mlir_vs_c, macOS arm64, LLVM 22.1.7, --jobs 8)
No program that was "same" at baseline is a real regression. The full run showed 4 programs that were not "same":
--only. They are load flakes; the machine's load average was about 100.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:variable has incomplete type, in programs that embed C from std.process and similar modules.expected expression.The floor in compiler/mlir_vs_c/floor.txt is not raised.
Gates
flowc: PASSbootstrap_from_c.sh --regen(fixed point at gen2), then--verify: ALL PASSRegenerated state
compiler/bootstrap/flowc_stage_a.candflowc_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,@atexitor@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
getenvoratexitwith a signature other than(ptr) -> ptror(ptr) -> i32now gets its own declaration, and the profiler's calls use the profiler's types. No corpus program does this.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.