From f8953b595dbaca59ffbe8ed2201d07c1de4d0329 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Tue, 18 Apr 2023 14:45:07 -0700 Subject: [PATCH] Fix SplitHugeSwitchPass off-by-one Summary: The determination to split and the computation of splits should match. Reviewed By: thezhangwei Differential Revision: D45091645 fbshipit-source-id: 9179dbfab61cedbeefaae8f139f23e482169b835 --- opt/split_huge_switches/SplitHugeSwitchPass.cpp | 3 ++- test/unit/SplitHugeSwitchTest.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/opt/split_huge_switches/SplitHugeSwitchPass.cpp b/opt/split_huge_switches/SplitHugeSwitchPass.cpp index b7e58a1bea..3c2841adcd 100644 --- a/opt/split_huge_switches/SplitHugeSwitchPass.cpp +++ b/opt/split_huge_switches/SplitHugeSwitchPass.cpp @@ -523,7 +523,7 @@ AnalysisData analyze(DexMethod* m, data.no_code = false; auto size = code->sum_opcode_sizes(); - if (size < insn_threshold) { + if (size <= insn_threshold) { data.under_insn_threshold = true; return data; } @@ -555,6 +555,7 @@ AnalysisData analyze(DexMethod* m, data.no_easy_expr = false; size_t nr_splits = (size_t)std::ceil(((float)size) / insn_threshold); + redex_assert(nr_splits > 1); auto switch_range = get_switch_range(*scoped_cfg, switch_it.block(), nr_splits); if (switch_range.cases < nr_splits) { diff --git a/test/unit/SplitHugeSwitchTest.cpp b/test/unit/SplitHugeSwitchTest.cpp index f583298214..e814232027 100644 --- a/test/unit/SplitHugeSwitchTest.cpp +++ b/test/unit/SplitHugeSwitchTest.cpp @@ -468,3 +468,14 @@ TEST_F(SplitHugeSwitchTest, Split3) { }); EXPECT_TRUE(res); } + +TEST_F(SplitHugeSwitchTest, NoSplitExactThreshold) { + auto res = test("(I)V", + SRC, + 28, + 0, + method_profiles::MethodProfiles(), + 0.0, + {pair("", SRC)}); + EXPECT_TRUE(res); +}