diff options
author | orivej <orivej@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:01 +0300 |
commit | 2d37894b1b037cf24231090eda8589bbb44fb6fc (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/libs/llvm12/lib/Analysis/IndirectCallPromotionAnalysis.cpp | |
parent | 718c552901d703c502ccbefdfc3c9028d608b947 (diff) | |
download | ydb-2d37894b1b037cf24231090eda8589bbb44fb6fc.tar.gz |
Restoring authorship annotation for <orivej@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/llvm12/lib/Analysis/IndirectCallPromotionAnalysis.cpp')
-rw-r--r-- | contrib/libs/llvm12/lib/Analysis/IndirectCallPromotionAnalysis.cpp | 206 |
1 files changed, 103 insertions, 103 deletions
diff --git a/contrib/libs/llvm12/lib/Analysis/IndirectCallPromotionAnalysis.cpp b/contrib/libs/llvm12/lib/Analysis/IndirectCallPromotionAnalysis.cpp index be00dcbd2d..b112ed2e44 100644 --- a/contrib/libs/llvm12/lib/Analysis/IndirectCallPromotionAnalysis.cpp +++ b/contrib/libs/llvm12/lib/Analysis/IndirectCallPromotionAnalysis.cpp @@ -1,104 +1,104 @@ -//===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// -// Helper methods for identifying profitable indirect call promotion -// candidates for an instruction when the indirect-call value profile metadata -// is available. -// -//===----------------------------------------------------------------------===// - -#include "llvm/Analysis/IndirectCallPromotionAnalysis.h" -#include "llvm/ADT/STLExtras.h" -#include "llvm/Analysis/IndirectCallVisitor.h" -#include "llvm/IR/InstIterator.h" -#include "llvm/IR/InstVisitor.h" -#include "llvm/IR/Instructions.h" -#include "llvm/IR/IntrinsicInst.h" -#include "llvm/ProfileData/InstrProf.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/Debug.h" +//===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// Helper methods for identifying profitable indirect call promotion +// candidates for an instruction when the indirect-call value profile metadata +// is available. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/IndirectCallPromotionAnalysis.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Analysis/IndirectCallVisitor.h" +#include "llvm/IR/InstIterator.h" +#include "llvm/IR/InstVisitor.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/IntrinsicInst.h" +#include "llvm/ProfileData/InstrProf.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" #include <memory> - -using namespace llvm; - -#define DEBUG_TYPE "pgo-icall-prom-analysis" - -// The percent threshold for the direct-call target (this call site vs the -// remaining call count) for it to be considered as the promotion target. -static cl::opt<unsigned> ICPRemainingPercentThreshold( - "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore, - cl::desc("The percentage threshold against remaining unpromoted indirect " - "call count for the promotion")); - -// The percent threshold for the direct-call target (this call site vs the -// total call count) for it to be considered as the promotion target. -static cl::opt<unsigned> - ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5), - cl::Hidden, cl::ZeroOrMore, - cl::desc("The percentage threshold against total " - "count for the promotion")); - -// Set the maximum number of targets to promote for a single indirect-call -// callsite. -static cl::opt<unsigned> - MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore, - cl::desc("Max number of promotions for a single indirect " - "call callsite")); - -ICallPromotionAnalysis::ICallPromotionAnalysis() { - ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions); -} - -bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count, - uint64_t TotalCount, - uint64_t RemainingCount) { - return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount && - Count * 100 >= ICPTotalPercentThreshold * TotalCount; -} - -// Indirect-call promotion heuristic. The direct targets are sorted based on -// the count. Stop at the first target that is not promoted. Returns the -// number of candidates deemed profitable. -uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates( - const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) { - ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals); - - LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst - << " Num_targets: " << NumVals << "\n"); - - uint32_t I = 0; - uint64_t RemainingCount = TotalCount; - for (; I < MaxNumPromotions && I < NumVals; I++) { - uint64_t Count = ValueDataRef[I].Count; - assert(Count <= RemainingCount); - LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count - << " Target_func: " << ValueDataRef[I].Value << "\n"); - - if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) { - LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n"); - return I; - } - RemainingCount -= Count; - } - return I; -} - -ArrayRef<InstrProfValueData> -ICallPromotionAnalysis::getPromotionCandidatesForInstruction( - const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount, - uint32_t &NumCandidates) { - bool Res = - getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions, - ValueDataArray.get(), NumVals, TotalCount); - if (!Res) { - NumCandidates = 0; - return ArrayRef<InstrProfValueData>(); - } - NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount); - return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals); -} + +using namespace llvm; + +#define DEBUG_TYPE "pgo-icall-prom-analysis" + +// The percent threshold for the direct-call target (this call site vs the +// remaining call count) for it to be considered as the promotion target. +static cl::opt<unsigned> ICPRemainingPercentThreshold( + "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore, + cl::desc("The percentage threshold against remaining unpromoted indirect " + "call count for the promotion")); + +// The percent threshold for the direct-call target (this call site vs the +// total call count) for it to be considered as the promotion target. +static cl::opt<unsigned> + ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5), + cl::Hidden, cl::ZeroOrMore, + cl::desc("The percentage threshold against total " + "count for the promotion")); + +// Set the maximum number of targets to promote for a single indirect-call +// callsite. +static cl::opt<unsigned> + MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore, + cl::desc("Max number of promotions for a single indirect " + "call callsite")); + +ICallPromotionAnalysis::ICallPromotionAnalysis() { + ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions); +} + +bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count, + uint64_t TotalCount, + uint64_t RemainingCount) { + return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount && + Count * 100 >= ICPTotalPercentThreshold * TotalCount; +} + +// Indirect-call promotion heuristic. The direct targets are sorted based on +// the count. Stop at the first target that is not promoted. Returns the +// number of candidates deemed profitable. +uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates( + const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) { + ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals); + + LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst + << " Num_targets: " << NumVals << "\n"); + + uint32_t I = 0; + uint64_t RemainingCount = TotalCount; + for (; I < MaxNumPromotions && I < NumVals; I++) { + uint64_t Count = ValueDataRef[I].Count; + assert(Count <= RemainingCount); + LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count + << " Target_func: " << ValueDataRef[I].Value << "\n"); + + if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) { + LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n"); + return I; + } + RemainingCount -= Count; + } + return I; +} + +ArrayRef<InstrProfValueData> +ICallPromotionAnalysis::getPromotionCandidatesForInstruction( + const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount, + uint32_t &NumCandidates) { + bool Res = + getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions, + ValueDataArray.get(), NumVals, TotalCount); + if (!Res) { + NumCandidates = 0; + return ArrayRef<InstrProfValueData>(); + } + NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount); + return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals); +} |