Skip to content

nvidia TileIR backend - #9733

Draft
abadams wants to merge 46 commits into
mainfrom
abadams/tile_ir
Draft

abadams wants to merge 46 commits into
mainfrom
abadams/tile_ir

Conversation

@abadams

@abadams abadams commented Nov 30, 2026

Copy link
Copy Markdown
Member

This is a prototype cuda backend that passes tile IR kernels to the driver instead of PTX. It gets the same matmul performance as the wmma work, but the schedule is much easier to write because the driver handles a lot of stuff for you (e.g. staging through shared). TileIR is intended to be more portable across nvidia microarchitectures than using PTX directly.

Most of the code is just tileIR bitcode emission. In Halide lowering, the gputhreads dimensions are just treated as vectorized dimensions, and large vectors are passed to the backend.

Opening as draft for discussion.

abadams and others added 30 commits April 17, 2026 11:40
Resolve conflicts with main and bring the CUDA Tile IR backend back to a
state where the full correctness suite passes under
host-cuda-cuda_capability_120-cuda_tile_ir (host and PTX CUDA also pass).

Notable changes on top of the merge:
- Target bytecode 13.4 and use alloca for per-thread local storage.
- Add Func::gpu_min_resident_blocks scheduling directive (occupancy hint).
- Emit tile stores for output instead of scalar stores.
- Thread register tiles and memory tokens through For/If regions,
  restoring the outer token after reduce regions.
- Native transpose, row-wise VectorReduce, power-of-two checks on
  concat/slice fast paths, nested ramp/broadcast support.
- Emit tile IR assert for AssertStmt in kernels.
- LICM no longer lifts expressions containing any vector subexpression.
- lower_mux only unwraps scalar broadcast indices.
- Skip wmma tests and gpu_thread_barrier under tile IR.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Factor region capture and carried-value handling into shared helpers,
merge register tile bookkeeping into one record, build VectorReduce
bodies from Halide Exprs with identities from Type::max/min, drop
RewrapInnermost from FuseGPUThreadLoops, and tidy RemapCUDATileIRLoops.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Unify binary/unary/comparison emission, make Call lowering table-driven,
share concat/extract helpers for shuffles, simplify Cast, atomics, block
id lookup, pointer casts and buffer element type tracking.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restructure the LowerMma matcher as a single function with early returns,
decompose linear expressions by substitution, and let emit_op take an op's
leading varint fields. Add write_varints, write_list and optional-operand
helpers for the remaining encodings. The generated bytecode is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Require constant operand strides when matching, as the codegen does, and
decompose both operands' base addresses the same way. A constant k offset
in A's base (e.g. from unrolling the k loop) now goes into its k tile
coordinate rather than its pointer offset.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Integer, float, and reduction-identity constants now share one bit
encoding. float16 constants previously emitted four-byte payloads,
producing invalid bytecode, and bfloat16 was tagged as f16 because
Type::is_float() is also true for bfloat.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ConstantOp already splats a scalar payload across a tile, so there's no
need to build vector constants with a reshape and broadcast.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tomics

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Key the type table on its entries rather than hand-built strings, share
section framing and little-endian writing, and build the function table
before the string and type tables it refers to instead of pre-registering
its entries.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ed helpers

The shared lower_euclidean_div/mod helpers had simplify() removed because it
canonicalized power-of-two masks back into Mod nodes. Handle power-of-two
divisors in the Tile IR pre-pass with shifts and masks instead, as CodeGen_C
does, so the shared helpers match main again.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Lower concat_bits everywhere during flatten_nested_ramps, which removes the
wrapper mutator, the Stmt lower_concat_bits export, and the backend's own
call to it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The reduce region is generated from the same Expr combiner as the
scalarized fallback, so SaturatingAdd (identity zero) no longer needs to
be scalarized. Checked with a vectorized atomic saturating_add reduction
on uint8 and uint16.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move the ReturnOp into the emitter, shadow buffer pointers with their
aligned versions, and pass the tensor view operands as op fields.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ister tile accessors

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add an overview comment to CodeGen_TileIR_Dev.cpp describing the
lowering passes, classes, and runtime path, and clarify related comments
in LICM, PartitionLoops, and TileIR.h.

A total VectorReduce of a non-power-of-two vector reduced the padding
lanes too, which gave wrong answers for min and max. Select the identity
into the padding lanes first. Add a regression test.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Atomic RMW ops on non-power-of-two vectors pass the padding mask, so
  the padding lanes no longer write past the end of the buffer.
- The compare-and-swap fallback runs one scalar CAS loop per lane, and
  threads the memory token through the loop. It previously passed a
  vector condition to cuda_tile.if, which the driver rejected.
- LowerMma only matches floating-point products. mmaf rejects integer
  operands.
- Float not-equal uses an unordered comparison, so it is true for NaN.
- trunc is select(x < 0, ceil(x), floor(x)) rather than a round trip
  through int32, which was wrong for magnitudes beyond 2^31 and inf.

Adds gpu_vectorized_atomics and gpu_integer_matmul tests, and large
inputs to the round test.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
abadams and others added 15 commits November 25, 2026 12:59
Atomic codegen matched the whole body against a single unpredicated
Store and emitted anything else non-atomically. It now works per Store,
like CodeGen_LLVM: a Store whose value loads from the stored buffer
becomes an atomic RMW or a compare-and-swap loop, and other Stores are
relaxed. Atomic nodes that need a mutex are a user error, as on PTX.

Inside a compare-and-swap loop, loads of the element being updated read
the compared value, and other loads of the buffer are relaxed, selecting
the compared value when the indices alias at runtime. This matches the
semantics of #9700 for the LLVM backends.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Matches the CUDA backend's error rather than emitting bytecode that
fails verification.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A Register allocation in a Tile IR kernel silently fell back to an
alloca when its accesses weren't one full-tile access, so dynamically
indexed registers (gpu_register_dynamic_index) weren't rejected.

