Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ctx_prof] Extend WorkloadImportsManager to use the contextual profile #98682

Merged
merged 9 commits into from
Jul 29, 2024
Merged
17 changes: 14 additions & 3 deletions llvm/lib/Transforms/IPO/FunctionImport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ static cl::opt<std::string> WorkloadDefinitions(
"}"),
cl::Hidden);

static cl::opt<bool> ImportAssumeUniqueLocal(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change should be removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is from #100448?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I was looking at diffs from my last review, and wasn't expecting things from other patches to show up (did it get rebased?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I needed to so I could get the llvm-ctxprof-util tool

"import-assume-unique-local", cl::init(false),
cl::desc(
"By default, a local-linkage global variable won't be imported in the "
"edge mod1:func -> mod2:local-var (from value profiles) since compiler "
"cannot assume mod2 is compiled with full path which gives local-var a "
"program-wide unique GUID. Set this option to true will help cross "
"module import of such variables. This is only safe if the compiler "
"user specify the full module path."),
cl::Hidden);

static cl::opt<std::string>
ContextualProfile("thinlto-pgo-ctx-prof",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test to exercise this handling?

Copy link
Member Author

@mtrofin mtrofin Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't currently have a textual representation of the contextual profile, and the import manager is internal to this file, meaning we can't write a unittest. I'd do either, though, if the new functionality was more involved, but it's really just taking data from the ctx profile reader - which is tested - and filling a set with it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant a lit test. I think it would be good to add (reading the binary format like I am assuming the ctx profile reader tests do?), otherwise this is untested functionality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right - need to figure out where to place it, because it would need to be a test that runs a program to generate a profile. Meaning it needs compiler-rt.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

cl::desc("Path to a contextual profile."), cl::Hidden);
Expand Down Expand Up @@ -679,8 +690,7 @@ class WorkloadImportsManager : public ModuleImportsManager {
}
auto Buffer = std::move(BufferOrErr.get());

BitstreamCursor Cursor(*Buffer);
PGOCtxProfileReader Reader(Cursor);
PGOCtxProfileReader Reader(Buffer->getBuffer());
auto Ctx = Reader.loadContexts();
if (!Ctx) {
report_fatal_error("Failed to parse contextual profiles");
Expand Down Expand Up @@ -731,7 +741,8 @@ class WorkloadImportsManager : public ModuleImportsManager {
}
if (!ContextualProfile.empty())
loadFromCtxProf();
loadFromJson();
else
loadFromJson();
LLVM_DEBUG({
for (const auto &[Root, Set] : Workloads) {
dbgs() << "[Workload] Root: " << Root << " we have " << Set.size()
Expand Down
72 changes: 72 additions & 0 deletions llvm/test/ThinLTO/X86/ctxprof.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
; Test workload based importing via -thinlto-pgo-ctx-prof
; Use external linkage symbols so we don't depend on module paths which are
; used when computing the GUIDs of internal linkage symbols.
; The functionality is shared with what workload.ll tests, so here we only care
; about testing the ctx profile is loaded and handled correctly.
;
; Set up
; RUN: rm -rf %t
; RUN: mkdir -p %t
; RUN: split-file %s %t
;
; RUN: opt -module-summary %t/m1.ll -o %t/m1.bc
; RUN: opt -module-summary %t/m2.ll -o %t/m2.bc
; RUN: llvm-dis %t/m1.bc -o - | FileCheck %s --check-prefix=GUIDS-1
; RUN: llvm-dis %t/m2.bc -o - | FileCheck %s --check-prefix=GUIDS-2
;
; GUIDS-1: name: "m1_f1"
; GUIDS-1-SAME: guid = 6019442868614718803
; GUIDS-2: name: "m2_f1"
; GUIDS-2-SAME: guid = 15593096274670919754
;
; RUN: rm -rf %t_baseline
; RUN: rm -rf %t_exp
; RUN: mkdir -p %t_baseline
; RUN: mkdir -p %t_exp
;
; Normal run. m1 shouldn't get m2_f1 because it's not referenced from there.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and ditto for m2 and m1_f1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

;
; RUN: llvm-lto2 run %t/m1.bc %t/m2.bc \
; RUN: -o %t_baseline/result.o -save-temps \
; RUN: -r %t/m1.bc,m1_f1,plx \
; RUN: -r %t/m2.bc,m2_f1,plx
; RUN: llvm-dis %t_baseline/result.o.1.3.import.bc -o - | FileCheck %s --check-prefix=NOPROF-1
; RUN: llvm-dis %t_baseline/result.o.2.3.import.bc -o - | FileCheck %s --check-prefix=NOPROF-2
;
; NOPROF-1-NOT: m2_f1()
; NOPROF-2-NOT: m1_f1()
;
; The run with workload definitions - same other options.
;
; RUN: echo '[ \
; RUN: {"Guid": 6019442868614718803, "Counters": [1], "Callsites": [[{"Guid": 15593096274670919754, "Counters": [1]}]]}, \
; RUN: {"Guid": 15593096274670919754, "Counters": [1], "Callsites": [[{"Guid": 6019442868614718803, "Counters": [1]}]]} \
; RUN: ]' > %t_exp/ctxprof.json
; RUN: llvm-ctxprof-util fromJSON --input %t_exp/ctxprof.json --output %t_exp/ctxprof.bitstream
; RUN: llvm-lto2 run %t/m1.bc %t/m2.bc \
; RUN: -o %t_exp/result.o -save-temps \
; RUN: -thinlto-pgo-ctx-prof=%t_exp/ctxprof.bitstream \
; RUN: -r %t/m1.bc,m1_f1,plx \
; RUN: -r %t/m2.bc,m2_f1,plx
; RUN: llvm-dis %t_exp/result.o.1.3.import.bc -o - | FileCheck %s --check-prefix=FIRST
; RUN: llvm-dis %t_exp/result.o.2.3.import.bc -o - | FileCheck %s --check-prefix=SECOND
;
;
; FIRST: m2_f1()
; SECOND: m1_f1()
;
;--- m1.ll
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define dso_local void @m1_f1() {
ret void
}

;--- m2.ll
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define dso_local void @m2_f1() {
ret void
}
Loading