Skip to content

Stale pointer issue in SROAGlobalAndAllocas leading to indeterministic debug info #5196

Closed

Description

Functional impact

The debug info (/Zi) output of compiling certain shaders is not deterministic (i.e. given identical inputs, the outputs are not always identical). Our content build pipeline relies on determinism for various things (caching etc.) so this is a pretty big problem for us. Having debugged the issue it is clear that certain processing is erroneously skipped for certain global variables, so this may be the source of correctness issues as well, but I have not been able to confirm that.

Analysis

I have debugged this issue at length, and I believe it is caused by use of stale pointers inside SROAGlobalAndAllocas.

  • At the start of the function pointers to all of the module's static globals (M.globals()) are added to to the staticGVs set.
  • During the course of the function, globals may be removed from the module and freed (eraseFromParent), leaving stale pointers in staticGVs.
  • The function can also allocate new globals. If it is "unlucky", the allocator will return the exact same pointer as one of the globals that was freed.
  • When it comes to process this new global, it first checks if it's in staticGVs. It should not be, because the set is only filled at the start with the initial set, so should not contain the newly-allocated variable. But if you were "unlucky" it will, since it happens to have the same pointer as one of the stale entries still in the set. When this happens, the new variable is erroneously skipped from further processing.
  • The end result is that this global does not have debug data added for it (like I said above, there may be other effects as well). This affects the final pdb data, and since it depends on the allocator reusing an address, it doesn't happen consistently, hence the indeterminism.

Proposed Fix

The issue can be resolved by removing entries from the staticGVs set before freeing them. From what I can tell this is done in just two places inside the function. So I propose adding a staticGVs.remove(GV); call before the 2 GV->eraseFromParent(); calls inside SROAGlobalAndAllocas.

Repro

  1. Download the attached HLSL file and powershell script.
  2. Remove .txt extension (github didn't like hlsl/ps1 files).
  3. Update the path to dxc.exe in the script.
  4. Run the script from the same directory as the HLSL file.

Files:
NonDetermisticDebugData.hlsl.txt
Repro.ps1.txt

The script is very basic/unsophisticated, but was useful for me when debugging this. It compiles the shader 100 times, and compares the hash of the pdb to the that from the first run. If it's different it prints out a message.

Note that due to the nature of this bug I found it to be extremely sensitive to seemingly unrelated factors -- even the path of the directory I put the files in seemed to affect it. I found that generally it would be "different" about 5-10% of the time, but sometimes it would be as low as 1-2% or as high as 50%, seemingly for no reason. Your millage may vary :).

I tried this with various version of DXC, including the latest release (v1.7.2212.1) and the current head (2dd8344), and all seemed to demonstrate the same problem.

Activity

  1. added
    bug
    on Jun 30, 2023
  2. moved this from 🆕 New to 👀 In review in HLSL Roadmapon Jun 30, 2023
  3. added a commit that references this issue on Sep 13, 2023
    f80b8d6
  4. moved this from 👀 In review to ✅ Done in HLSL Roadmapon Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions