Skip to content

Commit

Permalink
(optionally) run BranchPrefixHoisting
Browse files Browse the repository at this point in the history
Reviewed By: ssj933

Differential Revision: D66525536

fbshipit-source-id: 3077eb8eaf0805344b1898d4afec89adb3dfb633
  • Loading branch information
Nikolai Tillmann authored and facebook-github-bot committed Dec 3, 2024
1 parent 2554d17 commit 14122e0
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions libredex/ConfigFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ void ConfigFiles::load_inliner_config(inliner::InlinerConfig* inliner_config) {
jw.get("run_reg_alloc", false, shrinker_config.run_reg_alloc);
jw.get("run_fast_reg_alloc", false, shrinker_config.run_fast_reg_alloc);
jw.get("run_dedup_blocks", false, shrinker_config.run_dedup_blocks);
jw.get("run_branch_prefix_hoisting", false,
shrinker_config.run_branch_prefix_hoisting);
jw.get("debug", false, inliner_config->debug);
jw.get("blocklist", {}, inliner_config->blocklist);
jw.get("caller_blocklist", {}, inliner_config->caller_blocklist);
Expand Down
2 changes: 2 additions & 0 deletions libredex/GlobalConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void InlinerConfig::bind_config() {
bind("run_cse", shrinker.run_cse, shrinker.run_cse);
bind("run_dedup_blocks", shrinker.run_dedup_blocks,
shrinker.run_dedup_blocks);
bind("run_branch_prefix_hoisting", shrinker.run_branch_prefix_hoisting,
shrinker.run_branch_prefix_hoisting);
bind("run_copy_prop", shrinker.run_copy_prop, shrinker.run_copy_prop);
bind("run_reg_alloc", shrinker.run_reg_alloc, shrinker.run_reg_alloc);
bind("run_fast_reg_alloc", shrinker.run_fast_reg_alloc,
Expand Down
1 change: 1 addition & 0 deletions libredex/ShrinkerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct ShrinkerConfig {
bool run_reg_alloc{false};
bool run_fast_reg_alloc{false};
bool run_dedup_blocks{false};
bool run_branch_prefix_hoisting{false};

bool normalize_new_instances{true};

Expand Down
6 changes: 6 additions & 0 deletions opt/shrinker/ShrinkerPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void ShrinkerPass::bind_config() {
"Whether to run fast register allocation.");
bind("run_dedup_blocks", true, m_config.run_dedup_blocks,
"Whether to run dedup-blocks.");
bind("run_branch_prefix_hoisting", true, m_config.run_branch_prefix_hoisting,
"Whether to run branch-prefix hoisting.");

bind("compute_pure_methods", true, m_config.compute_pure_methods,
"Whether to compute pure methods with a relatively expensive analysis "
Expand Down Expand Up @@ -87,6 +89,8 @@ void ShrinkerPass::run_pass(DexStoresVector& stores,
shrinker.get_dedup_blocks_stats().insns_removed);
mgr.incr_metric("blocks_eliminated_by_dedup_blocks",
shrinker.get_dedup_blocks_stats().blocks_removed);
mgr.incr_metric("instructions_eliminated_branch_prefix_hoisting",
shrinker.get_branch_prefix_hoisting_stats());
mgr.incr_metric("methods_reg_alloced", shrinker.get_methods_reg_alloced());
mgr.incr_metric("localdce_init_class_instructions_added",
shrinker.get_local_dce_stats().init_class_instructions_added);
Expand All @@ -110,6 +114,8 @@ void ShrinkerPass::run_pass(DexStoresVector& stores,
shrinker.get_local_dce_seconds());
Timer::add_timer("Shrinker.Shrinking.DedupBlocks",
shrinker.get_dedup_blocks_seconds());
Timer::add_timer("Shrinker.Shrinking.BranchPrefixHoisting",
shrinker.get_branch_prefix_hoisting_seconds());
Timer::add_timer("Shrinker.Shrinking.RegAlloc",
shrinker.get_reg_alloc_seconds());
}
Expand Down
4 changes: 4 additions & 0 deletions service/method-inliner/MethodInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,8 @@ void run_inliner(
inliner.get_info().unreachable_insns);
mgr.incr_metric("instructions_eliminated_dedup_blocks",
shrinker.get_dedup_blocks_stats().insns_removed);
mgr.incr_metric("instructions_eliminated_branch_prefix_hoisting",
shrinker.get_branch_prefix_hoisting_stats());
mgr.incr_metric("blocks_eliminated_by_dedup_blocks",
shrinker.get_dedup_blocks_stats().blocks_removed);
mgr.incr_metric("methods_reg_alloced", shrinker.get_methods_reg_alloced());
Expand All @@ -1128,6 +1130,8 @@ void run_inliner(
shrinker.get_local_dce_seconds());
Timer::add_timer("Inliner.Shrinking.DedupBlocks",
shrinker.get_dedup_blocks_seconds());
Timer::add_timer("Inliner.Shrinking.BranchPrefixHoisting",
shrinker.get_branch_prefix_hoisting_seconds());
Timer::add_timer("Inliner.Shrinking.RegAlloc",
shrinker.get_reg_alloc_seconds());
Timer::add_timer("Inliner.Inlining.inline_callees",
Expand Down
19 changes: 18 additions & 1 deletion service/shrinker/Shrinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <fstream>

#include "BranchPrefixHoisting.h"
#include "ConstantUses.h"
#include "ConstructorParams.h"
#include "LinearScan.h"
#include "RandomForest.h"
Expand Down Expand Up @@ -86,7 +88,7 @@ Shrinker::Shrinker(
m_enabled(config.run_const_prop || config.run_cse ||
config.run_copy_prop || config.run_local_dce ||
config.run_reg_alloc || config.run_fast_reg_alloc ||
config.run_dedup_blocks),
config.run_dedup_blocks || config.run_branch_prefix_hoisting),
m_init_classes_with_side_effects(init_classes_with_side_effects),
m_pure_methods(configured_pure_methods),
m_finalish_field_names(configured_finalish_field_names),
Expand Down Expand Up @@ -218,6 +220,7 @@ void Shrinker::shrink_code(
copy_propagation_impl::Stats copy_prop_stats;
LocalDce::Stats local_dce_stats;
dedup_blocks_impl::Stats dedup_blocks_stats;
size_t branch_prefix_hoisting_stats{0};

code->build_cfg();
if (m_config.run_const_prop) {
Expand Down Expand Up @@ -312,6 +315,19 @@ void Shrinker::shrink_code(
dedup_blocks_stats = dedup_blocks.get_stats();
}

if (m_config.run_branch_prefix_hoisting) {
auto timer = m_branch_prefix_hoisting_timer.scope();
auto& cfg = code->cfg();
Lazy<const constant_uses::ConstantUses> constant_uses([&] {
return std::make_unique<const constant_uses::ConstantUses>(
cfg, is_static, declaring_type, proto->get_rtype(), proto->get_args(),
method_describer,
/* force_type_inference */ true);
});
branch_prefix_hoisting_stats = branch_prefix_hoisting_impl::process_cfg(
cfg, constant_uses, /* can_allocate_regs */ true);
}

auto data_after_dedup = get_features(kMMINLDataCollectionLevel);
if (traceEnabled(MMINL, kMMINLDataCollectionLevel)) {
TRACE(
Expand All @@ -331,6 +347,7 @@ void Shrinker::shrink_code(
m_dedup_blocks_stats += dedup_blocks_stats;
m_methods_shrunk++;
m_methods_reg_alloced += reg_alloc_inc;
m_branch_prefix_hoisting_stats += branch_prefix_hoisting_stats;
}

void Shrinker::log_metrics(ScopedMetrics& sm) const {
Expand Down
8 changes: 8 additions & 0 deletions service/shrinker/Shrinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Shrinker {
const dedup_blocks_impl::Stats& get_dedup_blocks_stats() const {
return m_dedup_blocks_stats;
}
size_t get_branch_prefix_hoisting_stats() const {
return m_branch_prefix_hoisting_stats;
}
size_t get_methods_shrunk() const { return m_methods_shrunk; }
size_t get_methods_reg_alloced() const { return m_methods_reg_alloced; }

Expand All @@ -104,6 +107,9 @@ class Shrinker {
double get_local_dce_seconds() const {
return m_local_dce_timer.get_seconds();
}
double get_branch_prefix_hoisting_seconds() const {
return m_branch_prefix_hoisting_timer.get_seconds();
}
double get_dedup_blocks_seconds() const {
return m_dedup_blocks_timer.get_seconds();
}
Expand Down Expand Up @@ -167,6 +173,8 @@ class Shrinker {
copy_propagation_impl::Stats m_copy_prop_stats;
AccumulatingTimer m_local_dce_timer;
LocalDce::Stats m_local_dce_stats;
AccumulatingTimer m_branch_prefix_hoisting_timer;
size_t m_branch_prefix_hoisting_stats{0};
AccumulatingTimer m_dedup_blocks_timer;
dedup_blocks_impl::Stats m_dedup_blocks_stats;
AccumulatingTimer m_reg_alloc_timer;
Expand Down

0 comments on commit 14122e0

Please sign in to comment.