RemapCUDATileIRLoops now keeps Register memory only for allocations
promote_gpu_registers would give per-thread registers to (shared
predicate holds_thread_registers), demoting the rest to Auto, since Tile
IR has no per-thread or per-warp registers. promote_gpu_registers then
runs get_subtile on the remaining ones in place, giving the same errors
as on PTX and dense constant subtile indices.

To consume those, the codegen's register tiles become a set of disjoint
dense subtiles, each its own SSA value, and predicated stores to them
select against the old value. This also promotes Auto allocations
accessed as several disjoint subtiles. RegisterTileAnalyzer now visits
let values, which it previously skipped, missing loads in them.

Shuffles that broadcast a vector or repeat each lane now use
reshape+broadcast instead of per-element extracts. With f31_in$0 in
register_shuffle promoted, the extract path faulted with
CUDA_ERROR_MISALIGNED_ADDRESS, probably a tileiras miscompile.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Type stored lanes in 16 bits, capping vectors at 65535 lanes, which is
too small for the tile-sized vectors a tile-based GPU backend
manipulates. Lanes are now a uint32_t (up to 2^31-1 usable, as lanes()
returns int), and the handle/struct metadata index shrinks to 16 bits to
keep sizeof(Type) == 8. The ABI halide_type_t is unaffected: it never
carried lanes.

Type::hash() can no longer memcpy the first 32 bits, so it mixes the
code, bits, and lanes explicitly. IRMatch's 4-byte BoundConstType
can't hold a 32-bit lane count, so the matcher binds full Types again.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Lets a schedule ask the GPU compiler to fit at least n blocks of a
kernel on one processor at once, trading per-block resources (mostly
registers) for occupancy. Like gpu_max_registers, it takes an explicit
DeviceAPI and only CUDA acts on it, where it becomes the PTX
.minnctapersm directive (via the nvvm.minctasm function attribute).
Other device APIs warn and ignore it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Buffer parameters gain device_alignment()/set_device_alignment(), the
device-side counterpart of host_alignment. It defaults to the element
size, and is serialized alongside host_alignment.

PTX codegen used host_alignment to reason about device pointers, which
says nothing about them. It now uses device_alignment, via a new
CodeGen_LLVM::external_buffer_alignment hook. The PTX rewrite of
4-wide 32-bit loads and stores into 128-bit accesses also only
considered the alignment of the index, so a device crop offset by a
non-multiple of 16 bytes (e.g. one element of a float buffer) failed
with CUDA_ERROR_MISALIGNED_ADDRESS when the buffer's min was
constrained. That rewrite now requires a 16-byte-aligned base pointer.

A requested device alignment is checked at runtime by a new
halide_device_check_alignment, injected after each copy_to_device or
device_malloc the pipeline does for the buffer. External buffers start
in an unknown state, so the first device use always goes through one of
these, which covers buffers that arrive with no device allocation.
The check asks the device interface for the pointer's alignment
through a new device_alignment member of
halide_device_interface_impl_t. Only CUDA implements it; on other
device APIs requesting a device alignment is a runtime error. A
misaligned pointer produces the new halide_error_code_unaligned_device_ptr
(-50).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Binding constant types in the matcher as full Types made
correctness_simplify execute 2.2% more instructions than main, because
comparing Types also checks handle and struct metadata, which bound
constants never have. Restoring the code+bits+lanes BoundConstType,
widened to 32-bit lanes, brings that to +0.2%, and lowering
local_laplacian from +0.44% to +0.15%.

Type::hash packs the low 16 bits of lanes above code and bits, matching
main's hash for any lane count main supports.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…le_ir

# Conflicts:
#	src/IRMatch.h
#	src/Type.h
… into abadams/tile_ir

# Conflicts:
#	src/CodeGen_GPU_Dev.h
#	src/Func.cpp
#	src/Func.h
#	test/correctness/gpu_min_resident_blocks.cpp
The operands only ever live on the device, and PTX codegen now takes
pointer alignment from device_alignment.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Decomposing by substitution needed the simplifier to prove
e == a * var + b, which it can't do for indices like
(y * 64 - min) * 1024. Matrix multiplies on buffers with unconstrained
mins fell back to a vector reduction, which made the cuda_mat_mul tile IR
kernel about 500x slower.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 20.30075% with 530 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.78%. Comparing base (e9485e1) to head (5f24add).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
src/TileIR.cpp 0.00% 226 Missing ⚠️
src/CodeGen_Vulkan_Dev.cpp 16.19% 168 Missing and 8 partials ⚠️
src/RemapCUDATileIRLoops.cpp 0.00% 35 Missing ⚠️
src/SpirvIR.cpp 0.00% 22 Missing ⚠️
src/TileIR.h 0.00% 14 Missing ⚠️
src/Lower.cpp 10.00% 6 Missing and 3 partials ⚠️
src/OffloadGPULoops.cpp 33.33% 3 Missing and 3 partials ⚠️
src/Target.cpp 44.44% 4 Missing and 1 partial ⚠️
src/DeviceInterface.cpp 20.00% 1 Missing and 3 partials ⚠️
src/Func.cpp 63.63% 3 Missing and 1 partial ⚠️
... and 13 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9733      +/-   ##
==========================================
- Coverage   73.24%   71.78%   -1.47%     
==========================================
  Files         266      270       +4     
  Lines       83749    84667     +918     
  Branches    20573    20937     +364     
==========================================
- Hits        61346    60778     -568     
- Misses      15859    17305    +1446     
- Partials     6544     6584      +40     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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