Przeglądaj źródła

/Fd now sets the shader debug name to the name specified. (#2023)

Adam Yang 6 lat temu
rodzic
commit
e97fbdb85f

+ 2 - 0
include/dxc/DxilContainer/DxilContainerAssembler.h

@@ -13,6 +13,7 @@
 
 #include <functional>
 #include "dxc/DxilContainer/DxilContainer.h"
+#include "llvm/ADT/StringRef.h"
 
 namespace hlsl {
 
@@ -48,6 +49,7 @@ DxilContainerWriter *NewDxilContainerWriter();
 void SerializeDxilContainerForModule(hlsl::DxilModule *pModule,
                                      AbstractMemoryStream *pModuleBitcode,
                                      AbstractMemoryStream *pStream,
+                                     llvm::StringRef DebugName,
                                      SerializeDxilFlags Flags);
 void SerializeDxilContainerForRootSignature(hlsl::RootSignatureHandle *pRootSigHandle,
                                      AbstractMemoryStream *pStream);

+ 1 - 1
include/dxc/Support/HLSLOptions.td

@@ -330,7 +330,7 @@ def Fc : JoinedOrSeparate<["-", "/"], "Fc">, MetaVarName<"<file>">, HelpText<"Ou
 //def Fx : JoinedOrSeparate<["-", "/"], "Fx">, MetaVarName<"<file>">, HelpText<"Output assembly code and hex listing file">;
 def Fh : JoinedOrSeparate<["-", "/"], "Fh">, MetaVarName<"<file>">, HelpText<"Output header file containing object code">, Flags<[DriverOption]>, Group<hlslcomp_Group>;
 def Fe : JoinedOrSeparate<["-", "/"], "Fe">, MetaVarName<"<file>">, HelpText<"Output warnings and errors to the given file">, Flags<[DriverOption]>, Group<hlslcomp_Group>;
-def Fd : JoinedOrSeparate<["-", "/"], "Fd">, MetaVarName<"<file>">, HelpText<"Write debug information to the given file or directory; trail \\ to auto-generate and imply Qstrip_priv">, Flags<[DriverOption]>, Group<hlslcomp_Group>;
+def Fd : JoinedOrSeparate<["-", "/"], "Fd">, MetaVarName<"<file>">, HelpText<"Write debug information to the given file or directory; trail \\ to auto-generate and imply Qstrip_priv">, Flags<[CoreOption, DriverOption]>, Group<hlslcomp_Group>;
 def Vn : JoinedOrSeparate<["-", "/"], "Vn">, MetaVarName<"<name>">, HelpText<"Use <name> as variable name in header file">, Flags<[DriverOption]>, Group<hlslcomp_Group>;
 def Cc : Flag<["-", "/"], "Cc">, HelpText<"Output color coded assembly listings">, Group<hlslcomp_Group>, Flags<[DriverOption]>;
 def Ni : Flag<["-", "/"], "Ni">, HelpText<"Output instruction numbers in assembly listings">, Group<hlslcomp_Group>, Flags<[DriverOption]>;

+ 41 - 17
lib/DxilContainer/DxilContainerAssembler.cpp

@@ -1464,6 +1464,7 @@ public:
 void hlsl::SerializeDxilContainerForModule(DxilModule *pModule,
                                            AbstractMemoryStream *pModuleBitcode,
                                            AbstractMemoryStream *pFinalStream,
+                                           llvm::StringRef DebugName,
                                            SerializeDxilFlags Flags) {
   // TODO: add a flag to update the module and remove information that is not part
   // of DXIL proper and is used only to assemble the container.
@@ -1590,30 +1591,53 @@ void hlsl::SerializeDxilContainerForModule(DxilModule *pModule,
       // If the debug name should be specific to the sources, base the name on the debug
       // bitcode, which will include the source references, line numbers, etc. Otherwise,
       // do it exclusively on the target shader bitcode.
+
       pHashStream = (int)(Flags & SerializeDxilFlags::DebugNameDependOnSource)
                         ? CComPtr<AbstractMemoryStream>(pModuleBitcode)
                         : CComPtr<AbstractMemoryStream>(pProgramStream);
+
+      // Use user specified debug name if a) it's given and b) it's not a path
+      bool UseDebugName = DebugName.size() && !DebugName.endswith(llvm::StringRef("\\"));
+
+      // Calculate the length of the name
+      const uint32_t NameLen = UseDebugName ? 
+        DebugName.size() :
+        DebugInfoNameHashLen +  DebugInfoNameSuffix;
+
+      // Calculate the size of the blob part.
       const uint32_t DebugInfoContentLen =
-          sizeof(DxilShaderDebugName) + DebugInfoNameHashLen +
-          DebugInfoNameSuffix + DebugInfoNameNullAndPad;
+          sizeof(DxilShaderDebugName) + NameLen + DebugInfoNameNullAndPad;
+
       writer.AddPart(DFCC_ShaderDebugName, DebugInfoContentLen, [&](AbstractMemoryStream *pStream) {
         DxilShaderDebugName NameContent;
         NameContent.Flags = 0;
-        NameContent.NameLength = DebugInfoNameHashLen + DebugInfoNameSuffix;
-        IFT(WriteStreamValue(pStream, NameContent));
-
-        ArrayRef<uint8_t> Data((uint8_t *)pHashStream->GetPtr(), pHashStream->GetPtrSize());
-        llvm::MD5 md5;
-        llvm::MD5::MD5Result md5Result;
-        SmallString<32> Hash;
-        md5.update(Data);
-        md5.final(md5Result);
-        md5.stringifyResult(md5Result, Hash);
-
-        ULONG cbWritten;
-        IFT(pStream->Write(Hash.data(), Hash.size(), &cbWritten));
-        const char SuffixAndPad[] = ".lld\0\0\0";
-        IFT(pStream->Write(SuffixAndPad, _countof(SuffixAndPad), &cbWritten));
+
+        if (UseDebugName) {
+          NameContent.NameLength = DebugName.size();
+          IFT(WriteStreamValue(pStream, NameContent));
+
+          ULONG cbWritten;
+          IFT(pStream->Write(DebugName.begin(), DebugName.size(), &cbWritten));
+          const char Pad[] = { '\0','\0','\0','\0' };
+          IFT(pStream->Write(Pad, _countof(Pad), &cbWritten));
+        }
+        else {
+          NameContent.NameLength = DebugInfoNameHashLen + DebugInfoNameSuffix;
+          IFT(WriteStreamValue(pStream, NameContent));
+
+          ArrayRef<uint8_t> Data((uint8_t *)pHashStream->GetPtr(), pHashStream->GetPtrSize());
+          llvm::MD5 md5;
+          llvm::MD5::MD5Result md5Result;
+          SmallString<32> Hash;
+          md5.update(Data);
+          md5.final(md5Result);
+          md5.stringifyResult(md5Result, Hash);
+
+          ULONG cbWritten;
+          IFT(pStream->Write(Hash.data(), Hash.size(), &cbWritten));
+          const char SuffixAndPad[] = { '.','l','l','d','\0','\0','\0','\0' };
+          IFT(pStream->Write(SuffixAndPad, _countof(SuffixAndPad), &cbWritten));
+        }
       });
     }
   }

+ 1 - 1
tools/clang/tools/dxcompiler/dxclinker.cpp

@@ -256,7 +256,7 @@ HRESULT STDMETHODCALLTYPE DxcLinker::Link(
           valHR = dxcutil::ValidateAndAssembleToContainer(
               std::move(pM), pOutputBlob, pMalloc, SerializeFlags,
               pOutputStream,
-              /*bDebugInfo*/ false, Diag);
+              /*bDebugInfo*/ false, llvm::StringRef(), Diag);
         } else {
           dxcutil::AssembleToContainer(std::move(pM), pOutputBlob, m_pMalloc,
                                        SerializeFlags, pOutputStream);

+ 1 - 1
tools/clang/tools/dxcompiler/dxcompilerobj.cpp

@@ -592,7 +592,7 @@ public:
           if (needsValidation) {
             valHR = dxcutil::ValidateAndAssembleToContainer(
                 action.takeModule(), pOutputBlob, m_pMalloc, SerializeFlags,
-                pOutputStream, opts.DebugInfo, compiler.getDiagnostics());
+                pOutputStream, opts.DebugInfo, opts.DebugFile, compiler.getDiagnostics());
           } else {
             dxcutil::AssembleToContainer(action.takeModule(),
                                                  pOutputBlob, m_pMalloc,

+ 9 - 2
tools/clang/tools/dxcompiler/dxcutil.cpp

@@ -78,16 +78,20 @@ public:
     CComPtr<AbstractMemoryStream> pContainerStream;
     IFT(CreateMemoryStream(pMalloc, &pContainerStream));
     SerializeDxilContainerForModule(&m_llvmModule->GetOrCreateDxilModule(),
-                                    pModuleBitcode, pContainerStream, Flags);
+                                    pModuleBitcode, pContainerStream, m_debugName, Flags);
 
     pDxilContainerBlob.Release();
     IFT(pContainerStream.QueryInterface(&pDxilContainerBlob));
   }
+  void SetDebugName(llvm::StringRef DebugName) {
+    m_debugName = DebugName;
+  }
 
   llvm::Module *get() { return m_llvmModule.get(); }
   llvm::Module *getWithDebugInfo() { return m_llvmModuleWithDebugInfo.get(); }
 
 private:
+  std::string m_debugName;
   std::unique_ptr<llvm::Module> m_llvmModule;
   std::unique_ptr<llvm::Module> m_llvmModuleWithDebugInfo;
 };
