Skip to content

Commit 96bf973

Browse files
committed
[ctxprof] Option to move a whole tree to its own module
1 parent 8741412 commit 96bf973

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 26 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,13 @@ 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+
Filename = Filename.substr(0, Filename.find_last_of('.'));
549+
}
550+
auto SetIter = Workloads.find(Filename);
551+
539552
if (SetIter == Workloads.end()) {
540553
LLVM_DEBUG(dbgs() << "[Workload] " << ModName
541554
<< " does not contain the root of any context.\n");
@@ -748,10 +761,18 @@ class WorkloadImportsManager : public ModuleImportsManager {
748761
<< RootVI.getSummaryList().size() << ". Skipping.\n");
749762
continue;
750763
}
751-
StringRef RootDefiningModule =
752-
RootVI.getSummaryList().front()->modulePath();
753-
LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid
754-
<< " is : " << RootDefiningModule << "\n");
764+
std::string RootDefiningModule =
765+
RootVI.getSummaryList().front()->modulePath().str();
766+
if (CtxprofMoveRootsToOwnModule) {
767+
RootDefiningModule = std::to_string(RootGuid);
768+
LLVM_DEBUG(
769+
dbgs() << "[Workload] Moving " << RootGuid
770+
<< " to a module with the filename without extension : "
771+
<< RootDefiningModule << "\n");
772+
} else {
773+
LLVM_DEBUG(dbgs() << "[Workload] Root defining module for " << RootGuid
774+
<< " is : " << RootDefiningModule << "\n");
775+
}
755776
auto &Set = Workloads[RootDefiningModule];
756777
Root.getContainedGuids(ContainedGUIDs);
757778
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)