https://github.com/pvelesko created https://github.com/llvm/llvm-project/pull/210504
`HIPSPVToolChain::adjustDebugInfoKind()` unconditionally forces `NoDebugInfo`, so compiling HIP device code for SPIR-V with `-g` produces device code with no debug metadata at all. Debuggers such as Intel's gdb-oneapi cannot resolve source lines or local variables in offloaded kernels as a result. The comment there attributes the force-disable to the SPIRV-LLVM-Translator aborting on `DW_OP_LLVM_convert`; the translator now lowers that operation, so the workaround is no longer needed. This PR: - stops clobbering the requested debug level in `adjustDebugInfoKind()`, letting `-g` flow into device codegen; - when `-g` (and not `-g0`) is given, requests debug info in the NonSemantic.Shader.DebugInfo form from the translator (`--spirv-debug-info-version=nonsemantic-shader-200`) and enables the required `SPV_KHR_non_semantic_info` extension. The translator accumulates `--spirv-ext` across occurrences, so this augments the extension list already selected. The default (no `-g`) SPIR-V output is unchanged. A driver test covers both the `-g` and no-`-g` cases. Draft: opened for feedback and CI. I have not yet been able to validate end-to-end on hardware (compile `-g -O0`, break in-kernel under gdb-oneapi, inspect locals); doing so is in progress. Feedback on whether `adjustDebugInfoKind()` should be dropped entirely rather than left as a no-op override is welcome. >From 2252edef3146e0e15b66a1822213ee24172c960e Mon Sep 17 00:00:00 2001 From: Paulius Velesko <[email protected]> Date: Sat, 18 Jul 2026 15:06:41 +0300 Subject: [PATCH] [HIPSPV] Preserve device debug info requested via -g HIPSPVToolChain::adjustDebugInfoKind() unconditionally forced NoDebugInfo, so compiling HIP device code with -g produced SPIR-V with no debug metadata. Debuggers such as Intel's gdb-oneapi could therefore not resolve source lines or local variables in offloaded kernels. The stated reason for disabling it (the SPIRV-LLVM-Translator aborting on DW_OP_LLVM_convert) no longer applies, as the translator now lowers that operation. Stop clobbering the requested debug level, and when -g is given emit debug info in the NonSemantic.Shader.DebugInfo form (--spirv-debug-info-version=nonsemantic-shader-200), enabling the required SPV_KHR_non_semantic_info extension. The default (no -g) output is unchanged. --- clang/lib/Driver/ToolChains/HIPSPV.cpp | 27 ++++++++++++++++++++++---- clang/test/Driver/hipspv-toolchain.hip | 23 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/clang/lib/Driver/ToolChains/HIPSPV.cpp b/clang/lib/Driver/ToolChains/HIPSPV.cpp index d6900c767d1f7..5d0cca27a4b39 100644 --- a/clang/lib/Driver/ToolChains/HIPSPV.cpp +++ b/clang/lib/Driver/ToolChains/HIPSPV.cpp @@ -112,6 +112,19 @@ void HIPSPV::Linker::constructLinkAndEmitSpirvCommand( TrArgs.push_back("--spirv-ext=+all"); } + // Preserve debug info requested via -g into the emitted SPIR-V using the + // NonSemantic.Shader.DebugInfo form. Downstream consumers such as Intel's IGC + // and gdb-oneapi use it to map device code back to source lines and local + // variables; the translator's default OpenCL.DebugInfo.100 form is not + // sufficient for that. Emitting the NonSemantic debug instructions requires + // the SPV_KHR_non_semantic_info extension. The translator accumulates + // --spirv-ext across occurrences, so this augments the list set above. + if (const Arg *A = Args.getLastArg(options::OPT_g_Group); + A && !A->getOption().matches(options::OPT_g0)) { + TrArgs.push_back("--spirv-ext=+SPV_KHR_non_semantic_info"); + TrArgs.push_back("--spirv-debug-info-version=nonsemantic-shader-200"); + } + InputInfo TrInput = InputInfo(types::TY_LLVM_BC, TempFile, ""); SPIRV::constructTranslateCommand(C, *this, JA, Output, TrInput, TrArgs); } @@ -333,10 +346,16 @@ VersionTuple HIPSPVToolChain::computeMSVCVersion(const Driver *D, void HIPSPVToolChain::adjustDebugInfoKind( llvm::codegenoptions::DebugInfoKind &DebugInfoKind, const llvm::opt::ArgList &Args) const { - // Debug info generation is disabled for SPIRV-LLVM-Translator - // which currently aborts on the presence of DW_OP_LLVM_convert. - // TODO: Enable debug info when the SPIR-V backend arrives. - DebugInfoKind = llvm::codegenoptions::NoDebugInfo; + // Historically device debug info was force-disabled here because the + // SPIRV-LLVM-Translator aborted on DW_OP_LLVM_convert debug expressions. The + // translator now lowers that operation, so honor the debug level the user + // requested (e.g. via -g) and let it flow into the emitted SPIR-V. + // constructLinkAndEmitSpirvCommand() enables the NonSemantic.Shader.DebugInfo + // form at translation time so downstream tools (e.g. gdb-oneapi) can consume + // it. Leaving DebugInfoKind untouched keeps the default (no -g) behavior, + // since the driver defaults it to NoDebugInfo. + (void)DebugInfoKind; + (void)Args; } LTOKind HIPSPVToolChain::getLTOMode(const llvm::opt::ArgList &Args, diff --git a/clang/test/Driver/hipspv-toolchain.hip b/clang/test/Driver/hipspv-toolchain.hip index 64d5d22a11d39..75e2ce82ccf10 100644 --- a/clang/test/Driver/hipspv-toolchain.hip +++ b/clang/test/Driver/hipspv-toolchain.hip @@ -129,3 +129,26 @@ // RUN: | FileCheck -DVERSION=%llvm-version-major --check-prefix=VERSIONED %s // VERSIONED: {{.*}}llvm-spirv-[[VERSION]] + +//----------------------------------------------------------------------------- +// Check that -g preserves device debug info by requesting the +// NonSemantic.Shader.DebugInfo form (and the extension it depends on) from the +// translator, and that no debug flags are emitted without -g. +// RUN: %clang -### --no-default-config -g -o %t.dummy.img \ +// RUN: --target=spirv64-unknown-chipstar %t.dummy.o \ +// RUN: --hip-path="%S/Inputs/hipspv" \ +// RUN: 2>&1 | FileCheck %s --check-prefix=DEBUG + +// DEBUG: {{".*llvm-spirv"}} "--spirv-max-version=1.2" +// DEBUG-SAME: "--spirv-ext=-all,+SPV_INTEL_function_pointers,+SPV_INTEL_subgroups" +// DEBUG-SAME: "--spirv-ext=+SPV_KHR_non_semantic_info" +// DEBUG-SAME: "--spirv-debug-info-version=nonsemantic-shader-200" + +// RUN: %clang -### --no-default-config -o %t.dummy.img \ +// RUN: --target=spirv64-unknown-chipstar %t.dummy.o \ +// RUN: --hip-path="%S/Inputs/hipspv" \ +// RUN: 2>&1 | FileCheck %s --check-prefix=NODEBUG + +// NODEBUG: {{".*llvm-spirv"}} +// NODEBUG-NOT: SPV_KHR_non_semantic_info +// NODEBUG-NOT: spirv-debug-info-version _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
