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_replace_id_with_synonym.h"
16
17 #include <algorithm>
18
19 #include "source/fuzz/data_descriptor.h"
20 #include "source/fuzz/fuzzer_util.h"
21 #include "source/fuzz/id_use_descriptor.h"
22 #include "source/opt/types.h"
23 #include "source/util/make_unique.h"
24
25 namespace spvtools {
26 namespace fuzz {
27
TransformationReplaceIdWithSynonym(protobufs::TransformationReplaceIdWithSynonym message)28 TransformationReplaceIdWithSynonym::TransformationReplaceIdWithSynonym(
29 protobufs::TransformationReplaceIdWithSynonym message)
30 : message_(std::move(message)) {}
31
TransformationReplaceIdWithSynonym(protobufs::IdUseDescriptor id_use_descriptor,uint32_t synonymous_id)32 TransformationReplaceIdWithSynonym::TransformationReplaceIdWithSynonym(
33 protobufs::IdUseDescriptor id_use_descriptor, uint32_t synonymous_id) {
34 *message_.mutable_id_use_descriptor() = std::move(id_use_descriptor);
35 message_.set_synonymous_id(synonymous_id);
36 }
37
IsApplicable(opt::IRContext * ir_context,const TransformationContext & transformation_context) const38 bool TransformationReplaceIdWithSynonym::IsApplicable(
39 opt::IRContext* ir_context,
40 const TransformationContext& transformation_context) const {
41 auto id_of_interest = message_.id_use_descriptor().id_of_interest();
42
43 // Does the fact manager know about the synonym?
44 auto data_descriptor_for_synonymous_id =
45 MakeDataDescriptor(message_.synonymous_id(), {});
46 if (!transformation_context.GetFactManager()->IsSynonymous(
47 MakeDataDescriptor(id_of_interest, {}),
48 data_descriptor_for_synonymous_id)) {
49 return false;
50 }
51
52 // Does the id use descriptor in the transformation identify an instruction?
53 auto use_instruction =
54 FindInstructionContainingUse(message_.id_use_descriptor(), ir_context);
55 if (!use_instruction) {
56 return false;
57 }
58
59 uint32_t type_id_of_interest =
60 ir_context->get_def_use_mgr()->GetDef(id_of_interest)->type_id();
61 uint32_t type_id_synonym = ir_context->get_def_use_mgr()
62 ->GetDef(message_.synonymous_id())
63 ->type_id();
64
65 // If the id of interest and the synonym are scalar or vector integer
66 // constants with different signedness, their use can only be swapped if the
67 // instruction is agnostic to the signedness of the operand.
68 if (!fuzzerutil::TypesAreCompatible(
69 ir_context, use_instruction->opcode(),
70 message_.id_use_descriptor().in_operand_index(), type_id_of_interest,
71 type_id_synonym)) {
72 return false;
73 }
74
75 // Is the use suitable for being replaced in principle?
76 if (!fuzzerutil::IdUseCanBeReplaced(
77 ir_context, transformation_context, use_instruction,
78 message_.id_use_descriptor().in_operand_index())) {
79 return false;
80 }
81
82 // The transformation is applicable if the synonymous id is available at the
83 // use point.
84 return fuzzerutil::IdIsAvailableAtUse(
85 ir_context, use_instruction,
86 message_.id_use_descriptor().in_operand_index(),
87 message_.synonymous_id());
88 }
89
Apply(spvtools::opt::IRContext * ir_context,TransformationContext *) const90 void TransformationReplaceIdWithSynonym::Apply(
91 spvtools::opt::IRContext* ir_context,
92 TransformationContext* /*unused*/) const {
93 auto instruction_to_change =
94 FindInstructionContainingUse(message_.id_use_descriptor(), ir_context);
95 instruction_to_change->SetInOperand(
96 message_.id_use_descriptor().in_operand_index(),
97 {message_.synonymous_id()});
98 ir_context->get_def_use_mgr()->EraseUseRecordsOfOperandIds(
99 instruction_to_change);
100 ir_context->get_def_use_mgr()->AnalyzeInstUse(instruction_to_change);
101
102 // No analyses need to be invalidated, since the transformation is local to a
103 // block, and the def-use analysis has been updated.
104 }
105
ToMessage() const106 protobufs::Transformation TransformationReplaceIdWithSynonym::ToMessage()
107 const {
108 protobufs::Transformation result;
109 *result.mutable_replace_id_with_synonym() = message_;
110 return result;
111 }
112
GetFreshIds() const113 std::unordered_set<uint32_t> TransformationReplaceIdWithSynonym::GetFreshIds()
114 const {
115 return std::unordered_set<uint32_t>();
116 }
117
118 } // namespace fuzz
119 } // namespace spvtools
120