Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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 "DXILResourceAnalysis.h"
11#include "DirectX.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/Analysis/DXILResource.h"
14#include "llvm/IR/PassManager.h"
15#include "llvm/InitializePasses.h"
16#include "llvm/Pass.h"
17#include "llvm/Support/FormatAdapters.h"
18#include "llvm/Support/FormatVariadic.h"
19#include "llvm/Support/raw_ostream.h"
20
21using namespacellvm;
22
23staticStringRefgetRCName(dxil::ResourceClass RC) {
24switch (RC) {
25case dxil::ResourceClass::SRV:
26return"SRV";
27case dxil::ResourceClass::UAV:
28return"UAV";
29case dxil::ResourceClass::CBuffer:
30return"cbuffer";
31case dxil::ResourceClass::Sampler:
32return"sampler";
33 }
34llvm_unreachable("covered switch");
35}
36
37staticStringRefgetRCPrefix(dxil::ResourceClass RC) {
38switch (RC) {
39case dxil::ResourceClass::SRV:
40return"t";
41case dxil::ResourceClass::UAV:
42return"u";
43case dxil::ResourceClass::CBuffer:
44return"cb";
45case dxil::ResourceClass::Sampler:
46return"s";
47 }
48llvm_unreachable("covered switch");
49}
50
51staticStringRefgetFormatName(constdxil::ResourceTypeInfo &RI) {
52if (RI.isTyped()) {
53switch (RI.getTyped().ElementTy) {
54case dxil::ElementType::I1:
55return"i1";
56case dxil::ElementType::I16:
57return"i16";
58case dxil::ElementType::U16:
59return"u16";
60case dxil::ElementType::I32:
61return"i32";
62case dxil::ElementType::U32:
63return"u32";
64case dxil::ElementType::I64:
65return"i64";
66case dxil::ElementType::U64:
67return"u64";
68case dxil::ElementType::F16:
69return"f16";
70case dxil::ElementType::F32:
71return"f32";
72case dxil::ElementType::F64:
73return"f64";
74case dxil::ElementType::SNormF16:
75return"snorm_f16";
76case dxil::ElementType::UNormF16:
77return"unorm_f16";
78case dxil::ElementType::SNormF32:
79return"snorm_f32";
80case dxil::ElementType::UNormF32:
81return"unorm_f32";
82case dxil::ElementType::SNormF64:
83return"snorm_f64";
84case dxil::ElementType::UNormF64:
85return"unorm_f64";
86case dxil::ElementType::PackedS8x32:
87return"p32i8";
88case dxil::ElementType::PackedU8x32:
89return"p32u8";
90case dxil::ElementType::Invalid:
91llvm_unreachable("Invalid ElementType");
92 }
93llvm_unreachable("Unhandled ElementType");
94 }elseif (RI.isStruct())
95return"struct";
96elseif (RI.isCBuffer() || RI.isSampler())
97return"NA";
98return"byte";
99}
100
101staticStringRefgetTextureDimName(dxil::ResourceKind RK) {
102switch (RK) {
103case dxil::ResourceKind::Texture1D:
104return"1d";
105case dxil::ResourceKind::Texture2D:
106return"2d";
107case dxil::ResourceKind::Texture3D:
108return"3d";
109case dxil::ResourceKind::TextureCube:
110return"cube";
111case dxil::ResourceKind::Texture1DArray:
112return"1darray";
113case dxil::ResourceKind::Texture2DArray:
114return"2darray";
115case dxil::ResourceKind::TextureCubeArray:
116return"cubearray";
117case dxil::ResourceKind::TBuffer:
118return"tbuffer";
119case dxil::ResourceKind::FeedbackTexture2D:
120return"fbtex2d";
121case dxil::ResourceKind::FeedbackTexture2DArray:
122return"fbtex2darray";
123case dxil::ResourceKind::Texture2DMS:
124return"2dMS";
125case dxil::ResourceKind::Texture2DMSArray:
126return"2darrayMS";
127case dxil::ResourceKind::Invalid:
128case dxil::ResourceKind::NumEntries:
129case dxil::ResourceKind::CBuffer:
130case dxil::ResourceKind::RawBuffer:
131case dxil::ResourceKind::Sampler:
132case dxil::ResourceKind::StructuredBuffer:
133case dxil::ResourceKind::TypedBuffer:
134case dxil::ResourceKind::RTAccelerationStructure:
135llvm_unreachable("Invalid ResourceKind for texture");
136 }
137llvm_unreachable("Unhandled ResourceKind");
138}
139
140namespace{
141structFormatResourceDimension
142 :publicllvm::FormatAdapter<const dxil::ResourceTypeInfo &> {
143explicit FormatResourceDimension(constdxil::ResourceTypeInfo &RI)
144 :llvm::FormatAdapter<const dxil::ResourceTypeInfo &>(RI) {}
145
146voidformat(llvm::raw_ostream &OS,StringRef Style) override{
147dxil::ResourceKind RK = Item.getResourceKind();
148switch (RK) {
149default: {
150OS <<getTextureDimName(RK);
151if (Item.isMultiSample())
152OS << Item.getMultiSampleCount();
153break;
154 }
155casedxil::ResourceKind::RawBuffer:
156casedxil::ResourceKind::StructuredBuffer:
157if (!Item.isUAV())
158OS <<"r/o";
159elseif (Item.getUAV().HasCounter)
160OS <<"r/w+cnt";
161else
162OS <<"r/w";
163break;
164casedxil::ResourceKind::TypedBuffer:
165OS <<"buf";
166break;
167casedxil::ResourceKind::RTAccelerationStructure:
168// TODO: dxc would print "ras" here. Can/should this happen?
169llvm_unreachable("RTAccelerationStructure printing is not implemented");
170 }
171 }
172};
173
174structFormatBindingID
175 :publicllvm::FormatAdapter<const dxil::ResourceBindingInfo &> {
176dxil::ResourceClass RC;
177
178explicit FormatBindingID(constdxil::ResourceBindingInfo &RBI,
179constdxil::ResourceTypeInfo &RTI)
180 :llvm::FormatAdapter<const dxil::ResourceBindingInfo &>(RBI),
181 RC(RTI.getResourceClass()) {}
182
183voidformat(llvm::raw_ostream &OS,StringRef Style) override{
184OS <<getRCPrefix(RC).upper() << Item.getBinding().RecordID;
185 }
186};
187
188structFormatBindingLocation
189 :publicllvm::FormatAdapter<const dxil::ResourceBindingInfo &> {
190dxil::ResourceClass RC;
191
192explicit FormatBindingLocation(constdxil::ResourceBindingInfo &RBI,
193constdxil::ResourceTypeInfo &RTI)
194 :llvm::FormatAdapter<const dxil::ResourceBindingInfo &>(RBI),
195 RC(RTI.getResourceClass()) {}
196
197voidformat(llvm::raw_ostream &OS,StringRef Style) override{
198constauto &Binding = Item.getBinding();
199OS <<getRCPrefix(RC) << Binding.LowerBound;
200if (Binding.Space)
201OS <<",space" << Binding.Space;
202 }
203};
204
205structFormatBindingSize
206 :publicllvm::FormatAdapter<const dxil::ResourceBindingInfo &> {
207explicit FormatBindingSize(constdxil::ResourceBindingInfo &RI)
208 :llvm::FormatAdapter<const dxil::ResourceBindingInfo &>(RI) {}
209
210voidformat(llvm::raw_ostream &OS,StringRef Style) override{
211uint32_tSize = Item.getBinding().Size;
212if (Size == std::numeric_limits<uint32_t>::max())
213OS <<"unbounded";
214else
215OS <<Size;
216 }
217};
218
219}// namespace
220
221staticvoidprettyPrintResources(raw_ostream &OS,constDXILBindingMap &DBM,
222DXILResourceTypeMap &DRTM,
223constdxil::Resources &MDResources) {
224// Column widths are arbitrary but match the widths DXC uses.
225OS <<";\n; Resource Bindings:\n;\n";
226OS <<formatv("; {0,-30} {1,10} {2,7} {3,11} {4,7} {5,14} {6,9}\n","Name",
227"Type","Format","Dim","ID","HLSL Bind","Count");
228OS <<formatv(
229"; {0,-+30} {1,-+10} {2,-+7} {3,-+11} {4,-+7} {5,-+14} {6,-+9}\n","","",
230"","","","","");
231
232// TODO: Do we want to sort these by binding or something like that?
233for (constdxil::ResourceBindingInfo &RBI : DBM) {
234constdxil::ResourceTypeInfo &RTI = DRTM[RBI.getHandleTy()];
235
236dxil::ResourceClass RC = RTI.getResourceClass();
237assert((RC != dxil::ResourceClass::CBuffer || !MDResources.hasCBuffers()) &&
238"Old and new cbuffer representations can't coexist");
239assert((RC != dxil::ResourceClass::UAV || !MDResources.hasUAVs()) &&
240"Old and new UAV representations can't coexist");
241
242StringRefName(RBI.getName());
243StringRefType(getRCName(RC));
244StringRef Format(getFormatName(RTI));
245 FormatResourceDimension Dim(RTI);
246 FormatBindingIDID(RBI, RTI);
247 FormatBindingLocation Bind(RBI, RTI);
248 FormatBindingSize Count(RBI);
249OS <<formatv("; {0,-30} {1,10} {2,7} {3,11} {4,7} {5,14} {6,9}\n",Name,
250Type, Format, Dim,ID, Bind, Count);
251 }
252
253if (MDResources.hasCBuffers())
254 MDResources.printCBuffers(OS);
255if (MDResources.hasUAVs())
256 MDResources.printUAVs(OS);
257
258OS <<";\n";
259}
260
261PreservedAnalysesDXILPrettyPrinterPass::run(Module &M,
262ModuleAnalysisManager &MAM) {
263constDXILBindingMap &DBM =MAM.getResult<DXILResourceBindingAnalysis>(M);
264DXILResourceTypeMap &DRTM =MAM.getResult<DXILResourceTypeAnalysis>(M);
265constdxil::Resources &MDResources =MAM.getResult<DXILResourceMDAnalysis>(M);
266prettyPrintResources(OS, DBM, DRTM, MDResources);
267returnPreservedAnalyses::all();
268}
269
270namespace{
271classDXILPrettyPrinterLegacy :publicllvm::ModulePass {
272raw_ostream &OS;// raw_ostream to print to.
273
274public:
275staticcharID;
276 DXILPrettyPrinterLegacy() :ModulePass(ID),OS(dbgs()) {
277initializeDXILPrettyPrinterLegacyPass(*PassRegistry::getPassRegistry());
278 }
279
280explicit DXILPrettyPrinterLegacy(raw_ostream &O) :ModulePass(ID),OS(O) {
281initializeDXILPrettyPrinterLegacyPass(*PassRegistry::getPassRegistry());
282 }
283
284StringRef getPassName() const override{
285return"DXIL Metadata Pretty Printer";
286 }
287
288bool runOnModule(Module &M)override;
289void getAnalysisUsage(AnalysisUsage &AU) const override{
290 AU.setPreservesAll();
291 AU.addRequired<DXILResourceTypeWrapperPass>();
292 AU.addRequired<DXILResourceBindingWrapperPass>();
293 AU.addRequired<DXILResourceMDWrapper>();
294 }
295};
296}// namespace
297
298char DXILPrettyPrinterLegacy::ID = 0;
299INITIALIZE_PASS_BEGIN(DXILPrettyPrinterLegacy,"dxil-pretty-printer",
300"DXIL Metadata Pretty Printer",true,true)
301INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
302INITIALIZE_PASS_DEPENDENCY(DXILResourceBindingWrapperPass)
303INITIALIZE_PASS_DEPENDENCY(DXILResourceMDWrapper)
304INITIALIZE_PASS_END(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
305 "DXILMetadata PrettyPrinter",true,true)
306
307bool DXILPrettyPrinterLegacy::runOnModule(Module &M) {
308constDXILBindingMap &DBM =
309 getAnalysis<DXILResourceBindingWrapperPass>().getBindingMap();
310DXILResourceTypeMap &DRTM =
311 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
312dxil::Resources &Res = getAnalysis<DXILResourceMDWrapper>().getDXILResource();
313prettyPrintResources(OS, DBM, DRTM, Res);
314returnfalse;
315}
316
317ModulePass *llvm::createDXILPrettyPrinterLegacyPass(raw_ostream &OS) {
318returnnew DXILPrettyPrinterLegacy(OS);
319}
const
aarch64 promote const
Definition:AArch64PromoteConstant.cpp:230
true
basic Basic Alias true
Definition:BasicAliasAnalysis.cpp:1981
prettyPrintResources
static void prettyPrintResources(raw_ostream &OS, const DXILBindingMap &DBM, DXILResourceTypeMap &DRTM, const dxil::Resources &MDResources)
Definition:DXILPrettyPrinter.cpp:221
getTextureDimName
static StringRef getTextureDimName(dxil::ResourceKind RK)
Definition:DXILPrettyPrinter.cpp:101
getRCPrefix
static StringRef getRCPrefix(dxil::ResourceClass RC)
Definition:DXILPrettyPrinter.cpp:37
getFormatName
static StringRef getFormatName(const dxil::ResourceTypeInfo &RI)
Definition:DXILPrettyPrinter.cpp:51
printer
dxil pretty printer
Definition:DXILPrettyPrinter.cpp:304
Printer
dxil pretty DXIL Metadata Pretty Printer
Definition:DXILPrettyPrinter.cpp:305
getRCName
static StringRef getRCName(dxil::ResourceClass RC)
Definition:DXILPrettyPrinter.cpp:23
DXILPrettyPrinter.h
DXILResourceAnalysis.h
DirectX.h
Name
std::string Name
Definition:ELFObjHandler.cpp:77
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
FormatAdapters.h
FormatVariadic.h
PassManager.h
This header defines various interfaces for pass management in LLVM.
InitializePasses.h
MAM
ModuleAnalysisManager MAM
Definition:PassBuilderBindings.cpp:63
INITIALIZE_PASS_DEPENDENCY
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition:PassSupport.h:55
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:57
INITIALIZE_PASS_BEGIN
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:52
Pass.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
StringRef.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition:PassManager.h:253
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition:PassManager.h:410
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition:PassAnalysisSupport.h:75
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition:PassAnalysisSupport.h:130
llvm::DXILBindingMap
Definition:DXILResource.h:423
llvm::DXILPrettyPrinterPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Definition:DXILPrettyPrinter.cpp:261
llvm::DXILResourceBindingAnalysis
Definition:DXILResource.h:502
llvm::DXILResourceBindingWrapperPass
Definition:DXILResource.h:527
llvm::DXILResourceMDAnalysis
Analysis pass that exposes the DXILResource for a module.
Definition:DXILResourceAnalysis.h:24
llvm::DXILResourceMDWrapper
The legacy pass manager's analysis pass to compute DXIL resource information.
Definition:DXILResourceAnalysis.h:35
llvm::DXILResourceTypeAnalysis
Definition:DXILResource.h:391
llvm::DXILResourceTypeMap
Definition:DXILResource.h:374
llvm::DXILResourceTypeWrapperPass
Definition:DXILResource.h:406
llvm::FormatAdapter
Definition:FormatAdapters.h:20
llvm::Metadata
Root of the metadata hierarchy.
Definition:Metadata.h:62
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition:Pass.h:251
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition:PassRegistry.cpp:24
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition:Analysis.h:111
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition:Analysis.h:117
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringRef::upper
std::string upper() const
Convert the given ASCII string to uppercase.
Definition:StringRef.cpp:118
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::dxil::ResourceBindingInfo
Definition:DXILResource.h:309
llvm::dxil::ResourceTypeInfo
Definition:DXILResource.h:202
llvm::dxil::ResourceTypeInfo::getResourceClass
dxil::ResourceClass getResourceClass() const
Definition:DXILResource.h:294
llvm::dxil::ResourceTypeInfo::isSampler
bool isSampler() const
Definition:DXILResource.cpp:300
llvm::dxil::ResourceTypeInfo::isTyped
bool isTyped() const
Definition:DXILResource.cpp:308
llvm::dxil::ResourceTypeInfo::isCBuffer
bool isCBuffer() const
Definition:DXILResource.cpp:296
llvm::dxil::ResourceTypeInfo::getTyped
TypedInfo getTyped() const
Definition:DXILResource.cpp:444
llvm::dxil::ResourceTypeInfo::isStruct
bool isStruct() const
Definition:DXILResource.cpp:304
llvm::dxil::Resources
Definition:DXILResource.h:115
llvm::dxil::Resources::hasUAVs
bool hasUAVs() const
Definition:DXILResource.h:121
llvm::dxil::Resources::printUAVs
void printUAVs(raw_ostream &OS) const
Definition:DXILResource.cpp:337
llvm::dxil::Resources::printCBuffers
void printCBuffers(raw_ostream &OS) const
Definition:DXILResource.cpp:341
llvm::dxil::Resources::hasCBuffers
bool hasCBuffers() const
Definition:DXILResource.h:124
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::support::detail::format_adapter::format
virtual void format(raw_ostream &S, StringRef Options)=0
uint32_t
unsigned
DXILResource.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition:CallingConv.h:24
llvm::dxil::ResourceKind
ResourceKind
The kind of resource for an SRV or UAV resource.
Definition:DXILABI.h:34
llvm::dxil::ResourceKind::TypedBuffer
@ TypedBuffer
llvm::dxil::ResourceKind::RTAccelerationStructure
@ RTAccelerationStructure
llvm::dxil::ResourceKind::StructuredBuffer
@ StructuredBuffer
llvm::dxil::ResourceKind::RawBuffer
@ RawBuffer
llvm::dxil::ResourceClass
ResourceClass
Definition:DXILABI.h:25
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::initializeDXILPrettyPrinterLegacyPass
void initializeDXILPrettyPrinterLegacyPass(PassRegistry &)
Initializer for DXILPrettyPrinter.
llvm::formatv
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
Definition:FormatVariadic.h:252
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::createDXILPrettyPrinterLegacyPass
ModulePass * createDXILPrettyPrinterLegacyPass(raw_ostream &OS)
Pass to pretty print DXIL metadata.
Definition:DXILPrettyPrinter.cpp:317
raw_ostream.h
llvm::dxil::ResourceTypeInfo::TypedInfo::ElementTy
dxil::ElementType ElementTy
Definition:DXILResource.h:238

Generated on Thu Jul 17 2025 14:43:50 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp