Skip to content

fix(mlirgen): generate distinct value for casts changing unsignedness to fix signed compares - #1583

Closed
godofecht wants to merge 11 commits into
mainfrom
mlir-parity-fix-run-exit-8884590759992762889
Closed

godofecht wants to merge 11 commits into
mainfrom
mlir-parity-fix-run-exit-8884590759992762889

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

This commit fixes the root cause behind several run-exit divergences (C 0, MLIR 24, C 0, MLIR 133) during C vs MLIR parity tests.

The issue occurred because the mlg_emit_cast_expr compiler step would modify the internal unsignedness flag (vuns) of a shared SSA value instead of creating a new value when compiling casts between types that have the same MLIR base type (e.g., casting i64 to u64). As a result, subsequent operations using the original variable (e.g., testing a < 0 where a was previously cast) would see the corrupted vuns flag and incorrectly compile to an unsigned comparison (like arith.cmpi ult), yielding incorrect behavior.

The solution intercepts casts where tu != vuns but from_eff == dst, and emits a distinct identical SSA value (via a no-op arith.ori %v, %v) using mlg_new_val(dst). The new value inherits the modified unsignedness logic, allowing the original value's unsignedness metadata to be preserved exactly as it was.

The single impacted test consts_calls.flow's MLIR parity golden has been updated to accommodate the expected output.


PR created automatically by Jules for task 8884590759992762889 started by @godofecht

… to fix signed compares

The compiler incorrectly mutated the 'vuns' (unsigned) attribute of an underlying SSA value when compiling an `as` cast to a type with a different signedness but the same MLIR base type (e.g. `i64` to `u64`). This caused subsequent usages of the original variable in signed comparisons (like `a < 0`) to incorrectly emit an unsigned comparison (`ult`), leading to miscompilations such as traps and bounds check failures. We now generate a distinct SSA value via `arith.ori` when the unsignedness differs, preserving the original variable's semantics.

Part of MLIR parity.

Fixes C 0 vs MLIR 24 (test_dynamic_invariants.flow) and out-of-bounds/bounds-check related diverges like C 0/MLIR 133.

Co-authored-by: godofecht <[email protected]>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

google-labs-jules Bot and others added 10 commits October 17, 2026 23:57
Fixes run-exit divergences where casts changing unsignedness (e.g. i64 to u64) mutated the underlying shared SSA variable's signedness flag. This caused subsequent usages of that variable in signed operations to miscompile to unsigned comparisons, which led to incorrect evaluation logic, bypassed bounds checks, and resulted in traps (C 0 vs MLIR 24, C 0 vs MLIR 133, C 139 vs MLIR 133, etc.). Fixed programs: test_dynamic_invariants and other related out-of-bounds/bounds-check diverged tests.

Co-authored-by: godofecht <[email protected]>
Regenerate mlir_consts_calls golden to match recent changes introducing arith.ori cast.

Co-authored-by: godofecht <[email protected]>
…-8884590759992762889-5015243584356900987

Fix mlir_consts_calls golden

Copy link
Copy Markdown
Contributor Author

Conflict consolidation: #1640 explicitly re-applies the original unsignedness-cast correctness fix from this PR against current main. Closing the stale conflicted original; retain #1640 as the integration PR.

@godofecht godofecht closed this Nov 1, 2026
@google-labs-jules

Copy link
Copy Markdown
Contributor

Conflict consolidation: #1640 explicitly re-applies the original unsignedness-cast correctness fix from this PR against current main. Closing the stale conflicted original; retain #1640 as the integration PR.

Understood. Acknowledging that this work is now obsolete and stopping work on this task.

godofecht added a commit that referenced this pull request Nov 1, 2026
Supersedes #1583 (re-applied on current main). Part of MLIR parity.

Updated several corpus digests in corpus.txt due to changed MLIR generation outputs resulting from the distinct value casts.

Co-authored-by: godofecht <[email protected]>
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