Skip to content

Commit 6252a27

Browse files
committed
[HLSL] Diagnose overlapping resource bindings
1 parent 74ad4ba commit 6252a27

File tree

6 files changed

+136
-12
lines changed

6 files changed

+136
-12
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ class ResourceInfo {
358358
return std::tie(RecordID, Space, LowerBound, Size) <
359359
std::tie(RHS.RecordID, RHS.Space, RHS.LowerBound, RHS.Size);
360360
}
361+
bool overlapsWith(const ResourceBinding &RHS) const {
362+
return Space == RHS.Space && LowerBound + Size - 1 >= RHS.LowerBound;
363+
}
361364
};
362365

363366
private:
@@ -394,8 +397,8 @@ class ResourceInfo {
394397
getAnnotateProps(Module &M, dxil::ResourceTypeInfo &RTI) const;
395398

396399
bool operator==(const ResourceInfo &RHS) const {
397-
return std::tie(Binding, HandleTy, Symbol) ==
398-
std::tie(RHS.Binding, RHS.HandleTy, RHS.Symbol);
400+
return std::tie(Binding, HandleTy, Symbol, Name) ==
401+
std::tie(RHS.Binding, RHS.HandleTy, RHS.Symbol, RHS.Name);
399402
}
400403
bool operator!=(const ResourceInfo &RHS) const { return !(*this == RHS); }
401404
bool operator<(const ResourceInfo &RHS) const {

llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "DXILPostOptimizationValidation.h"
1010
#include "DXILShaderFlags.h"
1111
#include "DirectX.h"
12+
#include "llvm/ADT/SmallString.h"
1213
#include "llvm/Analysis/DXILMetadataAnalysis.h"
1314
#include "llvm/Analysis/DXILResource.h"
1415
#include "llvm/IR/DiagnosticInfo.h"
@@ -50,15 +51,55 @@ static void reportInvalidDirection(Module &M, DXILResourceMap &DRM) {
5051
}
5152
}
5253

53-
} // namespace
54+
static void reportOverlappingError(Module &M, ResourceInfo R1,
55+
ResourceInfo R2) {
56+
SmallString<64> Message;
57+
raw_svector_ostream OS(Message);
58+
OS << "resource " << R1.getName() << " at register "
59+
<< R1.getBinding().LowerBound << " overlaps with resource " << R2.getName()
60+
<< " at register " << R2.getBinding().LowerBound << ", space "
61+
<< R2.getBinding().Space;
62+
M.getContext().diagnose(DiagnosticInfoGeneric(Message));
63+
}
5464

55-
PreservedAnalyses
56-
DXILPostOptimizationValidation::run(Module &M, ModuleAnalysisManager &MAM) {
57-
DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);
65+
static void reportOverlappingBinding(Module &M, DXILResourceMap &DRM) {
66+
if (DRM.empty())
67+
return;
5868

69+
for (auto ResList :
70+
{DRM.srvs(), DRM.uavs(), DRM.cbuffers(), DRM.samplers()}) {
71+
if (ResList.empty())
72+
continue;
73+
const ResourceInfo *PrevRI = &*ResList.begin();
74+
for (auto *I = ResList.begin() + 1; I != ResList.end(); ++I) {
75+
const ResourceInfo *RI = &*I;
76+
if (PrevRI->getBinding().overlapsWith(RI->getBinding())) {
77+
reportOverlappingError(M, *PrevRI, *RI);
78+
continue;
79+
}
80+
PrevRI = RI;
81+
}
82+
}
83+
}
84+
85+
static void reportErrors(Module &M, DXILResourceMap &DRM,
86+
DXILResourceBindingInfo &DRBI) {
5987
if (DRM.hasInvalidCounterDirection())
6088
reportInvalidDirection(M, DRM);
6189

90+
if (DRBI.hasOverlappingBinding())
91+
reportOverlappingBinding(M, DRM);
92+
93+
assert(!DRBI.hasImplicitBinding() && "implicit bindings should be handled in "
94+
"DXILResourceImplicitBinding pass");
95+
}
96+
} // namespace
97+
98+
PreservedAnalyses
99+
DXILPostOptimizationValidation::run(Module &M, ModuleAnalysisManager &MAM) {
100+
DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);
101+
DXILResourceBindingInfo &DRBI = MAM.getResult<DXILResourceBindingAnalysis>(M);
102+
reportErrors(M, DRM, DRBI);
62103
return PreservedAnalyses::all();
63104
}
64105

