Skip to content

Commit 02467f9

Browse files
authored
[ctxprof] Option to move a whole tree to its own module (llvm#133992)
Modules may contain a mix of functions that participate or don't participate in callgraphs covered by a contextual profile. We currently have been importing all the functions under a context root in the module defining that root, but if the other functions there are covered by flat profiles, the result is difficult to reason about. This patch allows moving everything under a context root (and that root) in its own module. For now, we expect a module with a filename matching the GUID of the function be present in the set of modules known by the linker. This mechanism can be improved in a later patch. Subsequent patches will handle implementing "move" instead of "import" semantics for the root function (because we want to make sure only one version of the root exists - so the optimizations we perform are actually the ones being observed at runtime).
1 parent fb7135e commit 02467f9

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/Support/ErrorHandling.h"
3939
#include "llvm/Support/FileSystem.h"
4040
#include "llvm/Support/JSON.h"
41+
#include "llvm/Support/Path.h"
4142
#include "llvm/Support/SourceMgr.h"
4243
#include "llvm/Support/raw_ostream.h"
4344
#include "llvm/Transforms/IPO/Internalize.h"
@@ -175,6 +176,12 @@ static cl::opt<std::string> WorkloadDefinitions(
175176

176177
extern cl::opt<std::string> UseCtxProfile;
177178

179+
static cl::opt<bool> CtxprofMoveRootsToOwnModule(
180+
"thinlto-move-ctxprof-trees",
181+
cl::desc("Move contextual profiling roots and the graphs under them in "
182+
"their own module."),
183+
cl::Hidden, cl::init(false));
184+
178185
namespace llvm {
179186
extern cl::opt<bool> EnableMemProfContextDisambiguation;
180187
}
@@ -535,7 +542,14 @@ class WorkloadImportsManager : public ModuleImportsManager {
535542
computeImportForModule(const GVSummaryMapTy &DefinedGVSummaries,
536543
StringRef ModName,
537544
FunctionImporter::ImportMapTy &ImportList) override {
538-
auto SetIter = Workloads.find(ModName);
545+
StringRef Filename = ModName;
546+
if (CtxprofMoveRootsToOwnModule) {
547+
Filename = sys::path::filename(ModName);
548+
// Drop the file extension.
549+
Filename = Filename.substr(0, Filename.find_last_of('.'));
550+
}
551+
auto SetIter = Workloads.find(Filename);
552+
539553
if (SetIter == Workloads.end()) {
540554
LLVM_DEBUG(dbgs() << "[Workload] " << ModName
541555
<< " does not contain the root of any context.\n");
@@ -748,10 +762,18 @@ class WorkloadImportsManager : public ModuleImportsManager {
748762
<< RootVI.getSummaryList().size() << ". Skipping.\n");
749763
continue;
750764
}
751-
StringRef RootDefiningModule =
752-
RootVI.getSummaryList().front()->modulePath();
753-
LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid
754-
<< " is : " << RootDefiningModule << "\n");
765+
std::string RootDefiningModule =
766+
RootVI.getSummaryList().front()->modulePath().str();
767+
if (CtxprofMoveRootsToOwnModule) {
768+
RootDefiningModule = std::to_string(RootGuid);
769+
LLVM_DEBUG(
770+
dbgs() << "[Workload] Moving " << RootGuid
771+
<< " to a module with the filename without extension : "
772+
<< RootDefiningModule << "\n");
773+
} else {
774+
LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid
775+
<< " is : " << RootDefiningModule << "\n");
776+
}
755777
auto &Set = Workloads[RootDefiningModule];
756778
Root.getContainedGuids(ContainedGUIDs);
757779
for (auto Guid : ContainedGUIDs)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; Test workload based importing via -thinlto-pgo-ctx-prof with moving the whole
2+
; graph to a new module.
3+
; Use external linkage symbols so we don't depend on module paths which are
4+
; used when computing the GUIDs of internal linkage symbols.
5+
;
6+
; Set up
7+
; RUN: rm -rf %t
8+
; RUN: mkdir -p %t
9+
; RUN: split-file %s %t
10+
;
11+
; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/m1.ll -o %t/m1.bc
12+
; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/m2.ll -o %t/m2.bc
13+
; RUN: opt -module-summary -passes=assign-guid,ctx-instr-gen %t/6019442868614718803.ll -o %t/6019442868614718803.bc
14+
15+
; 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 \
17+
; RUN: -o %t/result.o -save-temps \
18+
; RUN: -use-ctx-profile=%t/ctxprof.bitstream \
19+
; 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+
;
23+
;
24+
; CHECK: m1_f1()
25+
; CHECK: m2_f1()
26+
;
27+
;--- ctxprof.yaml
28+
Contexts:
29+
-
30+
Guid: 6019442868614718803
31+
TotalRootEntryCount: 5
32+
Counters: [1]
33+
Callsites:
34+
- -
35+
Guid: 15593096274670919754
36+
Counters: [1]
37+
38+
;--- m1.ll
39+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
40+
target triple = "x86_64-pc-linux-gnu"
41+
42+
define dso_local void @m1_f1() {
43+
ret void
44+
}
45+
46+
;--- m2.ll
47+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
48+
target triple = "x86_64-pc-linux-gnu"
49+
50+
define dso_local void @m2_f1() {
51+
ret void
52+
}
53+
54+
;--- 6019442868614718803.ll
55+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
56+
target triple = "x86_64-pc-linux-gnu"

0 commit comments

Comments
 (0)