#include "source/fuzz/transformation_replace_parameter_with_global.h"
#include <vector>
#include "source/fuzz/fuzzer_util.h"
namespace spvtools {
namespace fuzz {
TransformationReplaceParameterWithGlobal::
TransformationReplaceParameterWithGlobal(
protobufs::TransformationReplaceParameterWithGlobal message)
: message_(std::move(message)) {}
TransformationReplaceParameterWithGlobal::
TransformationReplaceParameterWithGlobal(
uint32_t function_type_fresh_id, uint32_t parameter_id,
uint32_t global_variable_fresh_id) {
message_.set_function_type_fresh_id(function_type_fresh_id);
message_.set_parameter_id(parameter_id);
message_.set_global_variable_fresh_id(global_variable_fresh_id);
}
bool TransformationReplaceParameterWithGlobal::IsApplicable(
opt::IRContext* ir_context,
const TransformationContext& transformation_context) const {
const auto* param_inst =
ir_context->get_def_use_mgr()->GetDef(message_.parameter_id());
if (!param_inst || param_inst->opcode() != spv::Op::OpFunctionParameter) {
return false;
}
const auto* function = fuzzerutil::GetFunctionFromParameterId(
ir_context, message_.parameter_id());
if (!function ||
fuzzerutil::FunctionIsEntryPoint(ir_context, function->result_id())) {
return false;
}
if (!IsParameterTypeSupported(ir_context, param_inst->type_id())) {
return false;
}
if (fuzzerutil::MaybeGetZeroConstant(ir_context, transformation_context,
param_inst->type_id(), false) == 0) {
return false;
}
if (!fuzzerutil::MaybeGetPointerType(ir_context, param_inst->type_id(),
spv::StorageClass::Private)) {
return false;
}
return fuzzerutil::IsFreshId(ir_context, message_.function_type_fresh_id()) &&
fuzzerutil::IsFreshId(ir_context,
message_.global_variable_fresh_id()) &&
message_.function_type_fresh_id() !=
message_.global_variable_fresh_id();
}
void TransformationReplaceParameterWithGlobal::Apply(
opt::IRContext* ir_context,
TransformationContext* transformation_context) const {
const auto* param_inst =
ir_context->get_def_use_mgr()->GetDef(message_.parameter_id());
assert(param_inst && "Parameter must exist");
fuzzerutil::AddGlobalVariable(
ir_context, message_.global_variable_fresh_id(),
fuzzerutil::MaybeGetPointerType(ir_context, param_inst->type_id(),
spv::StorageClass::Private),
spv::StorageClass::Private,
fuzzerutil::MaybeGetZeroConstant(ir_context, *transformation_context,
param_inst->type_id(), false));
auto* function = fuzzerutil::GetFunctionFromParameterId(
ir_context, message_.parameter_id());
assert(function && "Function must exist");
auto it = function->begin()->begin();
while (it != function->begin()->end() &&
!fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLoad, it)) {
++it;
}
assert(fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpLoad, it) &&
"Can't insert OpLoad or OpCopyMemory into the first basic block of "
"the function");
it.InsertBefore(MakeUnique<opt::Instruction>(
ir_context, spv::Op::OpLoad, param_inst->type_id(),
param_inst->result_id(),
opt::Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {message_.global_variable_fresh_id()}}}));
auto params = fuzzerutil::GetParameters(ir_context, function->result_id());
auto parameter_index = static_cast<uint32_t>(params.size());
for (uint32_t i = 0, n = static_cast<uint32_t>(params.size()); i < n; ++i) {
if (params[i]->result_id() == message_.parameter_id()) {
parameter_index = i;
break;
}
}
assert(parameter_index != params.size() &&
"Parameter must exist in the function");
for (auto* inst : fuzzerutil::GetCallers(ir_context, function->result_id())) {
assert(
fuzzerutil::CanInsertOpcodeBeforeInstruction(spv::Op::OpStore, inst) &&
"Can't insert OpStore right before the function call");
inst->InsertBefore(MakeUnique<opt::Instruction>(
ir_context, spv::Op::OpStore, 0, 0,
opt::Instruction::OperandList{
{SPV_OPERAND_TYPE_ID, {message_.global_variable_fresh_id()}},
{SPV_OPERAND_TYPE_ID,
{inst->GetSingleWordInOperand(parameter_index + 1)}}}));
inst->RemoveInOperand(parameter_index + 1);
}
fuzzerutil::RemoveParameter(ir_context, message_.parameter_id());
{
auto* old_function_type = fuzzerutil::GetFunctionType(ir_context, function);
assert(old_function_type && "Function has invalid type");
std::vector<uint32_t> parameter_type_ids;
for (uint32_t i = 1; i < old_function_type->NumInOperands(); ++i) {
if (i - 1 != parameter_index) {
parameter_type_ids.push_back(
old_function_type->GetSingleWordInOperand(i));
}
}
fuzzerutil::UpdateFunctionType(
ir_context, function->result_id(), message_.function_type_fresh_id(),
old_function_type->GetSingleWordInOperand(0), parameter_type_ids);
}
ir_context->InvalidateAnalysesExceptFor(
opt::IRContext::Analysis::kAnalysisNone);
if (transformation_context->GetFactManager()->IdIsIrrelevant(
message_.parameter_id())) {
transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
message_.global_variable_fresh_id());
}
}
protobufs::Transformation TransformationReplaceParameterWithGlobal::ToMessage()
const {
protobufs::Transformation result;
*result.mutable_replace_parameter_with_global() = message_;
return result;
}
bool TransformationReplaceParameterWithGlobal::IsParameterTypeSupported(
opt::IRContext* ir_context, uint32_t param_type_id) {
return fuzzerutil::CanCreateConstant(ir_context, param_type_id);
}
std::unordered_set<uint32_t>
TransformationReplaceParameterWithGlobal::GetFreshIds() const {
return {message_.function_type_fresh_id(),
message_.global_variable_fresh_id()};
}
}
}