Repository navigation
Conversation
Signed-off-by: Raul Estrada <[email protected]>
Signed-off-by: Raul Estrada <[email protected]>
|
Thanks for the PR. I'm putting this on hold along with #636. This one is stacked on top of it (the diff includes all of #636), so that conversation gates this PR either way. Notes specific to this one below. What this PR is about: A copyable struct passed to a kernel by value gets forced into local memory just because a read-only method takes #[kernel]
fn k(mesh: Mesh, level: usize, ...) { // mesh arrives by value, in registers
let n = mesh.count(level); // count() takes &self
}
// &self needs an address, so the compiler spills the argument:
// .local .b8 depot[24] <- stack slot created just for the borrow
// st.local x6 <- copy the already-in-registers arg into it
// ld.local <- runtime-indexed read of a small fieldThe struct was already in registers; the borrow alone puts it in memory. #400 saw a 56-byte local frame drop to zero by switching What we'd need before we go further:
Findings from the review:
What I'd recommend instead, if the measurement pans out:
|
|
I repeated the benchmark with device code generated natively for the RTX 5060's Both modules contain: The comparison used:
The workload passes a 56-byte structured-grid descriptor to the kernel by value. The borrowed variant queries runtime-selected fields through an inlined The native The prototype reported four replaced dynamic loads. Across the full measurement matrix: The median baseline borrowed time divided by baseline scalar-control time was To isolate the effect of the additional optimization pipeline, the scalar control was compiled through both revisions. Its median prototype-over-baseline speedup was Native All kernels reported zero stack frame and zero allocator spill loads/stores. The local-memory operations in the baseline PTX come from explicit aggregate materialization rather than register-allocation spills. The same benchmark previously produced a This provides a measurable benefit for the optimization opportunity. I will not extend the textual rewriter further; the next implementation step is a focused typed transformation in |
|
Closing the prototype now that the question it existed to answer is answered:
And again, thanks for this :) |
Related to #400
Summary
This PR extends the post-inline LLVM IR scalarizer to promote small runtime-indexed fields borrowed from by-value
Copyaggregates.It fixes the retained-local-memory pattern described in #400, where an aggregate is materialized in an
allocaonly because an inlined read-only method takes&self.Dependency: This PR is stacked on #636. Please review and merge #636 first.
The diff against
maincurrently includes the base PR and should collapse after #636 lands.Implementation
The scalarizer now recognizes eligible aggregate allocas that are:
It supports two initialization forms:
default<O2>pipeline.For scalarized aggregate initialization, the pass reconstructs contiguous scalar fields from their stored byte offsets.
Dynamic field loads are rewritten into:
selectchain.A subsequent
default<O2>pass removes the dead dynamic pointer arithmetic and allows SROA to eliminate the aggregate allocation.Safety constraints
The aggregate transformation is intentionally conservative.
It rejects:
The current range proof recognizes constant-divisor
uremexpressions and requires the resulting upper bound to fit within the discovered field length.The
small-array-iteratorsandcopy-aggregate-borrowsmodes are kept independent so enabling one transformation does not implicitly enable the other.Regression coverage
New unit coverage includes:
A new
copy_aggregate_borrowGPU example verifies:.local,ld.local, andst.localin the kernel PTX.Validation
Results:
GPU regression for #400:
Regression for the existing small-array iterator path: