Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit cf53871

Browse files
committed
[ThinLTO] Indirect call promotion fixes for promoted local functions
Summary: Fix a couple issues limiting the application of indirect call promotion in ThinLTO mode: - Invoke indirect call promotion before globalopt, since it may eliminate imported functions which appear unreferenced. - Invoke indirect call promotion with InLTO=true so that the PGOFuncName metadata is used to get the name for locals which would have been renamed during promotion. Reviewers: davidxl, mehdi_amini Subscribers: Prazek, llvm-commits, mehdi_amini Differential Revision: https://reviews.llvm.org/D24004 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280024 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e060ffb commit cf53871

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

lib/ProfileData/InstrProf.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ std::string getPGOFuncName(StringRef RawFuncName,
136136
// (when \c InLTO is true): LTO's internalization privatizes many global linkage
137137
// symbols. This happens after value profile annotation, but those internal
138138
// linkage functions should not have a source prefix.
139+
// Additionally, for ThinLTO mode, exported internal functions are promoted
140+
// and renamed. We need to ensure that the original internal PGO name is
141+
// used when computing the GUID that is compared against the profiled GUIDs.
139142
// To differentiate compiler generated internal symbols from original ones,
140143
// PGOFuncName meta data are created and attached to the original internal
141144
// symbols in the value profile annotation step

lib/Transforms/IPO/PassManagerBuilder.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ void PassManagerBuilder::populateModulePassManager(
405405

406406
addInitialAliasAnalysisPasses(MPM);
407407

408+
// For ThinLTO there are two passes of indirect call promotion. The
409+
// first is during the compile phase when PerformThinLTO=false and
410+
// intra-module indirect call targets are promoted. The second is during
411+
// the ThinLTO backend when PerformThinLTO=true, when we promote imported
412+
// inter-module indirect calls. For that we perform indirect call promotion
413+
// earlier in the pass pipeline, here before globalopt. Otherwise imported
414+
// available_externally functions look unreferenced and are removed.
415+
if (PerformThinLTO)
416+
MPM.add(createPGOIndirectCallPromotionLegacyPass(/*InLTO = */ true));
417+
408418
if (!DisableUnitAtATime) {
409419
// Infer attributes about declarations if possible.
410420
MPM.add(createInferFunctionAttrsLegacyPass());
@@ -427,11 +437,12 @@ void PassManagerBuilder::populateModulePassManager(
427437
/// PGO instrumentation is added during the compile phase for ThinLTO, do
428438
/// not run it a second time
429439
addPGOInstrPasses(MPM);
440+
// Indirect call promotion that promotes intra-module targets only.
441+
// For ThinLTO this is done earlier due to interactions with globalopt
442+
// for imported functions.
443+
MPM.add(createPGOIndirectCallPromotionLegacyPass());
430444
}
431445

432-
// Indirect call promotion that promotes intra-module targets only.
433-
MPM.add(createPGOIndirectCallPromotionLegacyPass());
434-
435446
if (EnableNonLTOGlobalsModRef)
436447
// We add a module alias analysis pass here. In part due to bugs in the
437448
// analysis infrastructure this "works" in that the analysis stays alive
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
22
target triple = "x86_64-unknown-linux-gnu"
33

4+
source_filename = "thinlto_indirect_call_promotion.c"
5+
46
define void @a() {
57
entry:
68
ret void
79
}
10+
11+
define internal void @c() !PGOFuncName !1 {
12+
entry:
13+
ret void
14+
}
15+
16+
!1 = !{!"thinlto_indirect_call_promotion.c:c"}

test/Transforms/PGOProfile/thinlto_indirect_call_promotion.ll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,33 @@
44
; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc
55

66
; RUN: opt -function-import -summary-file %t3.thinlto.bc %t.bc -o %t4.bc -print-imports 2>&1 | FileCheck %s --check-prefix=IMPORTS
7-
; IMPORTS: Import a
7+
; IMPORTS-DAG: Import a
8+
; IMPORTS-DAG: Import c
89

9-
; RUN: opt %t4.bc -pgo-icall-prom -S -icp-count-threshold=1 | FileCheck %s --check-prefix=ICALL-PROM
10-
; RUN: opt %t4.bc -pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-count-threshold=1 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
10+
; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S -icp-count-threshold=1 | FileCheck %s --check-prefix=ICALL-PROM
11+
; RUN: opt %t4.bc -icp-lto -pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-count-threshold=1 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
1112
; PASS-REMARK: Promote indirect call to a with count 1 out of 1
13+
; PASS-REMARK: Promote indirect call to c.llvm.0 with count 1 out of 1
1214

1315
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1416
target triple = "x86_64-unknown-linux-gnu"
1517

1618
@foo = external local_unnamed_addr global void ()*, align 8
19+
@bar = external local_unnamed_addr global void ()*, align 8
1720

1821
define i32 @main() local_unnamed_addr {
1922
entry:
2023
%0 = load void ()*, void ()** @foo, align 8
2124
; ICALL-PROM: br i1 %{{[0-9]+}}, label %if.true.direct_targ, label %if.false.orig_indirect, !prof [[BRANCH_WEIGHT:![0-9]+]]
2225
tail call void %0(), !prof !1
26+
%1 = load void ()*, void ()** @bar, align 8
27+
; ICALL-PROM: br i1 %{{[0-9]+}}, label %if.true.direct_targ1, label %if.false.orig_indirect2, !prof [[BRANCH_WEIGHT:![0-9]+]]
28+
tail call void %1(), !prof !2
2329
ret i32 0
2430
}
2531

2632
!1 = !{!"VP", i32 0, i64 1, i64 -6289574019528802036, i64 1}
33+
!2 = !{!"VP", i32 0, i64 1, i64 591260329866125152, i64 1}
2734

2835
; Should not have a VP annotation on new indirect call (check before and after
2936
; branch_weights annotation).

0 commit comments

Comments
 (0)