Skip to content

fix(mlir): abort on an unhandled effect operation, as the C backend does - #1558

Closed
godofecht wants to merge 5 commits into
mainfrom
fix/mlir-unhandled-effect
Closed

godofecht wants to merge 5 commits into
mainfrom
fix/mlir-unhandled-effect

Conversation

@godofecht

@godofecht godofecht commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Cause

The C backend's effect dispatch (E_op in cgen.flow) calls _flow_unhandled_effect when no installed handler has the operation. Under strict effects, the default, that prints flow: unhandled effect E.op (set FLOW_PERMISSIVE_EFFECTS=1 to allow zero defaults) to stderr and aborts. Only FLOW_PERMISSIVE_EFFECTS=1 at run time restores the zero default.

mlirgen's dispatch functions returned the zero default unconditionally. 19 corpus programs exited 0 under MLIR and 134 under C.

Fix

Both unhandled paths in the MLIR dispatch (no handler installed, or a handler without the operation) now call a new module helper, __flow_unhandled_effect("E.op"). It reads FLOW_PERMISSIVE_EFFECTS at run time and, when that is not 1, writes the same message to fd 2 and calls abort(). Under FLOWC_STRICT_EFFECTS=1 it is fixed strict, as flowc_cgen_eff_strict is. getenv is declared through a new LC_GETENV bit unless the memory profiler already declares it.

Regression test

compiler/fixtures/mlir/effects.flow now runs its handled cases first, flushes stdout, then performs an operation its handler lacks. The recorded run (effects.out) ends exit=134. The old backend printed partial 0.000000 and exited 0.

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

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

19 programs move from run-exit (C 134, MLIR 0) to same. The other changes are timing: examples/ai/flowlm_charlm.flow (run-timeout) and tests/lang/test_loops.flow / compiler/fixtures/mlir/loops.flow (c-fail-mlir) are now same, and tests/lang/test_c_import_string_time.flow and examples/concurrency/work_steal.flow went from same to nondet (clock and scheduling output). No program that was same is now failing.

Gates

  • flowc selftest: PASS
  • bootstrap_from_c.sh --regen, then --verify: PASS
  • self_host_full, roundtrip: PASS
  • parity_mlir: PASS (24 fixtures run, 2708 corpus programs)
  • tests/cgen: 152 passed
  • test-lang: 309 passed
  • tests/mlir_commands: 89 passed, after re-recording mlir_effects, llvm_effects and run_mlir_effects (the fixture changed, and its run now exits 134)
  • 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, so it is not from this change.

Regenerated state

  • compiler/bootstrap/flowc_stage_a.c and flowc_stage_a, in their own last commit.
  • compiler/fixtures/mlir/effects.mlir and effects.out, re-recorded.
  • tests/mlir_commands/expected/ for mlir_effects, llvm_effects and run_mlir_effects, re-recorded with --record, in their own commit.
  • compiler/fixtures/mlir/corpus.txt: digests re-recorded only for the 77 programs whose MLIR changed, all of which declare or import an effect (the dispatch functions gained the unhandled call). --record also wanted to add 11 programs that main never recorded (from [roadmap] Certified safety profiles #1546 and earlier); those are left out.

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

Risk

Low. Programs that perform an unhandled effect now abort under MLIR, as they already do under C. Programs relying on the zero default need FLOW_PERMISSIVE_EFFECTS=1 on both backends.

The handler slot in MLIR is still one global, where C keys it on the fiber. That is a separate gap (it is why examples/concurrency/fiber_suspend.flow still differs).

The C backend's effect dispatch calls _flow_unhandled_effect when no
installed handler has the operation: under strict effects (the default)
it prints 'flow: unhandled effect E.op (...)' to stderr and aborts, and
only FLOW_PERMISSIVE_EFFECTS=1 at run time restores the zero default.
mlirgen returned the zero default unconditionally, so 19 corpus programs
exited 0 under MLIR and 134 under C.

The MLIR dispatch functions now call __flow_unhandled_effect on both
unhandled paths (no handler, or a handler without the operation). The
helper reads FLOW_PERMISSIVE_EFFECTS at run time, or is fixed strict
under FLOWC_STRICT_EFFECTS=1, as flowc_cgen_eff_strict does.

compiler/fixtures/mlir/effects.flow now ends on a handler that lacks an
operation, so its recorded run exits 134; the old backend printed a zero
and exited 0. Corpus digests moved for the programs that declare or
import an effect.
@godofecht
godofecht force-pushed the fix/mlir-unhandled-effect branch from 5b6f7b1 to 5358054 Compare October 14, 2026 12:14
@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
#1594)

Resolved merge conflicts in:
- `compiler/bootstrap/flowc_stage_a`
- `compiler/fixtures/mlir/corpus.txt`
Regenerated `docs/demos/overview.md` to fix the stale documentation CI failure.

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 #1680, which explicitly migrates this unhandled-effect MLIR behavior onto the current main with a smaller, conflict-free patch. Closing this historical conflicting PR to avoid parallel attempts to merge obsolete generated bootstrap/golden files. Verification and review should continue on #1680; the original branch and discussion 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