Skip to content

[SPIR-V] Invalid debug data generated #7744

Open

Description

@gjaegy

Description
Recent version of the compiler produces nonsense debug data, making the NV driver crash when trying to debug a shader with RenderDoc.

Steps to Reproduce

Explanation from Baldur Karlsson (@baldurk) who had a look at the crash and was nice enough to take the time to analyze the issue:

In the attached SPIR-V which is the shader you submitted originally in your first report that you reported from v1.37, taking this struct for example:

struct imSHybridDepthRepartion
{
float2 m_vNearFarZ;
uint m_nGridDimZ;
float2 m_vSplitSchemeData;
float m_fDepthBias;
float m_fFarOverNear;

// member functions
};
these are the declarations of it:
%57 = OpString "imSHybridDepthRepartion"
%63 = OpString "this"

; source
%1520 = OpExtInst %void %2 DebugSource %45

; uint
%1507 = OpExtInst %void %2 DebugTypeBasic %40 %uint_32 %uint_6 %uint_0
; float
%1521 = OpExtInst %void %2 DebugTypeBasic %46 %uint_32 %uint_3 %uint_0
; float2
%1522 = OpExtInst %void %2 DebugTypeVector %1521 %uint_2

; m_vNearFarZ
%1524 = OpExtInst %void %2 DebugTypeMember %47 %1522 %1520 %uint_371 %uint_9 %uint_0 %uint_64 %uint_3
; m_nGridDimZ
%1528 = OpExtInst %void %2 DebugTypeMember %48 %1507 %1520 %uint_372 %uint_7 %uint_64 %uint_32 %uint_3
; m_vSplitSchemeData
%1531 = OpExtInst %void %2 DebugTypeMember %49 %1522 %1520 %uint_373 %uint_9 %uint_96 %uint_64 %uint_3
; m_fDepthBias
%1534 = OpExtInst %void %2 DebugTypeMember %50 %1521 %1520 %uint_374 %uint_8 %uint_160 %uint_32 %uint_3
; m_fFarOverNear
%1538 = OpExtInst %void %2 DebugTypeMember %51 %1521 %1520 %uint_375 %uint_8 %uint_192 %uint_32 %uint_3

; imSHybridDepthRepartion with members as above (%1543 on are functions)
%1542 = OpExtInstWithForwardRefsKHR %void %2 DebugTypeComposite %57 %uint_1 %1520 %uint_369 %uint_8 %1516 %57 %uint_224 %uint_3 %1524 %1528 %1531 %1534 %1538 %1543 %1547 %1551 %1564 %1555 %1559

; instance of type imSHybridDepthRepartion, 'this' variable
%1577 = OpExtInst %void %2 DebugLocalVariable %63 %1542 %1520 %uint_440 %uint_2 %1564 %uint_288 %uint_1
And here are a couple of examples of garbage debug values:
; empty debug expression
%1472 = OpExtInst %void %2 DebugExpression

; access chain 1 identifies m_nGridDimZ, impossible to index into
%14870 = OpExtInst %void %2 DebugValue %1577 %12323 %1472 %int_1 %int_0 %int_0

; similarly impossible access chain
%14882 = OpExtInst %void %2 DebugValue %1577 %12043 %1472 %int_1 %int_0 %int_1 %int_0 %int_0

Actual Behavior
RenderDoc crash because of a crash in NV drivers (out-of-bound access)

Environment

  • DXC version #50764bac3d4048144e9ada5f5a742c82cc97cc9a (31/07/2025)
  • Host Operating System Windows 11

