[llvm] [SPIRV] Emit NonSemantic DebugTypeComposite and DebugTypeMember. (PR #211638)

Manuel Carrasco via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 24 03:40:07 PDT 2026


================
@@ -742,6 +856,28 @@ void SPIRVNonSemanticDebugHandler::emitNonSemanticDebugStrings(
     ScopeToPathOpStringReg[SP] = emitOpStringIfNew(getDebugFullPath(SP), MAI);
   }
 
+  // Cache the OpStrings each DebugTypeComposite and its DebugTypeMembers use:
+  // the composite name, identifier (linkage name), and path, plus each member
+  // name and path.
+  for (const DICompositeType *CT : CompositeTypes) {
+    emitOpStringIfNew(CT->getName(), MAI);
+    emitOpStringIfNew(CT->getIdentifier(), MAI);
+    MCRegister PathReg =
----------------
mgcarrasco wrote:

I think this is becoming a usual pattern, maybe a refactor like this one is worth doing:

```diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
index 0e20afab60d5..cd97cf2a2647 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.cpp
@@ -359,6 +359,14 @@ MCRegister SPIRVNonSemanticDebugHandler::getCachedOpStringReg(StringRef S) {
   return It->second;
 }
 
+MCRegister SPIRVNonSemanticDebugHandler::emitAndCacheScopePathOpStringReg(
+    const DIScope *Scope, SPIRV::ModuleAnalysisInfo &MAI) {
+  MCRegister Reg = emitOpStringIfNew(getDebugFullPath(Scope), MAI);
+  if (Scope)
+    ScopeToPathOpStringReg[Scope] = Reg;
+  return Reg;
+}
+
 MCRegister SPIRVNonSemanticDebugHandler::getCachedScopePathOpStringReg(
     const DIScope *Scope, bool UseEmptyPathIfNullScope) {
   if (!Scope) {
@@ -853,7 +861,7 @@ void SPIRVNonSemanticDebugHandler::emitNonSemanticDebugStrings(
   for (const DISubprogram *SP : SubprogramDeclarations) {
     emitOpStringIfNew(SP->getName(), MAI);
     emitOpStringIfNew(SP->getLinkageName(), MAI);
-    ScopeToPathOpStringReg[SP] = emitOpStringIfNew(getDebugFullPath(SP), MAI);
+    emitAndCacheScopePathOpStringReg(SP, MAI);
   }
 
   // Cache the OpStrings each DebugTypeComposite and its DebugTypeMembers use:
@@ -862,29 +870,20 @@ void SPIRVNonSemanticDebugHandler::emitNonSemanticDebugStrings(
   for (const DICompositeType *CT : CompositeTypes) {
     emitOpStringIfNew(CT->getName(), MAI);
     emitOpStringIfNew(CT->getIdentifier(), MAI);
-    MCRegister PathReg =
-        emitOpStringIfNew(getDebugFullPath(CT->getFile()), MAI);
-    if (const DIFile *F = CT->getFile())
-      ScopeToPathOpStringReg[F] = PathReg;
+    emitAndCacheScopePathOpStringReg(CT->getFile(), MAI);
     for (const DINode *Element : CT->getElements()) {
       const auto *M = dyn_cast<DIDerivedType>(Element);
       if (!M || M->getTag() != dwarf::DW_TAG_member)
         continue;
       emitOpStringIfNew(M->getName(), MAI);
-      MCRegister MPathReg =
-          emitOpStringIfNew(getDebugFullPath(M->getFile()), MAI);
-      if (const DIFile *MF = M->getFile())
-        ScopeToPathOpStringReg[MF] = MPathReg;
+      emitAndCacheScopePathOpStringReg(M->getFile(), MAI);
     }
   }
 
   for (const auto &[GV, _] : GlobalVariableDebugInfoMap) {
     emitOpStringIfNew(GV->getName(), MAI);
     emitOpStringIfNew(GV->getLinkageName(), MAI);
-    SmallString<128> Path = getDebugFullPath(GV->getFile());
-    MCRegister PathReg = emitOpStringIfNew(Path, MAI);
-    if (const DIFile *F = GV->getFile())
-      ScopeToPathOpStringReg[F] = PathReg;
+    emitAndCacheScopePathOpStringReg(GV->getFile(), MAI);
   }
 
   CachedEmptyStringReg = emitOpStringIfNew("", MAI);
diff --git a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
index f9e3b555a235..b26fe6f01805 100644
--- a/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
+++ b/llvm/lib/Target/SPIRV/SPIRVNonSemanticDebugHandler.h
@@ -206,6 +206,12 @@ private:
   /// section 7 did not complete.
   MCRegister getCachedOpStringReg(StringRef S);
 
+  /// Section 7 only: emit path \c OpString for \p Scope and cache under \p
+  /// Scope when non-null. Returns the \c OpString result id (still emitted when
+  /// \p Scope is null, but not cached).
+  MCRegister emitAndCacheScopePathOpStringReg(const DIScope *Scope,
+                                              SPIRV::ModuleAnalysisInfo &MAI);
+
   /// Section 10 only: lookup path \c OpString id for \p Scope from
   /// \c ScopeToPathOpStringReg; asserts if missing or invalid. When
   /// \p UseEmptyPathIfNullScope is true and \p Scope is null, returns

```

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


More information about the llvm-commits mailing list