@@ -152,7 +156,7 @@ void ReadOptsAndValidate(hlsl::options::MainArgs &mainArgs,
 HRESULT ValidateAndAssembleToContainer(
     std::unique_ptr<llvm::Module> pM, CComPtr<IDxcBlob> &pOutputBlob,
     IMalloc *pMalloc, SerializeDxilFlags SerializeFlags,
-    CComPtr<AbstractMemoryStream> &pOutputStream, bool bDebugInfo,
+    CComPtr<AbstractMemoryStream> &pOutputStream, bool bDebugInfo, llvm::StringRef DebugName,
     clang::DiagnosticsEngine &Diag) {
   HRESULT valHR = S_OK;
 
@@ -176,6 +180,9 @@ HRESULT ValidateAndAssembleToContainer(
     // module.
     if (bDebugInfo) {
       llvmModule.CloneForDebugInfo();
+      if (DebugName.size()) {
+        llvmModule.SetDebugName(DebugName);
+      }
     }
   }
 

+ 1 - 1
tools/clang/tools/dxcompiler/dxcutil.h

@@ -41,7 +41,7 @@ namespace dxcutil {
 HRESULT ValidateAndAssembleToContainer(
     std::unique_ptr<llvm::Module> pM, CComPtr<IDxcBlob> &pOutputContainerBlob,
     IMalloc *pMalloc, hlsl::SerializeDxilFlags SerializeFlags,
-    CComPtr<hlsl::AbstractMemoryStream> &pModuleBitcode, bool bDebugInfo,
+    CComPtr<hlsl::AbstractMemoryStream> &pModuleBitcode, bool bDebugInfo, llvm::StringRef DebugName,
     clang::DiagnosticsEngine &Diag);
 void GetValidatorVersion(unsigned *pMajor, unsigned *pMinor);
 void AssembleToContainer(std::unique_ptr<llvm::Module> pM,

+ 1 - 1
tools/clang/tools/dxrfallbackcompiler/dxcutil.cpp

@@ -81,7 +81,7 @@ public:
     CComPtr<AbstractMemoryStream> pContainerStream;
     IFT(CreateMemoryStream(pMalloc, &pContainerStream));
     SerializeDxilContainerForModule(&m_llvmModule->GetOrCreateDxilModule(),
-                                    pModuleBitcode, pContainerStream, Flags);
+                                    pModuleBitcode, pContainerStream, llvm::StringRef(), Flags);
 
     pDxilContainerBlob.Release();
     IFT(pContainerStream.QueryInterface(&pDxilContainerBlob));