Skip to content

Commit 737f1de

Browse files
authored
Fix non-deterministic DXIL/PDB output for resource arrays (microsoft#8175)
DxilMutateResourceToHandle iterates a DenseSet<Value *> to process ConstantExpr GEPs on resource array globals. DenseSet iteration order depends on pointer hash values, which vary across runs due to ASLR. For each GEP, mutateCandidates() destroys the old ConstantExpr and creates a new one, prepending it to the global's use list. The non-deterministic processing order produces a non-deterministic use list, which TranslateDxilResourceUses later iterates to create named handles - resulting in different name suffixes across compilations. The fix is to change MutateValSet from DenseSet<Value *> to SetVector<Value *>, which preserves insertion order while maintaining O(1) lookup. Since collectCandidates() traverses the IR in a deterministic order, the insertion order is stable, producing a deterministic use list and consistent handle names. Fixes microsoft#8171 Co-authored-by: Copilot [email protected]
1 parent 720acdf commit 737f1de

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

docs/ReleaseNotes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ line upon naming the release. Refer to previous for appropriate section names.
3737
- `dx::IsDebuggerPresent()` returns true if a debugger is attached.
3838
- SPIR-V: `DebugBreak()` emits `NonSemantic.DebugBreak` extended instruction; `IsDebuggerPresent()` is not supported.
3939

40-
#### Other Changes
40+
#### Bug Fixes
41+
42+
- Fixed non-deterministic DXIL/PDB output when compiling shaders with resource arrays, debug info, and SM 6.6+. [#8171](https://github.com/microsoft/DirectXShaderCompiler/issues/8171)
4143
- Fixed mesh shader semantics that were incorrectly case sensitive.
4244

4345
### Version 1.9.2602

lib/HLSL/DxilPromoteResourcePasses.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "dxc/HLSL/DxilGenerationPass.h"
1717
#include "dxc/HLSL/HLModule.h"
1818
#include "llvm/ADT/DenseSet.h"
19+
#include "llvm/ADT/SetVector.h"
1920
#include "llvm/Analysis/AssumptionCache.h"
2021
#include "llvm/IR/Attributes.h"
2122
#include "llvm/IR/DebugInfo.h"
@@ -313,7 +314,7 @@ class DxilMutateResourceToHandle : public ModulePass {
313314
hlsl::OP *hlslOP = nullptr;
314315
Function *createHandleForLibOnHandle = nullptr;
315316
DxilTypeSystem *pTypeSys;
316-
DenseSet<Value *> MutateValSet;
317+
SetVector<Value *> MutateValSet;
317318
DenseMap<Type *, Type *> MutateTypeMap;
318319
};
319320

@@ -613,7 +614,7 @@ void DxilMutateResourceToHandle::collectCandidates(Module &M) {
613614

614615
for (Value *Val : newCandidates) {
615616
// New candidate find.
616-
if (MutateValSet.insert(Val).second) {
617+
if (MutateValSet.insert(Val)) {
617618
WorkList.emplace_back(Val);
618619
}
619620
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Test that compiling a shader with resource arrays produces deterministic output.
2+
// This is a regression test for https://github.com/microsoft/DirectXShaderCompiler/issues/8171
3+
// where compiling the same shader multiple times with -Zi and SM 6.6+ produced
4+
// non-identical disassembly due to non-deterministic handle name suffixes.
5+
6+
// RUN: %dxc -T cs_6_6 -E CSMain -Zi -Fc %t1 %s
7+
// RUN: %dxc -T cs_6_6 -E CSMain -Zi -Fc %t2 %s
8+
// RUN: diff %t1 %t2
9+
10+
Texture2D<float4> inMaps[16];
11+
RWTexture2D<float4> Output;
12+
13+
[numthreads(1,1,1)]
14+
void CSMain(uint3 threadID : SV_DispatchThreadID)
15+
{
16+
Output[threadID.xy] = inMaps[0][threadID.xy];
17+
Output[threadID.xy] = inMaps[1][threadID.xy];
18+
Output[threadID.xy] = inMaps[2][threadID.xy];
19+
Output[threadID.xy] = inMaps[3][threadID.xy];
20+
Output[threadID.xy] = inMaps[4][threadID.xy];
21+
Output[threadID.xy] = inMaps[5][threadID.xy];
22+
Output[threadID.xy] = inMaps[6][threadID.xy];
23+
}

0 commit comments

Comments
 (0)