Skip to content

Commit d8a91e2

Browse files
author
joaosaffran
committed
finish implementing && fix tests
1 parent 07aeb7f commit d8a91e2

File tree

6 files changed

+62
-55
lines changed

6 files changed

+62
-55
lines changed

llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,29 @@ static void reportErrors(Module &M, DXILResourceMap &DRM,
152152
RootSignatureBindingValidation Validation;
153153
Validation.addRsBindingInfo(*RSD, tripleToVisibility(MMI.ShaderProfile));
154154

155-
for (const auto &CBuf : DRM.cbuffers()) {
155+
for (const ResourceInfo &CBuf : DRM.cbuffers()) {
156156
ResourceInfo::ResourceBinding Binding = CBuf.getBinding();
157-
if (!Validation.checkCregBinding(Binding))
157+
if (!Validation.checkCRegBinding(Binding))
158158
reportRegNotBound(M, "cbuffer", Binding);
159159
}
160160

161-
for (const auto &CBuf : DRM.srvs()) {
162-
ResourceInfo::ResourceBinding Binding = CBuf.getBinding();
161+
for (const ResourceInfo &SRV : DRM.srvs()) {
162+
ResourceInfo::ResourceBinding Binding = SRV.getBinding();
163163
if (!Validation.checkTRegBinding(Binding))
164164
reportRegNotBound(M, "srv", Binding);
165165
}
166166

167-
for (const auto &CBuf : DRM.uavs()) {
168-
ResourceInfo::ResourceBinding Binding = CBuf.getBinding();
167+
for (const ResourceInfo &UAV : DRM.uavs()) {
168+
ResourceInfo::ResourceBinding Binding = UAV.getBinding();
169169
if (!Validation.checkURegBinding(Binding))
170170
reportRegNotBound(M, "uav", Binding);
171171
}
172+
173+
for (const ResourceInfo &Sampler : DRM.samplers()) {
174+
ResourceInfo::ResourceBinding Binding = Sampler.getBinding();
175+
if (!Validation.checkSamplerBinding(Binding))
176+
reportRegNotBound(M, "sampler", Binding);
177+
}
172178
}
173179
}
174180
} // namespace
@@ -198,10 +204,6 @@ void RootSignatureBindingValidation::addRsBindingInfo(
198204
RSD.ParametersContainer.getDescriptorTable(Loc);
199205

200206
for (const dxbc::RTS0::v2::DescriptorRange &Range : Table.Ranges) {
201-
if (Range.RangeType ==
202-
llvm::to_underlying(dxbc::DescriptorRangeType::Sampler))
203-
continue;
204-
205207
if (Header.ShaderVisibility ==
206208
llvm::to_underlying(dxbc::ShaderVisibility::All) ||
207209
Header.ShaderVisibility == llvm::to_underlying(Visibility))
@@ -213,29 +215,6 @@ void RootSignatureBindingValidation::addRsBindingInfo(
213215
}
214216
}
215217

216-
bool RootSignatureBindingValidation::checkCregBinding(
217-
ResourceInfo::ResourceBinding Binding) {
218-
return CRegBindingsMap.overlaps(
219-
combineUint32ToUint64(Binding.Space, Binding.LowerBound),
220-
combineUint32ToUint64(Binding.Space,
221-
Binding.LowerBound + Binding.Size - 1));
222-
}
223-
224-
bool RootSignatureBindingValidation::checkTRegBinding(
225-
ResourceInfo::ResourceBinding Binding) {
226-
return TRegBindingsMap.overlaps(
227-
combineUint32ToUint64(Binding.Space, Binding.LowerBound),
228-
combineUint32ToUint64(Binding.Space, Binding.LowerBound + Binding.Size));
229-
}
230-
231-
bool RootSignatureBindingValidation::checkURegBinding(
232-
ResourceInfo::ResourceBinding Binding) {
233-
return URegBindingsMap.overlaps(
234-
combineUint32ToUint64(Binding.Space, Binding.LowerBound),
235-
combineUint32ToUint64(Binding.Space,
236-
Binding.LowerBound + Binding.Size - 1));
237-
}
238-
239218
PreservedAnalyses
240219
DXILPostOptimizationValidation::run(Module &M, ModuleAnalysisManager &MAM) {
241220
DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);

llvm/lib/Target/DirectX/DXILPostOptimizationValidation.h

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ class RootSignatureBindingValidation {
3636
MapT CRegBindingsMap;
3737
MapT TRegBindingsMap;
3838
MapT URegBindingsMap;
39+
MapT SamplersBindingsMap;
3940

4041
void addRange(const dxbc::RTS0::v2::RootDescriptor &Desc, uint32_t Type) {
4142
assert((Type == llvm::to_underlying(dxbc::RootParameterType::CBV) ||
4243
Type == llvm::to_underlying(dxbc::RootParameterType::SRV) ||
4344
Type == llvm::to_underlying(dxbc::RootParameterType::UAV)) &&
44-
"Invalid Type");
45+
"Invalid Type in add Range Method");
4546

4647
llvm::dxil::ResourceInfo::ResourceBinding Binding;
4748
Binding.LowerBound = Desc.ShaderRegister;
@@ -53,19 +54,20 @@ class RootSignatureBindingValidation {
5354
uint64_t HighRange = combineUint32ToUint64(
5455
Binding.Space, Binding.LowerBound + Binding.Size - 1);
5556

57+
assert(LowRange <= HighRange && "Invalid range configuration");
58+
5659
switch (Type) {
5760

5861
case llvm::to_underlying(dxbc::RootParameterType::CBV):
5962
CRegBindingsMap.insert(LowRange, HighRange, Binding);
60-
return;
63+
break;
6164
case llvm::to_underlying(dxbc::RootParameterType::SRV):
6265
TRegBindingsMap.insert(LowRange, HighRange, Binding);
63-
return;
66+
break;
6467
case llvm::to_underlying(dxbc::RootParameterType::UAV):
6568
URegBindingsMap.insert(LowRange, HighRange, Binding);
66-
return;
69+
break;
6770
}
68-
llvm_unreachable("Invalid Type in add Range Method");
6971
}
7072

7173
void addRange(const dxbc::RTS0::v2::DescriptorRange &Range) {
@@ -80,33 +82,59 @@ class RootSignatureBindingValidation {
8082
uint64_t HighRange = combineUint32ToUint64(
8183
Binding.Space, Binding.LowerBound + Binding.Size - 1);
8284

85+
assert(LowRange <= HighRange && "Invalid range configuration");
86+
8387
switch (Range.RangeType) {
8488
case llvm::to_underlying(dxbc::DescriptorRangeType::CBV):
8589
CRegBindingsMap.insert(LowRange, HighRange, Binding);
86-
return;
90+
break;
8791
case llvm::to_underlying(dxbc::DescriptorRangeType::SRV):
8892
TRegBindingsMap.insert(LowRange, HighRange, Binding);
89-
return;
93+
break;
9094
case llvm::to_underlying(dxbc::DescriptorRangeType::UAV):
9195
URegBindingsMap.insert(LowRange, HighRange, Binding);
92-
return;
96+
break;
97+
case llvm::to_underlying(dxbc::DescriptorRangeType::Sampler):
98+
SamplersBindingsMap.insert(LowRange, HighRange, Binding);
99+
break;
93100
}
94-
llvm_unreachable("Invalid Type in add Range Method");
95101
}
96102

97103
public:
98104
RootSignatureBindingValidation()
99105
: Allocator(), CRegBindingsMap(Allocator), TRegBindingsMap(Allocator),
100-
URegBindingsMap(Allocator) {}
106+
URegBindingsMap(Allocator), SamplersBindingsMap(Allocator) {}
101107

102108
void addRsBindingInfo(mcdxbc::RootSignatureDesc &RSD,
103109
dxbc::ShaderVisibility Visibility);
104110

105-
bool checkCregBinding(dxil::ResourceInfo::ResourceBinding Binding);
111+
bool checkCRegBinding(dxil::ResourceInfo::ResourceBinding Binding) {
112+
return CRegBindingsMap.overlaps(
113+
combineUint32ToUint64(Binding.Space, Binding.LowerBound),
114+
combineUint32ToUint64(Binding.Space,
115+
Binding.LowerBound + Binding.Size - 1));
116+
}
106117

107-
bool checkTRegBinding(dxil::ResourceInfo::ResourceBinding Binding);
118+
bool checkTRegBinding(dxil::ResourceInfo::ResourceBinding Binding) {
119+
return TRegBindingsMap.overlaps(
120+
combineUint32ToUint64(Binding.Space, Binding.LowerBound),
121+
combineUint32ToUint64(Binding.Space,
122+
Binding.LowerBound + Binding.Size - 1));
123+
}
108124

109-
bool checkURegBinding(dxil::ResourceInfo::ResourceBinding Binding);
125+
bool checkURegBinding(dxil::ResourceInfo::ResourceBinding Binding) {
126+
return URegBindingsMap.overlaps(
127+
combineUint32ToUint64(Binding.Space, Binding.LowerBound),
128+
combineUint32ToUint64(Binding.Space,
129+
Binding.LowerBound + Binding.Size - 1));
130+
}
131+
132+
bool checkSamplerBinding(dxil::ResourceInfo::ResourceBinding Binding) {
133+
return SamplersBindingsMap.overlaps(
134+
combineUint32ToUint64(Binding.Space, Binding.LowerBound),
135+
combineUint32ToUint64(Binding.Space,
136+
Binding.LowerBound + Binding.Size - 1));
137+
}
110138
};
111139

112140
class DXILPostOptimizationValidation

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "llvm/Support/ErrorHandling.h"
2929
#include "llvm/Support/raw_ostream.h"
3030
#include <cstdint>
31-
#include <memory>
3231
#include <optional>
3332
#include <utility>
3433

@@ -639,10 +638,9 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M,
639638

640639
//===----------------------------------------------------------------------===//
641640
bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {
642-
if (HasRun)
643-
return false;
644-
FuncToRsMap = std::make_unique<RootSignatureBindingInfo>(
645-
RootSignatureBindingInfo(analyzeModule(M)));
641+
if (!FuncToRsMap)
642+
FuncToRsMap = std::make_unique<RootSignatureBindingInfo>(
643+
RootSignatureBindingInfo(analyzeModule(M)));
646644
return false;
647645
}
648646

llvm/lib/Target/DirectX/DXILRootSignature.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
/// Root Signatures.
1111
///
1212
//===----------------------------------------------------------------------===//
13+
#ifndef LLVM_LIB_TARGET_DIRECTX_DXILROOTSIGNATURE_H
14+
#define LLVM_LIB_TARGET_DIRECTX_DXILROOTSIGNATURE_H
1315

1416
#include "llvm/ADT/DenseMap.h"
1517
#include "llvm/Analysis/DXILMetadataAnalysis.h"
@@ -19,7 +21,6 @@
1921
#include "llvm/IR/PassManager.h"
2022
#include "llvm/MC/DXContainerRootSignature.h"
2123
#include "llvm/Pass.h"
22-
#include <memory>
2324
#include <optional>
2425

2526
namespace llvm {
@@ -84,7 +85,6 @@ class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {
8485
class RootSignatureAnalysisWrapper : public ModulePass {
8586
private:
8687
std::unique_ptr<RootSignatureBindingInfo> FuncToRsMap;
87-
bool HasRun = false;
8888

8989
public:
9090
static char ID;
@@ -111,3 +111,4 @@ class RootSignatureAnalysisPrinter
111111

112112
} // namespace dxil
113113
} // namespace llvm
114+
#endif

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-DescriptorTable-AllValidFlagCombinationsV1.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
1313
!2 = !{ ptr @main, !3, i32 1 } ; function, root signature
1414
!3 = !{ !5 } ; list of root signature elements
1515
!5 = !{ !"DescriptorTable", i32 0, !6, !7 }
16-
!6 = !{ !"Sampler", i32 0, i32 1, i32 0, i32 -1, i32 1 }
16+
!6 = !{ !"Sampler", i32 1, i32 1, i32 0, i32 -1, i32 1 }
1717
!7 = !{ !"UAV", i32 5, i32 1, i32 10, i32 5, i32 3 }
1818

1919

@@ -33,7 +33,7 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
3333
; DXC-NEXT: RangesOffset: 44
3434
; DXC-NEXT: Ranges:
3535
; DXC-NEXT: - RangeType: 3
36-
; DXC-NEXT: NumDescriptors: 0
36+
; DXC-NEXT: NumDescriptors: 1
3737
; DXC-NEXT: BaseShaderRegister: 1
3838
; DXC-NEXT: RegisterSpace: 0
3939
; DXC-NEXT: OffsetInDescriptorsFromTableStart: 4294967295

llvm/test/CodeGen/DirectX/llc-pipeline.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
; CHECK-NEXT: DXIL Module Metadata analysis
3232
; CHECK-NEXT: DXIL Shader Flag Analysis
3333
; CHECK-NEXT: DXIL Translate Metadata
34+
; CHECK-NEXT: DXIL Root Signature Analysis
3435
; CHECK-NEXT: DXIL Post Optimization Validation
3536
; CHECK-NEXT: DXIL Op Lowering
3637
; CHECK-NEXT: DXIL Prepare Module

0 commit comments

Comments
 (0)