#include "source/fuzz/fuzzer_pass_outline_functions.h"
#include <vector>
#include "source/fuzz/fuzzer_util.h"
#include "source/fuzz/instruction_descriptor.h"
#include "source/fuzz/transformation_outline_function.h"
#include "source/fuzz/transformation_split_block.h"
namespace spvtools {
namespace fuzz {
FuzzerPassOutlineFunctions::FuzzerPassOutlineFunctions(
opt::IRContext* ir_context, TransformationContext* transformation_context,
FuzzerContext* fuzzer_context,
protobufs::TransformationSequence* transformations,
bool ignore_inapplicable_transformations)
: FuzzerPass(ir_context, transformation_context, fuzzer_context,
transformations, ignore_inapplicable_transformations) {}
void FuzzerPassOutlineFunctions::Apply() {
std::vector<opt::Function*> original_functions;
for (auto& function : *GetIRContext()->module()) {
original_functions.push_back(&function);
}
for (auto& function : original_functions) {
if (!GetFuzzerContext()->ChoosePercentage(
GetFuzzerContext()->GetChanceOfOutliningFunction())) {
continue;
}
std::vector<opt::BasicBlock*> blocks;
for (auto& block : *function) {
blocks.push_back(&block);
}
auto entry_block = MaybeGetEntryBlockSuitableForOutlining(
blocks[GetFuzzerContext()->RandomIndex(blocks)]);
if (!entry_block) {
continue;
}
auto dominator_analysis = GetIRContext()->GetDominatorAnalysis(function);
auto postdominator_analysis =
GetIRContext()->GetPostDominatorAnalysis(function);
std::vector<opt::BasicBlock*> candidate_exit_blocks;
for (auto postdominates_entry_block = entry_block;
postdominates_entry_block != nullptr;
postdominates_entry_block = postdominator_analysis->ImmediateDominator(
postdominates_entry_block)) {
if (dominator_analysis->Dominates(entry_block,
postdominates_entry_block) &&
!GetIRContext()->GetStructuredCFGAnalysis()->IsContinueBlock(
postdominates_entry_block->id())) {
candidate_exit_blocks.push_back(postdominates_entry_block);
}
}
if (candidate_exit_blocks.empty()) {
continue;
}
auto exit_block = MaybeGetExitBlockSuitableForOutlining(
candidate_exit_blocks[GetFuzzerContext()->RandomIndex(
candidate_exit_blocks)]);
if (!exit_block) {
continue;
}
auto region_blocks = TransformationOutlineFunction::GetRegionBlocks(
GetIRContext(), entry_block, exit_block);
std::map<uint32_t, uint32_t> input_id_to_fresh_id;
for (auto id : TransformationOutlineFunction::GetRegionInputIds(
GetIRContext(), region_blocks, exit_block)) {
input_id_to_fresh_id[id] = GetFuzzerContext()->GetFreshId();
}
std::map<uint32_t, uint32_t> output_id_to_fresh_id;
for (auto id : TransformationOutlineFunction::GetRegionOutputIds(
GetIRContext(), region_blocks, exit_block)) {
output_id_to_fresh_id[id] = GetFuzzerContext()->GetFreshId();
}
TransformationOutlineFunction transformation(
entry_block->id(), exit_block->id(),
GetFuzzerContext()->GetFreshId(),
GetFuzzerContext()->GetFreshId(),
GetFuzzerContext()->GetFreshId(),
GetFuzzerContext()->GetFreshId(),
GetFuzzerContext()->GetFreshId(),
GetFuzzerContext()->GetFreshId(),
input_id_to_fresh_id,
output_id_to_fresh_id);
MaybeApplyTransformation(transformation);
}
}
opt::BasicBlock*
FuzzerPassOutlineFunctions::MaybeGetEntryBlockSuitableForOutlining(
opt::BasicBlock* entry_block) {
if (entry_block->IsLoopHeader()) {
auto predecessors =
GetIRContext()->cfg()->preds(entry_block->GetLabel()->result_id());
if (predecessors.size() < 2) {
return nullptr;
}
entry_block =
GetOrCreateSimpleLoopPreheader(entry_block->GetLabel()->result_id());
}
assert(!entry_block->IsLoopHeader() &&
"The entry block cannot be a loop header at this point.");
if (entry_block->begin()->opcode() == spv::Op::OpPhi ||
entry_block->begin()->opcode() == spv::Op::OpVariable) {
auto non_phi_or_var_inst = &*entry_block->begin();
while (non_phi_or_var_inst->opcode() == spv::Op::OpPhi ||
non_phi_or_var_inst->opcode() == spv::Op::OpVariable) {
non_phi_or_var_inst = non_phi_or_var_inst->NextNode();
}
uint32_t new_block_id = GetFuzzerContext()->GetFreshId();
ApplyTransformation(TransformationSplitBlock(
MakeInstructionDescriptor(GetIRContext(), non_phi_or_var_inst),
new_block_id));
entry_block = &*entry_block->GetParent()->FindBlock(new_block_id);
}
return entry_block;
}
opt::BasicBlock*
FuzzerPassOutlineFunctions::MaybeGetExitBlockSuitableForOutlining(
opt::BasicBlock* exit_block) {
assert(!GetIRContext()->GetStructuredCFGAnalysis()->IsContinueBlock(
exit_block->id()) &&
"A candidate exit block cannot be a continue target.");
if (GetIRContext()->GetStructuredCFGAnalysis()->IsMergeBlock(
exit_block->id())) {
uint32_t new_block_id = GetFuzzerContext()->GetFreshId();
auto split_before = &*exit_block->begin();
while (split_before->opcode() == spv::Op::OpPhi) {
split_before = split_before->NextNode();
}
if (!MaybeApplyTransformation(TransformationSplitBlock(
MakeInstructionDescriptor(GetIRContext(), split_before),
new_block_id))) {
return nullptr;
}
return &*exit_block->GetParent()->FindBlock(new_block_id);
}
return exit_block;
}
}
}