Просмотр исходного кода

Fix failure when including debug opts with preprocess or other modes. (#2812)

Tex Riddell 5 лет назад
Родитель
Сommit
2c8ea7a8f6
2 измененных файлов с 87 добавлено и 16 удалено
  1. 14 16
      tools/clang/tools/dxcompiler/dxcompilerobj.cpp
  2. 73 0
      tools/clang/unittests/HLSL/CompilerTest.cpp

+ 14 - 16
tools/clang/tools/dxcompiler/dxcompilerobj.cpp

@@ -876,24 +876,22 @@ public:
 
 
       bool hasErrorOccurred = compiler.getDiagnostics().hasErrorOccurred();
       bool hasErrorOccurred = compiler.getDiagnostics().hasErrorOccurred();
 
 
-// SPIRV change starts
+      bool writePDB = opts.IsDebugInfoEnabled() && produceFullContainer;
+
+      // SPIRV change starts
 #if defined(ENABLE_SPIRV_CODEGEN)
 #if defined(ENABLE_SPIRV_CODEGEN)
-      bool writePDB = !opts.GenSPIRV;
-#else
-      bool writePDB = true;
+      writePDB &= !opts.GenSPIRV;
 #endif
 #endif
-// SPIRV change ends
-      if (!hasErrorOccurred) {
-        if (writePDB && opts.IsDebugInfoEnabled() && !opts.CodeGenHighLevel &&
-            !opts.OptDump) {
-          CComPtr<IDxcBlob> pDebugBlob;
-          IFT(pOutputStream.QueryInterface(&pDebugBlob));
-          CComPtr<IDxcBlob> pStrippedContainer;
-          IFT(CreateContainerForPDB(m_pMalloc, pOutputBlob, pDebugBlob, &pStrippedContainer));
-          pDebugBlob.Release();
-          IFT(hlsl::pdb::WriteDxilPDB(m_pMalloc, pStrippedContainer, ShaderHashContent.Digest, &pDebugBlob));
-          IFT(pResult->SetOutputObject(DXC_OUT_PDB, pDebugBlob));
-        }
+      // SPIRV change ends
+
+      if (!hasErrorOccurred && writePDB) {
+        CComPtr<IDxcBlob> pDebugBlob;
+        IFT(pOutputStream.QueryInterface(&pDebugBlob));
+        CComPtr<IDxcBlob> pStrippedContainer;
+        IFT(CreateContainerForPDB(m_pMalloc, pOutputBlob, pDebugBlob, &pStrippedContainer));
+        pDebugBlob.Release();
+        IFT(hlsl::pdb::WriteDxilPDB(m_pMalloc, pStrippedContainer, ShaderHashContent.Digest, &pDebugBlob));
+        IFT(pResult->SetOutputObject(DXC_OUT_PDB, pDebugBlob));
       }
       }
 
 
       IFT(primaryOutput.SetObject(pOutputBlob, opts.DefaultTextCodePage));
       IFT(primaryOutput.SetObject(pOutputBlob, opts.DefaultTextCodePage));

+ 73 - 0
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -284,7 +284,9 @@ public:
   TEST_METHOD(PreprocessWhenValidThenOK)
   TEST_METHOD(PreprocessWhenValidThenOK)
   TEST_METHOD(LibGVStore)
   TEST_METHOD(LibGVStore)
   TEST_METHOD(PreprocessWhenExpandTokenPastingOperandThenAccept)
   TEST_METHOD(PreprocessWhenExpandTokenPastingOperandThenAccept)
+  TEST_METHOD(PreprocessWithDebugOptsThenOk)
   TEST_METHOD(WhenSigMismatchPCFunctionThenFail)
   TEST_METHOD(WhenSigMismatchPCFunctionThenFail)
+  TEST_METHOD(CompileOtherModesWithDebugOptsThenOk)
 
 
   TEST_METHOD(BatchSamples)
   TEST_METHOD(BatchSamples)
   TEST_METHOD(BatchD3DReflect)
   TEST_METHOD(BatchD3DReflect)
@@ -3218,6 +3220,77 @@ TEST_F(CompilerTest, PreprocessWhenExpandTokenPastingOperandThenAccept) {
                        text.c_str());
                        text.c_str());
 }
 }
 
 
