llvm.org GIT mirror llvm / 6db6348
IR: Consider two DISubprograms to be odr-equal if they have the same template parameters. In ValueMapper we create new operands for MDNodes and rely on MDNode::replaceWithUniqued to create a new MDNode with the specified operands. However this doesn't always actually happen correctly for DISubprograms because when we uniquify the new node, we only odr-compare it with existing nodes (MDNodeSubsetEqualImpl<DISubprogram>::isDeclarationOfODRMember). Although the TemplateParameters field can refer to a distinct DICompileUnit via DITemplateTypeParameter::type -> DICompositeType::scope -> DISubprogram::unit, it is not currently included in the odr comparison. As a result, we can end up getting our original DISubprogram back, which means we will have a cloned module referring to the DICompileUnit in the original module, which causes a verification error. The fix I implemented was to consider TemplateParameters to be one of the odr-equal properties. But I'm a little uncomfortable with this. In general it seems unsound to rely on distinct MDNodes never being reachable from nodes which we only check odr-equality of. My only long term suggestion would be to separate odr-uniquing from full uniquing. Differential Revision: https://reviews.llvm.org/D29240 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294240 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
2 changed file(s) with 77 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
611611 typedef MDNodeKeyImpl KeyTy;
612612 static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) {
613613 return isDeclarationOfODRMember(LHS.IsDefinition, LHS.Scope,
614 LHS.LinkageName, RHS);
614 LHS.LinkageName, LHS.TemplateParams, RHS);
615615 }
616616 static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) {
617617 return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(),
618 LHS->getRawLinkageName(), RHS);
618 LHS->getRawLinkageName(),
619 LHS->getRawTemplateParams(), RHS);
619620 }
620621
621622 /// Subprograms compare equal if they declare the same function in an ODR
622623 /// type.
623624 static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope,
624625 const MDString *LinkageName,
626 const Metadata *TemplateParams,
625627 const DISubprogram *RHS) {
626628 // Check whether the LHS is eligible.
627629 if (IsDefinition || !Scope || !LinkageName)
632634 return false;
633635
634636 // Compare to the RHS.
637 // FIXME: We need to compare template parameters here to avoid incorrect
638 // collisions in mapMetadata when RF_MoveDistinctMDs and a ODR-DISubprogram
639 // has a non-ODR template parameter (i.e., a DICompositeType that does not
640 // have an identifier). Eventually we should decouple ODR logic from
641 // uniquing logic.
635642 return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() &&
636 LinkageName == RHS->getRawLinkageName();
643 LinkageName == RHS->getRawLinkageName() &&
644 TemplateParams == RHS->getRawTemplateParams();
637645 }
638646 };
639647
0 ; RUN: opt -run-twice -verify -disable-debug-info-type-map -S -o - %s | FileCheck %s
1
2 ; Generated using:
3 ; $ cat p.cpp
4 ; void sink(void *);
5 ; class A {
6 ; public:
7 ; template void m_fn2() { static int a; }
8 ; virtual void m_fn1();
9 ; };
10 ; void foo() {
11 ; class B : public A {
12 ; public:
13 ; B() { m_fn2(); }
14 ; };
15 ; sink(new B);
16 ; }
17 ; $ clang++ -target x86_64-unknown-linux -fvisibility=hidden -O2 -g2 -flto -S p.cpp -o p.ll
18 ; # then manually removed function/gv definitions
19
20 ; Test that when the module is cloned it does not contain a reference to
21 ; the original DICompileUnit as a result of a collision between the cloned
22 ; DISubprogram for m_fn2 (which refers to the non-ODR entity B via
23 ; template parameters) and the original DISubprogram.
24
25 ; CHECK: DICompileUnit
26 ; CHECK-NOT: DICompileUnit
27
28 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
29 target triple = "x86_64-unknown-linux"
30
31 !llvm.dbg.cu = !{!0}
32 !llvm.module.flags = !{!28, !29}
33 !llvm.ident = !{!30}
34
35 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 5.0.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3)
36 !1 = !DIFile(filename: "p.cpp", directory: "/usr/local/google/home/pcc/b682773-2-repro/small2")
37 !2 = !{}
38 !3 = !{!4}
39 !4 = !DIGlobalVariableExpression(var: !5)
40 !5 = distinct !DIGlobalVariable(name: "a", scope: !6, file: !1, line: 5, type: !27, isLocal: true, isDefinition: true)
41 !6 = distinct !DISubprogram(name: "m_fn2", linkageName: "_ZN1A5m_fn2IZ3foovE1BEEvv", scope: !7, file: !1, line: 5, type: !8, isLocal: true, isDefinition: true, scopeLine: 5, flags: DIFlagPrototyped, isOptimized: true, unit: !0, templateParams: !11, declaration: !23, variables: !24)
42 !7 = !DICompositeType(tag: DW_TAG_class_type, name: "A", file: !1, line: 3, flags: DIFlagFwdDecl, identifier: "_ZTS1A")
43 !8 = !DISubroutineType(types: !9)
44 !9 = !{null, !10}
45 !10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
46 !11 = !{!12}
47 !12 = !DITemplateTypeParameter(type: !13)
48 !13 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "B", scope: !14, file: !1, line: 10, size: 64, elements: !17, vtableHolder: !7)
49 !14 = distinct !DISubprogram(name: "foo", linkageName: "_Z3foov", scope: !1, file: !1, line: 9, type: !15, isLocal: false, isDefinition: true, scopeLine: 9, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !2)
50 !15 = !DISubroutineType(types: !16)
51 !16 = !{null}
52 !17 = !{!18, !19}
53 !18 = !DIDerivedType(tag: DW_TAG_inheritance, scope: !13, baseType: !7, flags: DIFlagPublic)
54 !19 = !DISubprogram(name: "B", scope: !13, file: !1, line: 12, type: !20, isLocal: false, isDefinition: false, scopeLine: 12, flags: DIFlagPublic | DIFlagPrototyped, isOptimized: true)
55 !20 = !DISubroutineType(types: !21)
56 !21 = !{null, !22}
57 !22 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64, flags: DIFlagArtificial | DIFlagObjectPointer)
58 !23 = !DISubprogram(name: "m_fn2", linkageName: "_ZN1A5m_fn2IZ3foovE1BEEvv", scope: !7, file: !1, line: 5, type: !8, isLocal: false, isDefinition: false, scopeLine: 5, flags: DIFlagPublic | DIFlagPrototyped, isOptimized: true, templateParams: !11)
59 !24 = !{!25}
60 !25 = !DILocalVariable(name: "this", arg: 1, scope: !6, type: !26, flags: DIFlagArtificial | DIFlagObjectPointer)
61 !26 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
62 !27 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
63 !28 = !{i32 2, !"Dwarf Version", i32 4}
64 !29 = !{i32 2, !"Debug Info Version", i32 3}
65 !30 = !{!"clang version 5.0.0 "}