LLVM 22.0.0git
DXILPrettyPrinter.cpp
Go to the documentation of this file.
1//===- DXILPrettyPrinter.cpp - Print resources for textual DXIL -----------===//
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#include "DXILPrettyPrinter.h"
10#include "DirectX.h"
11#include "llvm/ADT/StringRef.h"
13#include "llvm/IR/PassManager.h"
15#include "llvm/Pass.h"
19
20using namespace llvm;
21
23 switch (RC) {
25 return "texture";
27 return "UAV";
29 return "cbuffer";
31 return "sampler";
32 }
33 llvm_unreachable("covered switch");
34}
35
37 switch (RC) {
39 return "t";
41 return "u";
43 return "cb";
45 return "s";
46 }
47 llvm_unreachable("covered switch");
48}
49
51 if (RI.isTyped()) {
52 switch (RI.getTyped().ElementTy) {
54 return "i1";
56 return "i16";
58 return "u16";
60 return "i32";
62 return "u32";
64 return "i64";
66 return "u64";
68 return "f16";
70 return "f32";
72 return "f64";
74 return "snorm_f16";
76 return "unorm_f16";
78 return "snorm_f32";
80 return "unorm_f32";
82 return "snorm_f64";
84 return "unorm_f64";
86 return "p32i8";
88 return "p32u8";
90 llvm_unreachable("Invalid ElementType");
91 }
92 llvm_unreachable("Unhandled ElementType");
93 } else if (RI.isStruct())
94 return "struct";
95 else if (RI.isCBuffer() || RI.isSampler())
96 return "NA";
97 return "byte";
98}
99
101 switch (RK) {
103 return "1d";
105 return "2d";
107 return "3d";
109 return "cube";
111 return "1darray";
113 return "2darray";
115 return "cubearray";
117 return "tbuffer";
119 return "fbtex2d";
121 return "fbtex2darray";
123 return "2dMS";
125 return "2darrayMS";
134 llvm_unreachable("Invalid ResourceKind for texture");
135 }
136 llvm_unreachable("Unhandled ResourceKind");
137}
138
139namespace {
140struct FormatResourceDimension
141 : public llvm::FormatAdapter<const dxil::ResourceTypeInfo &> {
142 FormatResourceDimension(const dxil::ResourceTypeInfo &RI, bool HasCounter)
143 : llvm::FormatAdapter<const dxil::ResourceTypeInfo &>(RI),
144 HasCounter(HasCounter) {}
145
146 bool HasCounter;
147
148 void format(llvm::raw_ostream &OS, StringRef Style) override {
149 dxil::ResourceKind RK = Item.getResourceKind();
150 switch (RK) {
151 default: {
152 OS << getTextureDimName(RK);
153 if (Item.isMultiSample())
154 OS << Item.getMultiSampleCount();
155 break;
156 }
157 case dxil::ResourceKind::RawBuffer:
158 case dxil::ResourceKind::StructuredBuffer:
159 if (!Item.isUAV())
160 OS << "r/o";
161 else if (HasCounter)
162 OS << "r/w+cnt";
163 else
164 OS << "r/w";
165 break;
166 case dxil::ResourceKind::TypedBuffer:
167 OS << "buf";
168 break;
169 case dxil::ResourceKind::CBuffer:
170 OS << "NA";
171 break;
172 case dxil::ResourceKind::RTAccelerationStructure:
173 // TODO: dxc would print "ras" here. Can/should this happen?
174 llvm_unreachable("RTAccelerationStructure printing is not implemented");
175 }
176 }
177};
178
179struct FormatBindingID
180 : public llvm::FormatAdapter<const dxil::ResourceInfo &> {
182
183 explicit FormatBindingID(const dxil::ResourceInfo &RI,
184 const dxil::ResourceTypeInfo &RTI)
185 : llvm::FormatAdapter<const dxil::ResourceInfo &>(RI),
186 RC(RTI.getResourceClass()) {}
187
188 void format(llvm::raw_ostream &OS, StringRef Style) override {
189 OS << getRCPrefix(RC).upper() << Item.getBinding().RecordID;
190 }
191};
192
193struct FormatBindingLocation
194 : public llvm::FormatAdapter<const dxil::ResourceInfo &> {
196
197 explicit FormatBindingLocation(const dxil::ResourceInfo &RI,
198 const dxil::ResourceTypeInfo &RTI)
199 : llvm::FormatAdapter<const dxil::ResourceInfo &>(RI),
200 RC(RTI.getResourceClass()) {}
201
202 void format(llvm::raw_ostream &OS, StringRef Style) override {
203 const auto &Binding = Item.getBinding();
204 OS << getRCPrefix(RC) << Binding.LowerBound;
205 if (Binding.Space)
206 OS << ",space" << Binding.Space;
207 }
208};
209
210struct FormatBindingSize
211 : public llvm::FormatAdapter<const dxil::ResourceInfo &> {
212 explicit FormatBindingSize(const dxil::ResourceInfo &RI)
213 : llvm::FormatAdapter<const dxil::ResourceInfo &>(RI) {}
214
215 void format(llvm::raw_ostream &OS, StringRef Style) override {
216 uint32_t Size = Item.getBinding().Size;
217 if (Size == std::numeric_limits<uint32_t>::max())
218 OS << "unbounded";
219 else
220 OS << Size;
221 }
222};
223
224} // namespace
225
227 DXILResourceTypeMap &DRTM) {
228 // Column widths are arbitrary but match the widths DXC uses.
229 OS << ";\n; Resource Bindings:\n;\n";
230 OS << formatv("; {0,-30} {1,10} {2,7} {3,11} {4,7} {5,14} {6,9}\n", "Name",
231 "Type", "Format", "Dim", "ID", "HLSL Bind", "Count");
232 OS << formatv(
233 "; {0,-+30} {1,-+10} {2,-+7} {3,-+11} {4,-+7} {5,-+14} {6,-+9}\n", "", "",
234 "", "", "", "", "");
235
236 // TODO: Do we want to sort these by binding or something like that?
237 for (const dxil::ResourceInfo &RI : DRM) {
238 const dxil::ResourceTypeInfo &RTI = DRTM[RI.getHandleTy()];
239
241 StringRef Name(RI.getName());
244 FormatResourceDimension Dim(RTI, RI.hasCounter());
245 FormatBindingID ID(RI, RTI);
246 FormatBindingLocation Bind(RI, RTI);
247 FormatBindingSize Count(RI);
248 OS << formatv("; {0,-30} {1,10} {2,7} {3,11} {4,7} {5,14} {6,9}\n", Name,
249 Type, Format, Dim, ID, Bind, Count);
250 }
251 OS << ";\n";
252}
253
261
262namespace {
263class DXILPrettyPrinterLegacy : public llvm::ModulePass {
264 raw_ostream &OS; // raw_ostream to print to.
265
266public:
267 static char ID;
268
269 explicit DXILPrettyPrinterLegacy(raw_ostream &O) : ModulePass(ID), OS(O) {}
270
271 StringRef getPassName() const override {
272 return "DXIL Metadata Pretty Printer";
273 }
274
275 bool runOnModule(Module &M) override;
276 void getAnalysisUsage(AnalysisUsage &AU) const override {
277 AU.setPreservesAll();
278 AU.addRequired<DXILResourceTypeWrapperPass>();
279 AU.addRequired<DXILResourceWrapperPass>();
280 }
281};
282} // namespace
283
284char DXILPrettyPrinterLegacy::ID = 0;
285INITIALIZE_PASS_BEGIN(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
286 "DXIL Metadata Pretty Printer", true, true)
289INITIALIZE_PASS_END(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
290 "DXIL Metadata Pretty Printer", true, true)
291
292bool DXILPrettyPrinterLegacy::runOnModule(Module &M) {
293 const DXILResourceMap &DRM =
294 getAnalysis<DXILResourceWrapperPass>().getResourceMap();
295 DXILResourceTypeMap &DRTM =
296 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
297 prettyPrintResources(OS, DRM, DRTM);
298 return false;
299}
300
302 return new DXILPrettyPrinterLegacy(OS);
303}
aarch64 promote const
static StringRef getTextureDimName(dxil::ResourceKind RK)
static void prettyPrintResources(raw_ostream &OS, const DXILResourceMap &DRM, DXILResourceTypeMap &DRTM)
static StringRef getRCPrefix(dxil::ResourceClass RC)
static StringRef getFormatName(const dxil::ResourceTypeInfo &RI)
static StringRef getRCName(dxil::ResourceClass RC)
DXIL Resource Implicit Binding
This header defines various interfaces for pass management in LLVM.
Machine Check Debug Module
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
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM_ABI std::string upper() const
Convert the given ASCII string to uppercase.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
dxil::ResourceClass getResourceClass() const
LLVM_ABI bool isSampler() const
LLVM_ABI bool isTyped() const
LLVM_ABI bool isCBuffer() const
LLVM_ABI TypedInfo getTyped() const
LLVM_ABI bool isStruct() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#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
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.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:126
ModulePass * createDXILPrettyPrinterLegacyPass(raw_ostream &OS)
Pass to pretty print DXIL metadata.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39