LLVM 20.0.0git
DXILResource.h
Go to the documentation of this file.
1//===- DXILResource.h - Representations of DXIL resources -------*- C++ -*-===//
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
9#ifndef LLVM_ANALYSIS_DXILRESOURCE_H
10#define LLVM_ANALYSIS_DXILRESOURCE_H
11
12#include "llvm/ADT/MapVector.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/IR/PassManager.h"
15#include "llvm/Pass.h"
18
19namespace llvm {
20class CallInst;
21class LLVMContext;
22class MDTuple;
23class Value;
24
25namespace dxil {
26
28 struct ResourceBinding {
29 uint32_t RecordID;
30 uint32_t Space;
31 uint32_t LowerBound;
32 uint32_t Size;
33
34 bool operator==(const ResourceBinding &RHS) const {
35 return std::tie(RecordID, Space, LowerBound, Size) ==
36 std::tie(RHS.RecordID, RHS.Space, RHS.LowerBound, RHS.Size);
37 }
38 bool operator!=(const ResourceBinding &RHS) const {
39 return !(*this == RHS);
40 }
41 };
42
43 struct UAVInfo {
44 bool GloballyCoherent;
45 bool HasCounter;
46 bool IsROV;
47
48 bool operator==(const UAVInfo &RHS) const {
49 return std::tie(GloballyCoherent, HasCounter, IsROV) ==
50 std::tie(RHS.GloballyCoherent, RHS.HasCounter, RHS.IsROV);
51 }
52 bool operator!=(const UAVInfo &RHS) const { return !(*this == RHS); }
53 };
54
55 struct StructInfo {
56 uint32_t Stride;
57 // Note: we store an integer here rather than using `MaybeAlign` because in
58 // GCC 7 MaybeAlign isn't trivial so having one in this union would delete
59 // our move constructor.
60 // See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0602r4.html
61 uint32_t AlignLog2;
62
63 bool operator==(const StructInfo &RHS) const {
64 return std::tie(Stride, AlignLog2) == std::tie(RHS.Stride, RHS.AlignLog2);
65 }
66 bool operator!=(const StructInfo &RHS) const { return !(*this == RHS); }
67 };
68
69 struct TypedInfo {
70 dxil::ElementType ElementTy;
72
73 bool operator==(const TypedInfo &RHS) const {
74 return std::tie(ElementTy, ElementCount) ==
75 std::tie(RHS.ElementTy, RHS.ElementCount);
76 }
77 bool operator!=(const TypedInfo &RHS) const { return !(*this == RHS); }
78 };
79
80 struct MSInfo {
81 uint32_t Count;
82
83 bool operator==(const MSInfo &RHS) const { return Count == RHS.Count; }
84 bool operator!=(const MSInfo &RHS) const { return !(*this == RHS); }
85 };
86
87 struct FeedbackInfo {
89
90 bool operator==(const FeedbackInfo &RHS) const { return Type == RHS.Type; }
91 bool operator!=(const FeedbackInfo &RHS) const { return !(*this == RHS); }
92 };
93
94 // Universal properties.
95 Value *Symbol;
96 StringRef Name;
97
100
101 ResourceBinding Binding = {};
102
103 // Resource class dependent properties.
104 // CBuffer, Sampler, and RawBuffer end here.
105 union {
106 UAVInfo UAVFlags; // UAV
109 };
110
111 // Resource kind dependent properties.
112 union {
113 StructInfo Struct; // StructuredBuffer
114 TypedInfo Typed; // All SRV/UAV except Raw/StructuredBuffer
115 FeedbackInfo Feedback; // FeedbackTexture
116 };
117
118 MSInfo MultiSample;
119
120public:
122 StringRef Name)
123 : Symbol(Symbol), Name(Name), RC(RC), Kind(Kind) {}
124
125 // Conditions to check before accessing union members.
126 bool isUAV() const;
127 bool isCBuffer() const;
128 bool isSampler() const;
129 bool isStruct() const;
130 bool isTyped() const;
131 bool isFeedback() const;
132 bool isMultiSample() const;
133
134 void bind(uint32_t RecordID, uint32_t Space, uint32_t LowerBound,
135 uint32_t Size) {
136 Binding.RecordID = RecordID;
137 Binding.Space = Space;
138 Binding.LowerBound = LowerBound;
139 Binding.Size = Size;
140 }
141 void setUAV(bool GloballyCoherent, bool HasCounter, bool IsROV) {
142 assert(isUAV() && "Not a UAV");
143 UAVFlags.GloballyCoherent = GloballyCoherent;
144 UAVFlags.HasCounter = HasCounter;
145 UAVFlags.IsROV = IsROV;
146 }
148 assert(isCBuffer() && "Not a CBuffer");
150 }
152 void setStruct(uint32_t Stride, MaybeAlign Alignment) {
153 assert(isStruct() && "Not a Struct");
154 Struct.Stride = Stride;
155 Struct.AlignLog2 = Alignment ? Log2(*Alignment) : 0;
156 }
158 assert(isTyped() && "Not Typed");
159 Typed.ElementTy = ElementTy;
160 Typed.ElementCount = ElementCount;
161 }
163 assert(isFeedback() && "Not Feedback");
164 Feedback.Type = Type;
165 }
167 assert(isMultiSample() && "Not MultiSampled");
168 MultiSample.Count = Count;
169 }
170
171 bool operator==(const ResourceInfo &RHS) const;
172
173 static ResourceInfo SRV(Value *Symbol, StringRef Name,
175 dxil::ResourceKind Kind);
176 static ResourceInfo RawBuffer(Value *Symbol, StringRef Name);
177 static ResourceInfo StructuredBuffer(Value *Symbol, StringRef Name,
178 uint32_t Stride, MaybeAlign Alignment);
179 static ResourceInfo Texture2DMS(Value *Symbol, StringRef Name,
180 dxil::ElementType ElementTy,
181 uint32_t ElementCount, uint32_t SampleCount);
182 static ResourceInfo Texture2DMSArray(Value *Symbol, StringRef Name,
183 dxil::ElementType ElementTy,
185 uint32_t SampleCount);
186
187 static ResourceInfo UAV(Value *Symbol, StringRef Name,
189 bool GloballyCoherent, bool IsROV,
190 dxil::ResourceKind Kind);
191 static ResourceInfo RWRawBuffer(Value *Symbol, StringRef Name,
192 bool GloballyCoherent, bool IsROV);
193 static ResourceInfo RWStructuredBuffer(Value *Symbol, StringRef Name,
194 uint32_t Stride, MaybeAlign Alignment,
195 bool GloballyCoherent, bool IsROV,
196 bool HasCounter);
197 static ResourceInfo RWTexture2DMS(Value *Symbol, StringRef Name,
198 dxil::ElementType ElementTy,
199 uint32_t ElementCount, uint32_t SampleCount,
200 bool GloballyCoherent);
201 static ResourceInfo RWTexture2DMSArray(Value *Symbol, StringRef Name,
202 dxil::ElementType ElementTy,
204 uint32_t SampleCount,
205 bool GloballyCoherent);
206 static ResourceInfo FeedbackTexture2D(Value *Symbol, StringRef Name,
207 dxil::SamplerFeedbackType FeedbackTy);
208 static ResourceInfo
210 dxil::SamplerFeedbackType FeedbackTy);
211
212 static ResourceInfo CBuffer(Value *Symbol, StringRef Name, uint32_t Size);
213
214 static ResourceInfo Sampler(Value *Symbol, StringRef Name,
216
217 MDTuple *getAsMetadata(LLVMContext &Ctx) const;
218
219 ResourceBinding getBinding() const { return Binding; }
220 std::pair<uint32_t, uint32_t> getAnnotateProps() const;
221
222 void print(raw_ostream &OS) const;
223};
224
225} // namespace dxil
226
228
229class DXILResourceAnalysis : public AnalysisInfoMixin<DXILResourceAnalysis> {
231
232 static AnalysisKey Key;
233
234public:
236
237 /// Gather resource info for the module \c M.
239};
240
241/// Printer pass for the \c DXILResourceAnalysis results.
242class DXILResourcePrinterPass : public PassInfoMixin<DXILResourcePrinterPass> {
243 raw_ostream &OS;
244
245public:
247
249
250 static bool isRequired() { return true; }
251};
252
254 std::unique_ptr<DXILResourceMap> ResourceMap;
255
256public:
257 static char ID; // Class identification, replacement for typeinfo
258
261
262 const DXILResourceMap &getResourceMap() const { return *ResourceMap; }
263 DXILResourceMap &getResourceMap() { return *ResourceMap; }
264
265 void getAnalysisUsage(AnalysisUsage &AU) const override;
266 bool runOnModule(Module &M) override;
267 void releaseMemory() override;
268
269 void print(raw_ostream &OS, const Module *M) const override;
270 void dump() const;
271};
272
274
275} // namespace llvm
276
277#endif // LLVM_ANALYSIS_DXILRESOURCE_H
uint64_t Size
This file implements a map that provides insertion order iteration.
This header defines various interfaces for pass management in LLVM.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
DXILResourceMap run(Module &M, ModuleAnalysisManager &AM)
Gather resource info for the module M.
Printer pass for the DXILResourceAnalysis results.
Definition: DXILResource.h:242
DXILResourcePrinterPass(raw_ostream &OS)
Definition: DXILResource.h:246
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
DXILResourceMap & getResourceMap()
Definition: DXILResource.h:263
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
const DXILResourceMap & getResourceMap() const
Definition: DXILResource.h:262
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Tuple of metadata.
Definition: Metadata.h:1472
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
ResourceInfo(dxil::ResourceClass RC, dxil::ResourceKind Kind, Value *Symbol, StringRef Name)
Definition: DXILResource.h:121
void setUAV(bool GloballyCoherent, bool HasCounter, bool IsROV)
Definition: DXILResource.h:141
static ResourceInfo RWTexture2DMS(Value *Symbol, StringRef Name, dxil::ElementType ElementTy, uint32_t ElementCount, uint32_t SampleCount, bool GloballyCoherent)
std::pair< uint32_t, uint32_t > getAnnotateProps() const
void setCBuffer(uint32_t Size)
Definition: DXILResource.h:147
void print(raw_ostream &OS) const
static ResourceInfo RWRawBuffer(Value *Symbol, StringRef Name, bool GloballyCoherent, bool IsROV)
dxil::SamplerType SamplerTy
Definition: DXILResource.h:108
void setTyped(dxil::ElementType ElementTy, uint32_t ElementCount)
Definition: DXILResource.h:157
void setMultiSample(uint32_t Count)
Definition: DXILResource.h:166
void setSampler(dxil::SamplerType Ty)
Definition: DXILResource.h:151
void setStruct(uint32_t Stride, MaybeAlign Alignment)
Definition: DXILResource.h:152
static ResourceInfo RWTexture2DMSArray(Value *Symbol, StringRef Name, dxil::ElementType ElementTy, uint32_t ElementCount, uint32_t SampleCount, bool GloballyCoherent)
MDTuple * getAsMetadata(LLVMContext &Ctx) const
void setFeedback(dxil::SamplerFeedbackType Type)
Definition: DXILResource.h:162
void bind(uint32_t RecordID, uint32_t Space, uint32_t LowerBound, uint32_t Size)
Definition: DXILResource.h:134
static ResourceInfo RWStructuredBuffer(Value *Symbol, StringRef Name, uint32_t Stride, MaybeAlign Alignment, bool GloballyCoherent, bool IsROV, bool HasCounter)
ResourceBinding getBinding() const
Definition: DXILResource.h:219
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
ResourceKind
The kind of resource for an SRV or UAV resource.
Definition: DXILABI.h:34
ResourceClass
Definition: DXILABI.h:25
SamplerFeedbackType
Definition: DXILABI.h:94
ElementType
The element type of an SRV or UAV resource.
Definition: DXILABI.h:58
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:2060
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
ModulePass * createDXILResourceWrapperPassPass()
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
MapVector< CallInst *, dxil::ResourceInfo > DXILResourceMap
Definition: DXILResource.h:227
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69