Skip to content

Commit 4d34b01

Browse files
author
joaosaffran
committed
Merge branch 'validation/textures-not-bind-root-signatures' into validation/overlapping-ranges
2 parents 6720815 + 24040a0 commit 4d34b01

14 files changed

+118
-39
lines changed

llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "llvm/ADT/IntervalMap.h"
1818
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
19-
#include "llvm/Support/DXILABI.h"
2019

2120
namespace llvm {
2221
namespace hlsl {
@@ -180,7 +179,7 @@ class RootSignatureBindingValidation {
180179
}
181180
};
182181
llvm::SmallVector<RangeInfo>
183-
findUnboundRanges(const llvm::SmallVectorImpl<RangeInfo> &Ranges,
182+
findUnboundRanges(const llvm::ArrayRef<RangeInfo> &Ranges,
184183
const llvm::ArrayRef<RangeInfo> &Bindings);
185184
} // namespace rootsig
186185
} // namespace hlsl

llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ findOverlappingRanges(llvm::SmallVector<RangeInfo> &Infos) {
317317
}
318318

319319
llvm::SmallVector<RangeInfo>
320-
findUnboundRanges(const llvm::SmallVectorImpl<RangeInfo> &Ranges,
320+
findUnboundRanges(const llvm::ArrayRef<RangeInfo> &Ranges,
321321
const llvm::ArrayRef<RangeInfo> &Bindings) {
322322
llvm::SmallVector<RangeInfo> Unbounds;
323323
for (const auto &Range : Ranges) {

llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include "DXILRootSignature.h"
1111
#include "DXILShaderFlags.h"
1212
#include "DirectX.h"
13+
#include "llvm/ADT/ArrayRef.h"
1314
#include "llvm/ADT/STLForwardCompat.h"
1415
#include "llvm/ADT/SmallString.h"
15-
#include "llvm/ADT/SmallVector.h"
1616
#include "llvm/Analysis/DXILMetadataAnalysis.h"
1717
#include "llvm/Analysis/DXILResource.h"
1818
#include "llvm/BinaryFormat/DXContainer.h"
@@ -22,8 +22,6 @@
2222
#include "llvm/IR/IntrinsicsDirectX.h"
2323
#include "llvm/IR/Module.h"
2424
#include "llvm/InitializePasses.h"
25-
#include "llvm/MC/DXContainerRootSignature.h"
26-
#include "llvm/Support/DXILABI.h"
2725

2826
#define DEBUG_TYPE "dxil-post-optimization-validation"
2927

@@ -151,7 +149,7 @@ static void reportRegNotBound(Module &M,
151149
OS << "register " << ResourceClassToString(Unbound.Class)
152150
<< " (space=" << Unbound.Space << ", register=" << Unbound.LowerBound
153151
<< ")"
154-
<< " is not defined in Root Signature";
152+
<< " does not have a binding in the Root Signature";
155153
M.getContext().diagnose(DiagnosticInfoGeneric(Message));
156154
}
157155

@@ -248,6 +246,44 @@ initRSBindingValidation(const mcdxbc::RootSignatureDesc &RSD,
248246
return Validation;
249247
}
250248

249+
static SmallVector<ResourceInfo::ResourceBinding>
250+
getRootDescriptorsBindingInfo(const mcdxbc::RootSignatureDesc &RSD,
251+
dxbc::ShaderVisibility Visibility) {
252+
253+
SmallVector<ResourceInfo::ResourceBinding> RDs;
254+
255+
for (size_t I = 0; I < RSD.ParametersContainer.size(); I++) {
256+
const auto &[Type, Loc] =
257+
RSD.ParametersContainer.getTypeAndLocForParameter(I);
258+
259+
const auto &Header = RSD.ParametersContainer.getHeader(I);
260+
if (Header.ShaderVisibility !=
261+
llvm::to_underlying(dxbc::ShaderVisibility::All) &&
262+
Header.ShaderVisibility != llvm::to_underlying(Visibility))
263+
continue;
264+
265+
switch (Type) {
266+
267+
case llvm::to_underlying(dxbc::RootParameterType::SRV):
268+
case llvm::to_underlying(dxbc::RootParameterType::UAV):
269+
case llvm::to_underlying(dxbc::RootParameterType::CBV): {
270+
dxbc::RTS0::v2::RootDescriptor Desc =
271+
RSD.ParametersContainer.getRootDescriptor(Loc);
272+
273+
ResourceInfo::ResourceBinding Binding;
274+
Binding.LowerBound = Desc.ShaderRegister;
275+
Binding.Space = Desc.RegisterSpace;
276+
Binding.Size = 1;
277+
278+
RDs.push_back(Binding);
279+
break;
280+
}
281+
}
282+
}
283+
284+
return RDs;
285+
}
286+
251287
std::optional<mcdxbc::RootSignatureDesc>
252288
getRootSignature(RootSignatureBindingInfo &RSBI,
253289
dxil::ModuleMetadataInfo &MMI) {
@@ -349,10 +385,22 @@ static bool reportOverlappingRanges(Module &M,
349385
}
350386

351387
static void reportInvalidHandleTy(
352-
Module &M,
388+
Module &M, const llvm::ArrayRef<dxil::ResourceInfo::ResourceBinding> &RDs,
353389
const iterator_range<SmallVectorImpl<dxil::ResourceInfo>::iterator>
354390
&Resources) {
355391
for (auto Res = Resources.begin(), End = Resources.end(); Res != End; Res++) {
392+
llvm::dxil::ResourceInfo::ResourceBinding Binding = Res->getBinding();
393+
bool IsBound = false;
394+
for (const auto &RD : RDs) {
395+
if (Binding.overlapsWith(RD)) {
396+
IsBound = true;
397+
break;
398+
}
399+
}
400+
401+
if (!IsBound)
402+
continue;
403+
356404
TargetExtType *Handle = Res->getHandleTy();
357405
auto *TypedBuffer = dyn_cast_or_null<TypedBufferExtType>(Handle);
358406
auto *Texture = dyn_cast_or_null<TextureExtType>(Handle);
@@ -402,8 +450,9 @@ static void reportErrors(Module &M, DXILResourceMap &DRM,
402450
if (auto RSD = getRootSignature(RSBI, MMI)) {
403451
if (reportOverlappingRanges(M, *RSD))
404452
return;
453+
dxbc::ShaderVisibility Visibility = tripleToVisibility(MMI.ShaderProfile);
405454
llvm::hlsl::rootsig::RootSignatureBindingValidation Validation =
406-
initRSBindingValidation(*RSD, tripleToVisibility(MMI.ShaderProfile));
455+
initRSBindingValidation(*RSD, Visibility);
407456

408457
reportUnboundRegisters(M, Validation, ResourceClass::CBuffer,
409458
DRM.cbuffers());
@@ -412,10 +461,13 @@ static void reportErrors(Module &M, DXILResourceMap &DRM,
412461
DRM.samplers());
413462
reportUnboundRegisters(M, Validation, ResourceClass::SRV, DRM.srvs());
414463

415-
reportInvalidHandleTy(M, DRM.cbuffers());
416-
reportInvalidHandleTy(M, DRM.srvs());
417-
reportInvalidHandleTy(M, DRM.uavs());
418-
reportInvalidHandleTy(M, DRM.samplers());
464+
SmallVector<ResourceInfo::ResourceBinding> RDs =
465+
getRootDescriptorsBindingInfo(*RSD, Visibility);
466+
467+
reportInvalidHandleTy(M, RDs, DRM.cbuffers());
468+
reportInvalidHandleTy(M, RDs, DRM.srvs());
469+
reportInvalidHandleTy(M, RDs, DRM.uavs());
470+
reportInvalidHandleTy(M, RDs, DRM.samplers());
419471
}
420472
}
421473
} // namespace

llvm/lib/Target/DirectX/DXILPostOptimizationValidation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/IR/PassManager.h"
1818

1919
namespace llvm {
20+
2021
class DXILPostOptimizationValidation
2122
: public PassInfoMixin<DXILPostOptimizationValidation> {
2223
public:

llvm/test/CodeGen/DirectX/rootsignature-validation-fail-cbuffer.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 | FileCheck %s
22

3-
; CHECK: error: register CBuffer (space=665, register=3) is not defined in Root Signature
3+
; CHECK: error: register CBuffer (space=665, register=3) does not have a binding in the Root Signature
44

55
; Root Signature(
66
; CBV(b3, space=666, visibility=SHADER_VISIBILITY_ALL)

llvm/test/CodeGen/DirectX/rootsignature-validation-fail-constants.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 | FileCheck %s
2-
; CHECK: error: register CBuffer (space=666, register=2) is not defined in Root Signature
2+
; CHECK: error: register CBuffer (space=666, register=2) does not have a binding in the Root Signature
33
; Root Signature(RootConstants(num32BitConstants=4, b2))
44

55
%__cblayout_CB = type <{ float }>

llvm/test/CodeGen/DirectX/rootsignature-validation-fail-sampler.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 | FileCheck %s
22

3-
; CHECK: error: register Sampler (space=2, register=3) is not defined in Root Signature
3+
; CHECK: error: register Sampler (space=2, register=3) does not have a binding in the Root Signature
44

55
; Root Signature(
66
; CBV(b3, space=666, visibility=SHADER_VISIBILITY_ALL)

llvm/test/CodeGen/DirectX/rootsignature-validation-fail-srv.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 | FileCheck %s
22

3-
; CHECK: error: register SRV (space=0, register=0) is not defined in Root Signature
3+
; CHECK: error: register SRV (space=0, register=0) does not have a binding in the Root Signature
44

55
; Root Signature(
66
; CBV(b3, space=666, visibility=SHADER_VISIBILITY_ALL)

llvm/test/CodeGen/DirectX/rootsignature-validation-fail-uav.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 | FileCheck %s
22

3-
; CHECK: error: register UAV (space=0, register=4294967295) is not defined in Root Signature
3+
; CHECK: error: register UAV (space=0, register=4294967295) does not have a binding in the Root Signature
44

55
; Root Signature(
66
; CBV(b3, space=666, visibility=SHADER_VISIBILITY_ALL)

llvm/test/CodeGen/DirectX/rootsignature-validation-textures-fail.ll

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44

55
; Root Signature(
6-
; CBV(b3, space=1, visibility=SHADER_VISIBILITY_ALL)
7-
; DescriptorTable(SRV(t0, space=0, numDescriptors=1), visibility=SHADER_VISIBILITY_ALL)
8-
; DescriptorTable(Sampler(s0, numDescriptors=2), visibility=SHADER_VISIBILITY_VERTEX)
9-
; DescriptorTable(UAV(u0, numDescriptors=unbounded), visibility=SHADER_VISIBILITY_ALL)
6+
; UAV(b0, space=0, visibility=SHADER_VISIBILITY_ALL)
107

118
@TB.str = private unnamed_addr constant [3 x i8] c"TB\00", align 1
129

@@ -19,11 +16,5 @@ entry:
1916
!dx.rootsignatures = !{!0}
2017

2118
!0 = !{ptr @CSMain, !1, i32 2}
22-
!1 = !{!2, !3, !5, !7}
23-
!2 = !{!"RootCBV", i32 0, i32 3, i32 1, i32 4}
24-
!3 = !{!"DescriptorTable", i32 0, !4}
25-
!4 = !{!"SRV", i32 1, i32 0, i32 0, i32 -1, i32 0}
26-
!5 = !{!"DescriptorTable", i32 0, !6}
27-
!6 = !{!"Sampler", i32 5, i32 3, i32 2, i32 -1, i32 0}
28-
!7 = !{!"DescriptorTable", i32 0, !8}
29-
!8 = !{!"UAV", i32 -1, i32 0, i32 0, i32 -1, i32 2}
19+
!1 = !{!2}
20+
!2 = !{!"RootUAV", i32 0, i32 0, i32 0, i32 4}

0 commit comments

Comments
 (0)