1010//
1111// ===----------------------------------------------------------------------===//
1212
13+ #include " DXILRootSignature.h"
1314#include " DXILShaderFlags.h"
1415#include " DirectX.h"
1516#include " llvm/ADT/SmallVector.h"
2526#include " llvm/MC/DXContainerPSVInfo.h"
2627#include " llvm/Pass.h"
2728#include " llvm/Support/MD5.h"
29+ #include " llvm/TargetParser/Triple.h"
2830#include " llvm/Transforms/Utils/ModuleUtils.h"
31+ #include < optional>
2932
3033using namespace llvm ;
3134using namespace llvm ::dxil;
@@ -41,6 +44,7 @@ class DXContainerGlobals : public llvm::ModulePass {
4144 GlobalVariable *buildSignature (Module &M, Signature &Sig, StringRef Name,
4245 StringRef SectionName);
4346 void addSignature (Module &M, SmallVector<GlobalValue *> &Globals);
47+ void addRootSignature (Module &M, SmallVector<GlobalValue *> &Globals);
4448 void addResourcesForPSV (Module &M, PSVRuntimeInfo &PSV);
4549 void addPipelineStateValidationInfo (Module &M,
4650 SmallVector<GlobalValue *> &Globals);
@@ -60,6 +64,7 @@ class DXContainerGlobals : public llvm::ModulePass {
6064 void getAnalysisUsage (AnalysisUsage &AU) const override {
6165 AU.setPreservesAll ();
6266 AU.addRequired <ShaderFlagsAnalysisWrapper>();
67+ AU.addRequired <RootSignatureAnalysisWrapper>();
6368 AU.addRequired <DXILMetadataAnalysisWrapperPass>();
6469 AU.addRequired <DXILResourceTypeWrapperPass>();
6570 AU.addRequired <DXILResourceBindingWrapperPass>();
@@ -73,6 +78,7 @@ bool DXContainerGlobals::runOnModule(Module &M) {
7378 Globals.push_back (getFeatureFlags (M));
7479 Globals.push_back (computeShaderHash (M));
7580 addSignature (M, Globals);
81+ addRootSignature (M, Globals);
7682 addPipelineStateValidationInfo (M, Globals);
7783 appendToCompilerUsed (M, Globals);
7884 return true ;
@@ -144,6 +150,36 @@ void DXContainerGlobals::addSignature(Module &M,
144150 Globals.emplace_back (buildSignature (M, OutputSig, " dx.osg1" , " OSG1" ));
145151}
146152
153+ void DXContainerGlobals::addRootSignature (Module &M,
154+ SmallVector<GlobalValue *> &Globals) {
155+
156+ dxil::ModuleMetadataInfo &MMI =
157+ getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata ();
158+
159+ // Root Signature in Library don't compile to DXContainer.
160+ if (MMI.ShaderProfile == llvm::Triple::Library)
161+ return ;
162+
163+ assert (MMI.EntryPropertyVec .size () == 1 );
164+
165+ auto &RSA = getAnalysis<RootSignatureAnalysisWrapper>();
166+ const Function *EntryFunction = MMI.EntryPropertyVec [0 ].Entry ;
167+ const auto &FuncRs = RSA.find (EntryFunction);
168+
169+ if (FuncRs == RSA.end ())
170+ return ;
171+
172+ const RootSignatureDesc &RS = FuncRs->second ;
173+ SmallString<256 > Data;
174+ raw_svector_ostream OS (Data);
175+
176+ RS.write (OS);
177+
178+ Constant *Constant =
179+ ConstantDataArray::getString (M.getContext (), Data, /* AddNull*/ false );
180+ Globals.emplace_back (buildContainerGlobal (M, Constant, " dx.rts0" , " RTS0" ));
181+ }
182+
147183void DXContainerGlobals::addResourcesForPSV (Module &M, PSVRuntimeInfo &PSV) {
148184 const DXILBindingMap &DBM =
149185 getAnalysis<DXILResourceBindingWrapperPass>().getBindingMap ();
0 commit comments