Skip to content

-Zi: a single-member struct inside an array of structs gets the wrong DW_OP_bit_piece offset #9186

Backlog

Description

Impact

Shader debuggers show wrong or missing values for variables whose type is an array of structs containing a single-member struct, such as a wrapper type. In the repro below:

  • b[1].w.v has no piece covering its bits, so a debugger shows it as unavailable.
  • b[0].tag gets b[1].w.v's value written over it, so it silently shows the wrong number.
  • With three or more elements, each b[i].w.v shows another element's value.
  • At -O3, the correct piece for b[0].tag can be dropped entirely.

This directly affects SM 6.10 linear algebra matrices. dx::linalg::Matrix is a single-member wrapper around its handle, so in struct { Matrix held; uint tag; } holders[2], holders[1].held lands on holders[0].tag. It also affects other single-member wrappers (strongly typed IDs/handles) and one-field base classes.

A nested struct with two or more members, and a plain array of the wrapper type, are both unaffected.

Repro

RWByteAddressBuffer Out;
struct W { uint v; };
struct S { W w; uint tag; };
[numthreads(1,1,1)] void main() {
  S b[2]; b[0].w.v = 10; b[0].tag = 11; b[1].w.v = 20; b[1].tag = 21;
  Out.Store(0, b[0].w.v + b[0].tag + b[1].w.v + b[1].tag);
}

dxc -T cs_6_0 -E main -Zi -Od -Qembed_debug -Fc out.ll repro.hlsl:

10 (b[0].w.v)  DW_OP_bit_piece, 0, 32
11 (b[0].tag)  DW_OP_bit_piece, 32, 32
20 (b[1].w.v)  DW_OP_bit_piece, 32, 32   <- should be 64, 32
21 (b[1].tag)  DW_OP_bit_piece, 96, 32

The debug type is correct (S is 64 bits, tag at offset 32). At -O3, 11's piece disappears and 20 still lands on 32. Pixel, vertex and library targets behave the same.

Cause

In addDebugInfoForElements (lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp), the array stride is only recorded if (NumInstances > 1 && Elems.size() > 1) (line 208); otherwise DIArrayDims.clear() (line 225).

  1. Splitting [2 x S] records a 64-bit stride.
  2. Splitting [2 x W] yields a single element, so that stride is thrown away and [2 x i32] ends up with no dx.dbg.varlayout.
  3. Later splitting (DxilConditionalMem2Reg.cpp:235 at -Od, SROA.cpp at -O3) then uses the element size, 32, as the stride.

A likely fix is to keep the inherited DIArrayDims when Elems.size() == 1. I haven't tested it.

Versions

  • Reproduces on v1.10.2611 (b6a4228f9), 1.10.2605.24 (c1e1fc784) and 1.7.2308.16.
  • ScalarReplAggregatesHLSL.cpp is unchanged on main (4488d1caf).

Related: #2072 added the dx.dbg.varlayout stride metadata; this is a gap in it. #8754 has a similar symptom but for mesh shader output arguments.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions