2020-06-23 14:00:28 +00:00
|
|
|
// Copyright (c) 2020 Vasyl Teliman
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2020-09-24 11:27:59 +00:00
|
|
|
#include "source/fuzz/transformation_permute_phi_operands.h"
|
|
|
|
|
2020-06-23 14:00:28 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
TransformationPermutePhiOperands::TransformationPermutePhiOperands(
|
2021-03-23 13:31:44 +00:00
|
|
|
protobufs::TransformationPermutePhiOperands message)
|
|
|
|
: message_(std::move(message)) {}
|
2020-06-23 14:00:28 +00:00
|
|
|
|
|
|
|
TransformationPermutePhiOperands::TransformationPermutePhiOperands(
|
|
|
|
uint32_t result_id, const std::vector<uint32_t>& permutation) {
|
|
|
|
message_.set_result_id(result_id);
|
|
|
|
|
|
|
|
for (auto index : permutation) {
|
|
|
|
message_.add_permutation(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformationPermutePhiOperands::IsApplicable(
|
|
|
|
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
|
|
|
|
// Check that |message_.result_id| is valid.
|
|
|
|
const auto* inst =
|
|
|
|
ir_context->get_def_use_mgr()->GetDef(message_.result_id());
|
2022-11-04 21:27:10 +00:00
|
|
|
if (!inst || inst->opcode() != spv::Op::OpPhi) {
|
2020-06-23 14:00:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that |message_.permutation| has expected size.
|
|
|
|
auto expected_permutation_size = inst->NumInOperands() / 2;
|
|
|
|
if (static_cast<uint32_t>(message_.permutation().size()) !=
|
|
|
|
expected_permutation_size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that |message_.permutation| has elements in range
|
|
|
|
// [0, expected_permutation_size - 1].
|
|
|
|
std::vector<uint32_t> permutation(message_.permutation().begin(),
|
|
|
|
message_.permutation().end());
|
|
|
|
assert(!fuzzerutil::HasDuplicates(permutation) &&
|
|
|
|
"Permutation has duplicates");
|
|
|
|
|
|
|
|
// We must check whether the permutation is empty first because in that case
|
|
|
|
// |expected_permutation_size - 1| will produce
|
|
|
|
// |std::numeric_limits<uint32_t>::max()| since it's an unsigned integer.
|
|
|
|
return permutation.empty() ||
|
|
|
|
fuzzerutil::IsPermutationOfRange(permutation, 0,
|
|
|
|
expected_permutation_size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformationPermutePhiOperands::Apply(
|
|
|
|
opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
|
|
|
|
auto* inst = ir_context->get_def_use_mgr()->GetDef(message_.result_id());
|
|
|
|
assert(inst);
|
|
|
|
|
|
|
|
opt::Instruction::OperandList permuted_operands;
|
|
|
|
permuted_operands.reserve(inst->NumInOperands());
|
|
|
|
|
|
|
|
for (auto index : message_.permutation()) {
|
|
|
|
permuted_operands.push_back(std::move(inst->GetInOperand(2 * index)));
|
|
|
|
permuted_operands.push_back(std::move(inst->GetInOperand(2 * index + 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
inst->SetInOperands(std::move(permuted_operands));
|
|
|
|
|
2021-03-17 09:28:36 +00:00
|
|
|
// Update the def-use manager.
|
2021-06-01 07:37:45 +00:00
|
|
|
ir_context->UpdateDefUse(inst);
|
2020-06-23 14:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protobufs::Transformation TransformationPermutePhiOperands::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_permute_phi_operands() = message_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
std::unordered_set<uint32_t> TransformationPermutePhiOperands::GetFreshIds()
|
|
|
|
const {
|
|
|
|
return std::unordered_set<uint32_t>();
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:00:28 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|