[Driver][DirectX] Add `/Qstrip_debug` flag (#204832) Add the flag that removes ILDB part from the main DXContainer.
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index 887c189..a1d4516 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td
@@ -9764,6 +9764,9 @@ def dxc_source_in_debug_module : DXCFlag<"Qsource_in_debug_module">, HelpText<"Embed source code into PDB debug module">; +def dxc_Qstrip_debug + : DXCFlag<"Qstrip_debug">, + HelpText<"Strip debug information from shader container (specified with /Fo <file>)">; def dxil_validator_version : Option<["/", "-"], "validator-version", KIND_SEPARATE>, Group<dxc_Group>, Flags<[HelpHidden]>, Visibility<[DXCOption, ClangOption, CC1Option]>,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 0a0915b..f201bf9 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3986,6 +3986,10 @@ CmdArgs.push_back("-mllvm"); CmdArgs.push_back("--dx-source-in-debug-module"); } + if (Args.hasArg(options::OPT_dxc_Qstrip_debug)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back("--dx-strip-debug"); + } } static void RenderOpenACCOptions(const Driver &D, const ArgList &Args,
diff --git a/clang/test/Driver/dxc_debug.hlsl b/clang/test/Driver/dxc_debug.hlsl index 6f37e59..90a02f1 100644 --- a/clang/test/Driver/dxc_debug.hlsl +++ b/clang/test/Driver/dxc_debug.hlsl
@@ -5,6 +5,7 @@ // RUN: %clang_dxc -Tlib_6_7 -### -Zi -Qembed_debug %s 2>&1 | FileCheck %s // RUN: %clang_dxc -Tlib_6_7 -### -Zi -Zss %s 2>&1 | FileCheck %s --check-prefix=CHECK,CHECK-ZSS // RUN: %clang_dxc -Tlib_6_7 -### /Zi -Qsource_in_debug_module %s 2>&1 | FileCheck %s --check-prefix=CHECK,CHECK-SIDM +// RUN: %clang_dxc -Tlib_6_7 -### -Zi -Qstrip_debug %s 2>&1 | FileCheck %s --check-prefix=CHECK,CHECK-STRIP // RUN: %clang_dxc -Tlib_6_7 -### -Zi -gcodeview %s 2>&1 | FileCheck %s -check-prefixes=CHECK,CHECK-CV // RUN: %clang_dxc -Tlib_6_7 -### -Zi -gdwarf %s 2>&1 | FileCheck %s -check-prefixes=CHECK,CHECK-DWARF // RUN: %clang_dxc -Tlib_6_7 -### -gcodeview -Zi %s 2>&1 | FileCheck %s -check-prefixes=CHECK,CHECK-CV @@ -18,6 +19,7 @@ // Check that the flags are converted to their llc equivalents. // CHECK-ZSS-SAME: -dx-Zss // CHECK-SIDM-SAME: --dx-source-in-debug-module +// CHECK-STRIP-SAME: --dx-strip-debug // Make sure dxc command line arguments are passed to clang invocation. // CHECK-SAME: -fdx-record-command-line // CHECK-CMD-SAME: --driver-mode=dxc -T lib_6_7 -### -g {{.*}}dxc_debug.hlsl
diff --git a/llvm/lib/MC/MCDXContainerWriter.cpp b/llvm/lib/MC/MCDXContainerWriter.cpp index 994b90f..99d4394 100644 --- a/llvm/lib/MC/MCDXContainerWriter.cpp +++ b/llvm/lib/MC/MCDXContainerWriter.cpp
@@ -19,6 +19,9 @@ cl::opt<bool> EmbedDebug("dx-embed-debug", cl::desc("Embed PDB in shader container")); +cl::opt<bool> + StripDebug("dx-strip-debug", + cl::desc("Strip debug information from shader bytecode")); MCDXContainerTargetWriter::~MCDXContainerTargetWriter() = default; @@ -139,7 +142,7 @@ bool DXContainerObjectWriter::shouldSkipSection(StringRef SectionName, size_t SectionSize) { // Do not write ILDB part if we're not embedding it. - if (!EmbedDebug && SectionName == "ILDB") + if (SectionName == "ILDB" && (!EmbedDebug || StripDebug)) return true; if (SectionName == "SRCI") return true;
diff --git a/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp index c17b571..9c61e4c 100644 --- a/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp +++ b/llvm/lib/Target/DirectX/DXILWriter/DXILWriterPass.cpp
@@ -37,6 +37,7 @@ using namespace llvm::dxil; extern cl::opt<bool> EmbedDebug; +extern cl::opt<bool> StripDebug; extern cl::opt<std::string> PdbDebugPath; cl::opt<bool> SourceInDebugModule( "dx-source-in-debug-module", @@ -237,9 +238,12 @@ bool HasDebugInfo = !M.debug_compile_units().empty(); - // Enable EmbedDebug if there is debug info, but it is not being written - // to a PDB file. - if (HasDebugInfo && !EmbedDebug && PdbDebugPath.empty()) + // If both StripDebug and EmbedDebug are specified, StripDebug is ignored. + if (StripDebug && EmbedDebug) + StripDebug = false; + // Enable EmbedDebug if there is debug info, but it is not being stripped + // or written to a PDB file. + if (HasDebugInfo && !StripDebug && !EmbedDebug && PdbDebugPath.empty()) EmbedDebug = true; if (!HasDebugInfo && EmbedDebug) reportFatalUsageError(
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/ContainerFlags.ll b/llvm/test/CodeGen/DirectX/ContainerData/ContainerFlags.ll index 1d14c32..4ea0709 100644 --- a/llvm/test/CodeGen/DirectX/ContainerData/ContainerFlags.ll +++ b/llvm/test/CodeGen/DirectX/ContainerData/ContainerFlags.ll
@@ -25,6 +25,19 @@ ; PDB-DAG: - Name: ILDB ; PDB-DAG: - Name: ILDN +;; Check that --dx-strip-debug strips ILDB from DXContainer, but still keeps ILDN part +; RUN: llc %S/Inputs/SourceInfo.ll --filetype=obj --dx-strip-debug -o %t.cso +; RUN: obj2yaml %t.cso | FileCheck %s --check-prefix=STRIP --implicit-check-not ILDB +; STRIP: Parts: +; STRIP-DAG: - Name: ILDN + +;; Check that --dx-strip-debug is ignored when provided along with --dx-embed-debug +; RUN: llc %S/Inputs/SourceInfo.ll --filetype=obj --dx-strip-debug --dx-embed-debug -o %t.cso +; RUN: obj2yaml %t.cso | FileCheck %s --check-prefix=STRIP-EMBED +; STRIP-EMBED: Parts: +; STRIP-EMBED-DAG: - Name: ILDB +; STRIP-EMBED-DAG: - Name: ILDN + ;; Check errors when trying to output debug info with no debug info present ; RUN: not llc %s --filetype=obj --dx-embed-debug -o %t.cso 2>&1 | FileCheck %s --check-prefix=ERROR-NODBG ; ERROR-NODBG: Missing debug info for embedding into the container