2019-05-29 15:42:46 +00:00
|
|
|
// Copyright (c) 2019 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "source/fuzz/transformation_split_block.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
2019-10-14 16:00:46 +00:00
|
|
|
#include "source/fuzz/instruction_descriptor.h"
|
2019-05-29 15:42:46 +00:00
|
|
|
#include "source/util/make_unique.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
TransformationSplitBlock::TransformationSplitBlock(
|
2021-03-23 13:31:44 +00:00
|
|
|
protobufs::TransformationSplitBlock message)
|
|
|
|
: message_(std::move(message)) {}
|
2019-05-29 15:42:46 +00:00
|
|
|
|
2019-10-14 16:00:46 +00:00
|
|
|
TransformationSplitBlock::TransformationSplitBlock(
|
|
|
|
const protobufs::InstructionDescriptor& instruction_to_split_before,
|
|
|
|
uint32_t fresh_id) {
|
|
|
|
*message_.mutable_instruction_to_split_before() = instruction_to_split_before;
|
2019-06-25 19:49:46 +00:00
|
|
|
message_.set_fresh_id(fresh_id);
|
|
|
|
}
|
2019-05-29 15:42:46 +00:00
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
bool TransformationSplitBlock::IsApplicable(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
|
|
|
|
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
|
2019-05-29 15:42:46 +00:00
|
|
|
// We require the id for the new block to be unused.
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-14 16:00:46 +00:00
|
|
|
auto instruction_to_split_before =
|
2020-04-02 14:54:46 +00:00
|
|
|
FindInstruction(message_.instruction_to_split_before(), ir_context);
|
2019-10-14 16:00:46 +00:00
|
|
|
if (!instruction_to_split_before) {
|
2019-08-05 17:00:13 +00:00
|
|
|
// The instruction describing the block we should split does not exist.
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
auto block_to_split =
|
|
|
|
ir_context->get_instr_block(instruction_to_split_before);
|
2019-10-14 16:00:46 +00:00
|
|
|
assert(block_to_split &&
|
|
|
|
"We should not have managed to find the "
|
|
|
|
"instruction if it was not contained in a block.");
|
2019-08-05 17:00:13 +00:00
|
|
|
|
2019-10-14 16:00:46 +00:00
|
|
|
if (block_to_split->IsLoopHeader()) {
|
2019-08-05 17:00:13 +00:00
|
|
|
// We cannot split a loop header block: back-edges would become invalid.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-14 16:00:46 +00:00
|
|
|
auto split_before = fuzzerutil::GetIteratorForInstruction(
|
|
|
|
block_to_split, instruction_to_split_before);
|
|
|
|
assert(split_before != block_to_split->end() &&
|
|
|
|
"At this point we know the"
|
|
|
|
" block split point exists.");
|
|
|
|
|
2019-08-05 17:00:13 +00:00
|
|
|
if (split_before->PreviousNode() &&
|
2022-11-04 21:27:10 +00:00
|
|
|
split_before->PreviousNode()->opcode() == spv::Op::OpSelectionMerge) {
|
2019-08-05 17:00:13 +00:00
|
|
|
// We cannot split directly after a selection merge: this would separate
|
|
|
|
// the merge from its associated branch or switch operation.
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (split_before->opcode() == spv::Op::OpVariable) {
|
2019-08-05 17:00:13 +00:00
|
|
|
// We cannot split directly after a variable; variables in a function
|
|
|
|
// must be contiguous in the entry block.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We cannot split before an OpPhi unless the OpPhi has exactly one
|
|
|
|
// associated incoming edge.
|
2022-11-04 21:27:10 +00:00
|
|
|
if (split_before->opcode() == spv::Op::OpPhi &&
|
2020-04-14 19:17:42 +00:00
|
|
|
split_before->NumInOperands() != 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Splitting the block must not separate the definition of an OpSampledImage
|
|
|
|
// from its use: the SPIR-V data rules require them to be in the same block.
|
2020-09-03 09:19:02 +00:00
|
|
|
return !fuzzerutil::
|
|
|
|
SplittingBeforeInstructionSeparatesOpSampledImageDefinitionFromUse(
|
|
|
|
block_to_split, instruction_to_split_before);
|
2019-05-29 15:42:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
void TransformationSplitBlock::Apply(
|
|
|
|
opt::IRContext* ir_context,
|
|
|
|
TransformationContext* transformation_context) const {
|
2019-10-14 16:00:46 +00:00
|
|
|
opt::Instruction* instruction_to_split_before =
|
2020-04-02 14:54:46 +00:00
|
|
|
FindInstruction(message_.instruction_to_split_before(), ir_context);
|
2019-10-14 16:00:46 +00:00
|
|
|
opt::BasicBlock* block_to_split =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_instr_block(instruction_to_split_before);
|
2019-10-14 16:00:46 +00:00
|
|
|
auto split_before = fuzzerutil::GetIteratorForInstruction(
|
|
|
|
block_to_split, instruction_to_split_before);
|
|
|
|
assert(split_before != block_to_split->end() &&
|
2019-08-05 17:00:13 +00:00
|
|
|
"If the transformation is applicable, we should have an "
|
|
|
|
"instruction to split on.");
|
2019-10-14 16:00:46 +00:00
|
|
|
|
2019-08-05 17:00:13 +00:00
|
|
|
// We need to make sure the module's id bound is large enough to add the
|
|
|
|
// fresh id.
|
2020-04-02 14:54:46 +00:00
|
|
|
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
|
2019-08-05 17:00:13 +00:00
|
|
|
// Split the block.
|
2020-04-02 14:54:46 +00:00
|
|
|
auto new_bb = block_to_split->SplitBasicBlock(ir_context, message_.fresh_id(),
|
2019-10-14 16:00:46 +00:00
|
|
|
split_before);
|
2019-08-05 17:00:13 +00:00
|
|
|
// The split does not automatically add a branch between the two parts of
|
|
|
|
// the original block, so we add one.
|
2021-03-27 12:13:23 +00:00
|
|
|
auto branch_instruction = MakeUnique<opt::Instruction>(
|
2022-11-04 21:27:10 +00:00
|
|
|
ir_context, spv::Op::OpBranch, 0, 0,
|
2019-10-14 16:00:46 +00:00
|
|
|
std::initializer_list<opt::Operand>{opt::Operand(
|
2021-03-27 12:13:23 +00:00
|
|
|
spv_operand_type_t::SPV_OPERAND_TYPE_ID, {message_.fresh_id()})});
|
|
|
|
auto branch_instruction_ptr = branch_instruction.get();
|
|
|
|
block_to_split->AddInstruction(std::move(branch_instruction));
|
|
|
|
|
|
|
|
// Inform the def-use manager about the branch instruction, and record its
|
|
|
|
// block.
|
|
|
|
ir_context->get_def_use_mgr()->AnalyzeInstDefUse(branch_instruction_ptr);
|
|
|
|
ir_context->set_instr_block(branch_instruction_ptr, block_to_split);
|
|
|
|
|
2019-08-05 17:00:13 +00:00
|
|
|
// If we split before OpPhi instructions, we need to update their
|
|
|
|
// predecessor operand so that the block they used to be inside is now the
|
|
|
|
// predecessor.
|
2021-03-27 12:13:23 +00:00
|
|
|
new_bb->ForEachPhiInst([block_to_split,
|
|
|
|
ir_context](opt::Instruction* phi_inst) {
|
2020-07-29 03:55:02 +00:00
|
|
|
assert(
|
|
|
|
phi_inst->NumInOperands() == 2 &&
|
|
|
|
"Precondition: a block can only be split before an OpPhi if the block"
|
|
|
|
"has exactly one predecessor.");
|
2019-10-14 16:00:46 +00:00
|
|
|
phi_inst->SetInOperand(1, {block_to_split->id()});
|
2021-03-27 12:13:23 +00:00
|
|
|
ir_context->UpdateDefUse(phi_inst);
|
2019-10-14 16:00:46 +00:00
|
|
|
});
|
2020-01-13 22:04:01 +00:00
|
|
|
|
2021-03-27 12:13:23 +00:00
|
|
|
// We have updated the def-use manager and the instruction to block mapping,
|
|
|
|
// but other analyses (especially control flow-related ones) need to be
|
|
|
|
// recomputed.
|
2020-10-01 10:48:47 +00:00
|
|
|
ir_context->InvalidateAnalysesExceptFor(
|
2021-03-27 12:13:23 +00:00
|
|
|
opt::IRContext::Analysis::kAnalysisDefUse |
|
|
|
|
opt::IRContext::Analysis::kAnalysisInstrToBlockMapping);
|
2020-10-01 10:48:47 +00:00
|
|
|
|
2020-01-13 22:04:01 +00:00
|
|
|
// If the block being split was dead, the new block arising from the split is
|
|
|
|
// also dead.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (transformation_context->GetFactManager()->BlockIsDead(
|
|
|
|
block_to_split->id())) {
|
|
|
|
transformation_context->GetFactManager()->AddFactBlockIsDead(
|
|
|
|
message_.fresh_id());
|
2020-01-13 22:04:01 +00:00
|
|
|
}
|
2019-05-29 15:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
protobufs::Transformation TransformationSplitBlock::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_split_block() = message_;
|
2019-05-29 15:42:46 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
std::unordered_set<uint32_t> TransformationSplitBlock::GetFreshIds() const {
|
|
|
|
return {message_.fresh_id()};
|
|
|
|
}
|
|
|
|
|
2019-05-29 15:42:46 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|