1 // Copyright (c) 2020 André Perez Maselco
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_expand_vector_reduction.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/instruction_descriptor.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
TransformationExpandVectorReduction(protobufs::TransformationExpandVectorReduction message)23 TransformationExpandVectorReduction::TransformationExpandVectorReduction(
24 protobufs::TransformationExpandVectorReduction message)
25 : message_(std::move(message)) {}
26
TransformationExpandVectorReduction(const uint32_t instruction_result_id,const std::vector<uint32_t> & fresh_ids)27 TransformationExpandVectorReduction::TransformationExpandVectorReduction(
28 const uint32_t instruction_result_id,
29 const std::vector<uint32_t>& fresh_ids) {
30 message_.set_instruction_result_id(instruction_result_id);
31 *message_.mutable_fresh_ids() =
32 google::protobuf::RepeatedField<google::protobuf::uint32>(
33 fresh_ids.begin(), fresh_ids.end());
34 }
35
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const36 bool TransformationExpandVectorReduction::IsApplicable(
37 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
38 auto* instruction =
39 ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
40
41 // |instruction| must be defined.
42 if (!instruction) {
43 return false;
44 }
45
46 // |instruction| must be OpAny or OpAll.
47 if (instruction->opcode() != SpvOpAny && instruction->opcode() != SpvOpAll) {
48 return false;
49 }
50
51 // |message_.fresh_ids.size| must have the exact number of fresh ids required
52 // to apply the transformation.
53 if (static_cast<uint32_t>(message_.fresh_ids().size()) !=
54 GetRequiredFreshIdCount(ir_context, instruction)) {
55 return false;
56 }
57
58 std::set<uint32_t> ids_used_by_this_transformation;
59 for (uint32_t fresh_id : message_.fresh_ids()) {
60 // All ids in |message_.fresh_ids| must be fresh.
61 if (!fuzzerutil::IsFreshId(ir_context, fresh_id)) {
62 return false;
63 }
64
65 // All fresh ids need to be distinct.
66 if (!CheckIdIsFreshAndNotUsedByThisTransformation(
67 fresh_id, ir_context, &ids_used_by_this_transformation)) {
68 return false;
69 }
70 }
71
72 return true;
73 }
74
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const75 void TransformationExpandVectorReduction::Apply(
76 opt::IRContext* ir_context,
77 TransformationContext* transformation_context) const {
78 auto* instruction =
79 ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
80 auto* vector = ir_context->get_def_use_mgr()->GetDef(
81 instruction->GetSingleWordInOperand(0));
82 uint32_t vector_component_count = ir_context->get_type_mgr()
83 ->GetType(vector->type_id())
84 ->AsVector()
85 ->element_count();
86
87 // Fresh id iterator.
88 auto fresh_id = message_.fresh_ids().begin();
89
90 // |vector_components| are the ids of the extracted components from |vector|.
91 std::vector<uint32_t> vector_components;
92
93 for (uint32_t i = 0; i < vector_component_count; i++) {
94 // Extracts the i-th |vector| component.
95 auto vector_component = opt::Instruction(
96 ir_context, SpvOpCompositeExtract, instruction->type_id(), *fresh_id++,
97 {{SPV_OPERAND_TYPE_ID, {vector->result_id()}},
98 {SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}}});
99 instruction->InsertBefore(MakeUnique<opt::Instruction>(vector_component));
100 fuzzerutil::UpdateModuleIdBound(ir_context, vector_component.result_id());
101 vector_components.push_back(vector_component.result_id());
102 }
103
104 // The first two |vector| components are used in the first logical operation.
105 auto logical_instruction = opt::Instruction(
106 ir_context,
107 instruction->opcode() == SpvOpAny ? SpvOpLogicalOr : SpvOpLogicalAnd,
108 instruction->type_id(), *fresh_id++,
109 {{SPV_OPERAND_TYPE_ID, {vector_components[0]}},
110 {SPV_OPERAND_TYPE_ID, {vector_components[1]}}});
111 instruction->InsertBefore(MakeUnique<opt::Instruction>(logical_instruction));
112 fuzzerutil::UpdateModuleIdBound(ir_context, logical_instruction.result_id());
113
114 // Evaluates the remaining components.
115 for (uint32_t i = 2; i < vector_components.size(); i++) {
116 logical_instruction = opt::Instruction(
117 ir_context, logical_instruction.opcode(), instruction->type_id(),
118 *fresh_id++,
119 {{SPV_OPERAND_TYPE_ID, {vector_components[i]}},
120 {SPV_OPERAND_TYPE_ID, {logical_instruction.result_id()}}});
121 instruction->InsertBefore(
122 MakeUnique<opt::Instruction>(logical_instruction));
123 fuzzerutil::UpdateModuleIdBound(ir_context,
124 logical_instruction.result_id());
125 }
126
127 ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
128
129 // If it's possible to make a synonym of |instruction|, then add the fact that
130 // the last |logical_instruction| is a synonym of |instruction|.
131 if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context,
132 instruction)) {
133 transformation_context->GetFactManager()->AddFactDataSynonym(
134 MakeDataDescriptor(logical_instruction.result_id(), {}),
135 MakeDataDescriptor(instruction->result_id(), {}));
136 }
137 }
138
ToMessage() const139 protobufs::Transformation TransformationExpandVectorReduction::ToMessage()
140 const {
141 protobufs::Transformation result;
142 *result.mutable_expand_vector_reduction() = message_;
143 return result;
144 }
145
GetRequiredFreshIdCount(opt::IRContext * ir_context,opt::Instruction * instruction)146 uint32_t TransformationExpandVectorReduction::GetRequiredFreshIdCount(
147 opt::IRContext* ir_context, opt::Instruction* instruction) {
148 // For each vector component, 1 OpCompositeExtract and 1 OpLogical* (except
149 // for the first component) instructions will be inserted.
150 return 2 * ir_context->get_type_mgr()
151 ->GetType(ir_context->get_def_use_mgr()
152 ->GetDef(instruction->GetSingleWordInOperand(0))
153 ->type_id())
154 ->AsVector()
155 ->element_count() -
156 1;
157 }
158
GetFreshIds() const159 std::unordered_set<uint32_t> TransformationExpandVectorReduction::GetFreshIds()
160 const {
161 std::unordered_set<uint32_t> result;
162 for (auto id : message_.fresh_ids()) {
163 result.insert(id);
164 }
165 return result;
166 }
167
168 } // namespace fuzz
169 } // namespace spvtools
170