[llvm] [SPIRV] Emit NonSemantic DebugFunctionDeclaration for DISubprograms (declarations). (PR #203615)

Juan Manuel Martinez CaamaƱo via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 06:21:06 PDT 2026


================
@@ -452,6 +464,86 @@ SPIRVNonSemanticDebugHandler::emitDebugTypeFunctionForSubroutineType(
   return getOrEmitDebugTypeFunction(Ops, VoidTypeReg, ExtInstSetReg, MAI);
 }
 
+std::optional<MCRegister>
+SPIRVNonSemanticDebugHandler::resolveDebugFunctionDeclarationParent(
+    const DISubprogram *SP) const {
+  // Current logic matches SPIRV-LLVM-Translator's logic for Parent operand.
+
+  if (const DIScope *Scope = SP->getScope()) {
+    if (!isa<DIFile>(Scope)) {
+      if (const DIType *Ty = dyn_cast<DIType>(Scope)) {
+        auto TIt = DebugTypeRegs.find(Ty);
+        if (TIt != DebugTypeRegs.end())
+          return TIt->second;
+      }
+      // TODO: Complete with other lookups once other scopes are supported
+      // (subclases of DIScope).
+      return std::nullopt;
+    }
+  }
+
+  const DICompileUnit *ParentCU = SP->getUnit();
+  if (!ParentCU && !CompileUnits.empty())
+    ParentCU = CompileUnits[0].TheCU;
+  if (!ParentCU)
+    return std::nullopt;
+  auto CUIt = CUToCompilationUnitDbgReg.find(ParentCU);
+  if (CUIt == CUToCompilationUnitDbgReg.end())
+    return std::nullopt;
+  return CUIt->second;
+}
+
+std::optional<MCRegister>
+SPIRVNonSemanticDebugHandler::emitDebugFunctionDeclaration(
+    const DISubprogram *SP, MCRegister VoidTypeReg, MCRegister I32TypeReg,
+    MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI) {
+  assert(SP && "SP must not be null in emitDebugFunctionDeclaration");
+  assert(!SP->isDefinition() &&
+         "SP must not be a definition in emitDebugFunctionDeclaration");
+
+  // The IR verifier already enforces that this cannot be null.
+  const DISubroutineType *ST = SP->getType();
+
+  auto FnTyIt = DebugTypeRegs.find(ST);
+  if (FnTyIt == DebugTypeRegs.end())
+    return std::nullopt;
+
+  auto ParentRegOpt = resolveDebugFunctionDeclarationParent(SP);
+  if (!ParentRegOpt)
+    return std::nullopt;
+
+  MCRegister ParentReg = *ParentRegOpt;
+
+  auto PathStrIt = ScopeToPathOpStringReg.find(SP);
+  assert(PathStrIt != ScopeToPathOpStringReg.end() &&
+         "declaration path OpString must be cached in "
+         "emitNonSemanticDebugStrings");
+  MCRegister FileStrReg = PathStrIt->second;
+  assert(FileStrReg.isValid() &&
+         "declaration path OpString id must be valid once cached");
+
+  MCRegister NameReg = getCachedOpStringReg(SP->getName());
+  MCRegister LinkageReg = getCachedOpStringReg(SP->getLinkageName());
+  MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
+                                                        ExtInstSetReg, MAI);
+
+  MCRegister LineReg =
+      emitOpConstantI32(static_cast<uint32_t>(SP->getLine()), I32TypeReg, MAI);
+  MCRegister ColReg = emitOpConstantI32(0, I32TypeReg, MAI);
----------------
jmmartinez wrote:

Is the column field always 0 for declarations ?

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


More information about the llvm-commits mailing list