Skip to content

Commit c9b10a9

Browse files
authored
[ThinLTO] Do not duplicate import a function that is actually defined in the current module #110064 (#111933)
Trying to land #110064 again after fixing test case
1 parent 08159e6 commit c9b10a9

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

llvm/lib/Linker/IRMover.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,15 @@ Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {
595595
if (!SGV)
596596
return nullptr;
597597

598+
// If SGV is from dest, it was already materialized when dest was loaded.
599+
if (SGV->getParent() == &DstM)
600+
return nullptr;
601+
598602
// When linking a global from other modules than source & dest, skip
599603
// materializing it because it would be mapped later when its containing
600604
// module is linked. Linking it now would potentially pull in many types that
601605
// may not be mapped properly.
602-
if (SGV->getParent() != &DstM && SGV->getParent() != SrcM.get())
606+
if (SGV->getParent() != SrcM.get())
603607
return nullptr;
604608

605609
Expected<Constant *> NewProto = linkGlobalValueProto(SGV, ForIndirectSymbol);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
define void @_Z8thinlto1v() unnamed_addr {
5+
%3 = alloca i64, align 4
6+
#dbg_declare(ptr %3, !14, !DIExpression(), !15)
7+
ret void
8+
}
9+
10+
!llvm.dbg.cu = !{!0}
11+
!llvm.module.flags = !{!2, !3, !4, !5}
12+
13+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
14+
!1 = !DIFile(filename: "B.cpp", directory: ".")
15+
!2 = !{i32 7, !"Dwarf Version", i32 4}
16+
!3 = !{i32 2, !"Debug Info Version", i32 3}
17+
!4 = !{i32 1, !"wchar_size", i32 4}
18+
!5 = !{i32 8, !"PIC Level", i32 2}
19+
!10 = distinct !DISubprogram(name: "thinlto1", linkageName: "_Z8thinlto1v", scope: !11, file: !11, line: 8, type: !12, scopeLine: 8, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
20+
!11 = !DIFile(filename: "b.cpp", directory: ".")
21+
!12 = !DISubroutineType(types: !13)
22+
!13 = !{null}
23+
!14 = !DILocalVariable(name: "a", arg: 1, scope: !10, file: !11, line: 18, type: !16)
24+
!15 = !DILocation(line: 18, column: 19, scope: !10)
25+
!16 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S<&func1>", file: !11, line: 2, size: 8, flags: DIFlagTypePassByValue, elements: !17, templateParams: !18, identifier: "_ZTS1SIXadL_Z5func1vEEE")
26+
!17 = !{}
27+
!18 = !{!19}
28+
!19 = !DITemplateValueParameter(name: "Func", type: !20, value: ptr undef)
29+
!20 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
; https://github.com/llvm/llvm-project/pull/110064
2+
; This test case checks if thinLTO correctly links metadata values in a specific
3+
; situation. Assume we are linking module B into module A, where an extern
4+
; function used in A is defined in B, but the function body has a
5+
; DITemplateValueParameter referring to another function back in A. The
6+
; compiler must check this other function is actually coming from A, thus
7+
; already materialized and does not require remapping. The IR here is modified
8+
; from the following source code.
9+
;
10+
; // A.h
11+
; template <void (*Func)()>
12+
; struct S {
13+
; void Impl() {
14+
; Func();
15+
; }
16+
; };
17+
;
18+
; void func1();
19+
;
20+
; // A.cpp
21+
; #include "A.h"
22+
; __attribute__((weak)) void func1() {}
23+
; extern void thinlto1();
24+
; void bar() {
25+
; S<func1> s; // Force instantiation of S<func1> in this compilation unit.
26+
; s.Impl();
27+
; thinlto1();
28+
; }
29+
;
30+
; // B.cpp
31+
; #include "A.h"
32+
; void thinlto1() {
33+
; S<func1> s;
34+
; }
35+
;
36+
; RUN: opt -module-summary -o %t1.bc %s
37+
; RUN: opt -module-summary -o %t2.bc %S/Inputs/ditemplatevalueparameter-remap.ll
38+
; RUN: llvm-lto2 run %t1.bc %t2.bc -o %t3 -save-temps \
39+
; RUN: -r=%t1.bc,_Z5func1v,p \
40+
; RUN: -r=%t1.bc,_Z3bazv,px \
41+
; RUN: -r=%t1.bc,_Z8thinlto1v,x \
42+
; RUN: -r=%t1.bc,_Z3barv,px \
43+
; RUN: -r=%t2.bc,_Z8thinlto1v,px
44+
; RUN: llvm-dis %t3.1.4.opt.bc -o - | FileCheck %s
45+
46+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
47+
target triple = "x86_64-unknown-linux-gnu"
48+
49+
$_Z5func1v = comdat any
50+
51+
define linkonce_odr void @_Z5func1v() unnamed_addr !dbg !10 {
52+
ret void
53+
}
54+
55+
; Dummy function to use _Z5func1v so that it is not treated as dead symbol.
56+
define void @_Z3bazv() {
57+
tail call void @_Z5func1v()
58+
ret void
59+
}
60+
61+
declare void @_Z8thinlto1v() unnamed_addr
62+
63+
; Check _Z8thinlto1v is inlined after thinLTO.
64+
; CHECK: void @_Z3barv()
65+
; CHECK-NOT: @_Z8thinlto1v()
66+
; CHECK-NEXT: ret void
67+
define void @_Z3barv() unnamed_addr !dbg !14 {
68+
tail call void @_Z8thinlto1v(), !dbg !25
69+
ret void
70+
}
71+
72+
!llvm.dbg.cu = !{!0}
73+
!llvm.module.flags = !{!2, !3, !4, !5}
74+
75+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
76+
!1 = !DIFile(filename: "A.cpp", directory: ".")
77+
!2 = !{i32 7, !"Dwarf Version", i32 4}
78+
!3 = !{i32 2, !"Debug Info Version", i32 3}
79+
!4 = !{i32 1, !"wchar_size", i32 4}
80+
!5 = !{i32 8, !"PIC Level", i32 2}
81+
!10 = distinct !DISubprogram(name: "func1", linkageName: "_Z5func1v", scope: !11, file: !11, line: 6, type: !12, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
82+
!11 = !DIFile(filename: "a.h", directory: ".")
83+
!12 = !DISubroutineType(types: !13)
84+
!13 = !{null}
85+
!14 = distinct !DISubprogram(name: "bar", linkageName: "_Z3barv", scope: !11, file: !11, line: 15, type: !12, scopeLine: 15, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !16)
86+
!16 = !{!17}
87+
!17 = !DILocalVariable(name: "s", scope: !14, file: !11, line: 10, type: !18)
88+
!18 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S<&func1>", file: !11, line: 2, size: 8, flags: DIFlagTypePassByValue, elements: !19, templateParams: !20, identifier: "_ZTS1SIXadL_Z5func1vEEE")
89+
!19 = !{}
90+
!20 = !{!21}
91+
!21 = !DITemplateValueParameter(name: "Func", type: !22, value: ptr @_Z5func1v)
92+
!22 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
93+
!25 = !DILocation(line: 16, column: 5, scope: !14)

0 commit comments

Comments
 (0)