Activity

  1. added
    bug
    spirv
    needs-triage
    on Sep 9, 2025
  2. gjaegy commented on Oct 6, 2025

    @gjaegy
    Author

    Hi, is there any update on that one ?

    Thanks a lot for your help !

  3. s-perron commented on Oct 10, 2025

    @s-perron
    Collaborator

    Steve Urquhart (@SteveUrquhart) FYI. Do you know anything about this? I have not looked at it yet.

  4. SteveUrquhart commented on Oct 10, 2025

    @SteveUrquhart
    Contributor

    I did just fix a bug that could lead to this. It's a one-liner in ProcessDebugFunction():
    ` source/opt/aggressive_dead_code_elim_pass.cpp | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)

    diff --git a/source/opt/aggressive_dead_code_elim_pass.cpp b/source/opt/aggressive_dead_code_elim_pass.cpp
    index fbc39629..f7a19ef3 100644
    --- a/source/opt/aggressive_dead_code_elim_pass.cpp
    +++ b/source/opt/aggressive_dead_code_elim_pass.cpp
    @@ -304,7 +304,7 @@ void AggressiveDCEPass::ProcessDebugInformation(
    NonSemanticShaderDebugInfo100DebugValue) {
    uint32_t id = inst->GetSingleWordInOperand(kDebugValueValue);
    auto def = get_def_use_mgr()->GetDef(id);

    •    if (!live_insts_.Set(def->unique_id())) {
      
    •    if (!IsLive(def)) {
           AddToWorklist(inst);
           uint32_t undef_id = Type2Undef(def->type_id());
           inst->SetInOperand(kDebugValueValue, {undef_id});
      

    `
    Without this change, the routine is accidentally adding dead DebugValue Value targets to the live_insts_

    My PR for this is in my approval queue here, but since it's a one-liner...

  5. gjaegy commented on Oct 13, 2025

    @gjaegy
    Author

    Good news, thanks for answering !

    So, to make sure I got it correctly, I could simply apply a patch on my side in that SPIRV-Tools source file, replacing the line
    if (!live_insts_.Set(def->unique_id())) {
    with
    if (!IsLive(def)) {

    And that should fix the issue, is that correct ?

  6. SteveUrquhart commented on Oct 13, 2025

    @SteveUrquhart
    Contributor

    That's correct. In your directxshadercompiler source tree, you'll find the file in external/SPIRV-Tools/source/opt. Patch it as you've shown, and then build a Release build of dxc.

    If you find this doesn't fix the issue, please let me know.

  7. gjaegy commented on Oct 14, 2025

    @gjaegy
    Author

    So I've fetched the latest head of the project, applied the patch above to "aggressive_dead_code_elim_pass.cpp" and recompiled DXT.

    I had some compilation errors in the unit tests, but it seems the .dll were successfully compiled so I gave it a try. I launched RenderDoc and took a capture of our software, but I am still getting a crash as soon as I try to debug some CS in RenderDoc.

    So I'm afraid this fix doesn't really help :/

    To ensure the unit test compilation error wasn't the issue, I tried to pull the latest official release of DXC (july), but the "aggressive_dead_code_elim_pass.cpp" code is different and I couldn't apply the patch as the same original line doesn't exist in that revision). For information we were using a version that's even older than the July release.

  8. gjaegy commented on Oct 14, 2025

    @gjaegy
    Author

    Baldur Karlsson (Renderdoc) had a look at the dump file sent to him after that RenderDoc crash, below his answer (hopefully that can help):

    This crashes on a real RenderDoc bug caused by undefined pointers, but on the latest nightly where that is fixed this still contains invalid DebugValues. E.g.
    %6099 = OpExtInst %void %2 DebugValue %2139 %5870 %5293 %int_5 %int_1 %int_0 %int_0
    where
    %2139 = OpExtInst %void %2 DebugLocalVariable %63 %2057 %1822 %uint_241 %uint_2 %2090 %uint_288 %uint_1
    %2057 = OpExtInstWithForwardRefsKHR %void %2 DebugTypeComposite %333 %uint_1 %1822 %uint_127 %uint_8 %1507 %333 %uint_1888 %uint_3 %2043 %2045 %2047 %2049 %2051 %2053 %2058 %2062 %2065 %2067 %2069 %2071 %2094 %2075 %2079 %2083 %2086 %2090
    %333 = OpString "imSFroxelAtmosphereData"

    so %6099 is the 'this' pointer of type 'imSFroxelAtmosphereData':

    struct imSFroxelData
    {
    float2 m_vFroxelXY;
    imSFroxelsDepthRepartion m_oRepartition;
    // ...
    };

    struct imSFroxelAtmosphereData
    {
    float m_fXiRatio;
    float m_fViewSpaceZ;
    float m_fCosViewRayInv; // 1.0 / cos(alpha) with cos(alpha) = dot(viewray, Z)
    float3 m_vTransmittance;
    float m_fViewSpaceZRequested;
    imSFroxelData m_oFroxelData;
    // ...
    };
    so a DebugValue of '5 1' would select this->m_oFroxelData->m_vFroxelXY which is what should be selected in this case as %5870 is a float2 pointer. an extra 0 would select the x component which is wrong but in bounds, but a further 0 is wrong.
    Baldur

  9. SteveUrquhart commented on Oct 14, 2025

    @SteveUrquhart
    Contributor

    These malformed DebugValue instructions are coming from a pass subsequent to ADCE. I'm not yet sure which pass, but I do agree with Baldur they are malformed.

    Steven Perron (@s-perron) , please assign this bug to me.

  10. gjaegy commented on Oct 15, 2025

    @gjaegy
    Author

    Great, thanks a lot for your help!

  11. SteveUrquhart commented on Oct 18, 2025

    @SteveUrquhart
    Contributor

    I'm not able to reproduce this problem. I'm not sure if the one liner we discussed above was applied before Baldur's comment regarding imSFroxelAtmosphereData. Or if I'm not building the shader correctly. I extracted the source string from the spv in broken_debugvalue.zip. I used the following command line to compile: dxc -E maincomp -T cs_6_6 -fspv-debug=vulkan -fvk-use-gl-layout -spirv -fspv-print-all -Qembed_debug broken_debugvalue.hlsl

    I had to comment out the automatically generated // Bindless resource fetching code to get it to compile. so something seems wrong right away.

    When I review imSFroxelAtmosphereData, it's not broken:
    %258 = OpString "imSFroxelAtmosphereData"
    %1851 = OpExtInstWithForwardRefsKHR %void %2 DebugTypeComposite %258 %uint_1 %1581 %uint_301 %uint_8 %1356 %258 %uint_256 %uint_3 %1838 %1840 %1842 %1844 %1846 %1848 %1852 %1856 %1898 %1859 %1863 %1866 %1869 %1872 %1876 %1879 %1881 %1884 %1888 %1892 %1895
    %1902 = OpExtInst %void %2 DebugLocalVariable %57 %1851 %1581 %uint_341 %uint_2 %1898 %uint_288 %uint_1
    %6694 = OpExtInst %void %2 DebugValue %1902 %6268 %5564 %int_1
    %14590 = OpExtInst %void %2 DebugValue %1902 %6205 %1316 %int_0
    %15702 = OpExtInst %void %2 DebugValue %1902 %6205 %1316 %int_4
    %37551 = OpExtInst %void %2 DebugValue %1902 %6205 %1316 %int_0
    %38663 = OpExtInst %void %2 DebugValue %1902 %6205 %1316 %int_4

    When I dig into a DebugValue with 4 indexes, it also appears correct:
    %38030 = OpExtInst %void %2 DebugValue %1409 %6205 %1316 %int_1 %int_0 %int_1 %int_0 %int_4
    %1409 = OpExtInst %void %2 DebugLocalVariable %57 %1382 %1360 %uint_440 %uint_2 %1404 %uint_288 %uint_1
    %1382 = OpExtInstWithForwardRefsKHR %void %2 DebugTypeComposite %51 %uint_1 %1360 %uint_369 %uint_8 %1356 %51 %uint_224 %uint_3 %1364 %1368 %1371 %1374 %1378 %1383 %1387 %1391 %1404 %1395 %1399
    %51 = OpString "imSHybridDepthRepartion"

    I've attached
    broken_debugvalue.new.spvasm.txt

  12. gjaegy commented on Oct 19, 2025

    @gjaegy
    Author
  13. gjaegy commented on Oct 27, 2025

    @gjaegy
    Author

    Hi, sorry for the delay, I'm back to work.

    Actually you should compile with -D imBINDLESS=1, that might explain the different behaviour.

    For reference, the arguments we use to compile the shaders are the following:

    dxc -E maincomp -T cs_6_6 -spirv -fspv-target-env=vulkan1.1 -Wall -Wno-unused-const-variable -D imBINDLESS=1 -fspv-extension=SPV_EXT_descriptor_indexing -fspv-extension=SPV_KHR_non_semantic_info -fspv-debug=vulkan-with-source -fspv-print-all broken_debugvalue.hlsl

    Does that help you to reproduce the problem ?

  14. SteveUrquhart commented on Oct 27, 2025

    @SteveUrquhart
    Contributor

    broken_debugvalue.latest.spvasm.txt is what I get using -D imBINDLESS=1. I don't have RenderDoc, much less a nightly build, so all I can do is desk check the spvasm. I don't see any malformed DebugValues associated with imSFroxelAtmosphereData:

    %273 = OpString "imSFroxelAtmosphereData"
    %2045 = OpExtInstWithForwardRefsKHR %void %2 DebugTypeComposite %273 %uint_1 %1746 %uint_301 %uint_8 %1509 %273 %uint_256 %uint_3 %2032 %2034 %2036 %2038 %2040 %2042 %2046 %2050 %2092 %2053 %2057 %2060 %2063 %2066 %2070 %2073 %2075 %2078 %2082 %2086 %2089
    %2096 = OpExtInst %void %2 DebugLocalVariable %58 %2045 %1746 %uint_341 %uint_2 %2092 %uint_288 %uint_1
    %7998 = OpExtInst %void %2 DebugValue %2096 %7570 %6943 %int_1
    %16071 = OpExtInst %void %2 DebugValue %2096 %7507 %1468 %int_0
    %17195 = OpExtInst %void %2 DebugValue %2096 %7507 %1468 %int_4
    %39140 = OpExtInst %void %2 DebugValue %2096 %7507 %1468 %int_0
    %40264 = OpExtInst %void %2 DebugValue %2096 %7507 %1468 %int_4

    Try the one line fix to the optimizer previously shown, or a dxc build that incorporates this SPIRV-Tools PR: KhronosGroup/SPIRV-Tools#6368

  15. gjaegy commented on Oct 28, 2025

    @gjaegy
    Author

    You are correct, I tried again this morning:

    • I fetched "715dacc6654fb3d67dda3ecfa3f3891846a10405"
    • I modified "aggressive_dead_code_elim_pass.cpp"
    • I recompiled DXC

    RenderDoc didn't crash this time, so I assume the code produced is correct. I have no idea why the same previous investigation led to other conclusion (likely human error LOL).
    [EDIT: actually my first try might have led to different result because of that commit that wasn't there two weeks ago https://github.com/microsoft/DirectXShaderCompiler/commit/3056ae2ab55a3891d1e214ae33bb3aa6bf2a62dc]

  16. gjaegy commented on Oct 29, 2025

    @gjaegy
    Author

    BTW I'm not sure the mentioned PR has been merged in the SPIRV tools main branch ?

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

Metadata

Metadata

Labels

bugneeds-triagespirv

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions