[TargetTransformInfo] move branch probability query from TargetLoweringInfo
This is no-functional-change intended (NFC), but needed to allow optimizer passes to use the API. See D98898 for a proposed usage by SimplifyCFG. I'm simplifying the code by removing the cl::opt. That was added back with the original commit in D19488, but I don't see any evidence in regression tests that it was used. Target-specific overrides can use the usual patterns to adjust as necessary. We could also restore that cl::opt, but it was not clear to me exactly how to do it in the convoluted TTI class structure.
This commit is contained in:
1 parent
fe5f66d925
commit
664d0c052c
7 files changed
+25
-22
No files matched your search
@@ -27,6 +27,7 @@
|
||||
#include "llvm/IR/PassManager.h"
|
||||
#include "llvm/Pass.h"
|
||||
#include "llvm/Support/AtomicOrdering.h"
|
||||
#include "llvm/Support/BranchProbability.h"
|
||||
#include "llvm/Support/DataTypes.h"
|
||||
#include "llvm/Support/InstructionCost.h"
|
||||
#include <functional>
|
||||
@@ -328,6 +329,10 @@ public:
|
||||
return getUserCost(U, Operands, CostKind);
|
||||
}
|
||||
|
||||
/// If a branch or a select condition is skewed in one direction by more than
|
||||
/// this factor, it is very likely to be predicted correctly.
|
||||
BranchProbability getPredictableBranchThreshold() const;
|
||||
|
||||
/// Return true if branch divergence exists.
|
||||
///
|
||||
/// Branch divergence has a significantly negative impact on GPU performance
|
||||
@@ -1400,6 +1405,7 @@ public:
|
||||
BlockFrequencyInfo *BFI) = 0;
|
||||
virtual int getUserCost(const User *U, ArrayRef<const Value *> Operands,
|
||||
TargetCostKind CostKind) = 0;
|
||||
virtual BranchProbability getPredictableBranchThreshold() = 0;
|
||||
virtual bool hasBranchDivergence() = 0;
|
||||
virtual bool useGPUDivergenceAnalysis() = 0;
|
||||
virtual bool isSourceOfDivergence(const Value *V) = 0;
|
||||
@@ -1691,6 +1697,9 @@ public:
|
||||
TargetCostKind CostKind) override {
|
||||
return Impl.getUserCost(U, Operands, CostKind);
|
||||
}
|
||||
BranchProbability getPredictableBranchThreshold() override {
|
||||
return Impl.getPredictableBranchThreshold();
|
||||
}
|
||||
bool hasBranchDivergence() override { return Impl.hasBranchDivergence(); }
|
||||
bool useGPUDivergenceAnalysis() override {
|
||||
return Impl.useGPUDivergenceAnalysis();
|
||||
|
||||
@@ -78,6 +78,16 @@ public:
|
||||
return TTI::TCC_Expensive;
|
||||
}
|
||||
|
||||
// Although this default value is arbitrary, it is not random. It is assumed
|
||||
// that a condition that evaluates the same way by a higher percentage than
|
||||
// this is best represented as control flow. Therefore, the default value N
|
||||
// should be set such that the win from N% correct executions is greater than
|
||||
// the loss from (100 - N)% mispredicted executions for the majority of
|
||||
// intended targets.
|
||||
BranchProbability getPredictableBranchThreshold() const {
|
||||
return BranchProbability(99, 100);
|
||||
}
|
||||
|
||||
bool hasBranchDivergence() const { return false; }
|
||||
|
||||
bool useGPUDivergenceAnalysis() const { return false; }
|
||||
|
||||
@@ -528,10 +528,6 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
/// If a branch or a select condition is skewed in one direction by more than
|
||||
/// this factor, it is very likely to be predicted correctly.
|
||||
virtual BranchProbability getPredictableBranchThreshold() const;
|
||||
|
||||
/// Return true if the following transform is beneficial:
|
||||
/// fold (conv (load x)) -> (load (conv*)x)
|
||||
/// On architectures that don't natively support some vector loads
|
||||
|
||||
@@ -228,6 +228,10 @@ int TargetTransformInfo::getUserCost(const User *U,
|
||||
return Cost;
|
||||
}
|
||||
|
||||
BranchProbability TargetTransformInfo::getPredictableBranchThreshold() const {
|
||||
return TTIImpl->getPredictableBranchThreshold();
|
||||
}
|
||||
|
||||
bool TargetTransformInfo::hasBranchDivergence() const {
|
||||
return TTIImpl->hasBranchDivergence();
|
||||
}
|
||||
|
||||
@@ -6587,7 +6587,7 @@ static bool isFormingBranchFromSelectProfitable(const TargetTransformInfo *TTI,
|
||||
uint64_t Sum = TrueWeight + FalseWeight;
|
||||
if (Sum != 0) {
|
||||
auto Probability = BranchProbability::getBranchProbability(Max, Sum);
|
||||
if (Probability > TLI->getPredictableBranchThreshold())
|
||||
if (Probability > TTI->getPredictableBranchThreshold())
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@
|
||||
#include "llvm/IR/IRBuilder.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/IR/Type.h"
|
||||
#include "llvm/Support/BranchProbability.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
#include "llvm/Support/CommandLine.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
@@ -114,17 +113,6 @@ static bool darwinHasSinCos(const Triple &TT) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Although this default value is arbitrary, it is not random. It is assumed
|
||||
// that a condition that evaluates the same way by a higher percentage than this
|
||||
// is best represented as control flow. Therefore, the default value N should be
|
||||
// set such that the win from N% correct executions is greater than the loss
|
||||
// from (100 - N)% mispredicted executions for the majority of intended targets.
|
||||
static cl::opt<int> MinPercentageForPredictableBranch(
|
||||
"min-predictable-branch", cl::init(99),
|
||||
cl::desc("Minimum percentage (0-100) that a condition must be either true "
|
||||
"or false to assume that the condition is predictable"),
|
||||
cl::Hidden);
|
||||
|
||||
void TargetLoweringBase::InitLibcalls(const Triple &TT) {
|
||||
#define HANDLE_LIBCALL(code, name) \
|
||||
setLibcallName(RTLIB::code, name);
|
||||
@@ -1736,10 +1724,6 @@ bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
|
||||
MMO.getAlign(), MMO.getFlags(), Fast);
|
||||
}
|
||||
|
||||
BranchProbability TargetLoweringBase::getPredictableBranchThreshold() const {
|
||||
return BranchProbability(MinPercentageForPredictableBranch, 100);
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// TargetTransformInfo Helpers
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@@ -46,7 +46,7 @@ STATISTIC(ExpectIntrinsicsHandled,
|
||||
// WARNING: these values are internal implementation detail of the pass.
|
||||
// They should not be exposed to the outside of the pass, front-end codegen
|
||||
// should emit @llvm.expect intrinsics instead of using these weights directly.
|
||||
// Transforms should use TargetLowering getPredictableBranchThreshold() hook.
|
||||
// Transforms should use TargetTransformInfo's getPredictableBranchThreshold().
|
||||
static cl::opt<uint32_t> LikelyBranchWeight(
|
||||
"likely-branch-weight", cl::Hidden, cl::init(2000),
|
||||
cl::desc("Weight of the branch likely to be taken (default = 2000)"));
|
||||
|
||||
Reference in new issue
Block a user