Skip to content

fix(mlir): a same-width cast no longer changes its operand's signedness - #1569

Closed
godofecht wants to merge 4 commits into
mainfrom
fix/mlir-cast-signedness
Closed

godofecht wants to merge 4 commits into
mainfrom
fix/mlir-cast-signedness

Conversation

@godofecht

@godofecht godofecht commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Cause

a as u64 for an i64 a needs no instruction, so mlg_emit_cast_expr returned a's own SSA value and set its unsigned flag. When a is a parameter or a local, the flag stayed on the variable: every later comparison on a was unsigned.

In compiler/src/dynamic_invariants.flow, flowc_dyn_i64_distance casts a as u64 in its first branch, so the next a < 0 became arith.cmpi ult, %arg0, 0, always false. tests/lang/test_dynamic_invariants.flow exited 24 under MLIR and 0 under C.

Fix

When a same-width integer cast changes signedness and its operand is a parameter or a local's current value, the cast yields a new value, arith.bitcast %v : T to T (it folds away), that carries the new signedness. A temporary operand is still marked in place, so programs without such a cast emit the same MLIR as before.

Regression test

compiler/fixtures/mlir/cast_signedness.flow is the distance case plus an i32 local cast to u32. The old backend printed 18446744073709551609 for |(-3) - 4| and exited 1; the recorded run is 1 4294967291 7 7, 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 1161

Now same: tests/lang/test_dynamic_invariants.flow and the new fixture. Four programs that were same were not in this run, and I checked each:

  • examples/concurrency/http_tls.flow (run-stdout) and tests/stdlib/test_gpu_model.flow (run-exit 28): same when re-run with --only on this branch's compiler. test_gpu_model also flaked on another branch (exit 133).
  • tests/stdlib/audio/test_bus_graph.flow (mlir-slow): clang takes most of the 300 s build limit on this program's IR when the machine is loaded. A build with main's compiler took 9 min 23 s wall (146 s CPU) during the same period, and the emit itself takes about a second on both compilers.
  • examples/concurrency/work_steal.flow (nondet): scheduler output, flaky on every branch.

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 cast_signedness.mlir and .out.
  • compiler/fixtures/mlir/corpus.txt: digests move for the 92 programs that cast a variable to the other signedness at the same width (mostly the dynamics DSL fixtures and the stdlib modules they import: ga, procgen, planet, automata, gif, checked_arith). The 11 programs main never recorded are left out.

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

Risk

Medium-low. The change touches many programs' MLIR, but only by inserting a no-op bitcast where a variable's signedness used to be overwritten. Any of those programs that compared the variable after such a cast was computing the wrong comparison before.

`a as u64` for an i64 a needs no instruction, so mlirgen returned a's
own SSA value and marked it unsigned. When a is a parameter or a local,
that mark stuck: later comparisons on a were unsigned. In
compiler/src/dynamic_invariants.flow, `a < 0` after `a as u64` became
an ult against 0, always false, so tests/lang/test_dynamic_invariants
exited 24 where C exits 0.

When the signedness changes and the operand is a parameter or a local's
current value, the cast now yields a new value (an arith.bitcast to the
same type, which folds away) that carries the new signedness. Other
operands are marked in place as before.

compiler/fixtures/mlir/cast_signedness.flow is the distance case; the
old backend printed 18446744073709551609 for |(-3) - 4| and exited 1.
Corpus digests moved for the programs that cast a variable this way.
…ts (#1593)

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

Closing this stale conflicting signedness-cast implementation as superseded by current-main PR #1640, which fixes the same shared-SSA signedness mutation by creating a distinct value. I linked the older distance/cast_signedness.flow regression case on #1640 to preserve coverage. The original branch and patch remain available for reference.

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