| 1 |
// Copyright (c) 2019 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_vector_shuffle.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 |
TransformationVectorShuffle::TransformationVectorShuffle( |
| 24 |
const spvtools::fuzz::protobufs::TransformationVectorShuffle& message) |
| 25 |
: message_(message) {} |
| 26 |
|
| 27 |
TransformationVectorShuffle::TransformationVectorShuffle( |
| 28 |
const protobufs::InstructionDescriptor& instruction_to_insert_before, |
| 29 |
uint32_t fresh_id, uint32_t vector1, uint32_t vector2, |
| 30 |
const std::vector<uint32_t>& component) { |
| 31 |
*message_.mutable_instruction_to_insert_before() = |
| 32 |
instruction_to_insert_before; |
| 33 |
message_.set_fresh_id(fresh_id); |
| 34 |
message_.set_vector1(vector1); |
| 35 |
message_.set_vector2(vector2); |
| 36 |
for (auto a_component : component) { |
| 37 |
message_.add_component(a_component); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
bool TransformationVectorShuffle::IsApplicable( |
| 42 |
opt::IRContext* ir_context, const TransformationContext& /*unused*/) const { |
| 43 |
// The fresh id must not already be in use. |
| 44 |
if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
// The instruction before which the shuffle will be inserted must exist. |
| 48 |
auto instruction_to_insert_before = |
| 49 |
FindInstruction(message_.instruction_to_insert_before(), ir_context); |
| 50 |
if (!instruction_to_insert_before) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
// The first vector must be an instruction with a type id |
| 54 |
auto vector1_instruction = |
| 55 |
ir_context->get_def_use_mgr()->GetDef(message_.vector1()); |
| 56 |
if (!vector1_instruction || !vector1_instruction->type_id()) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
// The second vector must be an instruction with a type id |
| 60 |
auto vector2_instruction = |
| 61 |
ir_context->get_def_use_mgr()->GetDef(message_.vector2()); |
| 62 |
if (!vector2_instruction || !vector2_instruction->type_id()) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
auto vector1_type = |
| 66 |
ir_context->get_type_mgr()->GetType(vector1_instruction->type_id()); |
| 67 |
// The first vector instruction's type must actually be a vector type. |
| 68 |
if (!vector1_type->AsVector()) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
auto vector2_type = |
| 72 |
ir_context->get_type_mgr()->GetType(vector2_instruction->type_id()); |
| 73 |
// The second vector instruction's type must actually be a vector type. |
| 74 |
if (!vector2_type->AsVector()) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
// The element types of the vectors must be the same. |
| 78 |
if (vector1_type->AsVector()->element_type() != |
| 79 |
vector2_type->AsVector()->element_type()) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
uint32_t combined_size = vector1_type->AsVector()->element_count() + |
| 83 |
vector2_type->AsVector()->element_count(); |
| 84 |
for (auto a_compoment : message_.component()) { |
| 85 |
// 0xFFFFFFFF is used to represent an undefined component. Unless |
| 86 |
// undefined, a component must be less than the combined size of the |
| 87 |
// vectors. |
| 88 |
if (a_compoment != 0xFFFFFFFF && a_compoment >= combined_size) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
// The module must already declare an appropriate type in which to store the |
| 93 |
// result of the shuffle. |
| 94 |
if (!GetResultTypeId(ir_context, *vector1_type->AsVector()->element_type())) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
// Each of the vectors used in the shuffle must be available at the insertion |
| 98 |
// point. |
| 99 |
for (auto used_instruction : {vector1_instruction, vector2_instruction}) { |
| 100 |
if (auto block = ir_context->get_instr_block(used_instruction)) { |
| 101 |
if (!ir_context->GetDominatorAnalysis(block->GetParent()) |
| 102 |
->Dominates(used_instruction, instruction_to_insert_before)) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// It must be legitimate to insert an OpVectorShuffle before the identified |
| 109 |
// instruction. |
| 110 |
return fuzzerutil::CanInsertOpcodeBeforeInstruction( |
| 111 |
SpvOpVectorShuffle, instruction_to_insert_before); |
| 112 |
} |
| 113 |
|
| 114 |
void TransformationVectorShuffle::Apply( |
| 115 |
opt::IRContext* ir_context, |
| 116 |
TransformationContext* transformation_context) const { |
| 117 |
// Make input operands for a shuffle instruction - these comprise the two |
| 118 |
// vectors being shuffled, followed by the integer literal components. |
| 119 |
opt::Instruction::OperandList shuffle_operands = { |
| 120 |
{SPV_OPERAND_TYPE_ID, {message_.vector1()}}, |
| 121 |
{SPV_OPERAND_TYPE_ID, {message_.vector2()}}}; |
| 122 |
for (auto a_component : message_.component()) { |
| 123 |
shuffle_operands.push_back( |
| 124 |
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {a_component}}); |
| 125 |
} |
| 126 |
|
| 127 |
uint32_t result_type_id = GetResultTypeId( |
| 128 |
ir_context, |
| 129 |
*GetVectorType(ir_context, message_.vector1())->element_type()); |
| 130 |
|
| 131 |
// Add a shuffle instruction right before the instruction identified by |
| 132 |
// |message_.instruction_to_insert_before|. |
| 133 |
FindInstruction(message_.instruction_to_insert_before(), ir_context) |
| 134 |
->InsertBefore(MakeUnique<opt::Instruction>( |
| 135 |
ir_context, SpvOpVectorShuffle, result_type_id, message_.fresh_id(), |
| 136 |
shuffle_operands)); |
| 137 |
fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id()); |
| 138 |
ir_context->InvalidateAnalysesExceptFor( |
| 139 |
opt::IRContext::Analysis::kAnalysisNone); |
| 140 |
|
| 141 |
AddDataSynonymFacts(ir_context, transformation_context); |
| 142 |
} |
| 143 |
|
| 144 |
protobufs::Transformation TransformationVectorShuffle::ToMessage() const { |
| 145 |
protobufs::Transformation result; |
| 146 |
*result.mutable_vector_shuffle() = message_; |
| 147 |
return result; |
| 148 |
} |
| 149 |
|
| 150 |
uint32_t TransformationVectorShuffle::GetResultTypeId( |
| 151 |
opt::IRContext* ir_context, const opt::analysis::Type& element_type) const { |
| 152 |
opt::analysis::Vector result_type( |
| 153 |
&element_type, static_cast<uint32_t>(message_.component_size())); |
| 154 |
return ir_context->get_type_mgr()->GetId(&result_type); |
| 155 |
} |
| 156 |
|
| 157 |
opt::analysis::Vector* TransformationVectorShuffle::GetVectorType( |
| 158 |
opt::IRContext* ir_context, uint32_t id_of_vector) { |
| 159 |
return ir_context->get_type_mgr() |
| 160 |
->GetType(ir_context->get_def_use_mgr()->GetDef(id_of_vector)->type_id()) |
| 161 |
->AsVector(); |
| 162 |
} |
| 163 |
|
| 164 |
std::unordered_set<uint32_t> TransformationVectorShuffle::GetFreshIds() const { |
| 165 |
return {message_.fresh_id()}; |
| 166 |
} |
| 167 |
|
| 168 |
void TransformationVectorShuffle::AddDataSynonymFacts( |
| 169 |
opt::IRContext* ir_context, |
| 170 |
TransformationContext* transformation_context) const { |
| 171 |
// If the new instruction is irrelevant (because it is in a dead block), it |
| 172 |
// cannot participate in any DataSynonym fact. |
| 173 |
if (transformation_context->GetFactManager()->IdIsIrrelevant( |
| 174 |
message_.fresh_id())) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
// Add synonym facts relating the defined elements of the shuffle result to |
| 179 |
// the vector components that they come from. |
| 180 |
for (uint32_t component_index = 0; |
| 181 |
component_index < static_cast<uint32_t>(message_.component_size()); |
| 182 |
component_index++) { |
| 183 |
uint32_t component = message_.component(component_index); |
| 184 |
if (component == 0xFFFFFFFF) { |
| 185 |
// This component is undefined, we do not introduce a synonym. |
| 186 |
continue; |
| 187 |
} |
| 188 |
// This describes the element of the result vector associated with |
| 189 |
// |component_index|. |
| 190 |
protobufs::DataDescriptor descriptor_for_result_component = |
| 191 |
MakeDataDescriptor(message_.fresh_id(), {component_index}); |
| 192 |
|
| 193 |
protobufs::DataDescriptor descriptor_for_source_component; |
| 194 |
|
| 195 |
// Get a data descriptor for the component of the input vector to which |
| 196 |
// |component| refers. |
| 197 |
if (component < |
| 198 |
GetVectorType(ir_context, message_.vector1())->element_count()) { |
| 199 |
// Check that the first vector can participate in data synonym facts. |
| 200 |
if (!fuzzerutil::CanMakeSynonymOf( |
| 201 |
ir_context, *transformation_context, |
| 202 |
ir_context->get_def_use_mgr()->GetDef(message_.vector1()))) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
descriptor_for_source_component = |
| 206 |
MakeDataDescriptor(message_.vector1(), {component}); |
| 207 |
} else { |
| 208 |
// Check that the second vector can participate in data synonym facts. |
| 209 |
if (!fuzzerutil::CanMakeSynonymOf( |
| 210 |
ir_context, *transformation_context, |
| 211 |
ir_context->get_def_use_mgr()->GetDef(message_.vector2()))) { |
| 212 |
continue; |
| 213 |
} |
| 214 |
auto index_into_vector_2 = |
| 215 |
component - |
| 216 |
GetVectorType(ir_context, message_.vector1())->element_count(); |
| 217 |
assert( |
| 218 |
index_into_vector_2 < |
| 219 |
GetVectorType(ir_context, message_.vector2())->element_count() && |
| 220 |
"Vector shuffle index is out of bounds."); |
| 221 |
descriptor_for_source_component = |
| 222 |
MakeDataDescriptor(message_.vector2(), {index_into_vector_2}); |
| 223 |
} |
| 224 |
|
| 225 |
// Add a fact relating this input vector component with the associated |
| 226 |
// result component. |
| 227 |
transformation_context->GetFactManager()->AddFactDataSynonym( |
| 228 |
descriptor_for_result_component, descriptor_for_source_component); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
} // namespace fuzz |
| 233 |
} // namespace spvtools |