LLVM 22.0.0git
DXILPostOptimizationValidation.cpp
Go to the documentation of this file.
1//===- DXILPostOptimizationValidation.cpp - Opt DXIL validation ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "DXILRootSignature.h"
11#include "DXILShaderFlags.h"
12#include "DirectX.h"
18#include "llvm/IR/IntrinsicsDirectX.h"
19#include "llvm/IR/Module.h"
22
23#define DEBUG_TYPE "dxil-post-optimization-validation"
24
25using namespace llvm;
26using namespace llvm::dxil;
27
29 using namespace dxbc;
30 switch (Type) {
31 case RootParameterType::Constants32Bit:
33 case RootParameterType::SRV:
34 return ResourceClass::SRV;
35 case RootParameterType::UAV:
36 return ResourceClass::UAV;
37 case RootParameterType::CBV:
39 case dxbc::RootParameterType::DescriptorTable:
40 llvm_unreachable("DescriptorTable is not convertible to ResourceClass");
41 }
42 llvm_unreachable("Unknown RootParameterType");
43}
44
46 for (const auto &UAV : DRM.uavs()) {
47 if (UAV.CounterDirection != ResourceCounterDirection::Invalid)
48 continue;
49
50 CallInst *ResourceHandle = nullptr;
51 for (CallInst *MaybeHandle : DRM.calls()) {
52 if (*DRM.find(MaybeHandle) == UAV) {
53 ResourceHandle = MaybeHandle;
54 break;
55 }
56 }
57
58 StringRef Message = "RWStructuredBuffers may increment or decrement their "
59 "counters, but not both.";
60 for (const auto &U : ResourceHandle->users()) {
61 const CallInst *CI = dyn_cast<CallInst>(U);
62 if (!CI && CI->getIntrinsicID() != Intrinsic::dx_resource_updatecounter)
63 continue;
64
65 M.getContext().diagnose(DiagnosticInfoGenericWithLoc(
66 Message, *CI->getFunction(), CI->getDebugLoc()));
67 }
68 }
69}
70
73 SmallString<128> Message;
74 raw_svector_ostream OS(Message);
75 OS << "resource " << R1.getName() << " at register "
76 << R1.getBinding().LowerBound << " overlaps with resource " << R2.getName()
77 << " at register " << R2.getBinding().LowerBound << " in space "
78 << R2.getBinding().Space;
79 M.getContext().diagnose(DiagnosticInfoGeneric(Message));
80}
81
83 [[maybe_unused]] bool ErrorFound = false;
84 for (const auto &ResList :
85 {DRM.srvs(), DRM.uavs(), DRM.cbuffers(), DRM.samplers()}) {
86 if (ResList.empty())
87 continue;
88 const ResourceInfo *PrevRI = &*ResList.begin();
89 for (auto *I = ResList.begin() + 1; I != ResList.end(); ++I) {
90 const ResourceInfo *CurrentRI = &*I;
91 const ResourceInfo *RI = CurrentRI;
92 while (RI != ResList.end() &&
93 PrevRI->getBinding().overlapsWith(RI->getBinding())) {
94 reportOverlappingError(M, *PrevRI, *RI);
95 ErrorFound = true;
96 RI++;
97 }
98 PrevRI = CurrentRI;
99 }
100 }
101 assert(ErrorFound && "this function should be called only when if "
102 "DXILResourceBindingInfo::hasOverlapingBinding() is "
103 "true, yet no overlapping binding was found");
104}
105
108 SmallString<160> Message;
109 raw_svector_ostream OS(Message);
110 StringRef RCName = getResourceClassName(RC);
111 OS << RCName << " at register " << Binding.LowerBound << " and space "
112 << Binding.Space << " is bound to a texture or typed buffer. " << RCName
113 << " root descriptors can only be Raw or Structured buffers.";
114 M.getContext().diagnose(DiagnosticInfoGeneric(Message));
115}
116
118 const llvm::hlsl::Binding &R2) {
119 SmallString<128> Message;
120
121 raw_svector_ostream OS(Message);
122 OS << "resource " << getResourceClassName(R1.RC) << " (space=" << R1.Space
123 << ", registers=[" << R1.LowerBound << ", " << R1.UpperBound
124 << "]) overlaps with resource " << getResourceClassName(R2.RC)
125 << " (space=" << R2.Space << ", registers=[" << R2.LowerBound << ", "
126 << R2.UpperBound << "])";
127 M.getContext().diagnose(DiagnosticInfoGeneric(Message));
128}
129
130static void
133 SmallString<128> Message;
134 raw_svector_ostream OS(Message);
135 OS << getResourceClassName(Class) << " register " << Unbound.LowerBound
136 << " in space " << Unbound.Space
137 << " does not have a binding in the Root Signature";
138 M.getContext().diagnose(DiagnosticInfoGeneric(Message));
139}
140
143 switch (ET) {
144 case Triple::Pixel:
145 return dxbc::ShaderVisibility::Pixel;
146 case Triple::Vertex:
147 return dxbc::ShaderVisibility::Vertex;
148 case Triple::Geometry:
149 return dxbc::ShaderVisibility::Geometry;
150 case Triple::Hull:
151 return dxbc::ShaderVisibility::Hull;
152 case Triple::Domain:
153 return dxbc::ShaderVisibility::Domain;
154 case Triple::Mesh:
155 return dxbc::ShaderVisibility::Mesh;
156 case Triple::Compute:
157 return dxbc::ShaderVisibility::All;
158 default:
159 llvm_unreachable("Invalid triple to shader stage conversion");
160 }
161}
162
164 const dxbc::RootFlags &Flags,
165 const dxbc::RootFlags &Mask) {
166 if ((Flags & Mask) != Mask)
167 return;
168
169 SmallString<128> Message;
170 raw_svector_ostream OS(Message);
171 OS << "Shader has root bindings but root signature uses a DENY flag to "
172 "disallow root binding access to the shader stage.";
173 M.getContext().diagnose(DiagnosticInfoGeneric(Message));
174}
175
176static std::optional<dxbc::RootFlags>
178 switch (ShaderProfile) {
179 case Triple::Pixel:
180 return dxbc::RootFlags::DenyPixelShaderRootAccess;
181 case Triple::Vertex:
182 return dxbc::RootFlags::DenyVertexShaderRootAccess;
183 case Triple::Geometry:
184 return dxbc::RootFlags::DenyGeometryShaderRootAccess;
185 case Triple::Hull:
186 return dxbc::RootFlags::DenyHullShaderRootAccess;
187 case Triple::Domain:
188 return dxbc::RootFlags::DenyDomainShaderRootAccess;
189 case Triple::Mesh:
190 return dxbc::RootFlags::DenyMeshShaderRootAccess;
192 return dxbc::RootFlags::DenyAmplificationShaderRootAccess;
193 default:
194 return std::nullopt;
195 }
196}
197
199 const mcdxbc::RootSignatureDesc &RSD,
201 DXILResourceMap &DRM,
202 DXILResourceTypeMap &DRTM) {
203
206
207 for (const mcdxbc::RootParameterInfo &ParamInfo : RSD.ParametersContainer) {
208 dxbc::ShaderVisibility ParamVisibility =
210 if (ParamVisibility != dxbc::ShaderVisibility::All &&
211 ParamVisibility != Visibility)
212 continue;
214 switch (ParamType) {
215 case dxbc::RootParameterType::Constants32Bit: {
218 Builder.trackBinding(dxil::ResourceClass::CBuffer, Const.RegisterSpace,
219 Const.ShaderRegister, Const.ShaderRegister,
220 &ParamInfo);
221 break;
222 }
223
224 case dxbc::RootParameterType::SRV:
225 case dxbc::RootParameterType::UAV:
226 case dxbc::RootParameterType::CBV: {
229 Builder.trackBinding(toResourceClass(ParamInfo.Type), Desc.RegisterSpace,
230 Desc.ShaderRegister, Desc.ShaderRegister,
231 &ParamInfo);
232
233 break;
234 }
235 case dxbc::RootParameterType::DescriptorTable: {
236 const mcdxbc::DescriptorTable &Table =
238
239 for (const mcdxbc::DescriptorRange &Range : Table.Ranges) {
240 uint32_t UpperBound =
241 Range.NumDescriptors == ~0U
242 ? Range.BaseShaderRegister
243 : Range.BaseShaderRegister + Range.NumDescriptors - 1;
244 Builder.trackBinding(Range.RangeType, Range.RegisterSpace,
245 Range.BaseShaderRegister, UpperBound, &ParamInfo);
246 }
247 break;
248 }
249 }
250 }
251
252 for (const mcdxbc::StaticSampler &S : RSD.StaticSamplers)
253 Builder.trackBinding(dxil::ResourceClass::Sampler, S.RegisterSpace,
255
256 Builder.calculateBindingInfo(
257 [&M](const llvm::hlsl::BindingInfoBuilder &Builder,
258 const llvm::hlsl::Binding &ReportedBinding) {
259 const llvm::hlsl::Binding &Overlaping =
260 Builder.findOverlapping(ReportedBinding);
261 reportOverlappingRegisters(M, ReportedBinding, Overlaping);
262 });
263
264 const hlsl::BoundRegs &BoundRegs = Builder.takeBoundRegs();
265 bool HasBindings = false;
266 for (const ResourceInfo &RI : DRM) {
267 const ResourceInfo::ResourceBinding &Binding = RI.getBinding();
268 const dxil::ResourceTypeInfo &RTI = DRTM[RI.getHandleTy()];
271
272 const llvm::hlsl::Binding *Reg =
273 BoundRegs.findBoundReg(RC, Binding.Space, Binding.LowerBound,
274 Binding.LowerBound + Binding.Size - 1);
275
276 if (!Reg) {
278 continue;
279 }
280
281 const auto *ParamInfo =
282 static_cast<const mcdxbc::RootParameterInfo *>(Reg->Cookie);
283
284 bool IsSRVOrUAV = RC == ResourceClass::SRV || RC == ResourceClass::UAV;
285 bool IsDescriptorTable =
286 ParamInfo->Type == dxbc::RootParameterType::DescriptorTable;
287 bool IsRawOrStructuredBuffer =
289 if (IsSRVOrUAV && !IsDescriptorTable && IsRawOrStructuredBuffer) {
291 continue;
292 }
293
294 HasBindings = true;
295 }
296
297 if (!HasBindings)
298 return;
299
300 if (std::optional<dxbc::RootFlags> Mask =
303}
304
308 if (MMI.EntryPropertyVec.size() == 0)
309 return nullptr;
310 return RSBI.getDescForFunction(MMI.EntryPropertyVec[0].Entry);
311}
312
317 DXILResourceTypeMap &DRTM) {
320
321 if (DRBI.hasOverlappingBinding())
323
324 assert(!DRBI.hasImplicitBinding() && "implicit bindings should be handled in "
325 "DXILResourceImplicitBinding pass");
326
327 if (mcdxbc::RootSignatureDesc *RSD = getRootSignature(RSBI, MMI))
328 validateRootSignature(M, *RSD, MMI, DRM, DRTM);
329}
330
333 DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);
336 ModuleMetadataInfo &MMI = MAM.getResult<DXILMetadataAnalysis>(M);
338
339 reportErrors(M, DRM, DRBI, RSBI, MMI, DRTM);
340 return PreservedAnalyses::all();
341}
342
343namespace {
344class DXILPostOptimizationValidationLegacy : public ModulePass {
345public:
346 bool runOnModule(Module &M) override {
347 DXILResourceMap &DRM =
348 getAnalysis<DXILResourceWrapperPass>().getResourceMap();
350 getAnalysis<DXILResourceBindingWrapperPass>().getBindingInfo();
352 getAnalysis<RootSignatureAnalysisWrapper>().getRSInfo();
354 getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
355 DXILResourceTypeMap &DRTM =
356 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
357
358 reportErrors(M, DRM, DRBI, RSBI, MMI, DRTM);
359 return false;
360 }
361 StringRef getPassName() const override {
362 return "DXIL Post Optimization Validation";
363 }
364 DXILPostOptimizationValidationLegacy() : ModulePass(ID) {}
365
366 static char ID; // Pass identification.
367 void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
368 AU.addRequired<DXILResourceWrapperPass>();
369 AU.addRequired<DXILResourceBindingWrapperPass>();
370 AU.addRequired<DXILMetadataAnalysisWrapperPass>();
371 AU.addRequired<RootSignatureAnalysisWrapper>();
372 AU.addRequired<DXILResourceTypeWrapperPass>();
373 AU.addPreserved<DXILResourceWrapperPass>();
374 AU.addPreserved<DXILResourceBindingWrapperPass>();
375 AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
376 AU.addPreserved<ShaderFlagsAnalysisWrapper>();
377 AU.addPreserved<RootSignatureAnalysisWrapper>();
378 }
379};
380char DXILPostOptimizationValidationLegacy::ID = 0;
381} // end anonymous namespace
382
383INITIALIZE_PASS_BEGIN(DXILPostOptimizationValidationLegacy, DEBUG_TYPE,
384 "DXIL Post Optimization Validation", false, false)
391INITIALIZE_PASS_END(DXILPostOptimizationValidationLegacy, DEBUG_TYPE,
392 "DXIL Post Optimization Validation", false, false)
393
395 return new DXILPostOptimizationValidationLegacy();
396}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static dxbc::ShaderVisibility tripleToVisibility(llvm::Triple::EnvironmentType ET)
static void reportRegNotBound(Module &M, ResourceClass Class, const llvm::dxil::ResourceInfo::ResourceBinding &Unbound)
static ResourceClass toResourceClass(dxbc::RootParameterType Type)
static std::optional< dxbc::RootFlags > getEnvironmentDenyFlagMask(Triple::EnvironmentType ShaderProfile)
static void reportOverlappingError(Module &M, ResourceInfo R1, ResourceInfo R2)
static void reportErrors(Module &M, DXILResourceMap &DRM, DXILResourceBindingInfo &DRBI, RootSignatureBindingInfo &RSBI, dxil::ModuleMetadataInfo &MMI, DXILResourceTypeMap &DRTM)
static void validateRootSignature(Module &M, const mcdxbc::RootSignatureDesc &RSD, dxil::ModuleMetadataInfo &MMI, DXILResourceMap &DRM, DXILResourceTypeMap &DRTM)
static void reportIfDeniedShaderStageAccess(Module &M, const dxbc::RootFlags &Flags, const dxbc::RootFlags &Mask)
static void reportInvalidHandleTyError(Module &M, ResourceClass RC, ResourceInfo::ResourceBinding Binding)
static mcdxbc::RootSignatureDesc * getRootSignature(RootSignatureBindingInfo &RSBI, dxil::ModuleMetadataInfo &MMI)
static void reportOverlappingBinding(Module &M, DXILResourceMap &DRM)
static void reportInvalidDirection(Module &M, DXILResourceMap &DRM)
static void reportOverlappingRegisters(Module &M, const llvm::hlsl::Binding &R1, const llvm::hlsl::Binding &R2)
DXIL Resource Implicit Binding
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:58
Register Reg
#define R2(n)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
ModuleAnalysisManager MAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the SmallString class.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
This class represents a function call, abstracting a target machine's calling convention.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM)
iterator find(const CallInst *Key)
bool hasInvalidCounterDirection() const
iterator_range< iterator > samplers()
iterator_range< iterator > srvs()
iterator_range< iterator > cbuffers()
iterator_range< iterator > uavs()
iterator_range< call_iterator > calls()
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
@ Amplification
Definition Triple.h:307
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
iterator_range< user_iterator > users()
Definition Value.h:426
StringRef getName() const
const ResourceBinding & getBinding() const
dxil::ResourceClass getResourceClass() const
dxil::ResourceKind getResourceKind() const
Wrapper pass for the legacy pass manager.
mcdxbc::RootSignatureDesc * getDescForFunction(const Function *F)
Builder class for creating a /c BindingInfo.
const Binding * findBoundReg(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, uint32_t UpperBound) const
A raw_ostream that writes to an SmallVector or SmallString.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
LLVM_ABI StringRef getResourceClassName(ResourceClass RC)
Definition DXILABI.cpp:21
ResourceKind
The kind of resource for an SRV or UAV resource.
Definition DXILABI.h:36
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
ModulePass * createDXILPostOptimizationValidationLegacyPass()
Pass to lowering LLVM intrinsic call to DXIL op function call.
Op::Description Desc
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Triple::EnvironmentType ShaderProfile
SmallVector< EntryProperties > EntryPropertyVec
bool overlapsWith(const ResourceBinding &RHS) const
dxil::ResourceClass RC
SmallVector< DescriptorRange > Ranges
const RootDescriptor & getRootDescriptor(size_t Index) const
const DescriptorTable & getDescriptorTable(size_t Index) const
const RootConstants & getConstant(size_t Index) const
SmallVector< StaticSampler > StaticSamplers
mcdxbc::RootParametersContainer ParametersContainer