-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[ctx_prof] CtxProfAnalysis: populate module data #102930
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f23165
[ctx_prof] CtxProfAnalysis: populate module data
mtrofin 58330d2
comments
mtrofin a9ec16a
Merge branch 'main' into ctx_analysis
mtrofin 862842b
addressed comments
mtrofin 9ef952e
moved AssignUniqueIDPass to Analysis so that llvm-link can build (als…
mtrofin 4cbb13a
Make tests resilient to test path
mtrofin 4a596bd
comments: mostly renamed stuff, also setting `source_filename` in tes…
mtrofin 1c4738a
Merge branch 'main' into ctx_analysis
mtrofin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,12 +14,14 @@ | |
#include "llvm/Analysis/CtxProfAnalysis.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/IR/Analysis.h" | ||
#include "llvm/IR/IntrinsicInst.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/ProfileData/PGOCtxProfReader.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/JSON.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h" | ||
|
||
#define DEBUG_TYPE "ctx_prof" | ||
|
||
|
@@ -64,10 +66,39 @@ Value toJSON(const PGOCtxProfContext::CallTargetMapTy &P) { | |
} // namespace json | ||
} // namespace llvm | ||
|
||
const char *AssignGUIDPass::GUIDMetadataName = "guid"; | ||
|
||
PreservedAnalyses AssignGUIDPass::run(Module &M, ModuleAnalysisManager &MAM) { | ||
for (auto &F : M.functions()) { | ||
if (F.isDeclaration()) | ||
continue; | ||
if (F.getMetadata(GUIDMetadataName)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the behaviour that we only assign a new guid if it does not exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
continue; | ||
const GlobalValue::GUID GUID = F.getGUID(); | ||
F.setMetadata(GUIDMetadataName, | ||
MDNode::get(M.getContext(), | ||
{ConstantAsMetadata::get(ConstantInt::get( | ||
Type::getInt64Ty(M.getContext()), GUID))})); | ||
} | ||
return PreservedAnalyses::none(); | ||
} | ||
|
||
GlobalValue::GUID AssignGUIDPass::getGUID(const Function &F) { | ||
if (F.isDeclaration()) { | ||
assert(GlobalValue::isExternalLinkage(F.getLinkage())); | ||
return GlobalValue::getGUID(F.getGlobalIdentifier()); | ||
} | ||
auto *MD = F.getMetadata(GUIDMetadataName); | ||
assert(MD && "guid not found for defined function"); | ||
return cast<ConstantInt>(cast<ConstantAsMetadata>(MD->getOperand(0)) | ||
->getValue() | ||
->stripPointerCasts()) | ||
->getZExtValue(); | ||
} | ||
AnalysisKey CtxProfAnalysis::Key; | ||
|
||
CtxProfAnalysis::Result CtxProfAnalysis::run(Module &M, | ||
ModuleAnalysisManager &MAM) { | ||
PGOContextualProfile CtxProfAnalysis::run(Module &M, | ||
ModuleAnalysisManager &MAM) { | ||
ErrorOr<std::unique_ptr<MemoryBuffer>> MB = MemoryBuffer::getFile(Profile); | ||
if (auto EC = MB.getError()) { | ||
M.getContext().emitError("could not open contextual profile file: " + | ||
|
@@ -81,7 +112,55 @@ CtxProfAnalysis::Result CtxProfAnalysis::run(Module &M, | |
toString(MaybeCtx.takeError())); | ||
return {}; | ||
} | ||
return Result(std::move(*MaybeCtx)); | ||
|
||
PGOContextualProfile Result; | ||
|
||
for (const auto &F : M) { | ||
if (F.isDeclaration()) | ||
continue; | ||
auto GUID = AssignGUIDPass::getGUID(F); | ||
assert(GUID && "guid not found for defined function"); | ||
const auto &Entry = F.begin(); | ||
uint32_t MaxCounters = 0; // we expect at least a counter. | ||
for (const auto &I : *Entry) | ||
if (auto *C = dyn_cast<InstrProfIncrementInst>(&I)) { | ||
MaxCounters = | ||
static_cast<uint32_t>(C->getNumCounters()->getZExtValue()); | ||
break; | ||
} | ||
if (!MaxCounters) | ||
continue; | ||
uint32_t MaxCallsites = 0; | ||
for (const auto &BB : F) | ||
for (const auto &I : BB) | ||
if (auto *C = dyn_cast<InstrProfCallsite>(&I)) { | ||
MaxCallsites = | ||
static_cast<uint32_t>(C->getNumCounters()->getZExtValue()); | ||
break; | ||
} | ||
auto [It, Ins] = Result.FuncInfo.insert( | ||
{GUID, PGOContextualProfile::FunctionInfo(F.getName())}); | ||
(void)Ins; | ||
assert(Ins); | ||
It->second.NextCallsiteIndex = MaxCallsites; | ||
It->second.NextCounterIndex = MaxCounters; | ||
} | ||
// If we made it this far, the Result is valid - which we mark by setting | ||
// .Profiles. | ||
// Trim first the roots that aren't in this module. | ||
DenseSet<GlobalValue::GUID> ProfiledGUIDs; | ||
for (auto &[RootGuid, _] : llvm::make_early_inc_range(*MaybeCtx)) | ||
if (!Result.FuncInfo.contains(RootGuid)) | ||
MaybeCtx->erase(RootGuid); | ||
Result.Profiles = std::move(*MaybeCtx); | ||
return Result; | ||
} | ||
|
||
GlobalValue::GUID | ||
PGOContextualProfile::getDefinedFunctionGUID(const Function &F) const { | ||
if (auto It = FuncInfo.find(AssignGUIDPass::getGUID(F)); It != FuncInfo.end()) | ||
return It->first; | ||
return 0; | ||
} | ||
|
||
PreservedAnalyses CtxProfAnalysisPrinterPass::run(Module &M, | ||
|
@@ -91,8 +170,16 @@ PreservedAnalyses CtxProfAnalysisPrinterPass::run(Module &M, | |
M.getContext().emitError("Invalid CtxProfAnalysis"); | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
OS << "Function Info:\n"; | ||
for (const auto &[Guid, FuncInfo] : C.FuncInfo) | ||
OS << Guid << " : " << FuncInfo.Name | ||
<< ". MaxCounterID: " << FuncInfo.NextCounterIndex | ||
<< ". MaxCallsiteID: " << FuncInfo.NextCallsiteIndex << "\n"; | ||
|
||
const auto JSONed = ::llvm::json::toJSON(C.profiles()); | ||
|
||
OS << "\nCurrent Profile:\n"; | ||
OS << formatv("{0:2}", JSONed); | ||
OS << "\n"; | ||
return PreservedAnalyses::all(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
; REQUIRES: x86_64-linux | ||
; | ||
; RUN: rm -rf %t | ||
; RUN: split-file %s %t | ||
; | ||
; Test that the GUID metadata survives through thinlink. | ||
; | ||
; RUN: llvm-ctxprof-util fromJSON --input=%t/profile.json --output=%t/profile.ctxprofdata | ||
; | ||
; RUN: opt -module-summary -passes='thinlto-pre-link<O2>' -use-ctx-profile=%t/profile.ctxprofdata -o %t/m1.bc %t/m1.ll | ||
; RUN: opt -module-summary -passes='thinlto-pre-link<O2>' -use-ctx-profile=%t/profile.ctxprofdata -o %t/m2.bc %t/m2.ll | ||
; | ||
; RUN: rm -rf %t/postlink | ||
; RUN: mkdir %t/postlink | ||
; | ||
; | ||
; RUN: llvm-lto2 run %t/m1.bc %t/m2.bc -o %t/ -thinlto-distributed-indexes \ | ||
; RUN: -use-ctx-profile=%t/profile.ctxprofdata \ | ||
; RUN: -r %t/m1.bc,f1,plx \ | ||
; RUN: -r %t/m2.bc,f1 \ | ||
; RUN: -r %t/m2.bc,entrypoint,plx | ||
; RUN: opt --passes='function-import,require<ctx-prof-analysis>,print<ctx-prof-analysis>' \ | ||
; RUN: -summary-file=%t/m2.bc.thinlto.bc -use-ctx-profile=%t/profile.ctxprofdata %t/m2.bc \ | ||
; RUN: -S -o %t/m2.post.ll 2> %t/profile.txt | ||
; RUN: diff %t/expected.txt %t/profile.txt | ||
;--- 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" | ||
|
||
source_filename = "random_path/m1.cc" | ||
|
||
define private void @f2() #0 !guid !0 { | ||
ret void | ||
} | ||
|
||
define void @f1() #0 { | ||
call void @f2() | ||
ret void | ||
} | ||
|
||
attributes #0 = { noinline } | ||
!0 = !{ i64 3087265239403591524 } | ||
|
||
;--- 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" | ||
|
||
source_filename = "random_path/m2.cc" | ||
|
||
declare void @f1() | ||
|
||
define void @entrypoint() { | ||
call void @f1() | ||
ret void | ||
} | ||
;--- profile.json | ||
[ | ||
{ | ||
"Callsites": [ | ||
[ | ||
{ | ||
"Callsites": [ | ||
[ | ||
{ | ||
"Counters": [ | ||
10 | ||
], | ||
"Guid": 3087265239403591524 | ||
} | ||
] | ||
], | ||
"Counters": [ | ||
7 | ||
], | ||
"Guid": 2072045998141807037 | ||
} | ||
] | ||
], | ||
"Counters": [ | ||
1 | ||
], | ||
"Guid": 10507721908651011566 | ||
} | ||
] | ||
;--- expected.txt | ||
Function Info: | ||
10507721908651011566 : entrypoint. MaxCounterID: 1. MaxCallsiteID: 1 | ||
3087265239403591524 : f2.llvm.0. MaxCounterID: 1. MaxCallsiteID: 0 | ||
2072045998141807037 : f1. MaxCounterID: 1. MaxCallsiteID: 1 | ||
|
||
Current Profile: | ||
[ | ||
{ | ||
"Callsites": [ | ||
[ | ||
{ | ||
"Callsites": [ | ||
[ | ||
{ | ||
"Counters": [ | ||
10 | ||
], | ||
"Guid": 3087265239403591524 | ||
} | ||
] | ||
], | ||
"Counters": [ | ||
7 | ||
], | ||
"Guid": 2072045998141807037 | ||
} | ||
] | ||
], | ||
"Counters": [ | ||
1 | ||
], | ||
"Guid": 10507721908651011566 | ||
} | ||
] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this performing the lookup 2x on an assert build? If so maybe rewrite this as
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya, but I wanted the code to be more readable - i.e. the assertion is that the function is known rather than its implementation.