-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ctx_prof] CtxProfAnalysis #102084
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
[ctx_prof] CtxProfAnalysis #102084
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6678970
[ctx_prof] CtxProfAnalysis
mtrofin 3ecd4d0
clang-format
mtrofin 6785fb0
comments
mtrofin e2ae86b
Merge branch 'main' into ctxprof_analysis
mtrofin f423a87
Fixed post- branch merge; also moved the `Result`
mtrofin 8b00e8c
Merge branch 'main' into ctxprof_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//===- CtxProfAnalysis.h - maintain contextual profile info -*- C++ ---*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
#ifndef LLVM_ANALYSIS_CTXPROFANALYSIS_H | ||
#define LLVM_ANALYSIS_CTXPROFANALYSIS_H | ||
|
||
#include "llvm/IR/GlobalValue.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/ProfileData/PGOCtxProfReader.h" | ||
#include <map> | ||
|
||
namespace llvm { | ||
|
||
class CtxProfAnalysis; | ||
|
||
/// The instrumented contextual profile, produced by the CtxProfAnalysis. | ||
class PGOContextualProfile { | ||
std::optional<PGOCtxProfContext::CallTargetMapTy> Profiles; | ||
|
||
public: | ||
explicit PGOContextualProfile(PGOCtxProfContext::CallTargetMapTy &&Profiles) | ||
: Profiles(std::move(Profiles)) {} | ||
PGOContextualProfile() = default; | ||
PGOContextualProfile(const PGOContextualProfile &) = delete; | ||
PGOContextualProfile(PGOContextualProfile &&) = default; | ||
|
||
operator bool() const { return Profiles.has_value(); } | ||
|
||
const PGOCtxProfContext::CallTargetMapTy &profiles() const { | ||
return *Profiles; | ||
} | ||
|
||
bool invalidate(Module &, const PreservedAnalyses &PA, | ||
ModuleAnalysisManager::Invalidator &) { | ||
// Check whether the analysis has been explicitly invalidated. Otherwise, | ||
// it's stateless and remains preserved. | ||
auto PAC = PA.getChecker<CtxProfAnalysis>(); | ||
return !PAC.preservedWhenStateless(); | ||
} | ||
}; | ||
|
||
class CtxProfAnalysis : public AnalysisInfoMixin<CtxProfAnalysis> { | ||
StringRef Profile; | ||
|
||
public: | ||
static AnalysisKey Key; | ||
explicit CtxProfAnalysis(StringRef Profile) : Profile(Profile) {}; | ||
|
||
using Result = PGOContextualProfile; | ||
|
||
PGOContextualProfile run(Module &M, ModuleAnalysisManager &MAM); | ||
}; | ||
|
||
class CtxProfAnalysisPrinterPass | ||
: public PassInfoMixin<CtxProfAnalysisPrinterPass> { | ||
raw_ostream &OS; | ||
|
||
public: | ||
explicit CtxProfAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {} | ||
|
||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); | ||
static bool isRequired() { return true; } | ||
}; | ||
} // namespace llvm | ||
#endif // LLVM_ANALYSIS_CTXPROFANALYSIS_H |
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,95 @@ | ||
//===- CtxProfAnalysis.cpp - contextual profile analysis ------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Implementation of the contextual profile analysis, which maintains contextual | ||
// profiling info through IPO passes. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Analysis/CtxProfAnalysis.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/IR/Analysis.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/ProfileData/PGOCtxProfReader.h" | ||
#include "llvm/Support/JSON.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
|
||
#define DEBUG_TYPE "ctx_prof" | ||
|
||
namespace llvm { | ||
namespace json { | ||
Value toJSON(const PGOCtxProfContext &P) { | ||
Object Ret; | ||
Ret["Guid"] = P.guid(); | ||
Ret["Counters"] = Array(P.counters()); | ||
if (P.callsites().empty()) | ||
return Ret; | ||
auto AllCS = | ||
::llvm::map_range(P.callsites(), [](const auto &P) { return P.first; }); | ||
auto MaxIt = ::llvm::max_element(AllCS); | ||
assert(MaxIt != AllCS.end() && "We should have a max value because the " | ||
"callsites collection is not empty."); | ||
Array CSites; | ||
// Iterate to, and including, the maximum index. | ||
for (auto I = 0U, Max = *MaxIt; I <= Max; ++I) { | ||
CSites.push_back(Array()); | ||
Array &Targets = *CSites.back().getAsArray(); | ||
if (P.hasCallsite(I)) | ||
for (const auto &[_, Ctx] : P.callsite(I)) | ||
Targets.push_back(toJSON(Ctx)); | ||
} | ||
Ret["Callsites"] = std::move(CSites); | ||
|
||
return Ret; | ||
} | ||
|
||
Value toJSON(const PGOCtxProfContext::CallTargetMapTy &P) { | ||
Array Ret; | ||
for (const auto &[_, Ctx] : P) | ||
Ret.push_back(toJSON(Ctx)); | ||
return Ret; | ||
} | ||
} // namespace json | ||
} // namespace llvm | ||
|
||
using namespace llvm; | ||
|
||
AnalysisKey CtxProfAnalysis::Key; | ||
|
||
CtxProfAnalysis::Result 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: " + | ||
EC.message()); | ||
return {}; | ||
} | ||
PGOCtxProfileReader Reader(MB.get()->getBuffer()); | ||
auto MaybeCtx = Reader.loadContexts(); | ||
if (!MaybeCtx) { | ||
M.getContext().emitError("contextual profile file is invalid: " + | ||
toString(MaybeCtx.takeError())); | ||
return {}; | ||
} | ||
return Result(std::move(*MaybeCtx)); | ||
} | ||
|
||
PreservedAnalyses CtxProfAnalysisPrinterPass::run(Module &M, | ||
ModuleAnalysisManager &MAM) { | ||
CtxProfAnalysis::Result &C = MAM.getResult<CtxProfAnalysis>(M); | ||
if (!C) { | ||
M.getContext().emitError("Invalid CtxProfAnalysis"); | ||
return PreservedAnalyses::all(); | ||
} | ||
const auto JSONed = ::llvm::json::toJSON(C.profiles()); | ||
|
||
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,56 @@ | ||
; RUN: split-file %s %t | ||
; RUN: llvm-ctxprof-util fromJSON --input=%t/profile.json --output=%t/profile.ctxprofdata | ||
; RUN: not opt -passes='require<ctx-prof-analysis>,print<ctx-prof-analysis>' \ | ||
; RUN: %t/empty.ll -S 2>&1 | FileCheck %s --check-prefix=NO-FILE | ||
|
||
; RUN: not opt -passes='require<ctx-prof-analysis>,print<ctx-prof-analysis>' \ | ||
; RUN: -use-ctx-profile=does_not_exist.ctxprofdata %t/empty.ll -S 2>&1 | FileCheck %s --check-prefix=NO-FILE | ||
|
||
; RUN: opt -passes='require<ctx-prof-analysis>,print<ctx-prof-analysis>' \ | ||
; RUN: -use-ctx-profile=%t/profile.ctxprofdata %t/empty.ll -S 2> %t/output.json | ||
; RUN: diff %t/profile.json %t/output.json | ||
|
||
; NO-FILE: error: could not open contextual profile file | ||
; | ||
; This is the reference profile, laid out in the format the json formatter will | ||
; output it from opt. | ||
;--- profile.json | ||
[ | ||
{ | ||
"Callsites": [ | ||
[], | ||
[ | ||
{ | ||
"Counters": [ | ||
4, | ||
5 | ||
], | ||
"Guid": 2000 | ||
}, | ||
{ | ||
"Counters": [ | ||
6, | ||
7, | ||
8 | ||
], | ||
"Guid": 18446744073709551613 | ||
} | ||
] | ||
], | ||
"Counters": [ | ||
1, | ||
2, | ||
3 | ||
], | ||
"Guid": 1000 | ||
}, | ||
{ | ||
"Counters": [ | ||
5, | ||
9, | ||
10 | ||
], | ||
"Guid": 18446744073709551612 | ||
} | ||
] | ||
;--- empty.ll |
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.
Uh oh!
There was an error while loading. Please reload this page.