Skip to content

fix(mlir): call the Flow definition of a libc name the C backend renames - #1565

Closed
godofecht wants to merge 4 commits into
mainfrom
fix/mlir-libc-clash-names
Closed

godofecht wants to merge 4 commits into
mainfrom
fix/mlir-libc-clash-names

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Cause

The C backend renames a Flow definition whose name clashes with a libc declaration (abs, random, index, div, wait and the rest of flowc_cgen_is_libc_clash) to __flowc_libc_<name> for the whole unit, with a #define. Every call then reaches the Flow function.

mlirgen's mlg_libc_mode treated such a definition as libc whenever its arity matched libc's. stdlib/math.flow's abs(x: f32) -> f32 became libc's integer abs, so abs(-0.25) was 0. tests/cgen/stdlib_math_f32_intrinsics.flow exited 1 under MLIR and 0 under C.

Fix

mlg_libc_mode returns 2 (the program's own function, emitted and called as __flowc_libc_<name>) for a function with a body whose name is on the C backend's list. The list is the C backend's own: flowc_cgen_is_libc_clash is now exported from cgen.flow and imported by mlirgen.flow.

Regression test

compiler/fixtures/mlir/libc_clash.flow defines abs(x: f32) and random(). The old backend printed 0.000000 1.000000 4 and exited 1; the recorded run is 0.250000 1.500000 4, exit 0, as C prints.

Counts (mlir_vs_c, full corpus, --jobs 8, macOS arm64, LLVM 22.1.7)

  • baseline main 7bbb4b3: same 1163
  • this branch alone: same 1163

Now same: tests/cgen/stdlib_math_f32_intrinsics.flow, examples/circuits/buck_converter.flow and the new fixture, plus two timing cases (examples/ai/flowlm_charlm.flow, tests/cgen/for_variable_step.flow). Five programs that were same were not in this run: examples/concurrency/http_middleware.flow (the C build exited 1), tests/stdlib/test_gpu_model.flow (MLIR exit 133), tests/lang/test_gpu_graph.flow (build timeout), and the two nondet cases seen on every branch (work_steal, test_c_import_string_time). I re-ran the first three with --only on this branch's compiler: all same, so those were load or port flakes during the full run.

Gates

  • flowc selftest: PASS
  • bootstrap_from_c.sh --regen, then --verify: PASS
  • self_host_full, roundtrip: PASS
  • parity_mlir: PASS
  • tests/cgen: 152 passed
  • test-lang: 309 passed
  • tests/mlir_commands: 89 passed
  • test-scripts: one failure, mlir_math_override, in its C backend half. It fails the same way with a flowc built from main's bootstrap C.

Regenerated state

  • compiler/bootstrap/flowc_stage_a.c and flowc_stage_a, in their own last commit.
  • New fixture goldens libc_clash.mlir and .out.
  • compiler/fixtures/mlir/corpus.txt: 9 digests move, for lib/stdlib/math.flow and the programs that define or import such a function. The 11 programs main never recorded are left out.

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

Risk

Low. The only behaviour change is for programs that define one of these names; they now get their own definition under MLIR, as under C. cgen.flow changes by one export keyword, so C output is unchanged (tests/cgen goldens pass).

The C backend renames a Flow definition named like a clashing libc
function (abs, random, index, div, wait and the rest of
flowc_cgen_is_libc_clash) to __flowc_libc_<name> for the whole unit, so
every call reaches the Flow function. mlirgen treated such a definition
as libc whenever its arity matched libc's, so stdlib/math.flow's
abs(x: f32) became libc's integer abs: abs(-0.25) was 0, and
tests/cgen/stdlib_math_f32_intrinsics exited 1 where C exits 0.

mlg_libc_mode now marks a defined function with one of those names as
the program's own (mode 2, emitted as __flowc_libc_<name>), using the C
backend's list, now exported from cgen.flow.

compiler/fixtures/mlir/libc_clash.flow defines abs and random; the old
backend printed 0.000000 1.000000 and exited 1. Corpus digests moved
for the programs that define or import such a function.
@godofecht
godofecht added this pull request to the merge queue Oct 14, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 14, 2026
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: godofecht <[email protected]>

Copy link
Copy Markdown
Contributor Author

Superseded by #1677 (migration onto current main; CI green). Closing this historical PR.

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