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(
|
|
|
|
const spvtools::fuzz::protobufs::TransformationSplitBlock& message)
|
|
|
|
: message_(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() &&
|
|
|
|
split_before->PreviousNode()->opcode() == SpvOpSelectionMerge) {
|
|
|
|
// We cannot split directly after a selection merge: this would separate
|
|
|
|
// the merge from its associated branch or switch operation.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (split_before->opcode() == SpvOpVariable) {
|
|
|
|
// 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.
|
2020-04-14 19:17:42 +00:00
|
|
|
if (split_before->opcode() == SpvOpPhi &&
|
|
|
|
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.
|
|
|
|
std::set<uint32_t> sampled_image_result_ids;
|
|
|
|
bool before_split = true;
|
|
|
|
for (auto& instruction : *block_to_split) {
|
|
|
|
if (&instruction == &*split_before) {
|
|
|
|
before_split = false;
|
|
|
|
}
|
|
|
|
if (before_split) {
|
|
|
|
if (instruction.opcode() == SpvOpSampledImage) {
|
|
|
|
sampled_image_result_ids.insert(instruction.result_id());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!instruction.WhileEachInId(
|
|
|
|
[&sampled_image_result_ids](uint32_t* id) -> bool {
|
|
|
|
return !sampled_image_result_ids.count(*id);
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
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.
|
2019-10-14 16:00:46 +00:00
|
|
|
block_to_split->AddInstruction(MakeUnique<opt::Instruction>(
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context, SpvOpBranch, 0, 0,
|
2019-10-14 16:00:46 +00:00
|
|
|
std::initializer_list<opt::Operand>{opt::Operand(
|
|
|
|
spv_operand_type_t::SPV_OPERAND_TYPE_ID, {message_.fresh_id()})}));
|
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.
|
2019-10-14 16:00:46 +00:00
|
|
|
new_bb->ForEachPhiInst([block_to_split](opt::Instruction* phi_inst) {
|
|
|
|
// The following assertion is a sanity check. It is guaranteed to hold
|
|
|
|
// if IsApplicable holds.
|
|
|
|
assert(phi_inst->NumInOperands() == 2 &&
|
|
|
|
"We can only split a block before an OpPhi if block has exactly "
|
|
|
|
"one predecessor.");
|
|
|
|
phi_inst->SetInOperand(1, {block_to_split->id()});
|
|
|
|
});
|
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-08-05 17:00:13 +00:00
|
|
|
// Invalidate all analyses
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->InvalidateAnalysesExceptFor(
|
|
|
|
opt::IRContext::Analysis::kAnalysisNone);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|