| 1 |
// Copyright (c) 2020 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "source/fuzz/transformation_equation_instruction.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/instruction_descriptor.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace fuzz { |
| 22 |
|
| 23 |
TransformationEquationInstruction::TransformationEquationInstruction( |
| 24 |
const spvtools::fuzz::protobufs::TransformationEquationInstruction& message) |
| 25 |
: message_(message) {} |
| 26 |
|
| 27 |
TransformationEquationInstruction::TransformationEquationInstruction( |
| 28 |
uint32_t fresh_id, SpvOp opcode, const std::vector<uint32_t>& in_operand_id, |
| 29 |
const protobufs::InstructionDescriptor& instruction_to_insert_before) { |
| 30 |
message_.set_fresh_id(fresh_id); |
| 31 |
message_.set_opcode(opcode); |
| 32 |
for (auto id : in_operand_id) { |
| 33 |
message_.add_in_operand_id(id); |
| 34 |
} |
| 35 |
*message_.mutable_instruction_to_insert_before() = |
| 36 |
instruction_to_insert_before; |
| 37 |
} |
| 38 |
|
| 39 |
bool TransformationEquationInstruction::IsApplicable( |
| 40 |
opt::IRContext* ir_context, |
| 41 |
const TransformationContext& transformation_context) const { |
| 42 |
// The result id must be fresh. |
| 43 |
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
// The instruction to insert before must exist. |
| 48 |
auto insert_before = |
| 49 |
FindInstruction(message_.instruction_to_insert_before(), ir_context); |
| 50 |
if (!insert_before) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
// The input ids must all exist, not be OpUndef, not be irrelevant, and be |
| 54 |
// available before this instruction. |
| 55 |
for (auto id : message_.in_operand_id()) { |
| 56 |
auto inst = ir_context->get_def_use_mgr()->GetDef(id); |
| 57 |
if (!inst) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
if (inst->opcode() == SpvOpUndef) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
if (transformation_context.GetFactManager()->IdIsIrrelevant(id)) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
if (!fuzzerutil::IdIsAvailableBeforeInstruction(ir_context, insert_before, |
| 67 |
id)) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return MaybeGetResultTypeId(ir_context) != 0; |
| 73 |
} |
| 74 |
|
| 75 |
void TransformationEquationInstruction::Apply( |
| 76 |
opt::IRContext* ir_context, |
| 77 |
TransformationContext* transformation_context) const { |
| 78 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id()); |
| 79 |
|
| 80 |
opt::Instruction::OperandList in_operands; |
| 81 |
std::vector<uint32_t> rhs_id; |
| 82 |
for (auto id : message_.in_operand_id()) { |
| 83 |
in_operands.push_back({SPV_OPERAND_TYPE_ID, {id}}); |
| 84 |
rhs_id.push_back(id); |
| 85 |
} |
| 86 |
|
| 87 |
FindInstruction(message_.instruction_to_insert_before(), ir_context) |
| 88 |
->InsertBefore(MakeUnique<opt::Instruction>( |
| 89 |
ir_context, static_cast<SpvOp>(message_.opcode()), |
| 90 |
MaybeGetResultTypeId(ir_context), message_.fresh_id(), |
| 91 |
std::move(in_operands))); |
| 92 |
|
| 93 |
ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone); |
| 94 |
|
| 95 |
// Add an equation fact as long as the result id is not irrelevant (it could |
| 96 |
// be if we are inserting into a dead block). |
| 97 |
if (!transformation_context->GetFactManager()->IdIsIrrelevant( |
| 98 |
message_.fresh_id())) { |
| 99 |
transformation_context->GetFactManager()->AddFactIdEquation( |
| 100 |
message_.fresh_id(), static_cast<SpvOp>(message_.opcode()), rhs_id); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
protobufs::Transformation TransformationEquationInstruction::ToMessage() const { |
| 105 |
protobufs::Transformation result; |
| 106 |
*result.mutable_equation_instruction() = message_; |
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
uint32_t TransformationEquationInstruction::MaybeGetResultTypeId( |
| 111 |
opt::IRContext* ir_context) const { |
| 112 |
auto opcode = static_cast<SpvOp>(message_.opcode()); |
| 113 |
switch (opcode) { |
| 114 |
case SpvOpConvertUToF: |
| 115 |
case SpvOpConvertSToF: { |
| 116 |
if (message_.in_operand_id_size() != 1) { |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
const auto* type = ir_context->get_type_mgr()->GetType( |
| 121 |
fuzzerutil::GetTypeId(ir_context, message_.in_operand_id(0))); |
| 122 |
if (!type) { |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
if (const auto* vector = type->AsVector()) { |
| 127 |
if (!vector->element_type()->AsInteger()) { |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
if (auto element_type_id = fuzzerutil::MaybeGetFloatType( |
| 132 |
ir_context, vector->element_type()->AsInteger()->width())) { |
| 133 |
return fuzzerutil::MaybeGetVectorType(ir_context, element_type_id, |
| 134 |
vector->element_count()); |
| 135 |
} |
| 136 |
|
| 137 |
return 0; |
| 138 |
} else { |
| 139 |
if (!type->AsInteger()) { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
return fuzzerutil::MaybeGetFloatType(ir_context, |
| 144 |
type->AsInteger()->width()); |
| 145 |
} |
| 146 |
} |
| 147 |
case SpvOpBitcast: { |
| 148 |
if (message_.in_operand_id_size() != 1) { |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
const auto* operand_inst = |
| 153 |
ir_context->get_def_use_mgr()->GetDef(message_.in_operand_id(0)); |
| 154 |
if (!operand_inst) { |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
const auto* operand_type = |
| 159 |
ir_context->get_type_mgr()->GetType(operand_inst->type_id()); |
| 160 |
if (!operand_type) { |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
// TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3539): |
| 165 |
// The only constraint on the types of OpBitcast's parameters is that |
| 166 |
// they must have the same number of bits. Consider improving the code |
| 167 |
// below to support this in full. |
| 168 |
if (const auto* vector = operand_type->AsVector()) { |
| 169 |
uint32_t component_type_id; |
| 170 |
if (const auto* int_type = vector->element_type()->AsInteger()) { |
| 171 |
component_type_id = |
| 172 |
fuzzerutil::MaybeGetFloatType(ir_context, int_type->width()); |
| 173 |
} else if (const auto* float_type = vector->element_type()->AsFloat()) { |
| 174 |
component_type_id = fuzzerutil::MaybeGetIntegerType( |
| 175 |
ir_context, float_type->width(), true); |
| 176 |
if (component_type_id == 0 || |
| 177 |
fuzzerutil::MaybeGetVectorType(ir_context, component_type_id, |
| 178 |
vector->element_count()) == 0) { |
| 179 |
component_type_id = fuzzerutil::MaybeGetIntegerType( |
| 180 |
ir_context, float_type->width(), false); |
| 181 |
} |
| 182 |
} else { |
| 183 |
assert(false && "Only vectors of numerical components are supported"); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
if (component_type_id == 0) { |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
return fuzzerutil::MaybeGetVectorType(ir_context, component_type_id, |
| 192 |
vector->element_count()); |
| 193 |
} else if (const auto* int_type = operand_type->AsInteger()) { |
| 194 |
return fuzzerutil::MaybeGetFloatType(ir_context, int_type->width()); |
| 195 |
} else if (const auto* float_type = operand_type->AsFloat()) { |
| 196 |
if (auto existing_id = fuzzerutil::MaybeGetIntegerType( |
| 197 |
ir_context, float_type->width(), true)) { |
| 198 |
return existing_id; |
| 199 |
} |
| 200 |
|
| 201 |
return fuzzerutil::MaybeGetIntegerType(ir_context, float_type->width(), |
| 202 |
false); |
| 203 |
} else { |
| 204 |
assert(false && |
| 205 |
"Operand is not a scalar or a vector of numerical type"); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
} |
| 209 |
case SpvOpIAdd: |
| 210 |
case SpvOpISub: { |
| 211 |
if (message_.in_operand_id_size() != 2) { |
| 212 |
return 0; |
| 213 |
} |
| 214 |
uint32_t first_operand_width = 0; |
| 215 |
uint32_t first_operand_type_id = 0; |
| 216 |
for (uint32_t index = 0; index < 2; index++) { |
| 217 |
auto operand_inst = ir_context->get_def_use_mgr()->GetDef( |
| 218 |
message_.in_operand_id(index)); |
| 219 |
if (!operand_inst || !operand_inst->type_id()) { |
| 220 |
return 0; |
| 221 |
} |
| 222 |
auto operand_type = |
| 223 |
ir_context->get_type_mgr()->GetType(operand_inst->type_id()); |
| 224 |
if (!(operand_type->AsInteger() || |
| 225 |
(operand_type->AsVector() && |
| 226 |
operand_type->AsVector()->element_type()->AsInteger()))) { |
| 227 |
return 0; |
| 228 |
} |
| 229 |
uint32_t operand_width = |
| 230 |
operand_type->AsInteger() |
| 231 |
? 1 |
| 232 |
: operand_type->AsVector()->element_count(); |
| 233 |
if (index == 0) { |
| 234 |
first_operand_width = operand_width; |
| 235 |
first_operand_type_id = operand_inst->type_id(); |
| 236 |
} else { |
| 237 |
assert(first_operand_width != 0 && |
| 238 |
"The first operand should have been processed."); |
| 239 |
if (operand_width != first_operand_width) { |
| 240 |
return 0; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
assert(first_operand_type_id != 0 && |
| 245 |
"A type must have been found for the first operand."); |
| 246 |
return first_operand_type_id; |
| 247 |
} |
| 248 |
case SpvOpLogicalNot: { |
| 249 |
if (message_.in_operand_id().size() != 1) { |
| 250 |
return 0; |
| 251 |
} |
| 252 |
auto operand_inst = |
| 253 |
ir_context->get_def_use_mgr()->GetDef(message_.in_operand_id(0)); |
| 254 |
if (!operand_inst || !operand_inst->type_id()) { |
| 255 |
return 0; |
| 256 |
} |
| 257 |
auto operand_type = |
| 258 |
ir_context->get_type_mgr()->GetType(operand_inst->type_id()); |
| 259 |
if (!(operand_type->AsBool() || |
| 260 |
(operand_type->AsVector() && |
| 261 |
operand_type->AsVector()->element_type()->AsBool()))) { |
| 262 |
return 0; |
| 263 |
} |
| 264 |
return operand_inst->type_id(); |
| 265 |
} |
| 266 |
case SpvOpSNegate: { |
| 267 |
if (message_.in_operand_id().size() != 1) { |
| 268 |
return 0; |
| 269 |
} |
| 270 |
auto operand_inst = |
| 271 |
ir_context->get_def_use_mgr()->GetDef(message_.in_operand_id(0)); |
| 272 |
if (!operand_inst || !operand_inst->type_id()) { |
| 273 |
return 0; |
| 274 |
} |
| 275 |
auto operand_type = |
| 276 |
ir_context->get_type_mgr()->GetType(operand_inst->type_id()); |
| 277 |
if (!(operand_type->AsInteger() || |
| 278 |
(operand_type->AsVector() && |
| 279 |
operand_type->AsVector()->element_type()->AsInteger()))) { |
| 280 |
return 0; |
| 281 |
} |
| 282 |
return operand_inst->type_id(); |
| 283 |
} |
| 284 |
default: |
| 285 |
assert(false && "Inappropriate opcode for equation instruction."); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
std::unordered_set<uint32_t> TransformationEquationInstruction::GetFreshIds() |
| 291 |
const { |
| 292 |
return {message_.fresh_id()}; |
| 293 |
} |
| 294 |
|
| 295 |
} // namespace fuzz |
| 296 |
} // namespace spvtools |