Debian Codegraph @

third_party/spirv-tools/source/fuzz/transformation_expand_vector_reduction.cpp

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