#include "source/fuzz/transformation_expand_vector_reduction.h"
#include "source/fuzz/fuzzer_util.h"
#include "source/fuzz/instruction_descriptor.h"
namespace spvtools {
namespace fuzz {
TransformationExpandVectorReduction::TransformationExpandVectorReduction(
protobufs::TransformationExpandVectorReduction message)
: message_(std::move(message)) {}
TransformationExpandVectorReduction::TransformationExpandVectorReduction(
const uint32_t instruction_result_id,
const std::vector<uint32_t>& fresh_ids) {
message_.set_instruction_result_id(instruction_result_id);
*message_.mutable_fresh_ids() =
google::protobuf::RepeatedField<google::protobuf::uint32>(
fresh_ids.begin(), fresh_ids.end());
}
bool TransformationExpandVectorReduction::IsApplicable(
opt::IRContext* ir_context, const TransformationContext& ) const {
auto* instruction =
ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
if (!instruction) {
return false;
}
if (instruction->opcode() != spv::Op::OpAny &&
instruction->opcode() != spv::Op::OpAll) {
return false;
}
if (static_cast<uint32_t>(message_.fresh_ids().size()) !=
GetRequiredFreshIdCount(ir_context, instruction)) {
return false;
}
std::set<uint32_t> ids_used_by_this_transformation;
for (uint32_t fresh_id : message_.fresh_ids()) {
if (!fuzzerutil::IsFreshId(ir_context, fresh_id)) {
return false;
}
if (!CheckIdIsFreshAndNotUsedByThisTransformation(
fresh_id, ir_context, &ids_used_by_this_transformation)) {
return false;
}
}
return true;
}
void TransformationExpandVectorReduction::Apply(
opt::IRContext* ir_context,
TransformationContext* transformation_context) const {
auto* instruction =
ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
auto* vector = ir_context->get_def_use_mgr()->GetDef(
instruction->GetSingleWordInOperand(0));
uint32_t vector_component_count = ir_context->get_type_mgr()
->GetType(vector->type_id())
->AsVector()
->element_count();
auto fresh_id = message_.fresh_ids().begin();
std::vector<uint32_t> vector_components;
for (uint32_t i = 0; i < vector_component_count; i++) {
auto vector_component =
opt::Instruction(ir_context, spv::Op::OpCompositeExtract,
instruction->type_id(), *fresh_id++,
{{SPV_OPERAND_TYPE_ID, {vector->result_id()}},
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}}});
instruction->InsertBefore(MakeUnique<opt::Instruction>(vector_component));
fuzzerutil::UpdateModuleIdBound(ir_context, vector_component.result_id());
vector_components.push_back(vector_component.result_id());
}
auto logical_instruction = opt::Instruction(
ir_context,
instruction->opcode() == spv::Op::OpAny ? spv::Op::OpLogicalOr
: spv::Op::OpLogicalAnd,
instruction->type_id(), *fresh_id++,
{{SPV_OPERAND_TYPE_ID, {vector_components[0]}},
{SPV_OPERAND_TYPE_ID, {vector_components[1]}}});
instruction->InsertBefore(MakeUnique<opt::Instruction>(logical_instruction));
fuzzerutil::UpdateModuleIdBound(ir_context, logical_instruction.result_id());
for (uint32_t i = 2; i < vector_components.size(); i++) {
logical_instruction = opt::Instruction(
ir_context, logical_instruction.opcode(), instruction->type_id(),
*fresh_id++,
{{SPV_OPERAND_TYPE_ID, {vector_components[i]}},
{SPV_OPERAND_TYPE_ID, {logical_instruction.result_id()}}});
instruction->InsertBefore(
MakeUnique<opt::Instruction>(logical_instruction));
fuzzerutil::UpdateModuleIdBound(ir_context,
logical_instruction.result_id());
}
ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context,
*instruction)) {
transformation_context->GetFactManager()->AddFactDataSynonym(
MakeDataDescriptor(logical_instruction.result_id(), {}),
MakeDataDescriptor(instruction->result_id(), {}));
}
}
protobufs::Transformation TransformationExpandVectorReduction::ToMessage()
const {
protobufs::Transformation result;
*result.mutable_expand_vector_reduction() = message_;
return result;
}
uint32_t TransformationExpandVectorReduction::GetRequiredFreshIdCount(
opt::IRContext* ir_context, opt::Instruction* instruction) {
return 2 * ir_context->get_type_mgr()
->GetType(ir_context->get_def_use_mgr()
->GetDef(instruction->GetSingleWordInOperand(0))
->type_id())
->AsVector()
->element_count() -
1;
}
std::unordered_set<uint32_t> TransformationExpandVectorReduction::GetFreshIds()
const {
std::unordered_set<uint32_t> result;
for (auto id : message_.fresh_ids()) {
result.insert(id);
}
return result;
}
}
}