Skip to content

Commit

Permalink
Empower DCE more
Browse files Browse the repository at this point in the history
Summary:
Use known-pure methods.

This fixes an issue where a Class argument may be removed and trigger the NoSpuriousGetClass checker in the caller.

Reviewed By: NTillmann, beicy

Differential Revision: D49935285

fbshipit-source-id: d2ec1c139a2b670e793b31f50bf9554f8f260940
  • Loading branch information
agampe authored and facebook-github-bot committed Oct 6, 2023
1 parent 88130c6 commit 1a05f1b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions opt/remove-unused-args/RemoveUnusedArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "OptData.h"
#include "OptDataDefs.h"
#include "PassManager.h"
#include "Purity.h"
#include "Resolver.h"
#include "Show.h"
#include "Walkers.h"
Expand Down Expand Up @@ -533,9 +534,9 @@ void run_cleanup(DexMethod* method,
cfg::ControlFlowGraph& cfg,
const init_classes::InitClassesWithSideEffects*
init_classes_with_side_effects,
const std::unordered_set<DexMethodRef*>& pure_methods,
std::mutex& mutex,
LocalDce::Stats& stats) {
std::unordered_set<DexMethodRef*> pure_methods;
auto local_dce = LocalDce(init_classes_with_side_effects, pure_methods);
local_dce.dce(cfg, /* normalize_new_instances */ true, method->get_class());
const auto& local_stats = local_dce.get_stats();
Expand Down Expand Up @@ -667,6 +668,7 @@ RemoveArgs::MethodStats RemoveArgs::update_method_protos(
run_cleanup(method,
cfg,
&m_init_classes_with_side_effects,
m_pure_methods,
local_dce_stats_mutex,
local_dce_stats);
}
Expand Down Expand Up @@ -745,6 +747,7 @@ std::pair<size_t, LocalDce::Stats> RemoveArgs::update_callsites() {
run_cleanup(method,
cfg,
&m_init_classes_with_side_effects,
m_pure_methods,
local_dce_stats_mutex,
local_dce_stats);
}
Expand All @@ -768,10 +771,11 @@ void RemoveUnusedArgsPass::run_pass(DexStoresVector& stores,
size_t num_method_protos_reordered_count = 0;
size_t num_iterations = 0;
LocalDce::Stats local_dce_stats;
auto pure_methods = get_pure_methods();
while (true) {
num_iterations++;
RemoveArgs rm_args(scope, init_classes_with_side_effects, m_blocklist,
m_total_iterations++);
pure_methods, m_total_iterations++);
auto pass_stats = rm_args.run(conf);
if (pass_stats.methods_updated_count == 0) {
break;
Expand Down
5 changes: 4 additions & 1 deletion opt/remove-unused-args/RemoveUnusedArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ class RemoveArgs {
const init_classes::InitClassesWithSideEffects&
init_classes_with_side_effects,
const std::vector<std::string>& blocklist,
const std::unordered_set<DexMethodRef*>& pure_methods,
size_t iteration = 0)
: m_scope(scope),
m_init_classes_with_side_effects(init_classes_with_side_effects),
m_blocklist(blocklist),
m_iteration(iteration) {}
m_iteration(iteration),
m_pure_methods(pure_methods) {}
RemoveArgs::PassStats run(ConfigFiles& conf);

private:
Expand All @@ -72,6 +74,7 @@ class RemoveArgs {
std::unordered_map<DexProto*, DexProto*> m_reordered_protos;
const std::vector<std::string>& m_blocklist;
size_t m_iteration;
const std::unordered_set<DexMethodRef*>& m_pure_methods;

DexTypeList::ContainerType get_live_arg_type_list(
DexMethod* method, const std::deque<uint16_t>& live_arg_idxs);
Expand Down
4 changes: 3 additions & 1 deletion test/unit/RemoveUnusedArgsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
struct RemoveUnusedArgsTest : public RedexTest {
remove_unused_args::RemoveArgs* m_remove_args;
std::vector<std::string> m_blocklist;
std::unordered_set<DexMethodRef*> m_pure_methods;

RemoveUnusedArgsTest() {
Scope dummy_scope;
Expand All @@ -34,7 +35,8 @@ struct RemoveUnusedArgsTest : public RedexTest {
auto dummy_cls = create_internal_class(dummy_t, obj_t, {});
dummy_scope.push_back(dummy_cls);
m_remove_args = new remove_unused_args::RemoveArgs(
dummy_scope, dummy_init_classes_with_side_effects, m_blocklist);
dummy_scope, dummy_init_classes_with_side_effects, m_blocklist,
m_pure_methods);
}

~RemoveUnusedArgsTest() {}
Expand Down

0 comments on commit 1a05f1b

Please sign in to comment.