Skip to content

Commit 61768b3

Browse files
authored
[ctxprof] Don't import roots elsewhere (#134012)
Block a context root from being imported by its callers. Suppose that happened. Its caller - usually a message pump - inlines its copy of the root. Then it (the root) and whatever it calls will be the non-contextually optimized callee versions.
1 parent b93376f commit 61768b3

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ class ModuleImportsManager {
516516
const ModuleSummaryIndex &Index,
517517
DenseMap<StringRef, FunctionImporter::ExportSetTy> *ExportLists = nullptr)
518518
: IsPrevailing(IsPrevailing), Index(Index), ExportLists(ExportLists) {}
519+
virtual bool canImport(ValueInfo VI) { return true; }
519520

520521
public:
521522
virtual ~ModuleImportsManager() = default;
@@ -544,6 +545,11 @@ class WorkloadImportsManager : public ModuleImportsManager {
544545
// determine if a module's import list should be done by the base
545546
// ModuleImportsManager or by us.
546547
StringMap<DenseSet<ValueInfo>> Workloads;
548+
// Track the roots to avoid importing them due to other callers. We want there
549+
// to be only one variant), for which we optimize according to the contextual
550+
// profile. "Variants" refers to copies due to importing - we want there to be
551+
// just one instance of this function.
552+
DenseSet<ValueInfo> Roots;
547553

548554
void
549555
computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
@@ -783,12 +789,15 @@ class WorkloadImportsManager : public ModuleImportsManager {
783789
}
784790
auto &Set = Workloads[RootDefiningModule];
785791
Root.getContainedGuids(ContainedGUIDs);
792+
Roots.insert(RootVI);
786793
for (auto Guid : ContainedGUIDs)
787794
if (auto VI = Index.getValueInfo(Guid))
788795
Set.insert(VI);
789796
}
790797
}
791798

799+
bool canImport(ValueInfo VI) override { return !Roots.contains(VI); }
800+
792801
public:
793802
WorkloadImportsManager(
794803
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
@@ -886,6 +895,15 @@ void ModuleImportsManager::computeImportForFunction(
886895
continue;
887896
}
888897

898+
if (!canImport(VI)) {
899+
LLVM_DEBUG(
900+
dbgs() << "Skipping over " << VI.getGUID()
901+
<< " because its import is handled in a different module.");
902+
assert(VI.getSummaryList().size() == 1 &&
903+
"The root was expected to be an external symbol");
904+
continue;
905+
}
906+
889907
auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
890908
if (Hotness == CalleeInfo::HotnessType::Hot)
891909
return ImportHotMultiplier;

llvm/test/ThinLTO/X86/ctxprof-separate-module.ll

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
; REQUIRES: asserts
12
; Test workload based importing via -thinlto-pgo-ctx-prof with moving the whole
23
; graph to a new module.
34
; Use external linkage symbols so we don't depend on module paths which are
@@ -10,19 +11,25 @@
1011
;
1112
; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/m1.ll -o %t/m1.bc
1213
; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/m2.ll -o %t/m2.bc
14+
; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/m3.ll -o %t/m3.bc
1315
; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/6019442868614718803.ll -o %t/6019442868614718803.bc
1416

1517
; RUN: llvm-ctxprof-util fromYAML --input %t/ctxprof.yaml --output %t/ctxprof.bitstream
16-
; RUN: llvm-lto2 run %t/m1.bc %t/m2.bc %t/6019442868614718803.bc -thinlto-move-ctxprof-trees \
18+
; RUN: llvm-lto2 run %t/m1.bc %t/m2.bc %t/m3.bc %t/6019442868614718803.bc -thinlto-move-ctxprof-trees \
1719
; RUN: -o %t/result.o -save-temps \
1820
; RUN: -use-ctx-profile=%t/ctxprof.bitstream \
1921
; RUN: -r %t/m1.bc,m1_f1,plx \
20-
; RUN: -r %t/m2.bc,m2_f1,plx
21-
; RUN: llvm-dis %t/result.o.3.3.import.bc -o - | FileCheck %s
22+
; RUN: -r %t/m2.bc,m2_f1,plx \
23+
; RUN: -r %t/m3.bc,m1_f1 \
24+
; RUN: -r %t/m3.bc,m3_f1,plx -debug-only=function-import 2>&1 | FileCheck %s --check-prefix=ABSENT-MSG
25+
; RUN: llvm-dis %t/result.o.4.3.import.bc -o - | FileCheck %s
26+
; RUN: llvm-dis %t/result.o.3.3.import.bc -o - | FileCheck %s --check-prefix=ABSENT
2227
;
2328
;
2429
; CHECK: m1_f1()
2530
; CHECK: m2_f1()
31+
; ABSENT: declare void @m1_f1()
32+
; ABSENT-MSG: Skipping over 6019442868614718803 because its import is handled in a different module.
2633
;
2734
;--- ctxprof.yaml
2835
Contexts:
@@ -51,6 +58,17 @@ define dso_local void @m2_f1() {
5158
ret void
5259
}
5360

61+
;--- m3.ll
62+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
63+
target triple = "x86_64-pc-linux-gnu"
64+
65+
declare void @m1_f1()
66+
67+
define dso_local void @m3_f1() {
68+
call void @m1_f1()
69+
ret void
70+
}
71+
5472
;--- 6019442868614718803.ll
5573
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5674
target triple = "x86_64-pc-linux-gnu"

0 commit comments

Comments
 (0)