Skip to content

Commit 4f6d51e

Browse files
committed
[ctx_prof] Insert the ctx prof flattener after the module inliner
1 parent d7fb5b9 commit 4f6d51e

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/Analysis/ScopedNoAliasAA.h"
2626
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
2727
#include "llvm/IR/PassManager.h"
28+
#include "llvm/Pass.h"
2829
#include "llvm/Passes/OptimizationLevel.h"
2930
#include "llvm/Passes/PassBuilder.h"
3031
#include "llvm/Support/CommandLine.h"
@@ -1011,6 +1012,11 @@ PassBuilder::buildModuleInlinerPipeline(OptimizationLevel Level,
10111012
IP.EnableDeferral = false;
10121013

10131014
MPM.addPass(ModuleInlinerPass(IP, UseInlineAdvisor, Phase));
1015+
if (!UseCtxProfile.empty() && Phase == ThinOrFullLTOPhase::ThinLTOPostLink) {
1016+
MPM.addPass(GlobalOptPass());
1017+
MPM.addPass(GlobalDCEPass());
1018+
MPM.addPass(PGOCtxProfFlatteningPass());
1019+
}
10141020

10151021
MPM.addPass(createModuleToFunctionPassAdaptor(
10161022
buildFunctionSimplificationPipeline(Level, Phase),
@@ -1734,11 +1740,14 @@ ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
17341740
MPM.addPass(GlobalDCEPass());
17351741
return MPM;
17361742
}
1737-
1738-
// Add the core simplification pipeline.
1739-
MPM.addPass(buildModuleSimplificationPipeline(
1740-
Level, ThinOrFullLTOPhase::ThinLTOPostLink));
1741-
1743+
if (!UseCtxProfile.empty()) {
1744+
MPM.addPass(
1745+
buildModuleInlinerPipeline(Level, ThinOrFullLTOPhase::ThinLTOPostLink));
1746+
} else {
1747+
// Add the core simplification pipeline.
1748+
MPM.addPass(buildModuleSimplificationPipeline(
1749+
Level, ThinOrFullLTOPhase::ThinLTOPostLink));
1750+
}
17421751
// Now add the optimization pipeline.
17431752
MPM.addPass(buildModuleOptimizationPipeline(
17441753
Level, ThinOrFullLTOPhase::ThinLTOPostLink));

llvm/lib/Transforms/IPO/ModuleInliner.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,10 @@ PreservedAnalyses ModuleInlinerPass::run(Module &M,
241241
// the post-inline cleanup and the next DevirtSCCRepeatedPass
242242
// iteration because the next iteration may not happen and we may
243243
// miss inlining it.
244-
if (tryPromoteCall(*ICB))
245-
NewCallee = ICB->getCalledFunction();
244+
// FIXME: enable for ctxprof.
245+
if (!CtxProf)
246+
if (tryPromoteCall(*ICB))
247+
NewCallee = ICB->getCalledFunction();
246248
}
247249
if (NewCallee)
248250
if (!NewCallee->isDeclaration())

llvm/test/Analysis/CtxProfAnalysis/inline.ll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@
3131
; CHECK-NEXT: %call2 = call i32 @a(i32 %x) #1
3232
; CHECK-NEXT: br label %exit
3333

34+
; Make sure the postlink thinlto pipeline is aware of ctxprof
35+
; RUN: opt -passes='thinlto<O2>' -use-ctx-profile=%t/profile.ctxprofdata \
36+
; RUN: %t/module.ll -S -o - | FileCheck %s --check-prefix=PIPELINE
37+
38+
; PIPELINE-LABEL: define i32 @entrypoint
39+
; PIPELINE-SAME: !prof ![[ENTRYPOINT_COUNT:[0-9]+]]
40+
; PIPELINE-LABEL: loop.i:
41+
; PIPELINE: br i1 %cond.i, label %loop.i, label %exit, !prof ![[LOOP_BW_INL:[0-9]+]]
42+
; PIPELINE-LABEL: define i32 @a
43+
; PIPELINE-LABEL: loop:
44+
; PIPELINE: br i1 %cond, label %loop, label %exit, !prof ![[LOOP_BW_ORIG:[0-9]+]]
45+
46+
; PIPELINE: ![[ENTRYPOINT_COUNT]] = !{!"function_entry_count", i64 10}
47+
; These are the weights of the inlined @a, where the counters were 2, 100 (2 for entry, 100 for loop)
48+
; PIPELINE: ![[LOOP_BW_INL]] = !{!"branch_weights", i32 98, i32 2}
49+
; These are the weights of the un-inlined @a, where the counters were 8, 500 (8 for entry, 500 for loop)
50+
; PIPELINE: ![[LOOP_BW_ORIG]] = !{!"branch_weights", i32 492, i32 8}
3451

3552
;--- module.ll
3653
define i32 @entrypoint(i32 %x) !guid !0 {

llvm/test/Other/opt-hot-cold-split.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: opt -mtriple=x86_64-- -hot-cold-split=true -passes='lto-pre-link<Os>' -debug-pass-manager < %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=LTO-PRELINK-Os
33
; RUN: opt -mtriple=x86_64-- -hot-cold-split=true -passes='thinlto-pre-link<Os>' -debug-pass-manager < %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=THINLTO-PRELINK-Os
44
; RUN: opt -mtriple=x86_64-- -hot-cold-split=true -passes='lto<Os>' -debug-pass-manager < %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=LTO-POSTLINK-Os
5-
; RUN: opt -mtriple=x86_64-- -hot-cold-split=true -passes='thinlto<Os>' -debug-pass-manager < %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=THINLTO-POSTLINK-Os
5+
; RUN: opt -mtriple=x86_64-- -hot-cold-split=true -LINK-Os
66

77
; REQUIRES: asserts
88

0 commit comments

Comments
 (0)