LLVM 19.0.0git
DXILResource.cpp
Go to the documentation of this file.
1//===- DXILResource.cpp - DXIL Resource helper objects --------------------===//
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/// \file This file contains helper objects for working with DXIL Resources.
10///
11//===----------------------------------------------------------------------===//
12
13#include "DXILResource.h"
14#include "CBufferDataLayout.h"
16#include "llvm/IR/IRBuilder.h"
17#include "llvm/IR/Metadata.h"
18#include "llvm/IR/Module.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/Format.h"
21
22using namespace llvm;
23using namespace llvm::dxil;
24
25template <typename T> void ResourceTable<T>::collect(Module &M) {
26 NamedMDNode *Entry = M.getNamedMetadata(MDName);
27 if (!Entry || Entry->getNumOperands() == 0)
28 return;
29
30 uint32_t Counter = 0;
31 for (auto *Res : Entry->operands()) {
32 Data.push_back(T(Counter++, hlsl::FrontendResource(cast<MDNode>(Res))));
33 }
34}
35
37 NamedMDNode *Entry = M.getNamedMetadata(MDName);
38 if (!Entry || Entry->getNumOperands() == 0)
39 return;
40
41 uint32_t Counter = 0;
42 for (auto *Res : Entry->operands()) {
43 Data.push_back(
44 ConstantBuffer(Counter++, hlsl::FrontendResource(cast<MDNode>(Res))));
45 }
46 // FIXME: share CBufferDataLayout with CBuffer load lowering.
47 // See https://github.com/llvm/llvm-project/issues/58381
48 CBufferDataLayout CBDL(M.getDataLayout(), /*IsLegacy*/ true);
49 for (auto &CB : Data)
50 CB.setSize(CBDL);
51}
52
54 UAVs.collect(M);
55 CBuffers.collect(M);
56}
57
59 : ID(I), GV(R.getGlobalVariable()), Name(""), Space(R.getSpace()),
60 LowerBound(R.getResourceIndex()), RangeSize(1) {
61 if (auto *ArrTy = dyn_cast<ArrayType>(GV->getValueType()))
62 RangeSize = ArrTy->getNumElements();
63}
64
66 switch (ElTy) {
68 return "invalid";
69 case ElementType::I1:
70 return "i1";
72 return "i16";
74 return "u16";
76 return "i32";
78 return "u32";
80 return "i64";
82 return "u64";
84 return "f16";
86 return "f32";
88 return "f64";
90 return "snorm_f16";
92 return "unorm_f16";
94 return "snorm_f32";
96 return "unorm_f32";
98 return "snorm_f64";
100 return "unorm_f64";
102 return "p32i8";
104 return "p32u8";
106 llvm_unreachable("All ElementType enums are handled in switch");
108
110 unsigned Alignment, raw_ostream &OS) {
111 switch (Kind) {
112 default:
113 // TODO: add vector size.
114 OS << right_justify(getElementTypeName(ElTy), Alignment);
115 break;
117 OS << right_justify("byte", Alignment);
118 break;
120 OS << right_justify("struct", Alignment);
121 break;
124 OS << right_justify("NA", Alignment);
125 break;
128 break;
129 }
130}
131
133 switch (Kind) {
136 return "invalid";
138 return "1d";
140 return "2d";
142 return "2dMS";
144 return "3d";
146 return "cube";
148 return "1darray";
150 return "2darray";
152 return "2darrayMS";
154 return "cubearray";
156 return "buf";
158 return "rawbuf";
160 return "structbuf";
162 return "cbuffer";
164 return "sampler";
166 return "tbuffer";
168 return "ras";
170 return "fbtex2d";
172 return "fbtex2darray";
173 }
174 llvm_unreachable("All ResourceKind enums are handled in switch");
175}
176
177void ResourceBase::printKind(ResourceKind Kind, unsigned Alignment,
178 raw_ostream &OS, bool SRV, bool HasCounter,
179 uint32_t SampleCount) {
180 switch (Kind) {
181 default:
182 OS << right_justify(getKindName(Kind), Alignment);
183 break;
184
187 if (SRV)
188 OS << right_justify("r/o", Alignment);
189 else {
190 if (!HasCounter)
191 OS << right_justify("r/w", Alignment);
192 else
193 OS << right_justify("r/w+cnt", Alignment);
194 }
195 break;
197 OS << right_justify("buf", Alignment);
198 break;
201 std::string DimName = getKindName(Kind).str();
202 if (SampleCount)
203 DimName += std::to_string(SampleCount);
204 OS << right_justify(DimName, Alignment);
205 } break;
208 OS << right_justify("NA", Alignment);
209 break;
212 break;
213 }
214}
215
217 StringRef BindingPrefix) const {
218 std::string ResID = IDPrefix.str();
219 ResID += std::to_string(ID);
220 OS << right_justify(ResID, 8);
221
222 std::string Bind = BindingPrefix.str();
223 Bind += std::to_string(LowerBound);
224 if (Space)
225 Bind += ",space" + std::to_string(Space);
226
227 OS << right_justify(Bind, 15);
228 if (RangeSize != UINT_MAX)
229 OS << right_justify(std::to_string(RangeSize), 6) << "\n";
230 else
231 OS << right_justify("unbounded", 6) << "\n";
232}
233
235 OS << "; " << left_justify(Name, 31);
236
237 OS << right_justify("UAV", 10);
238
239 printElementType(Shape, ExtProps.ElementType.value_or(ElementType::Invalid),
240 8, OS);
241
242 // FIXME: support SampleCount.
243 // See https://github.com/llvm/llvm-project/issues/58175
244 printKind(Shape, 12, OS, /*SRV*/ false, HasCounter);
245 // Print the binding part.
246 ResourceBase::print(OS, "U", "u");
247}
248
250 : ResourceBase(I, R) {}
251
253 CBufferSizeInBytes = DL.getTypeAllocSizeInBytes(GV->getValueType());
254}
255
257 OS << "; " << left_justify(Name, 31);
258
259 OS << right_justify("cbuffer", 10);
260
262
263 printKind(ResourceKind::CBuffer, 12, OS, /*SRV*/ false, /*HasCounter*/ false);
264 // Print the binding part.
265 ResourceBase::print(OS, "CB", "cb");
266}
267
268template <typename T> void ResourceTable<T>::print(raw_ostream &OS) const {
269 for (auto &Res : Data)
270 Res.print(OS);
271}
272
274 IRBuilder<> B(Ctx);
276 if (ElementType) {
277 Entries.emplace_back(
279 Entries.emplace_back(ConstantAsMetadata::get(
280 B.getInt32(static_cast<uint32_t>(*ElementType))));
281 }
282 if (Entries.empty())
283 return nullptr;
284 return MDNode::get(Ctx, Entries);
285}
286
288 MutableArrayRef<Metadata *> Entries) const {
289 IRBuilder<> B(Ctx);
290 Entries[0] = ConstantAsMetadata::get(B.getInt32(ID));
291 Entries[1] = ConstantAsMetadata::get(GV);
292 Entries[2] = MDString::get(Ctx, Name);
293 Entries[3] = ConstantAsMetadata::get(B.getInt32(Space));
294 Entries[4] = ConstantAsMetadata::get(B.getInt32(LowerBound));
295 Entries[5] = ConstantAsMetadata::get(B.getInt32(RangeSize));
296}
297
299 auto &Ctx = GV->getContext();
300 IRBuilder<> B(Ctx);
301 Metadata *Entries[11];
302 ResourceBase::write(Ctx, Entries);
303 Entries[6] =
304 ConstantAsMetadata::get(B.getInt32(static_cast<uint32_t>(Shape)));
305 Entries[7] = ConstantAsMetadata::get(B.getInt1(GloballyCoherent));
306 Entries[8] = ConstantAsMetadata::get(B.getInt1(HasCounter));
307 Entries[9] = ConstantAsMetadata::get(B.getInt1(IsROV));
308 Entries[10] = ExtProps.write(Ctx);
309 return MDNode::get(Ctx, Entries);
310}
311
313 auto &Ctx = GV->getContext();
314 IRBuilder<> B(Ctx);
315 Metadata *Entries[7];
316 ResourceBase::write(Ctx, Entries);
317
318 Entries[6] = ConstantAsMetadata::get(B.getInt32(CBufferSizeInBytes));
319 return MDNode::get(Ctx, Entries);
320}
321
322template <typename T> MDNode *ResourceTable<T>::write(Module &M) const {
323 if (Data.empty())
324 return nullptr;
326 for (auto &Res : Data)
327 MDs.emplace_back(Res.write());
328
329 NamedMDNode *Entry = M.getNamedMetadata(MDName);
330 if (Entry)
331 Entry->eraseFromParent();
332
333 return MDNode::get(M.getContext(), MDs);
334}
335
336void Resources::write(Module &M) const {
337 Metadata *ResourceMDs[4] = {nullptr, nullptr, nullptr, nullptr};
338
339 ResourceMDs[1] = UAVs.write(M);
340
341 ResourceMDs[2] = CBuffers.write(M);
342
343 bool HasResource = ResourceMDs[0] != nullptr || ResourceMDs[1] != nullptr ||
344 ResourceMDs[2] != nullptr || ResourceMDs[3] != nullptr;
345
346 if (HasResource) {
347 NamedMDNode *DXResMD = M.getOrInsertNamedMetadata("dx.resources");
348 DXResMD->addOperand(MDNode::get(M.getContext(), ResourceMDs));
349 }
350
351 NamedMDNode *Entry = M.getNamedMetadata("hlsl.uavs");
352 if (Entry)
353 Entry->eraseFromParent();
354}
355
357 O << ";\n"
358 << "; Resource Bindings:\n"
359 << ";\n"
360 << "; Name Type Format Dim "
361 "ID HLSL Bind Count\n"
362 << "; ------------------------------ ---------- ------- ----------- "
363 "------- -------------- ------\n";
364
365 CBuffers.print(O);
366 UAVs.print(O);
367}
368
369void Resources::dump() const { print(dbgs()); }
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
raw_pwrite_stream & OS
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static GlobalVariable * getGlobalVariable(Module &M, Type *Ty, WebAssemblyTargetMachine &TM, const char *Name)
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:528
Type * getValueType() const
Definition: GlobalValue.h:296
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2649
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1067
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:597
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
A tuple of MDNodes.
Definition: Metadata.h:1729
void addOperand(MDNode *M)
Definition: Metadata.cpp:1385
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
ConstantBuffer(uint32_t I, hlsl::FrontendResource R)
void setSize(CBufferDataLayout &DL)
void print(raw_ostream &O) const
static StringRef getElementTypeName(dxil::ElementType CompType)
void print(raw_ostream &O, StringRef IDPrefix, StringRef BindingPrefix) const
ResourceBase(uint32_t I, hlsl::FrontendResource R)
GlobalVariable * GV
Definition: DXILResource.h:34
static void printElementType(dxil::ResourceKind Kind, dxil::ElementType CompType, unsigned Alignment, raw_ostream &OS)
static void printKind(dxil::ResourceKind Kind, unsigned Alignment, raw_ostream &OS, bool SRV=false, bool HasCounter=false, uint32_t SampleCount=0)
static StringRef getKindName(dxil::ResourceKind Kind)
void write(LLVMContext &Ctx, MutableArrayRef< Metadata * > Entries) const
void print(raw_ostream &O) const
MDNode * write(Module &M) const
LLVM_DUMP_METHOD void dump() const
void print(raw_ostream &O) const
void write(Module &M) const
void collect(Module &M)
MDNode * write() const
void print(raw_ostream &O) const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ResourceKind
The kind of resource for an SRV or UAV resource.
Definition: DXILABI.h:44
ElementType
The element type of an SRV or UAV resource.
Definition: DXILABI.h:68
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FormattedString right_justify(StringRef Str, unsigned Width)
right_justify - add spaces before string so total output is Width characters.
Definition: Format.h:153
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition: Format.h:146
MDNode * write(LLVMContext &Ctx) const
std::optional< dxil::ElementType > ElementType
Definition: DXILResource.h:56