Skip to content

Bug: MLIR generator propagates unsigned-ness through u8->i32 casts, causing wrong shift semantics #418

Closed

Description

@godofecht

Summary

When casting from an unsigned type to a signed type (e.g. col[0] as i32 where col[0] is u8), the MLIR generator correctly uses zero-extend (arith.extui) but incorrectly marks the result SSA value as unsigned. This propagates through all downstream operations, making >> use arith.shrui (logical shift) instead of arith.shrsi (arithmetic shift).

Reproduction

let col: ptr<u8> = ...
let topdelta: i32 = col[0] as i32
let topscreen: i32 = -2071019
let yl: i32 = (topscreen + 65536 - 1) >> 16
# yl should be -31 (ashr) but MLIR produces 65505 (lshr)

Generated LLVM IR:

%29 = lshr i32 %27, %28    ; should be ashr

Root cause

In _emit_cast(), the integer widening path adds the result SSA to _ssa_unsigned whenever the source is unsigned, regardless of whether the target type is signed:

if unsigned:
    self._ssa_unsigned.add(cast_name)  # wrong when to_type is signed

Fix

Only propagate unsigned-ness when the target type itself is unsigned:

if unsigned and _is_unsigned(to_type):
    self._ssa_unsigned.add(cast_name)

The same fix applies to the truncation path below it.

Impact

Any code that casts a u8 to i32 and then right-shifts a negative value gets the wrong result. In doom-flow this caused sprite column y-coordinates to be off by ~65536 pixels, producing transient rendering mismatches during AI gameplay.

Activity

  1. added
    bug
    on Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions