Skip to content

Commit 58eec85

Browse files
authored
[DirectX] Move resource logic into PrettyPrinter and TranslateMetadata. NFC
Move the module level logic for resources into the pretty printer and translate metadata passes rather than embedding them in the DXILResource helper. This will make it easier to migrate towards the target extension type based approach to resources. Pull Request: #104446
1 parent c3776c1 commit 58eec85

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed

llvm/lib/Target/DirectX/DXILPrettyPrinter.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@
1212
#include "llvm/ADT/StringRef.h"
1313
#include "llvm/IR/PassManager.h"
1414
#include "llvm/Pass.h"
15+
#include "llvm/Support/FormatVariadic.h"
1516
#include "llvm/Support/raw_ostream.h"
1617

1718
using namespace llvm;
1819

1920
static void prettyPrintResources(raw_ostream &OS,
2021
const dxil::Resources &MDResources) {
21-
MDResources.print(OS);
22+
// Column widths are arbitrary but match the widths DXC uses.
23+
OS << ";\n; Resource Bindings:\n;\n";
24+
OS << formatv("; {0,-30} {1,10} {2,7} {3,11} {4,7} {5,14} {6,16}\n", "Name",
25+
"Type", "Format", "Dim", "ID", "HLSL Bind", "Count");
26+
OS << formatv(
27+
"; {0,-+30} {1,-+10} {2,-+7} {3,-+11} {4,-+7} {5,-+14} {6,-+16}\n", "",
28+
"", "", "", "", "", "");
29+
30+
if (MDResources.hasCBuffers())
31+
MDResources.printCBuffers(OS);
32+
if (MDResources.hasUAVs())
33+
MDResources.printUAVs(OS);
34+
35+
OS << ";\n";
2236
}
2337

2438
PreservedAnalyses DXILPrettyPrinterPass::run(Module &M,
@@ -63,7 +77,7 @@ INITIALIZE_PASS_END(DXILPrettyPrinterLegacy, "dxil-pretty-printer",
6377

6478
bool DXILPrettyPrinterLegacy::runOnModule(Module &M) {
6579
dxil::Resources &Res = getAnalysis<DXILResourceMDWrapper>().getDXILResource();
66-
Res.print(OS);
80+
prettyPrintResources(OS, Res);
6781
return false;
6882
}
6983

llvm/lib/Target/DirectX/DXILResource.cpp

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -333,37 +333,14 @@ template <typename T> MDNode *ResourceTable<T>::write(Module &M) const {
333333
return MDNode::get(M.getContext(), MDs);
334334
}
335335

336-
void 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();
336+
Metadata *Resources::writeUAVs(Module &M) const { return UAVs.write(M); }
337+
void Resources::printUAVs(raw_ostream &OS) const { UAVs.print(OS); }
338+
Metadata *Resources::writeCBuffers(Module &M) const {
339+
return CBuffers.write(M);
354340
}
341+
void Resources::printCBuffers(raw_ostream &OS) const { CBuffers.print(OS); }
355342

356-
void Resources::print(raw_ostream &O) const {
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);
343+
void Resources::dump() const {
344+
printCBuffers(dbgs());
345+
printUAVs(dbgs());
367346
}
368-
369-
void Resources::dump() const { print(dbgs()); }

llvm/lib/Target/DirectX/DXILResource.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ template <typename T> class ResourceTable {
103103
public:
104104
ResourceTable(StringRef Name) : MDName(Name) {}
105105
void collect(Module &M);
106+
bool empty() const { return Data.empty(); }
106107
MDNode *write(Module &M) const;
107108
void print(raw_ostream &O) const;
108109
};
@@ -117,8 +118,12 @@ class Resources {
117118

118119
public:
119120
void collect(Module &M);
120-
void write(Module &M) const;
121-
void print(raw_ostream &O) const;
121+
bool hasUAVs() const { return !UAVs.empty(); }
122+
Metadata *writeUAVs(Module &M) const;
123+
void printUAVs(raw_ostream &OS) const;
124+
bool hasCBuffers() const { return !CBuffers.empty(); }
125+
Metadata *writeCBuffers(Module &M) const;
126+
void printCBuffers(raw_ostream &OS) const;
122127
LLVM_DUMP_METHOD void dump() const;
123128
};
124129

llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@
2222
using namespace llvm;
2323
using namespace llvm::dxil;
2424

25+
static void emitResourceMetadata(Module &M,
26+
const dxil::Resources &MDResources) {
27+
Metadata *SRVMD = nullptr, *UAVMD = nullptr, *CBufMD = nullptr,
28+
*SmpMD = nullptr;
29+
bool HasResources = false;
30+
31+
if (MDResources.hasUAVs()) {
32+
UAVMD = MDResources.writeUAVs(M);
33+
HasResources = true;
34+
}
35+
36+
if (MDResources.hasCBuffers()) {
37+
CBufMD = MDResources.writeCBuffers(M);
38+
HasResources = true;
39+
}
40+
41+
if (!HasResources)
42+
return;
43+
44+
NamedMDNode *ResourceMD = M.getOrInsertNamedMetadata("dx.resources");
45+
ResourceMD->addOperand(
46+
MDNode::get(M.getContext(), {SRVMD, UAVMD, CBufMD, SmpMD}));
47+
}
48+
2549
static void translateMetadata(Module &M, const dxil::Resources &MDResources,
2650
const ComputedShaderFlags &ShaderFlags) {
2751
dxil::ValidatorVersionMD ValVerMD(M);
@@ -30,7 +54,7 @@ static void translateMetadata(Module &M, const dxil::Resources &MDResources,
3054
dxil::createShaderModelMD(M);
3155
dxil::createDXILVersionMD(M);
3256

33-
MDResources.write(M);
57+
emitResourceMetadata(M, MDResources);
3458

3559
dxil::createEntryMD(M, static_cast<uint64_t>(ShaderFlags));
3660
}

0 commit comments

Comments
 (0)