瀏覽代碼

Move Signature metadata to named node and some other cleanup.

Tex Riddell 8 年之前
父節點
當前提交
0647154af0

+ 5 - 2
include/dxc/HLSL/DxilMetadataHelper.h

@@ -72,6 +72,9 @@ public:
   // Entry points.
   static const char kDxilEntryPointsMDName[];
 
+  // Root Signature, for intermediate use, not valid in final DXIL module.
+  static const char kDxilRootSignatureMDName[];
+
   static const unsigned kDxilEntryPointNumFields  = 5;
   static const unsigned kDxilEntryPointFunction   = 0;  // Entry point function symbol.
   static const unsigned kDxilEntryPointName       = 1;  // Entry point unmangled name.
@@ -261,11 +264,11 @@ public:
   void LoadDxilSignatures(const llvm::MDOperand &MDO, DxilSignature &InputSig, 
                           DxilSignature &OutputSig, DxilSignature &PCSig);
   llvm::MDTuple *EmitSignatureMetadata(const DxilSignature &Sig);
-  llvm::Metadata *EmitRootSignature(RootSignatureHandle &RootSig);
+  void EmitRootSignature(RootSignatureHandle &RootSig);
   void LoadSignatureMetadata(const llvm::MDOperand &MDO, DxilSignature &Sig);
   llvm::MDTuple *EmitSignatureElement(const DxilSignatureElement &SE);
   void LoadSignatureElement(const llvm::MDOperand &MDO, DxilSignatureElement &SE);
