Skip to content

vectorization: f32x4_view_copy accesses lower to generic space, not .global #657

Closed

Description

@vyncint

Description

The vectorization example inspects its own generated PTX at runtime and asserts that f32x4_view_copy performs 128-bit ld.global/st.global transactions. It does not, and the example fails.

The copy is fused to 128 bits — but in the generic address space rather than global, so the example's ld.global search finds nothing. There is also a local-memory round trip in constructing the view.

Reproducer

cargo oxide run vectorization      # fails the assertion
cargo oxide build vectorization --arch sm_75   # PTX alone is enough to see it

Actual behavior

Runtime report:

f32x4       float4           16    16  ld.global.v2.b64       yes
f64x4       double4          32    16  ld.global.v2.b64       yes
  !! f32x4_view_copy has no 128-bit ld.global vector op
  !! f32x4_view_copy has no 128-bit st.global vector op

✗ FAILED: 2 problem(s)

The emitted memory operations inside f32x4_view_copy:

cvta.global.u64  %rd58, %rd25;
st.local.v2.b32  [%rd34+8], {%r33, %r29};
ld.local.b64     %rd5,  [%rd34+8];
cvta.global.u64  %rd60, %rd40;
st.local.v2.b32  [%rd48+8], {%r55, %r51};
ld.local.b64     %rd49, [%rd48+8];
ld.v2.b64        {%rd54, %rd55}, [%rd53];
st.v2.b64        [%rd56], {%rd54, %rd55};

Compare the direct f32x4 kernel in the same module, which is what the example expects:

ld.global.v2.b64  {%rd21, %rd22}, [%rd20];
st.global.v2.b64  [%rd3], {%rd21, %rd22};

So the width is right and the address space is not. The example's message is literally true but describes the wrong defect — worth fixing in the example's wording whatever the outcome here.

What I ruled out

  • Not architecture-specific. Building for sm_75 and sm_90 produces byte-identical output for this kernel, down to register numbers. (This also corrects an earlier version of this issue, which claimed newer hardware was needed to tell — the assertion reads generated PTX, so a compile-only build settles it with no GPU.)
  • Not a regression from fix(mir-lower): genericize named-space pointer values #626. I suspected the named-space genericization in fix(mir-lower): genericize named-space pointer values, since this kernel observes a pointer's representation. Building at 3f1cdaae^ and at 3f1cdaae gives identical PTX for this kernel, so that change is not the cause.
  • Not .align 1 on the parameters. Every kernel in the module declares .param .u64 .ptr .align 1, including the ones that do emit ld.global.v2.b64, so parameter alignment is not the discriminator.

Likely never passed. The kernel, its 128-bit assertion, and the raw-pointer round trip all arrived together in #512 (2026-07-28). CI compiles examples without executing them, and this check runs at execution time, so it appears never to have been evaluated in CI.

Hypothesis — superseded, see the comment below

I originally suspected output.as_mut_ptr(). That is wrong: the input side never touches as_mut_ptr and is genericized too. Probes since have narrowed the trigger to vector::as_vectors itself, with a minimal reproducer that needs no quad copy at all, and have ruled out from_raw_parts, the address-exposure cast, parameter alignment, and #626. See the comment below for the reproducer and the eliminations.

Question

Should the global window survive a as_mut_ptr() / from_raw_parts round trip when the pointer provably stays in one space, or is the example's expectation simply wrong for a view built this way? The answer decides whether the fix belongs in lowering or in the example, and I would rather not guess at that boundary. Happy to implement either.

Environment

  • GPU: Tesla T4 (sm_75, 15360 MiB); PTX comparison also built for sm_90
  • CUDA driver version: 595.71.05
  • nvcc: Build cuda_13.2.r13.2/compiler.37434383_0
  • Rust toolchain: nightly-2026-04-03 (per rust-toolchain.toml)
  • Source revision: e28248c1

Found while running the full example suite on sm_75; the compute-capability gating problems that surfaced in the same run are filed separately as #656 and are unrelated.

