Skip to content

ci: test against CUDA 13.5.0 as well as the baseline toolkit - #1813

Merged
roivanov merged 7 commits into
NVIDIA:mainfrom
roivanov:rivanov/ci-cuda-13-5-lanes
Nov 16, 2026
Merged

roivanov merged 7 commits into
NVIDIA:mainfrom
roivanov:rivanov/ci-cuda-13-5-lanes

Conversation

@roivanov

@roivanov roivanov commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Run the CUDA toolkit CI lanes against CUDA 13.5.0 as well as the baseline toolkit, run the cutile tile-library GPU tests on 13.5, and build the test tile library from its .mlir sources in CI instead of using checked-in bytecode. Until now the tile-library tests only ever ran their prebuilt fallback in CI, because no CUDA image ships cuda-tile-translate, and tests that need the 13.5 toolkit passed silently when they skipped.

Changes

  • unit-tests and examples-compile: the CUDA image is a matrix axis (the baseline plus nvcr.io/nvidia/cuda:13.5.0-devel-ubuntu24.04, which has the same digest as Docker Hub's image and no anonymous pull limit). Baseline jobs keep their names; new lanes are named ... (CUDA 13.5.0).
  • cutile-rs: the two Tile IR assembler jobs become one tile-ir job with a lane matrix. The base lane tile-ir-cuda13-2 is unchanged; the newest lane is now tile-ir-cuda13-5 (it was tile-ir-cuda13-4) on 13.5.0. It installs cuda-tileiras-13-5 when the image lacks it, checks that tileiras lists bytecode 13.5 and that tileirdisasm is present, runs the assemble_* tests of tile_ir_13_5, tile_ir_13_5_multicast and tile_ir_13_5_comms with --ignored, and sets CUTILE_REQUIRE_TILELIB=1. A required-checks list that names tile-ir-cuda13-4 needs updating.
  • New cuda-tile-translate job: builds the translator with cuda-tile-rs, caches the executable on the cuda-tile submodule commit and build.rs, checks it against the current sources and uploads it as an artifact.
  • gpu-tests: runs on 13.3.0 and 13.5.0. The 13.5.0 lane sets CUTILE_REQUIRE_TILELIB=1, so a skipped static_tilelib test fails instead of reporting as passed.
  • cutile tests: gpu::static_tilelib assembles its library from the .mlir sources with cuda-tile-translate (CUTILE_TILE_TRANSLATE, or the one next to tileiras); the prebuilt .tilebc files and their fallback are removed. A new cutile-ir test reads a 13.5 preview$call back with the toolkit's tileirdisasm.
  • cutile-rs/.github/actions/setup: new cache-key input, appended to the job id in the Rust cache key. tile-ir passes the lane name, so its two lanes no longer share a cache. examples-compile also adds the lane suffix to its failure-log artifact name, so two failing lanes don't collide.
  • CHANGELOG.md and the cuda-tile-rs README describe the new translator requirement.

Testing

CI on this PR passes, including every lane it adds (87 checks passed, 3 skipped, none failed):

  • tile-ir-cuda13-2 and tile-ir-cuda13-5, including the assemble_* tests on the newest lane.
  • cuda-tile-translate: the cold build and the checks against the current sources pass (10 min 35 s).
  • gpu-tests and gpu-tests (CUDA 13.5.0). The 13.5.0 lane sets CUTILE_REQUIRE_TILELIB=1 and runs all 16 static_tilelib tests with no skip.
  • compile all examples (device pipeline, GPU-less) (CUDA 13.5.0) and the unit-tests / cargo test -p ... (CUDA 13.5.0) lanes.

I did not run just -f cuda-oxide/Justfile check locally.

  • just -f cuda-oxide/Justfile check passes
  • cargo oxide run <example> passes, or smoketest (not applicable: CI and cutile test changes only)
  • New example added (not applicable)

Review notes:

  • The first cuda-tile-translate run on a cold cache builds LLVM and MLIR (10 min 35 s on this PR's run; the job timeout is 90 minutes). Later runs hit the cache.
  • Two commits are by Leopold Cambier, cherry-picked with his authorship and sign-offs (build the test tile library from source, build cuda-tile-translate and require the 13.5 tile-library tests).

Checklist

  • All commits signed off (git commit -s)
  • All commits cryptographically signed and shown as Verified on GitHub (git commit -S)
  • SPDX headers on new source files (no new source files)

roivanov and others added 7 commits November 16, 2026 09:32
Run the CUDA-toolkit lanes against the newest published CUDA image (13.5.0,
from nvcr.io, which has no anonymous pull limit) as well as the baseline:

- unit-tests: the CUDA image is a matrix axis crossed with the crate list.
- examples-compile: the same axis on the examples job.
- cutile-rs: the two Tile IR assembler jobs become one tile-ir job with a
  lane matrix. The base lane tile-ir-cuda13-2 keeps 13.2.0; the newest lane
  tile-ir-cuda13-5 (formerly tile-ir-cuda13-4) uses 13.5.0, installs
  cuda-tileiras-13-5 when the image lacks the assembler, and checks that
  tileiras lists bytecode 13.5.

Baseline lanes keep their job names; new lanes are named "(CUDA 13.5.0)".

Signed-off-by: Roman Ivanov <[email protected]>
…est is skipped

cargo test hides the output of passing tests, and official_translator_reads_13_5_preview_calls
returns early when no cuda-tile-translate accepting bytecode 13.5 sits next to
tileiras, so it can pass without running. Run it with --nocapture on the
newest job and fail on the skip message.

Signed-off-by: Roman Ivanov <[email protected]>
…e IR job

The assemble_* tests in tile_ir_13_5, tile_ir_13_5_multicast and
tile_ir_13_5_comms are #[ignore]d because they need a CUDA 13.5 assembler, so
the default run reports them as ignored and they never ran in CI. They need no
GPU and call the real tileiras (they fail, not skip, when it cannot assemble).
Run them with --ignored on the 13.5 job, and require that all three ran.

Suggested in review by elibol.

Signed-off-by: Roman Ivanov <[email protected]>
…n a skip

The cutile gpu::static_tilelib tests need Tile IR 13.5 foreign-call support.
The gpu-tests job ran only on CUDA 13.3, where they return early, and the
harness reports an early return as passed: all of them passed in 0.1 s each
without running.

- gpu-tests runs the same test packages on CUDA 13.3 (as before) and CUDA 13.5.0.
- The 13.5 job sets CUTILE_REQUIRE_TILELIB=1; static_tilelib then panics
  instead of skipping when 13.5 foreign calls or nvcc are missing.

Raised in review by nvlcambier and elibol.

Signed-off-by: Roman Ivanov <[email protected]>
The 13.5 translator test returns early, and passes, when no cuda-tile-translate
accepting bytecode 13.5 sits next to tileiras. The step that catches this ran
only on the newest job. Run the test once on both jobs, with its output shown
and at least one test required to pass, then check the outcome against the lane
in two steps selected with the lane flag: the newest job fails if the test was
skipped, the base job (an old assembler) fails if it was not, which also shows
the base job still has the old toolkit.

Signed-off-by: Roman Ivanov <[email protected]>
…uilt fallback

The static tile-library GPU tests (cutile gpu::static_tilelib) assemble the
interface and implementation of their library from .mlir sources with
cuda-tile-translate. The CUDA toolkit does not ship that tool, so the tests
fell back to bytecode checked in under tests/gpu/tilelib/prebuilt, and CI
only ever ran the fallback.

Remove the fallback and the prebuilt files. The library is always assembled
from its sources:

- CUTILE_TILE_TRANSLATE names cuda-tile-translate; without it, the one next
  to tileiras is used. cuda-tile-rs builds one from cuda-tile v13.5.0, and
  its output for both sources is byte-identical to the removed files.
- Without a translator the tests skip with a message. With
  CUTILE_REQUIRE_TILELIB=1 they fail instead, as they already do without
  Tile IR 13.5 or nvcc.

Add a cutile-ir test that reads a 13.5 preview$call back with tileirdisasm,
the disassembler every CUDA 13.5 toolkit installs next to tileiras. The
existing translator test needs cuda-tile-translate and so skips with a plain
toolkit; this one runs there, and fails instead of skipping when
CUTILE_REQUIRE_TILELIB=1.

Tested with the CUDA 13.5 toolkit (tileiras 13.5.37) on an A100:
- gpu static_tilelib with the cuda-tile-rs translator and no translator next
  to tileiras: 16 passed (one test thread, as in CI); compute-sanitizer
  memcheck reports 0 errors.
- No translator: the tests skip, and fail with CUTILE_REQUIRE_TILELIB=1.
- cutile-ir bytecode_13_5: 13 passed with a toolkit that has no translator,
  and with CUTILE_REFERENCE_TRANSLATE set to the cuda-tile-rs translator.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Leopold Cambier <[email protected]>
…brary tests

The CUDA 13.5 images have no cuda-tile-translate, so the "translator test ran"
gate on tile-ir-cuda13-5 could never pass, and the step after it, the 13.5
assembler tests, never ran.

- New job cuda-tile-translate: builds the translator with cuda-tile-rs from
  the cuda-tile submodule (which compiles LLVM and MLIR), caches the
  executable on the submodule commit and build.rs, and uploads it. On a cache
  hit it builds nothing. Every run assembles the test tile library sources
  with it, disassembles the result with the toolkit's tileirdisasm, and runs
  the cutile-ir 13.5 bytecode tests against it.
- gpu-tests downloads the translator and sets CUTILE_TILE_TRANSLATE, so the
  CUDA 13.5 job builds the test tile library from source. It keeps
  CUTILE_REQUIRE_TILELIB=1.
- tile-ir: remove the three translator steps. The newest lane sets
  CUTILE_REQUIRE_TILELIB=1, so the tileirdisasm test must run there, and it
  checks that tileirdisasm is installed. The base-lane check that the
  translator test skipped is removed: it passed because no image has the
  tool, not because the assembler is old.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Signed-off-by: Leopold Cambier <[email protected]>
@roivanov
roivanov force-pushed the rivanov/ci-cuda-13-5-lanes branch from 3fddc4b to bf148b3 Compare November 16, 2026 17:32

@elibol elibol left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@roivanov
roivanov merged commit f13e7df into NVIDIA:main Nov 16, 2026
90 checks passed
@roivanov
roivanov deleted the rivanov/ci-cuda-13-5-lanes branch November 16, 2026 23:07
nvlcambier added a commit that referenced this pull request Nov 19, 2026
## Why

Some tile-library functions work on global memory directly. They read
input from it, write output to it, or take a scratch buffer. Their Tile
IR declarations take a rank-0 `tile<ptr<T>>` operand.
`#[cutile::foreign]` accepted only tiles and scalars, so cuTile Rust
could not declare or call these functions. cuTile Python supports them
through `Array.foreign_pointer`, and Tile C++ through `ct::tile<T*,
ct::shape<>>`.

## What

- **Declarations take raw pointers.** In a `#[cutile::foreign]`
declaration, a `*const E` or `*mut E` parameter lowers to a rank-0
`tile<ptr<E>>` operand of `preview$call`. This follows the usual Rust
FFI split: an `extern` declaration takes raw pointers, and a wrapper
takes tensors and states the remaining obligations.
  - A pointer operand is always `in`, as in Tile IR.
- `*mut` documents that the library writes through the pointer. rustc
rejects a `*const` where `*mut` is declared.
- These are errors: `#[inout]` pointers, pointer results, and pointers
to non-element types.
- **`subtensor_mut_ptr()` on a mutable tensor (ranks 1–3) returns the
address of the current tile block's writable sub-tensor**, the element
`load()` reads first. A wrapper that takes `&mut Tensor` therefore gives
each block its own memory, like `load` and `store`. It is not named
`as_mut_ptr` because, unlike for slices, it is not the same address as
`as_ptr()`.
  - `as_ptr()` still returns the base of the whole view.
- Two compiler ops compute the offset: `get_static_extent` (the tile's
static extent) and `get_tensor_stride` (the view's static or dynamic
stride).
  - A cast fix lets `tile_to_pointer` work inside generic helpers.
- **Ordering needs no new design.** Foreign calls were already ordered
with every memory access. A new test checks that a load after a call
reads what the library wrote through a pointer, and that the library
sees every store before the call.
- **Docs:** a "Pointer operands" section in the tile-library guide, with
examples taken from the test library, and a CHANGELOG entry.

## Tests

- **Parser unit tests:** pointer parameters are `in` operands; the
rejected forms give clear errors.
- **IR tests (`foreign_calls.rs`):**
  - `*const` and `*mut` operands lower to `tile<ptr<…>>`;
  - a generic pointee is instantiated;
  - `u8` and `i8` pointees both become `ptr<i8>`.
- **An ordering test** for accesses around a pointer-operand call.
- **3 UI tests:** a `#[inout]` pointer, a pointer result, and a `*const`
passed where `*mut` is declared.
- **GPU tests (`static_tilelib`).** The static test tile library gains
`store_to` (writes through a pointer) and `load_from` (reads through a
pointer). Their SIMT side receives the pointer by value from a rank-0
local fragment. The GPU tests check:
- rank-1, rank-2 (3 × 2 grid) and rank-3 (2 × 3 × 2 grid) block tiles
from `subtensor_mut_ptr`, so every block-id axis contributes to the
address;
  - a pointer offset from `as_ptr`;
- in each case, several tile blocks, with the results checked on the
host.

Like the existing `static_tilelib` tests, these need a `tileiras` with
Tile IR 13.5 foreign calls. With an older `tileiras` they print SKIP and
pass; the CUDA 13.5 GPU lane sets `CUTILE_REQUIRE_TILELIB=1`, so there
they must run.

## Review follow-up

### `subtensor_mut_ptr` offsets are computed in `i64`

The offset `block id × tile extent × stride` was computed in `i32`. For
a `[32769, 65536]` `u8` tensor in `[1, 65536]` tiles, block 32768 got
−2³¹ instead of +2³¹, a pointer before the allocation. Each factor is
now sign-extended to `i64` before the products and the sum, and the base
is offset with `offset_tile`.

- `foreign_calls.rs`: the offsets of a rank-2 byte tensor are
`tile<i64>`, and no product or sum that feeds them is in `i32`.
- `gpu/subtensor_mut_ptr.rs` (runs in CI, needs no tile library): each
block writes a marker through its pointer, on both sides of the
2³¹-element boundary.
  - Rank 2 is the reviewer's case, where one product overflows.
- Rank 3, `[2, 16385, 65536]`, keeps every per-axis term in `i32` range,
but their sum crosses 2³¹.
  - Both tests fail with an illegal address on the old arithmetic.

### The book's wrapper is `unsafe`

`subtensor_mut_ptr` does not mask the out-of-bounds part of a partial
tile, as `load` and `store` do. The book's safe `store_tile` wrapper
wrote 31 elements out of bounds for a `[33]` tensor in `[32]` tiles. It
is now `unsafe fn`, with a `# Safety` section that requires a whole,
contiguous sub-tensor.

### Renamed to `subtensor_mut_ptr`; safety contracts live where memory
is accessed

- `block_tile_ptr()` / `BlockTilePtr` are now `subtensor_mut_ptr()` /
`SubtensorMutPtr`, matching how the docs describe each block's writable
sub-tensor (`gpu/block_tile_ptr.rs` is now `gpu/subtensor_mut_ptr.rs`).
- Getting the pointer stays safe, like `<[T]>::as_mut_ptr`. Its docs no
longer place obligations on the caller of the getter: they state the
pointer's properties (no bounds, not contiguous in general) and that the
bounds, contiguity and access requirements belong in the `# Safety`
contract of the function that reads or writes through it.
- The book's `store_to` / `load_from` declarations (and the test
library's) now carry that full contract: valid for writes (reads) of 32
contiguous `f32` inside one allocation, the library does not check
bounds, and no other block accesses them during the call. The
`round_trip` kernel and `store_tile` wrapper name the contract each
`SAFETY` comment meets.

### Rebased onto `main`

Rebased and squashed onto current `main`, which builds the test tile
library from its `.mlir` sources in CI (#1813): the branch's updates to
the prebuilt `.tilebc` files are dropped, and the `.mlir` sources carry
`store_to` / `load_from`.

## Fix: one cache key per set of tile libraries

### Why

While validating this PR,
`static_tilelib::global_accesses_after_a_foreign_call_execute` (already
on `main`) failed about 1 run in 5. It wasn't a `tileiras` bug, or a
problem with ordering after foreign calls. It failed every time (15/15)
whenever `default_tile_libraries_link_every_kernel` ran next to it:

1. That test sets the process-wide default libraries to the same
`test_lib.tilelib` the other test's launch already names.
2. `CompileOptions::with_default_tile_libraries` appends the defaults to
the launch's list, so the list became `[tilelib, tilelib]` instead of
`[tilelib]`.
3. The in-memory cache key (`TileFunctionKey`) included that list as
written. Two lists meant two keys, so the kernel compiled a second time
and a **second module** was loaded.
4. Each module has its own copy of every `Global`. The second launch's
counter started at 0 instead of 2, and the test failed (`CUTILE_JIT_LOG`
shows two compiles of the kernel).

This is a real bug, not only a flaky test. The guide says that naming a
library that is already a default "changes nothing". Instead it cost a
recompile and split a kernel's `Global` state between modules. Naming
the same library through another path (relative or absolute,
`dir/./lib`) did the same. CI didn't catch it because its `tileiras` was
13.3 at the time, so `static_tilelib` skipped there.

### What

The key's `compile_options` now has an empty `tile_libraries`. The
libraries are identified by `tile_libraries_digest` alone, which was
already in the key and is taken over the resolved, canonical,
de-duplicated libraries: paths, contents, dependencies and settings.
`Specialization` keeps the full options, and compiles and computes the
L2 key with them. The on-disk L2 key already hashed the linked
libraries, so it does not change.

### Tests

- `one_set_of_tile_libraries_is_one_cache_key` runs in CI. It needs no
tile-library toolchain, because computing a key only hashes the files.
- A duplicated library, a path through `.`, and a different order give
one key.
- No library, one more library, or a rebuilt library still give
different keys.
- `a_default_library_named_again_keeps_the_cache_key` (13.5): a launch
that names a default library again keys like the same launch without the
default.
- Both tests fail on the old key. The previously failing pair now passes
15/15, and `static_tilelib` passes 10/10.

## Validation

- After the rebase: `scripts/run_cpu_tests.sh` passes; `static_tilelib`
and `subtensor_mut_ptr` GPU tests pass (24/24) with a 13.5 `tileiras` on
an A100 and `CUTILE_REQUIRE_TILELIB=1`; `foreign_calls` (29) and
`foreign_ordering` (23) pass; clippy `-D warnings` is clean.
- `scripts/run_cpu_tests.sh` and the `static_tilelib` GPU tests pass
with a 13.5 `tileiras`. The pointer GPU tests run (no SKIP).
- compute-sanitizer memcheck reports 0 errors on the pointer GPU tests.
- The whole `gpu` test binary passes (132 tests), and so do `warmup`,
`warmup_suite`, `submission_lifetimes`, `kernel_compiler`,
`jit_env_overrides` and `foreign_calls`, all with a 13.5 `tileiras` on
an A100.
- compute-sanitizer memcheck reports 0 errors on the `subtensor_mut_ptr`
boundary tests, and (after the rebase) on the five pointer-operand
`static_tilelib` tests.
- `cargo fmt --check` and clippy are clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Leopold Cambier <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
uurl pushed a commit to uurl/cuda-rust that referenced this pull request Nov 21, 2026
…#1749)

## Why

Some tile-library functions work on global memory directly. They read
input from it, write output to it, or take a scratch buffer. Their Tile
IR declarations take a rank-0 `tile<ptr<T>>` operand.
`#[cutile::foreign]` accepted only tiles and scalars, so cuTile Rust
could not declare or call these functions. cuTile Python supports them
through `Array.foreign_pointer`, and Tile C++ through `ct::tile<T*,
ct::shape<>>`.

## What

- **Declarations take raw pointers.** In a `#[cutile::foreign]`
declaration, a `*const E` or `*mut E` parameter lowers to a rank-0
`tile<ptr<E>>` operand of `preview$call`. This follows the usual Rust
FFI split: an `extern` declaration takes raw pointers, and a wrapper
takes tensors and states the remaining obligations.
  - A pointer operand is always `in`, as in Tile IR.
- `*mut` documents that the library writes through the pointer. rustc
rejects a `*const` where `*mut` is declared.
- These are errors: `#[inout]` pointers, pointer results, and pointers
to non-element types.
- **`subtensor_mut_ptr()` on a mutable tensor (ranks 1–3) returns the
address of the current tile block's writable sub-tensor**, the element
`load()` reads first. A wrapper that takes `&mut Tensor` therefore gives
each block its own memory, like `load` and `store`. It is not named
`as_mut_ptr` because, unlike for slices, it is not the same address as
`as_ptr()`.
  - `as_ptr()` still returns the base of the whole view.
- Two compiler ops compute the offset: `get_static_extent` (the tile's
static extent) and `get_tensor_stride` (the view's static or dynamic
stride).
  - A cast fix lets `tile_to_pointer` work inside generic helpers.
- **Ordering needs no new design.** Foreign calls were already ordered
with every memory access. A new test checks that a load after a call
reads what the library wrote through a pointer, and that the library
sees every store before the call.
- **Docs:** a "Pointer operands" section in the tile-library guide, with
examples taken from the test library, and a CHANGELOG entry.

## Tests

- **Parser unit tests:** pointer parameters are `in` operands; the
rejected forms give clear errors.
- **IR tests (`foreign_calls.rs`):**
  - `*const` and `*mut` operands lower to `tile<ptr<…>>`;
  - a generic pointee is instantiated;
  - `u8` and `i8` pointees both become `ptr<i8>`.
- **An ordering test** for accesses around a pointer-operand call.
- **3 UI tests:** a `#[inout]` pointer, a pointer result, and a `*const`
passed where `*mut` is declared.
- **GPU tests (`static_tilelib`).** The static test tile library gains
`store_to` (writes through a pointer) and `load_from` (reads through a
pointer). Their SIMT side receives the pointer by value from a rank-0
local fragment. The GPU tests check:
- rank-1, rank-2 (3 × 2 grid) and rank-3 (2 × 3 × 2 grid) block tiles
from `subtensor_mut_ptr`, so every block-id axis contributes to the
address;
  - a pointer offset from `as_ptr`;
- in each case, several tile blocks, with the results checked on the
host.

Like the existing `static_tilelib` tests, these need a `tileiras` with
Tile IR 13.5 foreign calls. With an older `tileiras` they print SKIP and
pass; the CUDA 13.5 GPU lane sets `CUTILE_REQUIRE_TILELIB=1`, so there
they must run.

## Review follow-up

### `subtensor_mut_ptr` offsets are computed in `i64`

The offset `block id × tile extent × stride` was computed in `i32`. For
a `[32769, 65536]` `u8` tensor in `[1, 65536]` tiles, block 32768 got
−2³¹ instead of +2³¹, a pointer before the allocation. Each factor is
now sign-extended to `i64` before the products and the sum, and the base
is offset with `offset_tile`.

- `foreign_calls.rs`: the offsets of a rank-2 byte tensor are
`tile<i64>`, and no product or sum that feeds them is in `i32`.
- `gpu/subtensor_mut_ptr.rs` (runs in CI, needs no tile library): each
block writes a marker through its pointer, on both sides of the
2³¹-element boundary.
  - Rank 2 is the reviewer's case, where one product overflows.
- Rank 3, `[2, 16385, 65536]`, keeps every per-axis term in `i32` range,
but their sum crosses 2³¹.
  - Both tests fail with an illegal address on the old arithmetic.

### The book's wrapper is `unsafe`

`subtensor_mut_ptr` does not mask the out-of-bounds part of a partial
tile, as `load` and `store` do. The book's safe `store_tile` wrapper
wrote 31 elements out of bounds for a `[33]` tensor in `[32]` tiles. It
is now `unsafe fn`, with a `# Safety` section that requires a whole,
contiguous sub-tensor.

### Renamed to `subtensor_mut_ptr`; safety contracts live where memory
is accessed

- `block_tile_ptr()` / `BlockTilePtr` are now `subtensor_mut_ptr()` /
`SubtensorMutPtr`, matching how the docs describe each block's writable
sub-tensor (`gpu/block_tile_ptr.rs` is now `gpu/subtensor_mut_ptr.rs`).
- Getting the pointer stays safe, like `<[T]>::as_mut_ptr`. Its docs no
longer place obligations on the caller of the getter: they state the
pointer's properties (no bounds, not contiguous in general) and that the
bounds, contiguity and access requirements belong in the `# Safety`
contract of the function that reads or writes through it.
- The book's `store_to` / `load_from` declarations (and the test
library's) now carry that full contract: valid for writes (reads) of 32
contiguous `f32` inside one allocation, the library does not check
bounds, and no other block accesses them during the call. The
`round_trip` kernel and `store_tile` wrapper name the contract each
`SAFETY` comment meets.

### Rebased onto `main`

Rebased and squashed onto current `main`, which builds the test tile
library from its `.mlir` sources in CI (NVIDIA#1813): the branch's updates to
the prebuilt `.tilebc` files are dropped, and the `.mlir` sources carry
`store_to` / `load_from`.

## Fix: one cache key per set of tile libraries

### Why

While validating this PR,
`static_tilelib::global_accesses_after_a_foreign_call_execute` (already
on `main`) failed about 1 run in 5. It wasn't a `tileiras` bug, or a
problem with ordering after foreign calls. It failed every time (15/15)
whenever `default_tile_libraries_link_every_kernel` ran next to it:

1. That test sets the process-wide default libraries to the same
`test_lib.tilelib` the other test's launch already names.
2. `CompileOptions::with_default_tile_libraries` appends the defaults to
the launch's list, so the list became `[tilelib, tilelib]` instead of
`[tilelib]`.
3. The in-memory cache key (`TileFunctionKey`) included that list as
written. Two lists meant two keys, so the kernel compiled a second time
and a **second module** was loaded.
4. Each module has its own copy of every `Global`. The second launch's
counter started at 0 instead of 2, and the test failed (`CUTILE_JIT_LOG`
shows two compiles of the kernel).

This is a real bug, not only a flaky test. The guide says that naming a
library that is already a default "changes nothing". Instead it cost a
recompile and split a kernel's `Global` state between modules. Naming
the same library through another path (relative or absolute,
`dir/./lib`) did the same. CI didn't catch it because its `tileiras` was
13.3 at the time, so `static_tilelib` skipped there.

### What

The key's `compile_options` now has an empty `tile_libraries`. The
libraries are identified by `tile_libraries_digest` alone, which was
already in the key and is taken over the resolved, canonical,
de-duplicated libraries: paths, contents, dependencies and settings.
`Specialization` keeps the full options, and compiles and computes the
L2 key with them. The on-disk L2 key already hashed the linked
libraries, so it does not change.

### Tests

- `one_set_of_tile_libraries_is_one_cache_key` runs in CI. It needs no
tile-library toolchain, because computing a key only hashes the files.
- A duplicated library, a path through `.`, and a different order give
one key.
- No library, one more library, or a rebuilt library still give
different keys.
- `a_default_library_named_again_keeps_the_cache_key` (13.5): a launch
that names a default library again keys like the same launch without the
default.
- Both tests fail on the old key. The previously failing pair now passes
15/15, and `static_tilelib` passes 10/10.

## Validation

- After the rebase: `scripts/run_cpu_tests.sh` passes; `static_tilelib`
and `subtensor_mut_ptr` GPU tests pass (24/24) with a 13.5 `tileiras` on
an A100 and `CUTILE_REQUIRE_TILELIB=1`; `foreign_calls` (29) and
`foreign_ordering` (23) pass; clippy `-D warnings` is clean.
- `scripts/run_cpu_tests.sh` and the `static_tilelib` GPU tests pass
with a 13.5 `tileiras`. The pointer GPU tests run (no SKIP).
- compute-sanitizer memcheck reports 0 errors on the pointer GPU tests.
- The whole `gpu` test binary passes (132 tests), and so do `warmup`,
`warmup_suite`, `submission_lifetimes`, `kernel_compiler`,
`jit_env_overrides` and `foreign_calls`, all with a 13.5 `tileiras` on
an A100.
- compute-sanitizer memcheck reports 0 errors on the `subtensor_mut_ptr`
boundary tests, and (after the rebase) on the five pointer-operand
`static_tilelib` tests.
- `cargo fmt --check` and clippy are clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Leopold Cambier <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Raul Estrada <[email protected]>
uurl pushed a commit to uurl/cuda-rust that referenced this pull request Nov 21, 2026
…#1749)

## Why

Some tile-library functions work on global memory directly. They read
input from it, write output to it, or take a scratch buffer. Their Tile
IR declarations take a rank-0 `tile<ptr<T>>` operand.
`#[cutile::foreign]` accepted only tiles and scalars, so cuTile Rust
could not declare or call these functions. cuTile Python supports them
through `Array.foreign_pointer`, and Tile C++ through `ct::tile<T*,
ct::shape<>>`.

## What

- **Declarations take raw pointers.** In a `#[cutile::foreign]`
declaration, a `*const E` or `*mut E` parameter lowers to a rank-0
`tile<ptr<E>>` operand of `preview$call`. This follows the usual Rust
FFI split: an `extern` declaration takes raw pointers, and a wrapper
takes tensors and states the remaining obligations.
  - A pointer operand is always `in`, as in Tile IR.
- `*mut` documents that the library writes through the pointer. rustc
rejects a `*const` where `*mut` is declared.
- These are errors: `#[inout]` pointers, pointer results, and pointers
to non-element types.
- **`subtensor_mut_ptr()` on a mutable tensor (ranks 1–3) returns the
address of the current tile block's writable sub-tensor**, the element
`load()` reads first. A wrapper that takes `&mut Tensor` therefore gives
each block its own memory, like `load` and `store`. It is not named
`as_mut_ptr` because, unlike for slices, it is not the same address as
`as_ptr()`.
  - `as_ptr()` still returns the base of the whole view.
- Two compiler ops compute the offset: `get_static_extent` (the tile's
static extent) and `get_tensor_stride` (the view's static or dynamic
stride).
  - A cast fix lets `tile_to_pointer` work inside generic helpers.
- **Ordering needs no new design.** Foreign calls were already ordered
with every memory access. A new test checks that a load after a call
reads what the library wrote through a pointer, and that the library
sees every store before the call.
- **Docs:** a "Pointer operands" section in the tile-library guide, with
examples taken from the test library, and a CHANGELOG entry.

## Tests

- **Parser unit tests:** pointer parameters are `in` operands; the
rejected forms give clear errors.
- **IR tests (`foreign_calls.rs`):**
  - `*const` and `*mut` operands lower to `tile<ptr<…>>`;
  - a generic pointee is instantiated;
  - `u8` and `i8` pointees both become `ptr<i8>`.
- **An ordering test** for accesses around a pointer-operand call.
- **3 UI tests:** a `#[inout]` pointer, a pointer result, and a `*const`
passed where `*mut` is declared.
- **GPU tests (`static_tilelib`).** The static test tile library gains
`store_to` (writes through a pointer) and `load_from` (reads through a
pointer). Their SIMT side receives the pointer by value from a rank-0
local fragment. The GPU tests check:
- rank-1, rank-2 (3 × 2 grid) and rank-3 (2 × 3 × 2 grid) block tiles
from `subtensor_mut_ptr`, so every block-id axis contributes to the
address;
  - a pointer offset from `as_ptr`;
- in each case, several tile blocks, with the results checked on the
host.

Like the existing `static_tilelib` tests, these need a `tileiras` with
Tile IR 13.5 foreign calls. With an older `tileiras` they print SKIP and
pass; the CUDA 13.5 GPU lane sets `CUTILE_REQUIRE_TILELIB=1`, so there
they must run.

## Review follow-up

### `subtensor_mut_ptr` offsets are computed in `i64`

The offset `block id × tile extent × stride` was computed in `i32`. For
a `[32769, 65536]` `u8` tensor in `[1, 65536]` tiles, block 32768 got
−2³¹ instead of +2³¹, a pointer before the allocation. Each factor is
now sign-extended to `i64` before the products and the sum, and the base
is offset with `offset_tile`.

- `foreign_calls.rs`: the offsets of a rank-2 byte tensor are
`tile<i64>`, and no product or sum that feeds them is in `i32`.
- `gpu/subtensor_mut_ptr.rs` (runs in CI, needs no tile library): each
block writes a marker through its pointer, on both sides of the
2³¹-element boundary.
  - Rank 2 is the reviewer's case, where one product overflows.
- Rank 3, `[2, 16385, 65536]`, keeps every per-axis term in `i32` range,
but their sum crosses 2³¹.
  - Both tests fail with an illegal address on the old arithmetic.

### The book's wrapper is `unsafe`

`subtensor_mut_ptr` does not mask the out-of-bounds part of a partial
tile, as `load` and `store` do. The book's safe `store_tile` wrapper
wrote 31 elements out of bounds for a `[33]` tensor in `[32]` tiles. It
is now `unsafe fn`, with a `# Safety` section that requires a whole,
contiguous sub-tensor.

### Renamed to `subtensor_mut_ptr`; safety contracts live where memory
is accessed

- `block_tile_ptr()` / `BlockTilePtr` are now `subtensor_mut_ptr()` /
`SubtensorMutPtr`, matching how the docs describe each block's writable
sub-tensor (`gpu/block_tile_ptr.rs` is now `gpu/subtensor_mut_ptr.rs`).
- Getting the pointer stays safe, like `<[T]>::as_mut_ptr`. Its docs no
longer place obligations on the caller of the getter: they state the
pointer's properties (no bounds, not contiguous in general) and that the
bounds, contiguity and access requirements belong in the `# Safety`
contract of the function that reads or writes through it.
- The book's `store_to` / `load_from` declarations (and the test
library's) now carry that full contract: valid for writes (reads) of 32
contiguous `f32` inside one allocation, the library does not check
bounds, and no other block accesses them during the call. The
`round_trip` kernel and `store_tile` wrapper name the contract each
`SAFETY` comment meets.

### Rebased onto `main`

Rebased and squashed onto current `main`, which builds the test tile
library from its `.mlir` sources in CI (NVIDIA#1813): the branch's updates to
the prebuilt `.tilebc` files are dropped, and the `.mlir` sources carry
`store_to` / `load_from`.

## Fix: one cache key per set of tile libraries

### Why

While validating this PR,
`static_tilelib::global_accesses_after_a_foreign_call_execute` (already
on `main`) failed about 1 run in 5. It wasn't a `tileiras` bug, or a
problem with ordering after foreign calls. It failed every time (15/15)
whenever `default_tile_libraries_link_every_kernel` ran next to it:

1. That test sets the process-wide default libraries to the same
`test_lib.tilelib` the other test's launch already names.
2. `CompileOptions::with_default_tile_libraries` appends the defaults to
the launch's list, so the list became `[tilelib, tilelib]` instead of
`[tilelib]`.
3. The in-memory cache key (`TileFunctionKey`) included that list as
written. Two lists meant two keys, so the kernel compiled a second time
and a **second module** was loaded.
4. Each module has its own copy of every `Global`. The second launch's
counter started at 0 instead of 2, and the test failed (`CUTILE_JIT_LOG`
shows two compiles of the kernel).

This is a real bug, not only a flaky test. The guide says that naming a
library that is already a default "changes nothing". Instead it cost a
recompile and split a kernel's `Global` state between modules. Naming
the same library through another path (relative or absolute,
`dir/./lib`) did the same. CI didn't catch it because its `tileiras` was
13.3 at the time, so `static_tilelib` skipped there.

### What

The key's `compile_options` now has an empty `tile_libraries`. The
libraries are identified by `tile_libraries_digest` alone, which was
already in the key and is taken over the resolved, canonical,
de-duplicated libraries: paths, contents, dependencies and settings.
`Specialization` keeps the full options, and compiles and computes the
L2 key with them. The on-disk L2 key already hashed the linked
libraries, so it does not change.

### Tests

- `one_set_of_tile_libraries_is_one_cache_key` runs in CI. It needs no
tile-library toolchain, because computing a key only hashes the files.
- A duplicated library, a path through `.`, and a different order give
one key.
- No library, one more library, or a rebuilt library still give
different keys.
- `a_default_library_named_again_keeps_the_cache_key` (13.5): a launch
that names a default library again keys like the same launch without the
default.
- Both tests fail on the old key. The previously failing pair now passes
15/15, and `static_tilelib` passes 10/10.

## Validation

- After the rebase: `scripts/run_cpu_tests.sh` passes; `static_tilelib`
and `subtensor_mut_ptr` GPU tests pass (24/24) with a 13.5 `tileiras` on
an A100 and `CUTILE_REQUIRE_TILELIB=1`; `foreign_calls` (29) and
`foreign_ordering` (23) pass; clippy `-D warnings` is clean.
- `scripts/run_cpu_tests.sh` and the `static_tilelib` GPU tests pass
with a 13.5 `tileiras`. The pointer GPU tests run (no SKIP).
- compute-sanitizer memcheck reports 0 errors on the pointer GPU tests.
- The whole `gpu` test binary passes (132 tests), and so do `warmup`,
`warmup_suite`, `submission_lifetimes`, `kernel_compiler`,
`jit_env_overrides` and `foreign_calls`, all with a 13.5 `tileiras` on
an A100.
- compute-sanitizer memcheck reports 0 errors on the `subtensor_mut_ptr`
boundary tests, and (after the rebase) on the five pointer-operand
`static_tilelib` tests.
- `cargo fmt --check` and clippy are clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Leopold Cambier <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Raul Estrada <[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.

3 participants