Debian Codegraph @

third_party/spirv-tools/source/fuzz/transformation_make_vector_operation_dynamic.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_make_vector_operation_dynamic.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 TransformationMakeVectorOperationDynamic::
24 TransformationMakeVectorOperationDynamic(
25 const spvtools::fuzz::protobufs::
26 TransformationMakeVectorOperationDynamic& message)
27 : message_(message) {}
28
29 TransformationMakeVectorOperationDynamic::
30 TransformationMakeVectorOperationDynamic(uint32_t instruction_result_id,
31 uint32_t constant_index_id) {
32 message_.set_instruction_result_id(instruction_result_id);
33 message_.set_constant_index_id(constant_index_id);
34 }
35
36 bool TransformationMakeVectorOperationDynamic::IsApplicable(
37 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
38 // |instruction| must be a vector operation.
39 auto instruction =
40 ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
41 if (!IsVectorOperation(ir_context, instruction)) {
42 return false;
43 }
44
45 // |constant_index_instruction| must be defined as an integer instruction.
46 auto constant_index_instruction =
47 ir_context->get_def_use_mgr()->GetDef(message_.constant_index_id());
48 if (!constant_index_instruction || !constant_index_instruction->type_id() ||
49 !ir_context->get_type_mgr()
50 ->GetType(constant_index_instruction->type_id())
51 ->AsInteger()) {
52 return false;
53 }
54
55 return true;
56 }
57
58 void TransformationMakeVectorOperationDynamic::Apply(
59 opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
60 auto instruction =
61 ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
62
63 // The OpVectorInsertDynamic instruction has the vector and component operands
64 // in reverse order in relation to the OpCompositeInsert corresponding
65 // operands.
66 if (instruction->opcode() == SpvOpCompositeInsert) {
67 std::swap(instruction->GetInOperand(0), instruction->GetInOperand(1));
68 }
69
70 // Sets the literal operand to the equivalent constant.
71 instruction->SetInOperand(
72 instruction->opcode() == SpvOpCompositeExtract ? 1 : 2,
73 {message_.constant_index_id()});
74
75 // Sets the |instruction| opcode to the corresponding vector dynamic opcode.
76 instruction->SetOpcode(instruction->opcode() == SpvOpCompositeExtract
77 ? SpvOpVectorExtractDynamic
78 : SpvOpVectorInsertDynamic);
79 }
80
81 protobufs::Transformation TransformationMakeVectorOperationDynamic::ToMessage()
82 const {
83 protobufs::Transformation result;
84 *result.mutable_make_vector_operation_dynamic() = message_;
85 return result;
86 }
87
88 bool TransformationMakeVectorOperationDynamic::IsVectorOperation(
89 opt::IRContext* ir_context, opt::Instruction* instruction) {
90 // |instruction| must be defined and must be an OpCompositeExtract/Insert
91 // instruction.
92 if (!instruction || (instruction->opcode() != SpvOpCompositeExtract &&
93 instruction->opcode() != SpvOpCompositeInsert)) {
94 return false;
95 }
96
97 // The composite must be a vector.
98 auto composite_instruction =
99 ir_context->get_def_use_mgr()->GetDef(instruction->GetSingleWordInOperand(
100 instruction->opcode() == SpvOpCompositeExtract ? 0 : 1));
101 if (!ir_context->get_type_mgr()
102 ->GetType(composite_instruction->type_id())
103 ->AsVector()) {
104 return false;
105 }
106
107 return true;
108 }
109
110 std::unordered_set<uint32_t>
111 TransformationMakeVectorOperationDynamic::GetFreshIds() const {
112 return std::unordered_set<uint32_t>();
113 }
114
115 } // namespace fuzz
116 } // namespace spvtools