Repository navigation
Description
The gap
A whole-element copy of an over-aligned struct vectorizes. Reading its fields
does not — same type, same alignment, twice the loads.
#[repr(C, align(8))]
struct AlignedPoint { x: f32, y: f32 }
// 2x ld.global.b32
let p = pts[i];
*o = p.x + p.y;The vectorization example's f32x2 stays wide only because it does
output[i] = input[i] and never touches a lane. Spelling the same read
field-wise — or even as c.0[0] + c.0[1] through an array newtype — splits it
and loses the guarantee.
Root cause
convert_load takes the load's alignment from its own result type:
if let Some(align) = mir_type_abi_align(ctx, result_ty) {
set_op_alignment(ctx, llvm_load.get_operation(), align as u32);
}mir_type_abi_align records abi_align for aggregates and answers None for
scalars. So a field load exports with LLVM's default align 4, and the
aggregate's align(8) — which rustc did record — is dropped at the projection.
LoadStoreVectorizer will not widen past an alignment it cannot prove, so the
pair never fuses.
The fix works
Patching one attribute into the emitted IR, then running the project's own
opt -O3 + llc -march=nvptx64 -mcpu=sm_86:
| first field load | resulting PTX |
|---|---|
align 4 (today) |
2x ld.global.b32 |
align 8 |
1x ld.global.v2.b32 |
The aggregate is aligned to abi_align, so its field at byte offset is
aligned to gcd(abi_align, offset) — and to abi_align itself at offset zero.
Both numbers come from rustc's layout, so nothing would be claimed that the
source did not already guarantee.
What it is worth, measured
A10G (sm_86, driver 595.71.05), 4194304 threads, 30 timed launches after 5
warmup, every kernel bit-correct against a host reference. one_wide_load reads
the identical 8 bytes in a single load and splits in registers — the instruction
sequence the fix would produce:
| kernel | global loads | us/launch | GB/s |
|---|---|---|---|
struct_two_fields (streaming) |
2 | 104.7 | 480.5 |
one_wide_load (streaming ceiling) |
1 | 104.8 | 480.4 |
hot_two_fields (cache-resident) |
4 | 1253.7 | 40.1 |
hot_one_wide (cache-resident ceiling) |
2 | 628.5 | 80.1 |
Streaming: worth nothing (1.00x). At 480 GB/s the device is bandwidth-bound,
and two ld.global.b32 from consecutive addresses already coalesce into the same
128-byte transactions as one wide load. Halving the instruction count buys
nothing, and it would be wrong to claim otherwise.
Cache-resident: worth 1.99x. With a 4096-entry table and 64 dependent
lookups per thread, bandwidth is not the limit and load count is — the speedup
tracks the 2:1 load ratio almost exactly.
That second regime is the lookup-table shape #684 and #694 already optimised, so
it is not a hypothetical access pattern for this project.
Implementation note
The obvious place does not have the information. By the time convert_load
runs, mir.field_addr has already been rewritten to a GEP, and
operands_info.lookup_most_recent_of_type::<MirPtrType> does not resolve the
GEP's base — it is not an operand of the load, so its MIR type was never
recorded there. I verified each step by forcing a distinctive alignment through
it: the GEP matches and the index destructures, and the lookup is what returns
None.
So the aggregate type has to be carried forward from convert_field_addr, where
it is available, to the loads that consume the address — the shape
DynamicSmemAlignmentMap already uses in crate::context. That means widening
the conversion dispatch signatures, which is why I am filing this rather than
attaching a patch. Happy to implement it if the approach looks right.
Activity
perf(mir-lower): keep an over-aligned aggregate's alignment on its fi…
vyncint commentedon Aug 10, 2026
Fixed by #746, merged as
a362eccd.convert_field_addrnow stamps what a field address proves about its ownalignment, and
convert_loadreads it back, so an over-aligned aggregate'sabi_alignsurvives the projection:repr(C)— align 4ld.global.b32ld.global.b32(unchanged, correct)repr(C, align(8))ld.global.b32ld.global.v2.b32Measured on the A10G: the cache-resident lookup went 1253.9us -> 628.8us
(1.99x), with the align-4 control holding at ~1254us across both builds.
Streaming access did not move, as expected — it is bandwidth-bound.
Closing manually: the PR body's
Closes #745did not fire on the squash merge.vyncint commentedon Aug 10, 2026
Reopening — I closed this prematurely. The merge review on #746 scopes it correctly:
That matches what I measured while characterizing this: the
Chunk2([f32; 2])probe kernel (
c.0[0] + c.0[1]) stayed at 2xld.global.b32even with #746'schange in place, because the array-index address path never stamps what it
proves.
Remaining work: stamp the provable alignment on the array-element address
path the way
convert_field_addrnow does for struct/tuple fields — for aconstant index
iinto[T; N]at element sizes, the element address isaligned to
gcd(abi_align, i*s); for a runtime index, togcd(abi_align, s)(every element stride preserves that much). I'll take thisas a follow-up.
perf(mir-lower): carry proved alignment through an array-element addr…