Skip to content

Fix dropped alignment operand in byteAddressBufferLoad specialization - #2

Open
stramit wants to merge 1 commit into
masterfrom
fix/byteaddressbufferload-specialize-alignment
Open

Fix dropped alignment operand in byteAddressBufferLoad specialization#2
stramit wants to merge 1 commit into
masterfrom
fix/byteaddressbufferload-specialize-alignment

Conversation

@stramit

@stramit stramit commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Summary

specializeFuncsForBufferLoadArgs lumped kIROp_ByteAddressBufferLoad in with the generic two-operand "element access" instructions (GetElement, StructuredBufferLoad, etc.) in both gatherCallInfoForArg and getSpecializedValueForArg. byteAddressBufferLoad actually has three operands — (buffer, offset, alignment) — so the rewritten load inside the specialized callee was constructed with only two operands.

Downstream, legalizeByteAddressBufferOps::processLoad unconditionally reads operand index 2 to recover the alignment, hitting:

assert failure: slang-ir.h(710): index < getOperandCount()

during SPIR-V emission.

Repro

A struct large enough to trip the buffer-load specialization threshold, loaded via Buf.Load<T>() and passed by value into a helper:

struct Frame
{
    float4x4 a;
    float4x4 b;
    float3 v;
    float pad;
};

float3 makeRay(Frame f)
{
    float4 w = mul(float4(0, 0, 0, 1), f.b);
    return w.xyz + f.v;
}

uniform ByteAddressBuffer buf;
uniform RWTexture2D<float4> outTexture;
uniform uint width;
uniform uint height;

[shader("compute")]
[numthreads(8, 8, 1)]
void computeMain(uint3 dtid : SV_DispatchThreadID)
{
    if (dtid.x >= width || dtid.y >= height) return;
    Frame f = buf.Load<Frame>(0);
    outTexture[dtid.xy] = float4(makeRay(f), 1);
}

slangc repro.slang -target spirv -O0 -fvk-use-scalar-layout aborts with the assertion above.

Fix

Give byteAddressBufferLoad dedicated handling in both functions so the alignment operand survives specialization, and remove it from isElementAccessInst since it doesn't share the two-operand shape.

Test plan

  • New regression test tests/optimization/buffer-load-specialize-byte-address.slang exercises the failing pattern on SPIR-V and checks that the specialized helper takes the offset and alignment as separate uint parameters.
  • No existing tests change.

Generated by Claude Code

@stramit
stramit force-pushed the fix/byteaddressbufferload-specialize-alignment branch 7 times, most recently from b6ccba0 to 2917f9e Compare August 21, 2026 09:10
specializeFuncsForBufferLoadArgs lumped kIROp_ByteAddressBufferLoad in
with the generic two-operand "element access" instructions (GetElement,
StructuredBufferLoad, etc.) in both getCallInfoForArg and
getSpecializedValueForArg. byteAddressBufferLoad actually has three
operands -- (buffer, offset, alignment) -- so the rewritten load inside
the specialized callee was constructed with only two operands, and
legalizeByteAddressBufferOps::processLoad hit
    assert failure: slang-ir.h(710): index < getOperandCount()
during SPIR-V emission on a minimal reproducer (a struct large enough
to trip the buffer-load specialization threshold loaded via
Buf.Load<T>() and passed by value into a helper).

Give byteAddressBufferLoad dedicated handling in both functions:
 - Recurse on the buffer.
 - Route the runtime offset through as a specialized parameter, matching
   the element-access path (including the NonUniformResourceIndex
   attribute for parity).
 - Bake the alignment into the specialization key, matching the
   field-access path's handling of structKey. alignment is declared
   constexpr in hlsl.meta.slang and byte-address legalization asserts
   as<IRIntLit>(alignment); reusing the original literal preserves that
   invariant so downstream passes see the constant they require.

Add a regression test under tests/optimization that exercises the
failing pattern on SPIR-V and checks the specialized helper takes only
the offset as a runtime uint parameter.
@stramit
stramit force-pushed the fix/byteaddressbufferload-specialize-alignment branch from 2917f9e to 7569d9f Compare August 21, 2026 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant