Welcome to mirror list, hosted at ThFree Co, Russian Federation.

instruction_message.cpp « fuzz « source - github.com/KhronosGroup/SPIRV-Tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e8ac71b2f6cbc9797f669f479c7960e758363b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "source/fuzz/instruction_message.h"

#include "source/fuzz/fuzzer_util.h"

namespace spvtools {
namespace fuzz {

protobufs::Instruction MakeInstructionMessage(
    spv::Op opcode, uint32_t result_type_id, uint32_t result_id,
    const opt::Instruction::OperandList& input_operands) {
  protobufs::Instruction result;
  result.set_opcode(uint32_t(opcode));
  result.set_result_type_id(result_type_id);
  result.set_result_id(result_id);
  for (auto& operand : input_operands) {
    auto operand_message = result.add_input_operand();
    operand_message->set_operand_type(static_cast<uint32_t>(operand.type));
    for (auto operand_word : operand.words) {
      operand_message->add_operand_data(operand_word);
    }
  }
  return result;
}

protobufs::Instruction MakeInstructionMessage(
    const opt::Instruction* instruction) {
  opt::Instruction::OperandList input_operands;
  for (uint32_t input_operand_index = 0;
       input_operand_index < instruction->NumInOperands();
       input_operand_index++) {
    input_operands.push_back(instruction->GetInOperand(input_operand_index));
  }
  return MakeInstructionMessage(instruction->opcode(), instruction->type_id(),
                                instruction->result_id(), input_operands);
}

std::unique_ptr<opt::Instruction> InstructionFromMessage(
    opt::IRContext* ir_context,
    const protobufs::Instruction& instruction_message) {
  // First, update the module's id bound with respect to the new instruction,
  // if it has a result id.
  if (instruction_message.result_id()) {
    fuzzerutil::UpdateModuleIdBound(ir_context,
                                    instruction_message.result_id());
  }
  // Now create a sequence of input operands from the input operand data in the
  // protobuf message.
  opt::Instruction::OperandList in_operands;
  for (auto& operand_message : instruction_message.input_operand()) {
    opt::Operand::OperandData operand_data;
    for (auto& word : operand_message.operand_data()) {
      operand_data.push_back(word);
    }
    in_operands.push_back(
        {static_cast<spv_operand_type_t>(operand_message.operand_type()),
         operand_data});
  }
  // Create and return the instruction.
  return MakeUnique<opt::Instruction>(
      ir_context, static_cast<spv::Op>(instruction_message.opcode()),
      instruction_message.result_type_id(), instruction_message.result_id(),
      in_operands);
}

}  // namespace fuzz
}  // namespace spvtools