-  void LoadRootSignature(const llvm::MDOperand &MDO, RootSignatureHandle &RootSig);
+  void LoadRootSignature(RootSignatureHandle &RootSig);
 
   // Resources.
   llvm::MDTuple *EmitDxilResourceTuple(llvm::MDTuple *pSRVs, llvm::MDTuple *pUAVs, 

+ 1 - 1
include/dxc/HLSL/DxilModule.h

@@ -287,7 +287,7 @@ private:
   // DXIL metadata serialization/deserialization.
   llvm::MDTuple *EmitDxilResources();
   void LoadDxilResources(const llvm::MDOperand &MDO);
-  llvm::MDTuple *EmitDxilShaderProperties(bool bStripRootSignature);
+  llvm::MDTuple *EmitDxilShaderProperties();
   void LoadDxilShaderProperties(const llvm::MDOperand &MDO);
 
   // Helpers.

+ 7 - 7
include/dxc/HLSL/DxilValidation.h

@@ -236,24 +236,24 @@ HRESULT ValidateDxilModule(_In_ llvm::Module *pModule,
 
 bool VerifySignatureMatches(_In_ llvm::Module *pModule,
                             hlsl::DXIL::SignatureKind SigKind,
-                            _In_reads_bytes_(SigSize) void *pSigData,
-                            _In_ unsigned SigSize);
+                            _In_reads_bytes_(SigSize) const void *pSigData,
+                            _In_ uint32_t SigSize);
 
 // PSV = data for Pipeline State Validation
 bool VerifyPSVMatches(_In_ llvm::Module *pModule,
-                      _In_reads_bytes_(PSVSize) void *pPSVData,
-                      _In_ unsigned PSVSize);
+                      _In_reads_bytes_(PSVSize) const void *pPSVData,
+                      _In_ uint32_t PSVSize);
 
 bool VerifyFeatureInfoMatches(_In_ llvm::Module *pModule,
                               _In_reads_bytes_(FeatureInfoSize) const void *pFeatureInfoData,
-                              _In_ unsigned FeatureInfoSize);
+                              _In_ uint32_t FeatureInfoSize);
 
 // Validate the container parts, assuming supplied module is valid, loaded from the container provided
 struct DxilContainerHeader;
 HRESULT ValidateDxilContainerParts(_In_ llvm::Module *pModule,
                                    _In_opt_ llvm::Module *pDebugModule,
                                    _In_reads_bytes_(ContainerSize) const DxilContainerHeader *pContainer,
-                                   _In_ unsigned ContainerSize);
+                                   _In_ uint32_t ContainerSize);
 
 // Loads module, validating load, but not module.
 HRESULT ValidateLoadModule(_In_reads_bytes_(ILLength) const char *pIL,
@@ -269,7 +269,7 @@ HRESULT ValidateDxilBitcode(_In_reads_bytes_(ILLength) const char *pIL,
 
 // Full container validation, including ValidateDxilModule
 HRESULT ValidateDxilContainer(_In_reads_bytes_(ContainerSize) const void *pContainer,
-                              _In_ unsigned ContainerSize,
+                              _In_ uint32_t ContainerSize,
                               _In_ llvm::raw_ostream &DiagStream);
 
 }

+ 1 - 1
lib/HLSL/DxilContainerAssembler.cpp

@@ -531,7 +531,7 @@ public:
 
   __override uint32_t size() const {
     uint32_t partSize = 0;
-    for (auto &&part : m_Parts) {
+    for (auto &part : m_Parts) {
       partSize += part.Header.PartSize;
     }
     return (uint32_t)GetDxilContainerSizeFromParts((uint32_t)m_Parts.size(), partSize);

+ 20 - 5
lib/HLSL/DxilMetadataHelper.cpp

@@ -46,6 +46,9 @@ const char DxilMDHelper::kDxilControlFlowHintMDName[]                 = "dx.cont
 const char DxilMDHelper::kDxilPreciseAttributeMDName[]                = "dx.precise";
 const char DxilMDHelper::kDxilValidatorVersionMDName[]                = "dx.valver";
 
+// This named metadata is not valid in final module (should be moved to DxilContainer)
+const char DxilMDHelper::kDxilRootSignatureMDName[]                   = "dx.rootSignature";
+
 static std::array<const char *, 6> DxilMDNames = {
   DxilMDHelper::kDxilVersionMDName,
   DxilMDHelper::kDxilShaderModelMDName,
@@ -224,16 +227,21 @@ MDTuple *DxilMDHelper::EmitDxilSignatures(const DxilSignature &InputSig,
   return pSignatureTupleMD;
 }
 
-llvm::Metadata *DxilMDHelper::EmitRootSignature(RootSignatureHandle &RootSig) {
+void DxilMDHelper::EmitRootSignature(RootSignatureHandle &RootSig) {
   if (RootSig.IsEmpty()) {
-    return nullptr;
+    return;
   }
 
   RootSig.EnsureSerializedAvailable();
   Constant *V = llvm::ConstantDataArray::get(
       m_Ctx, llvm::ArrayRef<uint8_t>(RootSig.GetSerializedBytes(),
                                      RootSig.GetSerializedSize()));
-  return ConstantAsMetadata::get(V);
+
+  NamedMDNode *pRootSignatureNamedMD = m_pModule->getNamedMetadata(kDxilRootSignatureMDName);
+  IFTBOOL(pRootSignatureNamedMD == nullptr, DXC_E_INCORRECT_DXIL_METADATA);
+  pRootSignatureNamedMD = m_pModule->getOrInsertNamedMetadata(kDxilRootSignatureMDName);
+  pRootSignatureNamedMD->addOperand(MDNode::get(m_Ctx, {ConstantAsMetadata::get(V)}));
+  return ;
 }
 
 void DxilMDHelper::LoadDxilSignatures(const MDOperand &MDO, 
@@ -279,10 +287,17 @@ void DxilMDHelper::LoadSignatureMetadata(const MDOperand &MDO, DxilSignature &Si
   }
 }
 
-void DxilMDHelper::LoadRootSignature(const MDOperand &MDO, RootSignatureHandle &Sig) {
-  if (MDO.get() == nullptr)
+void DxilMDHelper::LoadRootSignature(RootSignatureHandle &Sig) {
+  NamedMDNode *pRootSignatureNamedMD = m_pModule->getNamedMetadata(kDxilRootSignatureMDName);
+  if(!pRootSignatureNamedMD)
     return;
 
+  IFTBOOL(pRootSignatureNamedMD->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
+
+  MDNode *pNode = pRootSignatureNamedMD->getOperand(0);
+  IFTBOOL(pNode->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
+  const MDOperand &MDO = pNode->getOperand(0);
+
   const ConstantAsMetadata *pMetaData = dyn_cast<ConstantAsMetadata>(MDO.get());
   IFTBOOL(pMetaData != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
   const ConstantDataArray *pData =

+ 12 - 29
lib/HLSL/DxilModule.cpp

@@ -826,24 +826,10 @@ const RootSignatureHandle &DxilModule::GetRootSignature() const {
 }
 
 void DxilModule::StripRootSignatureFromMetadata() {
-  const llvm::NamedMDNode *pEntries = m_pMDHelper->GetDxilEntryPoints();
-  IFTBOOL(pEntries->getNumOperands() == 1, DXC_E_INCORRECT_DXIL_METADATA);
-
-  Function *pEntryFunc;
-  string EntryName;
-  const llvm::MDOperand *pSignatures, *pResources, *pProperties;
-  m_pMDHelper->GetDxilEntryPoint(pEntries->getOperand(0), pEntryFunc, EntryName, pSignatures, pResources, pProperties);
-
-  MDTuple *pMDSignatures = pSignatures->get() ? dyn_cast<MDTuple>(pSignatures->get()) : nullptr;
-  MDTuple *pMDResources = pResources->get() ? dyn_cast<MDTuple>(pResources->get()) : nullptr;
-
-  MDTuple *pMDProperties = EmitDxilShaderProperties(/*bStripRootSignature*/true);
-  MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(pEntryFunc, m_EntryName, pMDSignatures, pMDResources, pMDProperties);
-  vector<MDNode *> Entries;
-  Entries.emplace_back(pEntry);
-  NamedMDNode *pEntryPointsNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilEntryPointsMDName);
-  GetModule()->eraseNamedMetadata(pEntryPointsNamedMD);
-  m_pMDHelper->EmitDxilEntryPoints(Entries);
+  NamedMDNode *pRootSignatureNamedMD = GetModule()->getNamedMetadata(DxilMDHelper::kDxilRootSignatureMDName);
+  if (pRootSignatureNamedMD) {
+    GetModule()->eraseNamedMetadata(pRootSignatureNamedMD);
+  }
 }
 
 void DxilModule::ResetInputSignature(DxilSignature *pValue) {
@@ -918,13 +904,17 @@ void DxilModule::EmitDxilMetadata() {
                                                            *m_OutputSignature,
                                                            *m_PatchConstantSignature);
   MDTuple *pMDResources = EmitDxilResources();
-  MDTuple *pMDProperties = EmitDxilShaderProperties(/*bStripRootSignature*/false);
+  MDTuple *pMDProperties = EmitDxilShaderProperties();
   m_pMDHelper->EmitDxilTypeSystem(GetTypeSystem(), m_LLVMUsed);
   EmitLLVMUsed();
   MDTuple *pEntry = m_pMDHelper->EmitDxilEntryPointTuple(GetEntryFunction(), m_EntryName, pMDSignatures, pMDResources, pMDProperties);
   vector<MDNode *> Entries;
   Entries.emplace_back(pEntry);
   m_pMDHelper->EmitDxilEntryPoints(Entries);
+
+  if (!m_RootSignature->IsEmpty()) {
+    m_pMDHelper->EmitRootSignature(*m_RootSignature.get());
+  }
 }
 
 bool DxilModule::IsKnownNamedMetaData(llvm::NamedMDNode &Node) {
@@ -954,6 +944,8 @@ void DxilModule::LoadDxilMetadata() {
   LoadDxilResources(*pResources);
   LoadDxilShaderProperties(*pProperties);
   m_pMDHelper->LoadDxilTypeSystem(*m_pTypeSystem.get());
+
+  m_pMDHelper->LoadRootSignature(*m_RootSignature.get());
 }
 
 MDTuple *DxilModule::EmitDxilResources() {
@@ -1048,7 +1040,7 @@ void DxilModule::LoadDxilResources(const llvm::MDOperand &MDO) {
   }
 }
 
-MDTuple *DxilModule::EmitDxilShaderProperties(bool bStripRootSignature) {
+MDTuple *DxilModule::EmitDxilShaderProperties() {
   vector<Metadata *> MDVals;
 
   // DXIL shader flags.
@@ -1100,11 +1092,6 @@ MDTuple *DxilModule::EmitDxilShaderProperties(bool bStripRootSignature) {
     MDVals.emplace_back(pMDTuple);
   }
 
-  if (!bStripRootSignature && !m_RootSignature->IsEmpty()) {
-    MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilRootSignatureTag));
-    MDVals.emplace_back(m_pMDHelper->EmitRootSignature(*m_RootSignature.get()));
-  }
-
   if (!MDVals.empty())
     return MDNode::get(m_Ctx, MDVals);
   else
@@ -1158,10 +1145,6 @@ void DxilModule::LoadDxilShaderProperties(const MDOperand &MDO) {
                                    m_MaxTessellationFactor);
       break;
 
-    case DxilMDHelper::kDxilRootSignatureTag:
-      m_pMDHelper->LoadRootSignature(MDO, *m_RootSignature.get());
-      break;
-
     default:
       DXASSERT(false, "Unknown extended shader properties tag");
       break;

+ 9 - 9
lib/HLSL/DxilValidation.cpp

@@ -4034,7 +4034,7 @@ static void VerifyBlobPartMatches(_In_ ValidationContext &ValCtx,
                                   _In_ LPCSTR pName,
                                   DxilPartWriter *pWriter,
                                   _In_reads_bytes_opt_(Size) const void *pData,
-                                  _In_ unsigned Size) {
+                                  _In_ uint32_t Size) {
   if (!pData && pWriter->size()) {
     // No blob part, but writer says non-zero size is expected.
     ValCtx.EmitFormatError(ValidationRule::ContainerPartMissing, pName);
@@ -4067,7 +4067,7 @@ static void VerifyBlobPartMatches(_In_ ValidationContext &ValCtx,
 static void VerifySignatureMatches(_In_ ValidationContext &ValCtx,
                                    DXIL::SignatureKind SigKind,
                                    _In_reads_bytes_opt_(SigSize) const void *pSigData,
-                                   _In_ unsigned SigSize) {
+                                   _In_ uint32_t SigSize) {
   // Generate corresponding signature from module and memcmp
 
   const char *pName = nullptr;
@@ -4092,7 +4092,7 @@ _Use_decl_annotations_
 bool VerifySignatureMatches(llvm::Module *pModule,
                             DXIL::SignatureKind SigKind,
                             const void *pSigData,
-                            unsigned SigSize) {
+                            uint32_t SigSize) {
   std::string diagStr;
   raw_string_ostream diagStream(diagStr);
   DiagnosticPrinterRawOStream DiagPrinter(diagStream);
@@ -4106,7 +4106,7 @@ bool VerifySignatureMatches(llvm::Module *pModule,
 
 static void VerifyPSVMatches(_In_ ValidationContext &ValCtx,
                              _In_reads_bytes_(PSVSize) const void *pPSVData,
-                             _In_ unsigned PSVSize) {
+                             _In_ uint32_t PSVSize) {
   // generate PSV data from module and memcmp
   unique_ptr<DxilPartWriter> pWriter(NewPSVWriter(ValCtx.DxilMod));
   VerifyBlobPartMatches(ValCtx, "Pipeline State Validation", pWriter.get(), pPSVData, PSVSize);
@@ -4115,7 +4115,7 @@ static void VerifyPSVMatches(_In_ ValidationContext &ValCtx,
 _Use_decl_annotations_
 bool VerifyPSVMatches(llvm::Module *pModule,
                       const void *pPSVData,
-                      unsigned PSVSize) {
+                      uint32_t PSVSize) {
   std::string diagStr;
   raw_string_ostream diagStream(diagStr);
   DiagnosticPrinterRawOStream DiagPrinter(diagStream);
@@ -4129,7 +4129,7 @@ bool VerifyPSVMatches(llvm::Module *pModule,
 
 static void VerifyFeatureInfoMatches(_In_ ValidationContext &ValCtx,
                                      _In_reads_bytes_(FeatureInfoSize) const void *pFeatureInfoData,
-                                     _In_ unsigned FeatureInfoSize) {
+                                     _In_ uint32_t FeatureInfoSize) {
   // generate Feature Info data from module and memcmp
   unique_ptr<DxilPartWriter> pWriter(NewFeatureInfoWriter(ValCtx.DxilMod));
   VerifyBlobPartMatches(ValCtx, "Feature Info", pWriter.get(), pFeatureInfoData, FeatureInfoSize);
@@ -4138,7 +4138,7 @@ static void VerifyFeatureInfoMatches(_In_ ValidationContext &ValCtx,
 _Use_decl_annotations_
 bool VerifyFeatureInfoMatches(llvm::Module *pModule,
                               const void *pFeatureInfoData,
-                              unsigned FeatureInfoSize) {
+                              uint32_t FeatureInfoSize) {
   std::string diagStr;
   raw_string_ostream diagStream(diagStr);
   DiagnosticPrinterRawOStream DiagPrinter(diagStream);
@@ -4154,7 +4154,7 @@ _Use_decl_annotations_
 HRESULT ValidateDxilContainerParts(llvm::Module *pModule,
                                    llvm::Module *pDebugModule,
                                    const DxilContainerHeader *pContainer,
-                                   unsigned ContainerSize) {
+                                   uint32_t ContainerSize) {
 
   DXASSERT_NOMSG(pModule);
   if (!pContainer || !IsValidDxilContainer(pContainer, ContainerSize)) {
@@ -4383,7 +4383,7 @@ HRESULT ValidateDxilBitcode(
 
 _Use_decl_annotations_
 HRESULT ValidateDxilContainer(const void *pContainer,
-                              unsigned ContainerSize,
+                              uint32_t ContainerSize,
                               llvm::raw_ostream &DiagStream) {
 
   LLVMContext Ctx, DbgCtx;

+ 8 - 9
lib/HLSL/HLModule.cpp

@@ -467,6 +467,10 @@ void HLModule::EmitHLMetadata() {
     NamedMDNode * resTyAnnotations = m_pModule->getOrInsertNamedMetadata(kHLDxilResourceTypeAnnotationMDName);
     resTyAnnotations->addOperand(EmitResTyAnnotations());
   }
+
+  if (!m_RootSignature->IsEmpty()) {
+    m_pMDHelper->EmitRootSignature(*m_RootSignature.get());
+  }
 }
 
 void HLModule::LoadHLMetadata() {
@@ -541,6 +545,8 @@ void HLModule::LoadHLMetadata() {
     if (MDResTyAnnotations->getNumOperands())
       LoadResTyAnnotations(MDResTyAnnotations->getOperand(0));
   }
+
+  m_pMDHelper->LoadRootSignature(*m_RootSignature.get());
 }
 
 void HLModule::ClearHLMetadata(llvm::Module &M) {
@@ -553,6 +559,7 @@ void HLModule::ClearHLMetadata(llvm::Module &M) {
     if (name == DxilMDHelper::kDxilVersionMDName ||
         name == DxilMDHelper::kDxilShaderModelMDName ||
         name == DxilMDHelper::kDxilEntryPointsMDName ||
+        name == DxilMDHelper::kDxilRootSignatureMDName ||
         name == DxilMDHelper::kDxilResourcesMDName ||
         name == DxilMDHelper::kDxilTypeSystemMDName ||
         name == kHLDxilFunctionPropertiesMDName || // TODO: adjust to proper name
@@ -691,12 +698,7 @@ void HLModule::LoadResTyAnnotations(const llvm::MDOperand &MDO) {
 }
 
 MDTuple *HLModule::EmitHLShaderProperties() {
-  vector<Metadata *> MDVals;
-  if (!m_RootSignature->IsEmpty()) {
-    MDVals.emplace_back(m_pMDHelper->Uint32ToConstMD(DxilMDHelper::kDxilRootSignatureTag));
-    MDVals.emplace_back(m_pMDHelper->EmitRootSignature(*m_RootSignature.get()));
-  }
-  return MDNode::get(m_Ctx, MDVals);
+  return nullptr;
 }
 
 void HLModule::LoadHLShaderProperties(const MDOperand &MDO) {
@@ -711,9 +713,6 @@ void HLModule::LoadHLShaderProperties(const MDOperand &MDO) {
     const MDOperand &MDO = pTupleMD->getOperand(iNode + 1);
     IFTBOOL(MDO.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
     switch (Tag) {
-    case DxilMDHelper::kDxilRootSignatureTag:
-      m_pMDHelper->LoadRootSignature(MDO, *m_RootSignature.get());
-      break;
     default:
       // Ignore other extended properties for now.
       break;