Skip to content

Commit

Permalink
Refactor iterator use
Browse files Browse the repository at this point in the history
Summary:
Do not use a "bad" iterator for cases that do not need one. This bit-rotted a slow-debug check.

Instead be more explicit about it with a `std::optional`.

Reviewed By: jimmycFB

Differential Revision: D48211769

fbshipit-source-id: bc8c0b826f121a617c4f2a9b52ebe7cc4df4ce5d
  • Loading branch information
agampe authored and facebook-github-bot committed Aug 10, 2023
1 parent 6da0489 commit 250e104
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions opt/instrument/BlockInstrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <fstream>
#include <limits>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <tuple>
Expand Down Expand Up @@ -167,11 +168,23 @@ struct BlockInfo {
cfg::Block* block;
loop_impl::Loop* loop;
BlockType type;
IRList::iterator it;
std::optional<IRList::iterator> it;
BitId bit_id;
size_t index_id;
std::vector<cfg::Block*> merge_in;

BlockInfo(cfg::Block* b, loop_impl::Loop* l, BlockType t)
: block(b),
loop(l),
type(t),
it(std::nullopt),
bit_id(std::numeric_limits<BitId>::max()),
index_id(std::numeric_limits<size_t>::max()) {
redex_assert(t == BlockType::Unspecified || t == BlockType::Empty ||
t == BlockType::Catch || t == BlockType::Useless ||
(t & BlockType::NoSourceBlock) == BlockType::NoSourceBlock);
}

BlockInfo(cfg::Block* b,
loop_impl::Loop* l,
BlockType t,
Expand Down Expand Up @@ -590,8 +603,8 @@ auto insert_prologue_insts(cfg::ControlFlowGraph& cfg,
IRInstruction* eb_insn = nullptr;
if (!blocks.empty()) {
auto& b = blocks.front();
if (b.block == eb && b.it != b.block->end()) {
eb_insn = b.it->insn;
if (b.block == eb && b.it) {
eb_insn = (*b.it)->insn;
}
}

Expand All @@ -614,6 +627,13 @@ auto insert_prologue_insts(cfg::ControlFlowGraph& cfg,
if (b.block == eb) {
continue;
}
// Skip if it's not instrumentable.
if (!b.is_instrumentable()) {
redex_assert(!b.it);
continue;
}
redex_assert(b.it);

auto is_in_block = [&]() {
for (auto it = b.block->begin(); it != b.block->end(); ++it) {
if (it == b.it) {
Expand All @@ -623,7 +643,7 @@ auto insert_prologue_insts(cfg::ControlFlowGraph& cfg,
return false;
};
assert_log(b.block->end() == b.it || is_in_block(), "%s\n%s",
SHOW(b.block), SHOW(*b.it));
SHOW(b.block), SHOW(**b.it));
}
}

Expand Down Expand Up @@ -987,7 +1007,7 @@ void create_block_info(
if (!has_opcodes) {
if (!source_blocks::has_source_blocks(block)) {
trg_block_info->update_merge(
{block, trg_block_info->loop, BlockType::Empty, {}});
{block, trg_block_info->loop, BlockType::Empty});
return;
}

Expand All @@ -999,7 +1019,7 @@ void create_block_info(
TRACE(INSTRUMENT, 9, "Not instrumenting empty block B%zu", block->id());
block_mapping.at(*next_opt)->merge_in.push_back(block);
trg_block_info->update_merge(
{block, trg_block_info->loop, BlockType::Empty, {}});
{block, trg_block_info->loop, BlockType::Empty});
return;
}
}
Expand All @@ -1009,7 +1029,7 @@ void create_block_info(
// we don't instrument catch blocks with the hope these blocks are cold.
if (block->is_catch() && !options.instrument_catches) {
trg_block_info->update_merge(
{block, trg_block_info->loop, BlockType::Catch, {}});
{block, trg_block_info->loop, BlockType::Catch});
return;
}

Expand All @@ -1035,7 +1055,7 @@ void create_block_info(
block->id(), SHOW(block));
block_mapping.at(*next_opt)->merge_in.push_back(block);
trg_block_info->update_merge(
{block, trg_block_info->loop, BlockType::Useless, {}});
{block, trg_block_info->loop, BlockType::Useless});
return;
}
}
Expand All @@ -1047,7 +1067,7 @@ void create_block_info(
if (!options.instrument_blocks_without_source_block &&
!source_blocks::has_source_blocks(block) && !block->succs().empty()) {
trg_block_info->update_merge(
{block, trg_block_info->loop, BlockType::NoSourceBlock | type, {}});
{block, trg_block_info->loop, BlockType::NoSourceBlock | type});
return;
}

Expand Down Expand Up @@ -1089,8 +1109,7 @@ auto get_blocks_to_instrument(const DexMethod* m,
block_info_list.reserve(blocks.size());
std::unordered_map<const cfg::Block*, BlockInfo*> block_mapping;
for (cfg::Block* b : blocks) {
block_info_list.emplace_back(b, LI.get_loop_for(b), BlockType::Unspecified,
b->end());
block_info_list.emplace_back(b, LI.get_loop_for(b), BlockType::Unspecified);
block_mapping[b] = &block_info_list.back();
}

Expand Down Expand Up @@ -1134,7 +1153,7 @@ void insert_block_coverage_computations(const std::vector<BlockInfo>& blocks,
inst->set_literal(static_cast<int16_t>(1ULL << (bit_id % BIT_VECTOR_SIZE)));
inst->set_src(0, reg_vectors.at(vector_id));
inst->set_dest(reg_vectors.at(vector_id));
block->insert_before(block->to_cfg_instruction_iterator(insert_pos), inst);
block->insert_before(block->to_cfg_instruction_iterator(*insert_pos), inst);
}
}

Expand Down

0 comments on commit 250e104

Please sign in to comment.