Activity

  1. changed the title [-]vectorization: `f32x4_view_copy` emits no 128-bit vector load/store[/-] [+]vectorization: f32x4_view_copy accesses lower to generic space, not .global[/+] on Aug 4, 2026
  2. vyncint commented on Aug 4, 2026

    @vyncint
    ContributorAuthor

    Narrowed this down with compile-only PTX probes. The trigger is vector::as_vectors itself, not the example's quad copy — which makes this an issue about the API rather than about one kernel.

    Minimal reproducer

    No 16-byte copy, no as_mut_ptr, no from_raw_parts_mut. Reading a single lane through an as_vectors view is enough:

    #[kernel]
    pub fn probe_as_vectors_lane(input: &[f32], mut output: DisjointSlice<f32>) {
        let idx = thread::index_1d();
        let i = idx.get();
        let Some(quads) = vector::as_vectors::<F32x4>(input) else {
            return;
        };
        if i < quads.len()
            && let Some(o) = output.get_mut(idx)
        {
            *o = quads[i].as_slice()[0];
        }
    }

    This kernel carries its own control: the input is read through as_vectors, the output is written through the ordinary get_mut accessor. Same kernel, same launch, --arch sm_75:

    cvta.global.u64  %rd40, %rd24;              <- input pointer: global -> generic
    st.local.v2.b32  [%rd32+8], {%r31, %r27};   <- (ptr, len) spilled to local memory
    ld.local.b64     %rd33, [%rd32+8];          <- reloaded
    ld.b32           %r32, [%rd6];              <- generic load, no .global
    st.global.b32    [%rd7], %r32;              <- output via get_mut: still .global
    

    The view's (ptr, len) pair round-trips through local memory, and the pointer that comes back out has no address-space provenance left — so every subsequent access through the view is generic.

    What this is not

    I tested each of these and they are all innocent:

    Suspect Result
    .param ... .align 1 on pointer parameters Every kernel in the module has it, including ones that emit ld.global.v2.b64. Not the discriminator.
    The genericization in #626 Building at 3f1cdaae^ and 3f1cdaae gives byte-identical PTX for the affected kernel, register numbers included. Not a regression from it.
    core::slice::from_raw_parts A probe rebuilding the slice from its own pointer at the same element type keeps ld.global.b32.
    slice.as_ptr() as usize (the alignment check's address exposure) A probe doing exactly that, and nothing else, keeps ld.global.b32.
    Architecture sm_75 and sm_90 produce byte-identical PTX for this kernel.

    So the remaining difference between the probe that keeps .global and as_vectors, which does not, is narrow: the .cast::<V>() to an over-aligned element type, and/or the Option<&[V]> view not being scalarized away. I did not manage to separate those two, and did not want to guess again after four wrong suspects.

    Why this is worth more than the failing example

    vector::as_vectors documents itself as the path to full-width transactions: "Both are checked once here; every access through the returned slice is then a full-width transaction" (crates/cuda-device/src/vector.rs). If every access through such a view is generic-space and the view spills to local memory first, the API's documented benefit does not materialise, and the vectorization example is simply the only thing that noticed — because it is the only place that inspects the emitted PTX, and CI never executes it.

    The narrow question, if it helps: is the Option<&[V]> expected to be scalarized here, and is the local round trip what destroys the address space, or is the .cast::<V>() alone enough to genericize? Happy to keep digging if you tell me which layer to look in.

    All PTX above is from cargo oxide build, no GPU execution required.

  3. vyncint commented on Aug 5, 2026

    @vyncint
    ContributorAuthor

    Correcting my previous comment: the probe_as_vectors_lane reproducer in it is invalid, and this module's own documentation says so.

    crates/cuda-device/src/vector.rs:28-42 documents, measured on sm_86:

    how the element is used                     LDG          STG
    `*out = input[i]`, never decomposed         LDG.E.128    STG.E.128
    lanes read, whole value assembled+stored    LDG.E x4     STG.E.128
    lanes read, scalar stored                   LDG.E x4     STG.E
    

    Touching individual lanes lets the optimiser split the local apart, and it then loads the lanes separately no matter how the type is aligned.

    My probe read a single lane (quads[i].as_slice()[0]), which is the third row — documented to scalarize. So it demonstrates intended behaviour, not this bug. Please disregard that reproducer. The case that matters is still the original one in f32x4_view_copy, where the quad is never decomposed:

    out_quads[i] = quads[i];

    What still stands from that comment: the eliminations. .align 1 parameters, #626, core::slice::from_raw_parts, and slice.as_ptr() as usize were each tested with probes that keep ld.global, and sm_75/sm_90 remain byte-identical. Those all speak to the address space, which is unaffected by the lane-decomposition caveat above.

    One more negative result since: adding #[inline] to as_vectors and as_vectors_mut changes the emitted PTX not at all — byte-identical, register numbers included. So the st.local/ld.local pair is not a call-return spill; these generics were already being inlined. That kills the return-through-memory explanation too.

    The question I should have asked first

    LDG and STG in the table above are SASS. The example asserts on PTX text, searching for a 128-bit ld.global:

    !! f32x4_view_copy has no 128-bit ld.global vector op
    

    But what it emits is ld.v2.b64 — 128 bits wide, in the generic space. ptxas may well lower that to a perfectly good LDG.E.128, in which case the documented contract is met, the generated code is fine, and the example's assertion is simply checking the wrong thing — the .global qualifier in PTX rather than the transaction width in SASS.

    I could not settle this: the AMI I have carries ptxas, compute-sanitizer, ncu and cuda-gdb, but neither cuobjdump nor nvdisasm, so I have no way to read SASS. For anyone who has them, one command decides whether this issue is a codegen defect or an over-strict assertion:

    ptxas -arch=sm_75 -O3 vectorization.ptx -o v.cubin && cuobjdump -sass v.cubin
    

    If f32x4_view_copy shows LDG.E.128/STG.E.128, this is an example bug and I am happy to send that patch — the message would also want rewording, since "has no 128-bit ld.global vector op" describes the wrong property either way. If it shows four scalar LDG.E, it is a real codegen issue and the generic address space is the lead worth pulling.

    Apologies for the noise in the previous comment.

  4. vyncint commented on Aug 5, 2026

    @vyncint
    ContributorAuthor

    Got SASS. It answers the question I left open two comments ago: this is a codegen pessimization, not an over-strict assertion — though the example's wording is wrong too.

    Needed cuda-cuobjdump-13-2 and cuda-nvdisasm-13-2; the AMI I use ships neither, and cuobjdump fails with "Could not find executable file 'nvdisasm'" if only the first is installed.

    Measured on sm_86

    ptxas -arch=sm_86 -O3, memory operations per kernel:

    f32x4 (direct) f32x4_view_copy (via as_vectors)
    global LDG.E.128 ×1, STG.E.128 ×1 —
    generic — LD.E.128 ×1, ST.E.128 ×1
    local — LDL.64 ×2, STL.64 ×2
    total 2 6

    The view path also carries 37 PRMT instructions, 14 LOP3.LUT, 10 SEL and 8 SHF.R.U64 — byte-level permutes and shifts that reassemble the quad piecewise. The direct kernel has none of that.

    What that means

    The 128-bit width is achieved — ld.v2.b64 does become LD.E.128, so my worry that the transaction was being split was wrong. Two real costs remain:

    1. Generic addressing. LD.E/ST.E rather than LDG.E/STG.E: the access goes through the generic window instead of the global one.
    2. A local-memory round trip. Four LDL.64/STL.64 plus the PRMT chain, none of which the direct path needs — the quad is spilled and reassembled byte-wise.

    So the documented contract in vector.rs:28-42 ("every access through the returned slice is then a full-width transaction", LDG.E.128/STG.E.128 measured on sm_86) does not hold for a view taken with as_vectors, and the gap is 2 memory operations versus 6.

    Two separate fixes

    The example's message is inaccurate and should be reworded whatever happens to the codegen. "has no 128-bit ld.global vector op" describes only the address space; the 128-bit op exists. Something like "performs its 128-bit access through the generic window, plus a local round trip" would describe what is actually there. I am happy to send that patch on its own — it is a strict improvement even if the codegen stays as it is.

    The codegen question is now narrow. Everything I ruled out earlier still holds (.align 1 parameters, #626, from_raw_parts alone, as_ptr() as usize alone, #[inline] on the constructors, and any architecture dependence — sm_75/sm_86/sm_90 all agree). What remains is the .cast::<V>() to an over-aligned element type and the Option<&[V]> view that is not scalarized away, which is where the local traffic comes from.

    If you can say which layer should own that — keeping the global window across the cast, or scalarizing the Option so the slice never reaches memory — I will implement it. I would rather not pick that boundary myself; it is the same layering call that closed #636 and #638.

  5. added
    bug
    perf
    IR-lowering
    on Aug 6, 2026
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-loweringbugperf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions