Skip to content

Commit

Permalink
add configurable params
Browse files Browse the repository at this point in the history
Summary: make cross-dex-penalty configurable. behavior-preserving. will tune the param later.

Reviewed By: NTillmann

Differential Revision: D55080922

fbshipit-source-id: 8ff1bbca1122e5d07c68e6ccff848ff2b2faab17
  • Loading branch information
beicy authored and facebook-github-bot committed Mar 21, 2024
1 parent bf79a37 commit 02e0027
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
9 changes: 9 additions & 0 deletions opt/methodinline/MethodInlinePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ void MethodInlinePass::bind_config() {
bind("insn_has_lit_cost_3",
DEFAULT_COST_CONFIG.insn_has_lit_cost_3,
m_inliner_cost_config.insn_has_lit_cost_3);
bind("cross_dex_penalty_coe1",
DEFAULT_COST_CONFIG.cross_dex_penalty_coe1,
m_inliner_cost_config.cross_dex_penalty_coe1);
bind("cross_dex_penalty_coe2",
DEFAULT_COST_CONFIG.cross_dex_penalty_coe2,
m_inliner_cost_config.cross_dex_penalty_coe2);
bind("cross_dex_penalty_const",
DEFAULT_COST_CONFIG.cross_dex_penalty_const,
m_inliner_cost_config.cross_dex_penalty_const);
}

void MethodInlinePass::run_pass(DexStoresVector& stores,
Expand Down
31 changes: 22 additions & 9 deletions service/method-inliner/Inliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ std::unordered_set<DexType*> gather_resolved_init_class_types(
return refined_init_class_types;
}

// Inlining methods into different classes might lead to worse cross-dex-ref
// minimization results. \returns the estimated cross dex penalty caused by
// inlining.
float estimate_cross_dex_penalty(const InlinedCost* inlined_cost,
const InlinerCostConfig& inliner_cost_config,
bool use_other_refs) {
float cross_dex_penalty =
inliner_cost_config.cross_dex_penalty_coe1 * inlined_cost->method_refs;
if (use_other_refs &&
(inlined_cost->method_refs + inlined_cost->other_refs) > 0) {
cross_dex_penalty +=
inliner_cost_config.cross_dex_penalty_coe2 * inlined_cost->other_refs +
inliner_cost_config.cross_dex_penalty_const;
}
return cross_dex_penalty;
}

} // namespace

MultiMethodInliner::MultiMethodInliner(
Expand Down Expand Up @@ -1582,11 +1599,9 @@ bool MultiMethodInliner::too_many_callers(const DexMethod* callee) {
} else {
// Inlining methods into different classes might lead to worse
// cross-dex-ref minimization results.
cross_dex_penalty = inlined_cost->method_refs;
if (callee_caller_refs->classes > 1 &&
(inlined_cost->method_refs + inlined_cost->other_refs) > 0) {
cross_dex_penalty++;
}
cross_dex_penalty = estimate_cross_dex_penalty(
inlined_cost, m_inliner_cost_config,
callee_caller_refs->classes > 1 ? true : false);
}
}

Expand Down Expand Up @@ -1671,10 +1686,8 @@ bool MultiMethodInliner::should_inline_at_call_site(
(caller == nullptr || caller->get_class() != callee->get_class())) {
// Inlining methods into different classes might lead to worse
// cross-dex-ref minimization results.
cross_dex_penalty = inlined_cost->method_refs;
if (inlined_cost->method_refs + inlined_cost->other_refs > 0) {
cross_dex_penalty++;
}
cross_dex_penalty =
estimate_cross_dex_penalty(inlined_cost, m_inliner_cost_config, true);
}

float invoke_cost =
Expand Down
9 changes: 9 additions & 0 deletions service/method-inliner/Inliner.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ struct InlinerCostConfig {
size_t insn_has_lit_cost_2;

size_t insn_has_lit_cost_3;

// Those configs are used to calculate the penalty for worse cross-dex-ref
// minimization results due to inlining.
size_t cross_dex_penalty_coe1;
size_t cross_dex_penalty_coe2;
size_t cross_dex_penalty_const;
};

const struct InlinerCostConfig DEFAULT_COST_CONFIG = {
Expand All @@ -99,6 +105,9 @@ const struct InlinerCostConfig DEFAULT_COST_CONFIG = {
4, // insn_has_lit_cost_1
2, // insn_has_lit_cost_2
1, // insn_has_lit_cost_3
1, // cross_dex_penalty_coe1;
0, // cross_dex_penalty_coe2;
1, // cross_dex_penalty_const;
};

// All call-sites of a callee.
Expand Down

0 comments on commit 02e0027

Please sign in to comment.