+TEST_F(CompilerTest, PreprocessWithDebugOptsThenOk) {
+  // Make sure debug options, such as -Zi and -Fd,
+  // are simply ignored when preprocessing
+
+  CComPtr<IDxcCompiler> pCompiler;
+  CComPtr<IDxcOperationResult> pResult;
+  CComPtr<IDxcBlobEncoding> pSource;
+  DxcDefine defines[2];
+  defines[0].Name = L"MYDEF";
+  defines[0].Value = L"int";
+  defines[1].Name = L"MYOTHERDEF";
+  defines[1].Value = L"123";
+  VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
+  CreateBlobFromText(
+    "// First line\r\n"
+    "MYDEF g_int = MYOTHERDEF;\r\n"
+    "#define FOO BAR\r\n"
+    "int FOO;", &pSource);
+
+  LPCWSTR extraOptions[] = {L"-Zi", L"-Fd", L"file.pdb", L"-Qembed_debug"};
+
+  VERIFY_SUCCEEDED(pCompiler->Preprocess(pSource, L"file.hlsl",
+    extraOptions, _countof(extraOptions),
+    defines, _countof(defines), nullptr,
+    &pResult));
+  HRESULT hrOp;
+  VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
+  VERIFY_SUCCEEDED(hrOp);
+
+  CComPtr<IDxcBlob> pOutText;
+  VERIFY_SUCCEEDED(pResult->GetResult(&pOutText));
+  std::string text(BlobToUtf8(pOutText));
+  VERIFY_ARE_EQUAL_STR(
+    "#line 1 \"file.hlsl\"\n"
+    "\n"
+    "int g_int = 123;\n"
+    "\n"
+    "int BAR;\n", text.c_str());
+}
+
+TEST_F(CompilerTest, CompileOtherModesWithDebugOptsThenOk) {
+  // Make sure debug options, such as -Zi and -Fd,
+  // are simply ignored when compiling in modes:
+  // /Odump -ast-dump -fcgl -rootsig_1_0
+
+  CComPtr<IDxcCompiler> pCompiler;
+  CComPtr<IDxcBlobEncoding> pSource;
+  VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
+  CreateBlobFromText(
+    "#define RS \"CBV(b0)\"\n"
+    "[RootSignature(RS)]\n"
+    "float main(float i : IN) : OUT { return i * 2.0F; }",
+    &pSource);
+
+  auto testWithOpts = [&](LPCWSTR entry, LPCWSTR target, llvm::ArrayRef<LPCWSTR> mainOpts) -> HRESULT {
+    std::vector<LPCWSTR> opts(mainOpts);
+    opts.insert(opts.end(), {L"-Zi", L"-Fd", L"file.pdb"});
+    CComPtr<IDxcOperationResult> pResult;
+    VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"file.hlsl",
+      entry, target, opts.data(), opts.size(),
+      nullptr, 0, nullptr, &pResult));
+    HRESULT hrOp;
+    VERIFY_SUCCEEDED(pResult->GetStatus(&hrOp));
+    return hrOp;
+  };
+  VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"/Odump"}));
+  VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-ast-dump"}));
+  VERIFY_SUCCEEDED(testWithOpts(L"main", L"vs_6_0", {L"-fcgl"}));
+  VERIFY_SUCCEEDED(testWithOpts(L"RS", L"rootsig_1_0", {}));
+}
+
 TEST_F(CompilerTest, WhenSigMismatchPCFunctionThenFail) {
 TEST_F(CompilerTest, WhenSigMismatchPCFunctionThenFail) {
   CComPtr<IDxcCompiler> pCompiler;
   CComPtr<IDxcCompiler> pCompiler;
   CComPtr<IDxcOperationResult> pResult;
   CComPtr<IDxcOperationResult> pResult;