Skip to content

Commit 5c7ed7e

Browse files
author
joaosaffran
committed
filtering root signatures not associated with entry function
1 parent 9f8e512 commit 5c7ed7e

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
#include "DirectX.h"
1515
#include "llvm/ADT/StringSwitch.h"
1616
#include "llvm/ADT/Twine.h"
17+
#include "llvm/Analysis/DXILMetadataAnalysis.h"
1718
#include "llvm/IR/Constants.h"
19+
#include "llvm/IR/Function.h"
1820
#include "llvm/IR/Module.h"
19-
#include <cstdint>
21+
#include "llvm/InitializePasses.h"
22+
#include "llvm/Pass.h"
2023

2124
using namespace llvm;
2225
using namespace llvm::dxil;
@@ -80,7 +83,7 @@ static bool parseRootSignatureElement(ModuleRootSignature *MRS,
8083
return true;
8184
}
8285

83-
bool ModuleRootSignature::parse(NamedMDNode *Root) {
86+
bool ModuleRootSignature::parse(NamedMDNode *Root, const Function *EF) {
8487
bool HasError = false;
8588

8689
/** Root Signature are specified as following in the metadata:
@@ -96,11 +99,25 @@ bool ModuleRootSignature::parse(NamedMDNode *Root) {
9699
*/
97100

98101
for (const MDNode *Node : Root->operands()) {
99-
100102
if (Node->getNumOperands() != 2)
101103
return reportError("Invalid format for Root Signature Definition. Pairs "
102104
"of function, root signature expected.");
103105

106+
Metadata *MD = Node->getOperand(0).get();
107+
if (auto *VAM = llvm::dyn_cast<llvm::ValueAsMetadata>(MD)) {
108+
llvm::Value *V = VAM->getValue();
109+
if (Function *F = dyn_cast<Function>(V)) {
110+
if (F != EF)
111+
continue;
112+
} else {
113+
return reportError(
114+
"Root Signature MD node, first element is not a function.");
115+
}
116+
} else {
117+
return reportError(
118+
"Root Signature MD node, first element is not a function.");
119+
}
120+
104121
// Get the Root Signature Description from the function signature pair.
105122
MDNode *RS = dyn_cast<MDNode>(Node->getOperand(1).get());
106123

@@ -120,12 +137,13 @@ bool ModuleRootSignature::parse(NamedMDNode *Root) {
120137
return HasError;
121138
}
122139

123-
ModuleRootSignature ModuleRootSignature::analyzeModule(Module &M) {
140+
ModuleRootSignature ModuleRootSignature::analyzeModule(Module &M,
141+
const Function *F) {
124142
ModuleRootSignature MRS;
125143

126144
NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
127145
if (RootSignatureNode) {
128-
if (MRS.parse(RootSignatureNode))
146+
if (MRS.parse(RootSignatureNode, F))
129147
llvm_unreachable("Invalid Root Signature Metadata.");
130148
}
131149

@@ -136,22 +154,43 @@ AnalysisKey RootSignatureAnalysis::Key;
136154

137155
ModuleRootSignature RootSignatureAnalysis::run(Module &M,
138156
ModuleAnalysisManager &AM) {
139-
return ModuleRootSignature::analyzeModule(M);
157+
auto MMI = AM.getResult<DXILMetadataAnalysis>(M);
158+
159+
if (MMI.ShaderProfile == Triple::Library)
160+
return ModuleRootSignature();
161+
162+
assert(MMI.EntryPropertyVec.size() == 1);
163+
164+
const Function *EntryFunction = MMI.EntryPropertyVec[0].Entry;
165+
return ModuleRootSignature::analyzeModule(M, EntryFunction);
140166
}
141167

142168
//===----------------------------------------------------------------------===//
143169
bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {
144170

145-
this->MRS = MRS = ModuleRootSignature::analyzeModule(M);
171+
dxil::ModuleMetadataInfo &MMI =
172+
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
173+
174+
if (MMI.ShaderProfile == Triple::Library)
175+
return false;
176+
assert(MMI.EntryPropertyVec.size() == 1);
177+
178+
const Function *EntryFunction = MMI.EntryPropertyVec[0].Entry;
179+
this->MRS = MRS = ModuleRootSignature::analyzeModule(M, EntryFunction);
146180

147181
return false;
148182
}
149183

150184
void RootSignatureAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
151185
AU.setPreservesAll();
186+
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
152187
}
153188

154189
char RootSignatureAnalysisWrapper::ID = 0;
155190

156-
INITIALIZE_PASS(RootSignatureAnalysisWrapper, "dx-root-signature-analysis",
157-
"DXIL Root Signature Analysis", true, true)
191+
INITIALIZE_PASS_BEGIN(RootSignatureAnalysisWrapper,
192+
"dx-root-signature-analysis",
193+
"DXIL Root Signature Analysis", true, true)
194+
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
195+
INITIALIZE_PASS_END(RootSignatureAnalysisWrapper, "dx-root-signature-analysis",
196+
"DXIL Root Signature Analysis", true, true)

llvm/lib/Target/DirectX/DXILRootSignature.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ struct ModuleRootSignature {
3434

3535
ModuleRootSignature() = default;
3636

37-
bool parse(NamedMDNode *Root);
37+
bool parse(NamedMDNode *Root, const Function *F);
3838

39-
static ModuleRootSignature analyzeModule(Module &M);
39+
static ModuleRootSignature analyzeModule(Module &M, const Function *F);
4040
};
4141

4242
class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ target triple = "dxil-unknown-shadermodel6.0-compute"
55

66
; CHECK: @dx.rts0 = private constant [24 x i8] c"{{.*}}", section "RTS0", align 4
77

8-
98
define void @main() #0 {
109
entry:
1110
ret void

0 commit comments

Comments
 (0)