Repository navigation
Description
Problem
A projected read from an addressable local array is lowered by loading/copying the whole aggregate, then indexing into the copy.
Self-contained kernel example:
use cuda_device::{DisjointSlice, kernel, thread};
const ROWS: usize = 4;
const COLS: usize = 2;
#[kernel]
pub fn projected_read_copies_whole_array(
row_arg: usize,
col_arg: usize,
mut out: DisjointSlice<f64>,
) {
let idx = thread::index_1d();
if idx.get() != 0 {
return;
}
let mut scratch = [[0.0_f64; COLS]; ROWS];
scratch[0] = [1.0, 2.0];
scratch[1] = [3.0, 4.0];
scratch[2] = [5.0, 6.0];
scratch[3] = [7.0, 8.0];
let row = row_arg & (ROWS - 1);
let col = col_arg & (COLS - 1);
let value = scratch[row][col];
if let Some(slot) = out.get_mut(idx) {
*slot = value;
}
}row_arg and col_arg are kernel parameters intentionally used to keep the row and column indices dynamic, so the indexing cannot be constant-folded away.
Current generated IR on main at 6f663d1 includes:
%v36 = load [4 x [2 x double]], ptr %v10, align 8
%v37 = alloca [4 x [2 x double]], align 8
store [4 x [2 x double]] %v36, ptr %v37, align 8
%v38 = getelementptr inbounds [4 x [2 x double]], ptr %v37, i32 0, i64 %v32
%v39 = load [2 x double], ptr %v38, align 8
%v40 = alloca [2 x double], align 8
store [2 x double] %v39, ptr %v40, align 8
%v41 = getelementptr inbounds [2 x double], ptr %v40, i32 0, i64 %v33
%v42 = load double, ptr %v41, align 8Expected behavior
The read should stay address-based until the final scalar load, equivalent to:
%row_ptr = getelementptr inbounds [4 x [2 x double]], ptr %scratch, i32 0, i64 %row
%val_ptr = getelementptr inbounds [2 x double], ptr %row_ptr, i32 0, i64 %col
%value = load double, ptr %val_ptrNo whole [4 x [2 x double]] load/store and no temporary aggregate copy should be needed.
Why this matters
For small arrays this is mostly noisy IR. For larger scratch arrays it becomes a major resource issue. In a real kernel with a [[f64; 6]; 1024] local scratch buffer, this pattern creates a second ~49 KiB stack object and a large amount of local-memory traffic.
This looks like it may be a place/value lowering issue: ordinary place reads like scratch[row][col] appear to be going through a value-projection path that materializes aggregates. They should use the existing place-address machinery and emit one final scalar load.
Repro
Add the kernel above as a small cuda-oxide example and build with NVVM IR output:
cargo oxide build <example-name> --emit-nvvm-ir --arch sm_100Then inspect the generated .ll file:
grep -n "load \[4 x \[2 x double\]\]\|alloca \[4 x \[2 x double\]\]\|store \[4 x \[2 x double\]\]" <example-name>.llAcceptance criteria
- Projected reads from addressable local arrays lower to address computation plus a final scalar load.
- The repro no longer emits a whole-array load
[4 x [2 x double]]followed by a temporary whole-array alloca/store. - The nested projection also avoids materializing an intermediate
[2 x double]row copy before the final scalar load. - Nested indexing such as
scratch[row][col]still produces correct runtime results.
Activity
fix(NVIDIA#231): lower projected reads through place addresses
Merge pull request #249 from pattersonjack/fix-231-addressable-place-…
build(deps): bump github/codeql-action in the all-actions group (NVID…