Skip to content

cuTile: integer-to-integer conversions, and arithmetic on generic scalars #1659

Open

Description

@CarlKCarlK

What problem does this solve?

Two gaps make kernels that are generic over an integer element type awkward to write. Neither is a miscompile; the code is rejected.

  1. convert_scalar and convert_tile only convert to and from floats. Converting between integer types is rejected by the JIT:

    let zero: K = convert_scalar(0i32);   // K = u32: Unsupported conversion "i32" -> "u32"
    let zero: K = convert_scalar(0u32);   // K = u64: Unsupported conversion "u32" -> "u64"

    So a kernel generic over K: ElementType can't make a K constant from a literal. We pass key_zero, key_one and a digit mask as kernel arguments instead.

  2. A generic K scalar has no arithmetic. ElementType doesn't imply Add or Sub, so key_one - key_one fails in Rust's type checker, while the same thing on a Tile<K, _> works. We broadcast to tiles to derive constants.

Proposed API / behavior

let zero: K = convert_scalar(0u32);       // integer widening, narrowing, signedness changes
let mask: K = convert_scalar(7i32);
let none: K = mask + one;                 // scalar arithmetic on generic element types

Conversions could follow Rust's as rules for integers, or use explicit exti/trunci-style semantics.

Alternatives considered

Host-supplied constants and tile arithmetic, as described above. It works, but every generic kernel then carries extra parameters, and the reason isn't obvious to readers.

Additional context

Seen at cuda-rust ad0a42c while writing src/gpu/cutile.rs in range-set-blaze. The comment at the top of the kernels module describes the workaround.

AI contribution: drafted with Claude Code (Anthropic); I reviewed it before posting.

Activity

  1. added
    enhancement
    TBD
    on Oct 27, 2026
  2. veljjanoski commented on Oct 28, 2026

    @veljjanoski
    Contributor

    I'd like to take part 1, the integer-to-integer conversions, if that's welcome.

    I reproduced it on main (a4a939a), compile-only. convert_scalar and convert_tile reject every integer pair I tried: i32 → u32, u32 → u64, i32 → i8, i32 → i64 and i64 → i32. The last two hit the existing "not yet implemented" TODO in compile_intrinsic.rs. i32 → f32 works, and so does calling exti directly on a tile.

    My plan is to follow Rust's as rules, using ops cuTile already has:

    • widening: exti, sign-extending a signed source and zero-extending an unsigned one;
    • narrowing: trunci with no overflow flag, so it wraps like as;
    • same width (i32 ↔ u32): no op, since both are i32 in Tile IR; only the Rust type changes.

    Tests: compile tests that check the emitted ops, and a GPU test that compares the results with as on the host, including negative values and values that don't fit. I can run the GPU tests on sm_86.

    I'd leave part 2 (arithmetic on a generic scalar) to you, since it changes what ElementType implies.

  3. added
    cutile-rs
    on Nov 12, 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

    TBDcutile-rsenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions