Skip to content

Reading a lane of an over-aligned aggregate drops its abi_align, costing 2x on cache-resident lookups #745

Closed

Description

@vyncint

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

  1. added
    perf
    IR-lowering
    codegen
    on Aug 10, 2026
  2. vyncint commented on Aug 10, 2026

    @vyncint
    ContributorAuthor

    Fixed by #746, merged as a362eccd.

    convert_field_addr now stamps what a field address proves about its own
    alignment, and convert_load reads it back, so an over-aligned aggregate's
    abi_align survives the projection:

    element before after
    repr(C) — align 4 2x ld.global.b32 2x ld.global.b32 (unchanged, correct)
    repr(C, align(8)) 2x ld.global.b32 1x ld.global.v2.b32

    Measured 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 #745 did not fire on the squash merge.

  3. vyncint commented on Aug 10, 2026

    @vyncint
    ContributorAuthor

    Reopening — I closed this prematurely. The merge review on #746 scopes it correctly:

    This PR fixes the first one, reading named fields (c.x + c.y). The second
    one wraps the values in an array (c.0[0] + c.0[1]), and indexing into an
    array goes through a different address-computation path that doesn't yet
    record the alignment, so those reads still come out as two narrow loads
    instead of one wide one. That makes this a partial fix for #745; doing the
    same trick for array elements in a follow-up would finish it.

    That matches what I measured while characterizing this: the Chunk2([f32; 2])
    probe kernel (c.0[0] + c.0[1]) stayed at 2x ld.global.b32 even with #746's
    change 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_addr now does for struct/tuple fields — for a
    constant index i into [T; N] at element size s, the element address is
    aligned to gcd(abi_align, i*s); for a runtime index, to
    gcd(abi_align, s) (every element stride preserves that much). I'll take this
    as a follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    IR-loweringcodegenperf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions