-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[SimplifyCFG][PGO] Reuse existing setBranchWeights
#160629
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,15 @@ | |
|
||
#include "llvm/IR/ProfDataUtils.h" | ||
|
||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/MDBuilder.h" | ||
#include "llvm/IR/Metadata.h" | ||
#include "llvm/Support/CommandLine.h" | ||
|
||
using namespace llvm; | ||
|
||
|
@@ -84,10 +86,31 @@ static void extractFromBranchWeightMD(const MDNode *ProfileData, | |
} | ||
} | ||
|
||
/// Push the weights right to fit in uint32_t. | ||
static SmallVector<uint32_t> fitWeights(ArrayRef<uint64_t> Weights) { | ||
SmallVector<uint32_t> Ret; | ||
Ret.reserve(Weights.size()); | ||
uint64_t Max = *llvm::max_element(Weights); | ||
if (Max > UINT_MAX) { | ||
unsigned Offset = 32 - llvm::countl_zero(Max); | ||
for (const uint64_t &Value : Weights) | ||
Ret.push_back(static_cast<uint32_t>(Value >> Offset)); | ||
} else { | ||
append_range(Ret, Weights); | ||
} | ||
return Ret; | ||
} | ||
|
||
} // namespace | ||
|
||
namespace llvm { | ||
|
||
cl::opt<bool> ElideAllZeroBranchWeights("elide-all-zero-branch-weights", | ||
#if defined(LLVM_ENABLE_PROFCHECK) | ||
cl::init(false) | ||
#else | ||
cl::init(true) | ||
#endif | ||
); | ||
const char *MDProfLabels::BranchWeights = "branch_weights"; | ||
const char *MDProfLabels::ExpectedBranchWeights = "expected"; | ||
const char *MDProfLabels::ValueProfile = "VP"; | ||
|
@@ -282,12 +305,23 @@ bool hasExplicitlyUnknownBranchWeights(const Instruction &I) { | |
} | ||
|
||
void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights, | ||
bool IsExpected) { | ||
bool IsExpected, bool ElideAllZero) { | ||
if ((ElideAllZeroBranchWeights && ElideAllZero) && | ||
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. should be || or && ? With &&, the zeros can not be elided (and default behavior is changed). 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. ElideAllZeroBranchWeights is true by default. It's only false when profcheck is enabled. Then, the SimplifyCFG callers, which were the place where zero-elision was happening, call the new API with 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. should the default parameter to be true instead of 'false'? 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.
no, because the behavior elsewhere was to not elide. Only SimplifyCFG had an eliding variant (I mean, there could be other passes with local variants, and we'll address accordingly) 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. I could rename the compiler flag to |
||
llvm::all_of(Weights, [](uint32_t V) { return V == 0; })) { | ||
I.setMetadata(LLVMContext::MD_prof, nullptr); | ||
return; | ||
} | ||
|
||
MDBuilder MDB(I.getContext()); | ||
MDNode *BranchWeights = MDB.createBranchWeights(Weights, IsExpected); | ||
I.setMetadata(LLVMContext::MD_prof, BranchWeights); | ||
} | ||
|
||
void setFittedBranchWeights(Instruction &I, ArrayRef<uint64_t> Weights, | ||
bool IsExpected, bool ElideAllZero) { | ||
setBranchWeights(I, fitWeights(Weights), IsExpected, ElideAllZero); | ||
} | ||
|
||
SmallVector<uint32_t> downscaleWeights(ArrayRef<uint64_t> Weights, | ||
std::optional<uint64_t> KnownMaxCount) { | ||
uint64_t MaxCount = KnownMaxCount.has_value() ? KnownMaxCount.value() | ||
|
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.
nit:
SmallVector
has a constructor that accepts asize_t
, so this can be combined with the previous line.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.
that
resize
s. I wantedreserve
, so I can use push_back