[clang] [llvm] Disentangle DISubprogram ODR type method uniquing from metadata uniquing (PR #217042)
Kristina Bessonova via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 01:49:16 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();
----------------
chbessonova wrote:
I think the cycle test that Vladislav @dzhidzhoev used for another issue also works here
```
; RUN: llvm-as %s -o - | llvm-dis
!named = !{!0}
!0 = !DISubprogram(name: "f", linkageName: "f", scope: !1, type: !2, spFlags: 0)
!1 = !DICompositeType(tag: DW_TAG_structure_type, name: "C", elements: !3)
!2 = !DISubroutineType(types: !4)
!3 = !{!0}
!4 = !{null}
```
Unless I'm missing smth we can hit this branch only in a cycle of uniqued nodes, e.g. a uniqued class that lists its methods in `elements:`. Otherwise the writer guarantees the scope comes first (distinct nodes go first and uniqued operands precede their users). clang always makes class-like types distinct, so clang-generated input never goes here (and MLIR afaik); I'm not sure about other frontends, though.
I'm okay with either option - fix and keep the branch with the test or remove the branch and skip uniquing in the case the scope hasn't been read yet (unless someone comes up with a real case). I'm not sure about the assert though. W/o it we'd never know this happens, but asserting on a valid IR (even if no known frontends emits it) dmaybe not a good idea either.
https://github.com/llvm/llvm-project/pull/217042
More information about the cfe-commits
mailing list