[llvm] [SPIRV] LLVM IR DIScope nodes are now emitted after their dependencies. (PR #227361)

Manuel Carrasco via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 02:46:29 PDT 2026


================
@@ -833,13 +835,160 @@ MCRegister SPIRVNonSemanticDebugHandler::findOrEmitOpTypeInt32(
   return Reg;
 }
 
-std::optional<MCRegister> SPIRVNonSemanticDebugHandler::emitDebugTypePointer(
+// Each node emits its dependencies first. E.g. for `void f() { struct L; }`,
+// node L emits f, then L. For `struct S { S *p; }`, S* is a back edge, so S
+// is emitted without member p.
+SPIRVNonSemanticDebugHandler::EmitResult
+SPIRVNonSemanticDebugHandler::getOrCreateDebugScope(const DIScope *S) {
+  if (!S || FailedScopes.contains(S))
+    return EmitResult::unsupported();
+  if (auto Reg = lookupOptReg(DebugScopeRegs, S))
+    return EmitResult::emitted(*Reg);
+
+  // Mark the node as in progress while its dependencies are resolved/emitted.
+  // This also prevents cycles from happening.
+  if (!ScopesInProgress.insert(S).second)
+    return EmitResult::inProgress();
+  EmitResult R = emitDebugScope(S);
+  ScopesInProgress.erase(S);
+  assert((!R || !GlobalNSDIEnabled) &&
+         "debug type or scope created after module-scope NSDI emission");
+  if (R)
+    DebugScopeRegs[S] = R.Reg;
+  else if (R.Status == EmitStatus::Unsupported)
+    FailedScopes.insert(S);
----------------
mgcarrasco wrote:

If I got it right, with the proposed changes we won't be able to handle the following scenario.

Suppose we have `getOrCreateDebugScope(struct S { S *p; })`. The recursive call to `getOrCreateDebugScope(S*)` would fail to emit it because `struct S` is still in progress, so it would keep `S*` in ScopesInProgress (forever). Then, `struct S` is emitted but without its `S*` member.  

If we later call `getOrCreateDebugScope(S*)` in the walk, with the proposed changes, we won't be able to emit it even if `struct S` was emitted. In the original code, we would be able to emit `S*` in this second retry.

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


More information about the llvm-commits mailing list