[llvm] [SPIRV] Migrate NSDI emission from a machine pass to DebugHandlerBase (PR #191212)

Diego Novillo via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 13 02:25:36 PDT 2026


================
@@ -0,0 +1,370 @@
+//===-- SPIRVNonSemanticDebugHandler.cpp - NSDI AsmPrinter handler -*- C++
+//-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "SPIRVNonSemanticDebugHandler.h"
+#include "MCTargetDesc/SPIRVMCTargetDesc.h"
+#include "SPIRVSubtarget.h"
+#include "SPIRVUtils.h"
+#include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/CodeGen/AsmPrinter.h"
+#include "llvm/IR/DebugInfoMetadata.h"
+#include "llvm/IR/DebugProgramInstruction.h"
+#include "llvm/IR/Module.h"
+#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCStreamer.h"
+#include "llvm/Support/Path.h"
+
+using namespace llvm;
+
+SPIRVNonSemanticDebugHandler::SPIRVNonSemanticDebugHandler(AsmPrinter &AP)
+    : DebugHandlerBase(&AP) {}
+
+// Map DWARF source language codes to NonSemantic.Shader.DebugInfo.100 source
+// language codes. Values are from the SourceLanguage enum in the
+// NonSemantic.Shader.DebugInfo.100 specification, section 4.3.
+unsigned SPIRVNonSemanticDebugHandler::toNSDISrcLang(unsigned DwarfSrcLang) {
+  switch (DwarfSrcLang) {
+  case dwarf::DW_LANG_OpenCL:
+    return 3; // OpenCL_C
+  case dwarf::DW_LANG_OpenCL_CPP:
+    return 4; // OpenCL_CPP
+  case dwarf::DW_LANG_CPP_for_OpenCL:
+    return 6; // CPP_for_OpenCL
+  case dwarf::DW_LANG_GLSL:
+    return 2; // GLSL
+  case dwarf::DW_LANG_HLSL:
+    return 5; // HLSL
+  case dwarf::DW_LANG_SYCL:
+    return 7; // SYCL
+  case dwarf::DW_LANG_Zig:
+    return 12; // Zig
+  default:
+    return 0; // Unknown
+  }
+}
+
+void SPIRVNonSemanticDebugHandler::beginModule(Module *M) {
+  // The base class sets Asm = nullptr when the module has no compile units,
+  // and initializes lexical scope tracking otherwise.
+  DebugHandlerBase::beginModule(M);
+
+  if (!Asm)
+    return;
+
+  // Collect compile-unit info: file paths and source languages.
+  for (const DICompileUnit *CU : M->debug_compile_units()) {
+    const DIFile *File = CU->getFile();
+    CompileUnitInfo Info;
+    sys::path::append(Info.FilePath, File->getDirectory(), File->getFilename());
+    // getName() returns the language code regardless of whether the name is
+    // versioned. getUnversionedName() would assert on versioned names.
+    Info.SpirvSourceLanguage = toNSDISrcLang(CU->getSourceLanguage().getName());
+    CompileUnits.push_back(std::move(Info));
+  }
+
+  // Collect DWARF and debug-info version numbers from module flags.
+  if (const NamedMDNode *Flags = M->getNamedMetadata("llvm.module.flags")) {
+    for (const auto *Op : Flags->operands()) {
+      const MDOperand &NameOp = Op->getOperand(1);
+      if (NameOp.equalsStr("Dwarf Version"))
----------------
dnovillo wrote:

Done.

https://github.com/llvm/llvm-project/pull/191212


More information about the llvm-commits mailing list