Skip to content

[InlineCost] Consider the default branch when calculating cost #77856

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 6 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/Analysis/InlineModelFeatureMaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace llvm {
M(int64_t, {1}, jump_table_penalty, "Accumulation of costs for jump tables") \
M(int64_t, {1}, case_cluster_penalty, \
"Accumulation of costs for case clusters") \
M(int64_t, {1}, switch_default_dest_penalty, \
"Accumulation of costs for switch default destination") \
M(int64_t, {1}, switch_penalty, \
"Accumulation of costs for switch statements") \
M(int64_t, {1}, unsimplified_common_instructions, \
Expand Down
7 changes: 7 additions & 0 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class DataLayout;
class StringRef;
class Type;
class Value;
class UnreachableInst;

//===----------------------------------------------------------------------===//
// AllocaInst Class
Expand Down Expand Up @@ -3505,6 +3506,12 @@ class SwitchInst : public Instruction {
return cast<BasicBlock>(getOperand(1));
}

/// Returns true if the default branch must result in immediate undefined
/// behavior, false otherwise.
bool defaultDestUndefined() const {
return isa<UnreachableInst>(getDefaultDest()->getFirstNonPHIOrDbg());
}

void setDefaultDest(BasicBlock *DefaultCase) {
setOperand(1, reinterpret_cast<Value*>(DefaultCase));
}
Expand Down
21 changes: 13 additions & 8 deletions llvm/lib/Analysis/InlineCost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {

/// Called at the end of processing a switch instruction, with the given
/// number of case clusters.
virtual void onFinalizeSwitch(unsigned JumpTableSize,
unsigned NumCaseCluster) {}
virtual void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
bool DefaultDestUndefined) {}

/// Called to account for any other instruction not specifically accounted
/// for.
Expand Down Expand Up @@ -699,15 +699,16 @@ class InlineCostCallAnalyzer final : public CallAnalyzer {
CallPenalty));
}

void onFinalizeSwitch(unsigned JumpTableSize,
unsigned NumCaseCluster) override {
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
bool DefaultDestUndefined) override {
if (!DefaultDestUndefined)
addCost(2 * InstrCost);
// If suitable for a jump table, consider the cost for the table size and
// branch to destination.
// Maximum valid cost increased in this function.
if (JumpTableSize) {
int64_t JTCost =
static_cast<int64_t>(JumpTableSize) * InstrCost + 4 * InstrCost;

addCost(JTCost);
return;
}
Expand Down Expand Up @@ -1153,6 +1154,7 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
// heuristics in the ML inliner.
static constexpr int JTCostMultiplier = 4;
static constexpr int CaseClusterCostMultiplier = 2;
static constexpr int SwitchDefaultDestCostMultiplier = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defined this constant but hardcoded 2 above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied it from CaseClusterCostMultiplier.

static constexpr int SwitchCostMultiplier = 2;

// FIXME: These are taken from the heuristic-based cost visitor: we should
Expand Down Expand Up @@ -1231,8 +1233,11 @@ class InlineCostFeaturesAnalyzer final : public CallAnalyzer {
}
}

void onFinalizeSwitch(unsigned JumpTableSize,
unsigned NumCaseCluster) override {
void onFinalizeSwitch(unsigned JumpTableSize, unsigned NumCaseCluster,
bool DefaultDestUndefined) override {
if (!DefaultDestUndefined)
increment(InlineCostFeatureIndex::switch_default_dest_penalty,
SwitchDefaultDestCostMultiplier * InstrCost);

if (JumpTableSize) {
int64_t JTCost = static_cast<int64_t>(JumpTableSize) * InstrCost +
Expand Down Expand Up @@ -2461,7 +2466,7 @@ bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) {
unsigned NumCaseCluster =
TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI);

onFinalizeSwitch(JumpTableSize, NumCaseCluster);
onFinalizeSwitch(JumpTableSize, NumCaseCluster, SI.defaultDestUndefined());
return false;
}

Expand Down
Loading