[clang] [llvm] Disentangle DISubprogram ODR type method uniquing from metadata uniquing (PR #217042)

Orlando Cazalet-Hyams via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 08:57:20 PST 2026


================
@@ -2178,46 +2183,97 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
     }
 
     Metadata *CUorFn = getMDOrNull(Record[12 + OffsetB]);
-    DISubprogram *SP = GET_OR_DISTINCT(
-        DISubprogram,
-        (Context,
-         getDITypeRefOrNull(Record[1]),           // scope
-         getMDString(Record[2]),                  // name
-         getMDString(Record[3]),                  // linkageName
-         getMDOrNull(Record[4]),                  // file
-         Record[5],                               // line
-         getMDOrNull(Record[6]),                  // type
-         Record[7 + OffsetA],                     // scopeLine
-         getDITypeRefOrNull(Record[8 + OffsetA]), // containingType
-         Record[10 + OffsetA],                    // virtualIndex
-         HasThisAdj ? Record[16 + OffsetB] : 0,   // thisAdjustment
-         Flags,                                   // flags
-         SPFlags,                                 // SPFlags
-         HasUnit ? CUorFn : nullptr,              // unit
-         getMDOrNull(Record[13 + OffsetB]),       // templateParams
-         getMDOrNull(Record[14 + OffsetB]),       // declaration
-         getMDOrNull(Record[15 + OffsetB]),       // retainedNodes
-         HasThrownTypes ? getMDOrNull(Record[17 + OffsetB])
-                        : nullptr, // thrownTypes
-         HasAnnotations ? getMDOrNull(Record[18 + OffsetB])
-                        : nullptr, // annotations
-         HasTargetFuncName ? getMDString(Record[19 + OffsetB])
-                           : nullptr, // targetFuncName
-         UsesKeyInstructions));
-    MetadataList.assignValue(SP, NextMetadataNo);
-    NextMetadataNo++;
 
-    if (IsDistinct)
-      NewDistinctSPs.push_back(SP);
+    DISubprogram *SP = nullptr;
+    Metadata *Scope = getDITypeRefOrNull(Record[1]);
+    bool MaybeODRUnique = Context.isODRUniquingDebugTypes() && !IsDistinct &&
+                          !(SPFlags & DISubprogram::SPFlagDefinition) &&
+                          getMDString(Record[3]) && Scope;
+
+    if (MaybeODRUnique && !isa<DIScope>(Scope)) {
+      // The scope is a temporary forward reference, meaning we can't perform
+      // ODR-uniquing yet. In order to perform ODR uniquing later the SP must
+      // be replacable, so create a temporary one.
+      TempDISubprogram Tmp = DISubprogram::getTemporary(
+          Context,
+          getDITypeRefOrNull(Record[1]),           // scope
+          getMDString(Record[2]),                  // name
+          getMDString(Record[3]),                  // linkageName
+          getMDOrNull(Record[4]),                  // file
+          Record[5],                               // line
+          getMDOrNull(Record[6]),                  // type
+          Record[7 + OffsetA],                     // scopeLine
+          getDITypeRefOrNull(Record[8 + OffsetA]), // containingType
+          Record[10 + OffsetA],                    // virtualIndex
+          HasThisAdj ? Record[16 + OffsetB] : 0,   // thisAdjustment
+          Flags,                                   // flags
+          SPFlags,                                 // SPFlags
+          HasUnit ? CUorFn : nullptr,              // unit
+          getMDOrNull(Record[13 + OffsetB]),       // templateParams
+          getMDOrNull(Record[14 + OffsetB]),       // declaration
+          getMDOrNull(Record[15 + OffsetB]),       // retainedNodes
+          HasThrownTypes ? getMDOrNull(Record[17 + OffsetB])
+                         : nullptr, // thrownTypes
+          HasAnnotations ? getMDOrNull(Record[18 + OffsetB])
+                         : nullptr, // annotations
+          HasTargetFuncName ? getMDString(Record[19 + OffsetB])
+                            : nullptr, // targetFuncName
+          UsesKeyInstructions);
+      Context.getDebugTypeODRUniquer()->addUnresolvedODRSubprogramDecl(
+          std::move(Tmp));
+      SP = Tmp.get();
----------------
OCHyams wrote:

I'm not sure that we run into this "naturally". I've not manage to find or write a reproducer... I could add a test that operates on bitcode I've hand-modified (/produced with a modified LLVM), but that's not particularly maintainable. I could add an assert that we don't hit this codepath? wdyt @chbessonova ?

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


More information about the cfe-commits mailing list