2019-05-31 08:59:06 +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_move_block_down.h"
|
|
|
|
|
|
|
|
#include "source/opt/basic_block.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
TransformationMoveBlockDown::TransformationMoveBlockDown(
|
2021-03-23 13:31:44 +00:00
|
|
|
protobufs::TransformationMoveBlockDown message)
|
|
|
|
: message_(std::move(message)) {}
|
2019-05-31 08:59:06 +00:00
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
TransformationMoveBlockDown::TransformationMoveBlockDown(uint32_t id) {
|
|
|
|
message_.set_block_id(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformationMoveBlockDown::IsApplicable(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
|
2019-05-31 08:59:06 +00:00
|
|
|
// Go through every block in every function, looking for a block whose id
|
|
|
|
// matches that of the block we want to consider moving down.
|
2020-04-02 14:54:46 +00:00
|
|
|
for (auto& function : *ir_context->module()) {
|
2019-05-31 08:59:06 +00:00
|
|
|
for (auto block_it = function.begin(); block_it != function.end();
|
|
|
|
++block_it) {
|
2019-06-25 19:49:46 +00:00
|
|
|
if (block_it->id() == message_.block_id()) {
|
2019-05-31 08:59:06 +00:00
|
|
|
// We have found a match.
|
|
|
|
if (block_it == function.begin()) {
|
|
|
|
// The block is the first one appearing in the function. We are not
|
|
|
|
// allowed to move this block down.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Record the block we would like to consider moving down.
|
2019-06-25 19:49:46 +00:00
|
|
|
opt::BasicBlock* block_matching_id = &*block_it;
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!ir_context->GetDominatorAnalysis(&function)->IsReachable(
|
2019-06-26 14:32:25 +00:00
|
|
|
block_matching_id)) {
|
|
|
|
// The block is not reachable. We are not allowed to move it down.
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-31 08:59:06 +00:00
|
|
|
// Now see whether there is some block following that block in program
|
|
|
|
// order.
|
|
|
|
++block_it;
|
|
|
|
if (block_it == function.end()) {
|
|
|
|
// There is no such block; i.e., the block we are considering moving
|
|
|
|
// is the last one in the function. The transformation thus does not
|
|
|
|
// apply.
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-25 19:49:46 +00:00
|
|
|
opt::BasicBlock* next_block_in_program_order = &*block_it;
|
2019-05-31 08:59:06 +00:00
|
|
|
// We can move the block of interest down if and only if it does not
|
|
|
|
// dominate the block that comes next.
|
2020-04-02 14:54:46 +00:00
|
|
|
return !ir_context->GetDominatorAnalysis(&function)->Dominates(
|
2019-05-31 08:59:06 +00:00
|
|
|
block_matching_id, next_block_in_program_order);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We did not find a matching block, so the transformation is not applicable:
|
|
|
|
// there is no relevant block to move.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:54:46 +00:00
|
|
|
void TransformationMoveBlockDown::Apply(
|
|
|
|
opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
|
2019-05-31 08:59:06 +00:00
|
|
|
// Go through every block in every function, looking for a block whose id
|
|
|
|
// matches that of the block we want to move down.
|
2020-04-02 14:54:46 +00:00
|
|
|
for (auto& function : *ir_context->module()) {
|
2019-05-31 08:59:06 +00:00
|
|
|
for (auto block_it = function.begin(); block_it != function.end();
|
|
|
|
++block_it) {
|
2019-06-25 19:49:46 +00:00
|
|
|
if (block_it->id() == message_.block_id()) {
|
2019-05-31 08:59:06 +00:00
|
|
|
++block_it;
|
|
|
|
assert(block_it != function.end() &&
|
|
|
|
"To be able to move a block down, it needs to have a "
|
|
|
|
"program-order successor.");
|
2019-06-25 19:49:46 +00:00
|
|
|
function.MoveBasicBlockToAfter(message_.block_id(), &*block_it);
|
2019-09-26 09:56:43 +00:00
|
|
|
// For performance, it is vital to keep the dominator analysis valid
|
|
|
|
// (which due to https://github.com/KhronosGroup/SPIRV-Tools/issues/2889
|
|
|
|
// requires keeping the CFG analysis valid).
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->InvalidateAnalysesExceptFor(
|
2019-09-26 09:56:43 +00:00
|
|
|
opt::IRContext::Analysis::kAnalysisDefUse |
|
|
|
|
opt::IRContext::Analysis::kAnalysisCFG |
|
|
|
|
opt::IRContext::Analysis::kAnalysisDominatorAnalysis);
|
2019-05-31 08:59:06 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(false && "No block was found to move down.");
|
|
|
|
}
|
|
|
|
|
2019-06-25 19:49:46 +00:00
|
|
|
protobufs::Transformation TransformationMoveBlockDown::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_move_block_down() = message_;
|
2019-05-31 08:59:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
std::unordered_set<uint32_t> TransformationMoveBlockDown::GetFreshIds() const {
|
|
|
|
return std::unordered_set<uint32_t>();
|
|
|
|
}
|
|
|
|
|
2019-05-31 08:59:06 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|