@@ -68,10 +109,9 @@ class DXILPostOptimizationValidationLegacy : public ModulePass {
68109
bool runOnModule(Module &M) override {
69110
DXILResourceMap &DRM =
70111
getAnalysis<DXILResourceWrapperPass>().getResourceMap();
71-
72-
if (DRM.hasInvalidCounterDirection())
73-
reportInvalidDirection(M, DRM);
74-
112+
DXILResourceBindingInfo &DRBI =
113+
getAnalysis<DXILResourceBindingWrapperPass>().getBindingInfo();
114+
reportErrors(M, DRM, DRBI);
75115
return false;
76116
}
77117
StringRef getPassName() const override {
@@ -82,7 +122,9 @@ class DXILPostOptimizationValidationLegacy : public ModulePass {
82122
static char ID; // Pass identification.
83123
void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
84124
AU.addRequired<DXILResourceWrapperPass>();
125+
AU.addRequired<DXILResourceBindingWrapperPass>();
85126
AU.addPreserved<DXILResourceWrapperPass>();
127+
AU.addPreserved<DXILResourceBindingWrapperPass>();
86128
AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
87129
AU.addPreserved<ShaderFlagsAnalysisWrapper>();
88130
}
@@ -92,6 +134,7 @@ char DXILPostOptimizationValidationLegacy::ID = 0;
92134

93135
INITIALIZE_PASS_BEGIN(DXILPostOptimizationValidationLegacy, DEBUG_TYPE,
94136
"DXIL Post Optimization Validation", false, false)
137+
INITIALIZE_PASS_DEPENDENCY(DXILResourceBindingWrapperPass)
95138
INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
96139
INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass)
97140
INITIALIZE_PASS_END(DXILPostOptimizationValidationLegacy, DEBUG_TYPE,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.3-library %s 2>&1 | FileCheck %s
2+
3+
; Check overlap error for two resource arrays.
4+
5+
; CHECK: error: resource A at register 0 overlaps with resource B at register 5, space 0
6+
7+
@A.str = private unnamed_addr constant [2 x i8] c"A\00", align 1
8+
@B.str = private unnamed_addr constant [2 x i8] c"B\00", align 1
9+
10+
define void @test_overlapping() {
11+
entry:
12+
; RWBuffer<float> A[10] : register(u0);
13+
; RWBuffer<float> B[10] : register(u5);
14+
%h1 = call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 10, i32 4, i1 false, ptr @A.str)
15+
%h2 = call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 5, i32 10, i32 4, i1 false, ptr @B.str)
16+
ret void
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.3-library %s 2>&1 | FileCheck %s
2+
3+
; Check overlap error for two resources with identical binding
4+
5+
; CHECK: error: resource R at register 5 overlaps with resource S at register 5, space 10
6+
7+
@R.str = private unnamed_addr constant [2 x i8] c"R\00", align 1
8+
@S.str = private unnamed_addr constant [2 x i8] c"S\00", align 1
9+
10+
define void @test_overlapping() {
11+
entry:
12+
; RWBuffer<float> R : register(u5, space10);
13+
; RWBuffer<float> S : register(u5, space10);
14+
%h1 = call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding(i32 10, i32 5, i32 1, i32 0, i1 false, ptr @R.str)
15+
%h2 = call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding(i32 10, i32 5, i32 1, i32 0, i1 false, ptr @S.str)
16+
ret void
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; Use llc for this test so that we don't abort after the first error.
2+
; RUN: not llc %s -o /dev/null 2>&1 | FileCheck %s
3+
4+
; Check multiple overlap errors.
5+
; Also check different resource class with same binding values is ok (no error expected).
6+
7+
target triple = "dxil-pc-shadermodel6.3-library"
8+
9+
; CHECK: error: resource C at register 0 overlaps with resource A at register 5, space 0
10+
; CHECK: error: resource C at register 0 overlaps with resource B at register 9, space 0
11+
12+
@A.str = private unnamed_addr constant [2 x i8] c"A\00", align 1
13+
@B.str = private unnamed_addr constant [2 x i8] c"B\00", align 1
14+
@C.str = private unnamed_addr constant [2 x i8] c"C\00", align 1
15+
@S.str = private unnamed_addr constant [2 x i8] c"S\00", align 1
16+
17+
; Fake globals to store handles in; this is to make sure the handlefrombinding calls
18+
; are not optimized away by llc.
19+
@One = internal global { target("dx.RawBuffer", float, 0, 0) } poison, align 4
20+
@Two = internal global { target("dx.RawBuffer", float, 0, 0) } poison, align 4
21+
@Three = internal global { target("dx.RawBuffer", float, 0, 0) } poison, align 4
22+
@Four = internal global { target("dx.TypedBuffer", float, 1, 0, 0) } poison, align 4
23+
24+
define void @test_overlapping() "hlsl.export" {
25+
entry:
26+
; StructuredBuffer<float> A : register(t5);
27+
; StructuredBuffer<float> B : register(t9);
28+
; StructuredBuffer<float> C[10] : register(t0);
29+
; RWBuffer<float> S[10] : register(u0);
30+
31+
%h1 = call target("dx.RawBuffer", float, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 5, i32 1, i32 0, i1 false, ptr @A.str)
32+
store target("dx.RawBuffer", float, 0, 0) %h1, ptr @One, align 4
33+
34+
%h2 = call target("dx.RawBuffer", float, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 9, i32 1, i32 0, i1 false, ptr @B.str)
35+
store target("dx.RawBuffer", float, 0, 0) %h2, ptr @Two, align 4
36+
37+
%h3 = call target("dx.RawBuffer", float, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 10, i32 4, i1 false, ptr @C.str)
38+
store target("dx.RawBuffer", float, 0, 0) %h3, ptr @Three, align 4
39+
40+
%h4 = call target("dx.TypedBuffer", float, 1, 0, 0) @llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false, ptr @S.str)
41+
store target("dx.TypedBuffer", float, 1, 0, 0) %h4, ptr @Four, align 4
42+
43+
ret void
44+
}

llvm/test/CodeGen/DirectX/ShaderFlags/typed-uav-load-additional-formats.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ define <4 x float> @multicomponent() #0 {
2626
; CHECK: Function onecomponent : 0x00000000
2727
define float @onecomponent() #0 {
2828
%res = call target("dx.TypedBuffer", float, 1, 0, 0)
29-
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false, ptr null)
29+
@llvm.dx.resource.handlefrombinding(i32 0, i32 1, i32 1, i32 0, i1 false, ptr null)
3030
%load = call {float, i1} @llvm.dx.resource.load.typedbuffer(
3131
target("dx.TypedBuffer", float, 1, 0, 0) %res, i32 0)
3232
%val = extractvalue {float, i1} %load, 0
@@ -36,7 +36,7 @@ define float @onecomponent() #0 {
3636
; CHECK: Function noload : 0x00000000
3737
define void @noload(<4 x float> %val) #0 {
3838
%res = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
39-
@llvm.dx.resource.handlefrombinding(i32 0, i32 0, i32 1, i32 0, i1 false, ptr null)
39+
@llvm.dx.resource.handlefrombinding(i32 0, i32 2, i32 1, i32 0, i1 false, ptr null)
4040
call void @llvm.dx.resource.store.typedbuffer(
4141
target("dx.TypedBuffer", <4 x float>, 1, 0, 0) %res, i32 0,
4242
<4 x float> %val)

0 commit comments

Comments
 (0)