2020-03-04 14:54:08 +00:00
|
|
|
// Copyright (c) 2020 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_equation_instruction.h"
|
|
|
|
|
|
|
|
#include "source/fuzz/fuzzer_util.h"
|
|
|
|
#include "source/fuzz/instruction_descriptor.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace fuzz {
|
|
|
|
|
|
|
|
TransformationEquationInstruction::TransformationEquationInstruction(
|
2021-03-23 13:31:44 +00:00
|
|
|
protobufs::TransformationEquationInstruction message)
|
|
|
|
: message_(std::move(message)) {}
|
2020-03-04 14:54:08 +00:00
|
|
|
|
|
|
|
TransformationEquationInstruction::TransformationEquationInstruction(
|
2022-11-04 21:27:10 +00:00
|
|
|
uint32_t fresh_id, spv::Op opcode,
|
|
|
|
const std::vector<uint32_t>& in_operand_id,
|
2020-03-04 14:54:08 +00:00
|
|
|
const protobufs::InstructionDescriptor& instruction_to_insert_before) {
|
|
|
|
message_.set_fresh_id(fresh_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
message_.set_opcode(uint32_t(opcode));
|
2020-03-04 14:54:08 +00:00
|
|
|
for (auto id : in_operand_id) {
|
|
|
|
message_.add_in_operand_id(id);
|
|
|
|
}
|
|
|
|
*message_.mutable_instruction_to_insert_before() =
|
|
|
|
instruction_to_insert_before;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransformationEquationInstruction::IsApplicable(
|
2020-07-22 18:03:58 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
const TransformationContext& transformation_context) const {
|
2020-03-04 14:54:08 +00:00
|
|
|
// The result id must be fresh.
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
|
2020-03-04 14:54:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-16 10:59:05 +00:00
|
|
|
|
2020-03-04 14:54:08 +00:00
|
|
|
// The instruction to insert before must exist.
|
|
|
|
auto insert_before =
|
2020-04-02 14:54:46 +00:00
|
|
|
FindInstruction(message_.instruction_to_insert_before(), ir_context);
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!insert_before) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-19 16:32:56 +00:00
|
|
|
// The input ids must all exist, not be OpUndef, not be irrelevant, and be
|
|
|
|
// available before this instruction.
|
2020-03-04 14:54:08 +00:00
|
|
|
for (auto id : message_.in_operand_id()) {
|
2020-04-02 14:54:46 +00:00
|
|
|
auto inst = ir_context->get_def_use_mgr()->GetDef(id);
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!inst) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
if (inst->opcode() == spv::Op::OpUndef) {
|
2020-03-04 14:54:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-24 11:27:59 +00:00
|
|
|
if (transformation_context.GetFactManager()->IdIsIrrelevant(id)) {
|
2020-07-22 18:03:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-02 14:54:46 +00:00
|
|
|
if (!fuzzerutil::IdIsAvailableBeforeInstruction(ir_context, insert_before,
|
2020-03-04 14:54:08 +00:00
|
|
|
id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 10:59:05 +00:00
|
|
|
return MaybeGetResultTypeId(ir_context) != 0;
|
2020-03-04 14:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransformationEquationInstruction::Apply(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context,
|
|
|
|
TransformationContext* transformation_context) const {
|
|
|
|
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
|
2020-03-04 14:54:08 +00:00
|
|
|
|
|
|
|
opt::Instruction::OperandList in_operands;
|
|
|
|
std::vector<uint32_t> rhs_id;
|
|
|
|
for (auto id : message_.in_operand_id()) {
|
|
|
|
in_operands.push_back({SPV_OPERAND_TYPE_ID, {id}});
|
|
|
|
rhs_id.push_back(id);
|
|
|
|
}
|
|
|
|
|
2021-03-17 09:28:46 +00:00
|
|
|
auto insert_before =
|
|
|
|
FindInstruction(message_.instruction_to_insert_before(), ir_context);
|
2021-03-26 10:22:06 +00:00
|
|
|
opt::Instruction* new_instruction =
|
|
|
|
insert_before->InsertBefore(MakeUnique<opt::Instruction>(
|
2022-11-04 21:27:10 +00:00
|
|
|
ir_context, static_cast<spv::Op>(message_.opcode()),
|
2021-03-26 10:22:06 +00:00
|
|
|
MaybeGetResultTypeId(ir_context), message_.fresh_id(),
|
|
|
|
std::move(in_operands)));
|
2021-03-17 09:28:46 +00:00
|
|
|
|
2021-03-26 10:22:06 +00:00
|
|
|
ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
|
|
|
|
ir_context->set_instr_block(new_instruction,
|
2021-03-17 09:28:46 +00:00
|
|
|
ir_context->get_instr_block(insert_before));
|
2020-03-04 14:54:08 +00:00
|
|
|
|
2020-10-19 16:32:56 +00:00
|
|
|
// Add an equation fact as long as the result id is not irrelevant (it could
|
|
|
|
// be if we are inserting into a dead block).
|
|
|
|
if (!transformation_context->GetFactManager()->IdIsIrrelevant(
|
|
|
|
message_.fresh_id())) {
|
|
|
|
transformation_context->GetFactManager()->AddFactIdEquation(
|
2022-11-04 21:27:10 +00:00
|
|
|
message_.fresh_id(), static_cast<spv::Op>(message_.opcode()), rhs_id);
|
2020-10-19 16:32:56 +00:00
|
|
|
}
|
2020-03-04 14:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protobufs::Transformation TransformationEquationInstruction::ToMessage() const {
|
|
|
|
protobufs::Transformation result;
|
|
|
|
*result.mutable_equation_instruction() = message_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-16 10:59:05 +00:00
|
|
|
uint32_t TransformationEquationInstruction::MaybeGetResultTypeId(
|
2020-04-02 14:54:46 +00:00
|
|
|
opt::IRContext* ir_context) const {
|
2022-11-04 21:27:10 +00:00
|
|
|
auto opcode = static_cast<spv::Op>(message_.opcode());
|
2020-07-16 10:59:05 +00:00
|
|
|
switch (opcode) {
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpConvertUToF:
|
|
|
|
case spv::Op::OpConvertSToF: {
|
2020-07-16 10:59:05 +00:00
|
|
|
if (message_.in_operand_id_size() != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* type = ir_context->get_type_mgr()->GetType(
|
|
|
|
fuzzerutil::GetTypeId(ir_context, message_.in_operand_id(0)));
|
|
|
|
if (!type) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto* vector = type->AsVector()) {
|
|
|
|
if (!vector->element_type()->AsInteger()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto element_type_id = fuzzerutil::MaybeGetFloatType(
|
|
|
|
ir_context, vector->element_type()->AsInteger()->width())) {
|
|
|
|
return fuzzerutil::MaybeGetVectorType(ir_context, element_type_id,
|
|
|
|
vector->element_count());
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (!type->AsInteger()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuzzerutil::MaybeGetFloatType(ir_context,
|
|
|
|
type->AsInteger()->width());
|
|
|
|
}
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpBitcast: {
|
2020-07-16 13:07:09 +00:00
|
|
|
if (message_.in_operand_id_size() != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* operand_inst =
|
|
|
|
ir_context->get_def_use_mgr()->GetDef(message_.in_operand_id(0));
|
|
|
|
if (!operand_inst) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto* operand_type =
|
|
|
|
ir_context->get_type_mgr()->GetType(operand_inst->type_id());
|
|
|
|
if (!operand_type) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3539):
|
|
|
|
// The only constraint on the types of OpBitcast's parameters is that
|
|
|
|
// they must have the same number of bits. Consider improving the code
|
|
|
|
// below to support this in full.
|
|
|
|
if (const auto* vector = operand_type->AsVector()) {
|
|
|
|
uint32_t component_type_id;
|
|
|
|
if (const auto* int_type = vector->element_type()->AsInteger()) {
|
|
|
|
component_type_id =
|
|
|
|
fuzzerutil::MaybeGetFloatType(ir_context, int_type->width());
|
|
|
|
} else if (const auto* float_type = vector->element_type()->AsFloat()) {
|
|
|
|
component_type_id = fuzzerutil::MaybeGetIntegerType(
|
|
|
|
ir_context, float_type->width(), true);
|
|
|
|
if (component_type_id == 0 ||
|
|
|
|
fuzzerutil::MaybeGetVectorType(ir_context, component_type_id,
|
|
|
|
vector->element_count()) == 0) {
|
|
|
|
component_type_id = fuzzerutil::MaybeGetIntegerType(
|
|
|
|
ir_context, float_type->width(), false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(false && "Only vectors of numerical components are supported");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (component_type_id == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuzzerutil::MaybeGetVectorType(ir_context, component_type_id,
|
|
|
|
vector->element_count());
|
|
|
|
} else if (const auto* int_type = operand_type->AsInteger()) {
|
|
|
|
return fuzzerutil::MaybeGetFloatType(ir_context, int_type->width());
|
|
|
|
} else if (const auto* float_type = operand_type->AsFloat()) {
|
|
|
|
if (auto existing_id = fuzzerutil::MaybeGetIntegerType(
|
|
|
|
ir_context, float_type->width(), true)) {
|
|
|
|
return existing_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuzzerutil::MaybeGetIntegerType(ir_context, float_type->width(),
|
|
|
|
false);
|
|
|
|
} else {
|
|
|
|
assert(false &&
|
|
|
|
"Operand is not a scalar or a vector of numerical type");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpIAdd:
|
|
|
|
case spv::Op::OpISub: {
|
2020-07-16 10:59:05 +00:00
|
|
|
if (message_.in_operand_id_size() != 2) {
|
2020-03-04 14:54:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint32_t first_operand_width = 0;
|
|
|
|
uint32_t first_operand_type_id = 0;
|
|
|
|
for (uint32_t index = 0; index < 2; index++) {
|
2020-04-02 14:54:46 +00:00
|
|
|
auto operand_inst = ir_context->get_def_use_mgr()->GetDef(
|
|
|
|
message_.in_operand_id(index));
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!operand_inst || !operand_inst->type_id()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto operand_type =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_type_mgr()->GetType(operand_inst->type_id());
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!(operand_type->AsInteger() ||
|
|
|
|
(operand_type->AsVector() &&
|
|
|
|
operand_type->AsVector()->element_type()->AsInteger()))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint32_t operand_width =
|
|
|
|
operand_type->AsInteger()
|
|
|
|
? 1
|
|
|
|
: operand_type->AsVector()->element_count();
|
|
|
|
if (index == 0) {
|
|
|
|
first_operand_width = operand_width;
|
|
|
|
first_operand_type_id = operand_inst->type_id();
|
|
|
|
} else {
|
|
|
|
assert(first_operand_width != 0 &&
|
|
|
|
"The first operand should have been processed.");
|
|
|
|
if (operand_width != first_operand_width) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(first_operand_type_id != 0 &&
|
|
|
|
"A type must have been found for the first operand.");
|
|
|
|
return first_operand_type_id;
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpLogicalNot: {
|
2020-03-04 14:54:08 +00:00
|
|
|
if (message_.in_operand_id().size() != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto operand_inst =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_def_use_mgr()->GetDef(message_.in_operand_id(0));
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!operand_inst || !operand_inst->type_id()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto operand_type =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_type_mgr()->GetType(operand_inst->type_id());
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!(operand_type->AsBool() ||
|
|
|
|
(operand_type->AsVector() &&
|
|
|
|
operand_type->AsVector()->element_type()->AsBool()))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return operand_inst->type_id();
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
case spv::Op::OpSNegate: {
|
2020-03-04 14:54:08 +00:00
|
|
|
if (message_.in_operand_id().size() != 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto operand_inst =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_def_use_mgr()->GetDef(message_.in_operand_id(0));
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!operand_inst || !operand_inst->type_id()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto operand_type =
|
2020-04-02 14:54:46 +00:00
|
|
|
ir_context->get_type_mgr()->GetType(operand_inst->type_id());
|
2020-03-04 14:54:08 +00:00
|
|
|
if (!(operand_type->AsInteger() ||
|
|
|
|
(operand_type->AsVector() &&
|
|
|
|
operand_type->AsVector()->element_type()->AsInteger()))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return operand_inst->type_id();
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(false && "Inappropriate opcode for equation instruction.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:12:49 +00:00
|
|
|
std::unordered_set<uint32_t> TransformationEquationInstruction::GetFreshIds()
|
|
|
|
const {
|
|
|
|
return {message_.fresh_id()};
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:54:08 +00:00
|
|
|
} // namespace fuzz
|
|
|
|
} // namespace spvtools
|