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/id_use_descriptor.h"
16
17 #include "source/fuzz/instruction_descriptor.h"
18
19 namespace spvtools {
20 namespace fuzz {
21
FindInstructionContainingUse(const protobufs::IdUseDescriptor & id_use_descriptor,opt::IRContext * context)22 opt::Instruction* FindInstructionContainingUse(
23 const protobufs::IdUseDescriptor& id_use_descriptor,
24 opt::IRContext* context) {
25 auto result =
26 FindInstruction(id_use_descriptor.enclosing_instruction(), context);
27 if (!result) {
28 return nullptr;
29 }
30 if (id_use_descriptor.in_operand_index() >= result->NumInOperands()) {
31 return nullptr;
32 }
33 if (result->GetSingleWordInOperand(id_use_descriptor.in_operand_index()) !=
34 id_use_descriptor.id_of_interest()) {
35 return nullptr;
36 }
37 return result;
38 }
39
MakeIdUseDescriptor(uint32_t id_of_interest,const protobufs::InstructionDescriptor & enclosing_instruction,uint32_t in_operand_index)40 protobufs::IdUseDescriptor MakeIdUseDescriptor(
41 uint32_t id_of_interest,
42 const protobufs::InstructionDescriptor& enclosing_instruction,
43 uint32_t in_operand_index) {
44 protobufs::IdUseDescriptor result;
45 result.set_id_of_interest(id_of_interest);
46 *result.mutable_enclosing_instruction() = enclosing_instruction;
47 result.set_in_operand_index(in_operand_index);
48 return result;
49 }
50
MakeIdUseDescriptorFromUse(opt::IRContext * context,opt::Instruction * inst,uint32_t in_operand_index)51 protobufs::IdUseDescriptor MakeIdUseDescriptorFromUse(
52 opt::IRContext* context, opt::Instruction* inst,
53 uint32_t in_operand_index) {
54 const auto& in_operand = inst->GetInOperand(in_operand_index);
55 assert(spvIsInIdType(in_operand.type));
56 return MakeIdUseDescriptor(in_operand.words[0],
57 MakeInstructionDescriptor(context, inst),
58 in_operand_index);
59 }
60
61 } // namespace fuzz
62 } // namespace spvtools
63