Coverage Report

Created: 2023-03-01 07:33

/src/spirv-tools/source/val/instruction.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2015-2016 The Khronos Group Inc.
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
#ifndef SOURCE_VAL_INSTRUCTION_H_
16
#define SOURCE_VAL_INSTRUCTION_H_
17
18
#include <cassert>
19
#include <cstdint>
20
#include <functional>
21
#include <utility>
22
#include <vector>
23
24
#include "source/ext_inst.h"
25
#include "source/table.h"
26
#include "spirv-tools/libspirv.h"
27
28
namespace spvtools {
29
namespace val {
30
31
class BasicBlock;
32
class Function;
33
34
/// Wraps the spv_parsed_instruction struct along with use and definition of the
35
/// instruction's result id
36
class Instruction {
37
 public:
38
  explicit Instruction(const spv_parsed_instruction_t* inst);
39
40
  /// Registers the use of the Instruction in instruction \p inst at \p index
41
  void RegisterUse(const Instruction* inst, uint32_t index);
42
43
12.2M
  uint32_t id() const { return inst_.result_id; }
44
34.1M
  uint32_t type_id() const { return inst_.type_id; }
45
191M
  spv::Op opcode() const { return static_cast<spv::Op>(inst_.opcode); }
46
47
  /// Returns the Function where the instruction was defined. nullptr if it was
48
  /// defined outside of a Function
49
6.42M
  const Function* function() const { return function_; }
50
1.97M
  void set_function(Function* func) { function_ = func; }
51
52
  /// Returns the BasicBlock where the instruction was defined. nullptr if it
53
  /// was defined outside of a BasicBlock
54
3.00M
  const BasicBlock* block() const { return block_; }
55
1.97M
  void set_block(BasicBlock* b) { block_ = b; }
56
57
  /// Returns a vector of pairs of all references to this instruction's result
58
  /// id. The first element is the instruction in which this result id was
59
  /// referenced and the second is the index of the word in that instruction
60
  /// where this result id appeared
61
998k
  const std::vector<std::pair<const Instruction*, uint32_t>>& uses() const {
62
998k
    return uses_;
63
998k
  }
64
65
  /// The word used to define the Instruction
66
32.3M
  uint32_t word(size_t index) const { return words_[index]; }
67
68
  /// The words used to define the Instruction
69
6.35M
  const std::vector<uint32_t>& words() const { return words_; }
70
71
  /// Returns the operand at |idx|.
72
31.6M
  const spv_parsed_operand_t& operand(size_t idx) const {
73
31.6M
    return operands_[idx];
74
31.6M
  }
75
76
  /// The operands of the Instruction
77
49.4M
  const std::vector<spv_parsed_operand_t>& operands() const {
78
49.4M
    return operands_;
79
49.4M
  }
80
81
  /// Provides direct access to the stored C instruction object.
82
22.0k
  const spv_parsed_instruction_t& c_inst() const { return inst_; }
83
84
  /// Provides direct access to instructions spv_ext_inst_type_t object.
85
148k
  const spv_ext_inst_type_t& ext_inst_type() const {
86
148k
    return inst_.ext_inst_type;
87
148k
  }
88
89
3.31M
  bool IsNonSemantic() const {
90
3.31M
    return opcode() == spv::Op::OpExtInst &&
91
3.31M
           spvExtInstIsNonSemantic(inst_.ext_inst_type);
92
3.31M
  }
93
94
  /// True if this is an OpExtInst for debug info extension.
95
596k
  bool IsDebugInfo() const {
96
596k
    return opcode() == spv::Op::OpExtInst &&
97
596k
           spvExtInstIsDebugInfo(inst_.ext_inst_type);
98
596k
  }
99
100
  // Casts the words belonging to the operand under |index| to |T| and returns.
101
  template <typename T>
102
11.0M
  T GetOperandAs(size_t index) const {
103
11.0M
    const spv_parsed_operand_t& o = operands_.at(index);
104
11.0M
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
11.0M
  }
unsigned int spvtools::val::Instruction::GetOperandAs<unsigned int>(unsigned long) const
Line
Count
Source
102
8.71M
  T GetOperandAs(size_t index) const {
103
8.71M
    const spv_parsed_operand_t& o = operands_.at(index);
104
8.71M
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
8.71M
  }
spv::ExecutionModel spvtools::val::Instruction::GetOperandAs<spv::ExecutionModel>(unsigned long) const
Line
Count
Source
102
49.4k
  T GetOperandAs(size_t index) const {
103
49.4k
    const spv_parsed_operand_t& o = operands_.at(index);
104
49.4k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
49.4k
  }
spv::StorageClass spvtools::val::Instruction::GetOperandAs<spv::StorageClass>(unsigned long) const
Line
Count
Source
102
1.94M
  T GetOperandAs(size_t index) const {
103
1.94M
    const spv_parsed_operand_t& o = operands_.at(index);
104
1.94M
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
1.94M
  }
spv::Decoration spvtools::val::Instruction::GetOperandAs<spv::Decoration>(unsigned long) const
Line
Count
Source
102
93.0k
  T GetOperandAs(size_t index) const {
103
93.0k
    const spv_parsed_operand_t& o = operands_.at(index);
104
93.0k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
93.0k
  }
spv::BuiltIn spvtools::val::Instruction::GetOperandAs<spv::BuiltIn>(unsigned long) const
Line
Count
Source
102
2.90k
  T GetOperandAs(size_t index) const {
103
2.90k
    const spv_parsed_operand_t& o = operands_.at(index);
104
2.90k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
2.90k
  }
unsigned int const spvtools::val::Instruction::GetOperandAs<unsigned int const>(unsigned long) const
Line
Count
Source
102
63.2k
  T GetOperandAs(size_t index) const {
103
63.2k
    const spv_parsed_operand_t& o = operands_.at(index);
104
63.2k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
63.2k
  }
spv::LoopControlShift spvtools::val::Instruction::GetOperandAs<spv::LoopControlShift>(unsigned long) const
Line
Count
Source
102
32.7k
  T GetOperandAs(size_t index) const {
103
32.7k
    const spv_parsed_operand_t& o = operands_.at(index);
104
32.7k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
32.7k
  }
spv::Op spvtools::val::Instruction::GetOperandAs<spv::Op>(unsigned long) const
Line
Count
Source
102
54
  T GetOperandAs(size_t index) const {
103
54
    const spv_parsed_operand_t& o = operands_.at(index);
104
54
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
54
  }
Unexecuted instantiation: NonSemanticClspvReflectionInstructions spvtools::val::Instruction::GetOperandAs<NonSemanticClspvReflectionInstructions>(unsigned long) const
spv::Capability spvtools::val::Instruction::GetOperandAs<spv::Capability>(unsigned long) const
Line
Count
Source
102
32.0k
  T GetOperandAs(size_t index) const {
103
32.0k
    const spv_parsed_operand_t& o = operands_.at(index);
104
32.0k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
32.0k
  }
spv::AddressingModel spvtools::val::Instruction::GetOperandAs<spv::AddressingModel>(unsigned long) const
Line
Count
Source
102
24.2k
  T GetOperandAs(size_t index) const {
103
24.2k
    const spv_parsed_operand_t& o = operands_.at(index);
104
24.2k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
24.2k
  }
spv::MemoryModel spvtools::val::Instruction::GetOperandAs<spv::MemoryModel>(unsigned long) const
Line
Count
Source
102
24.2k
  T GetOperandAs(size_t index) const {
103
24.2k
    const spv_parsed_operand_t& o = operands_.at(index);
104
24.2k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
24.2k
  }
spv::FunctionControlMask spvtools::val::Instruction::GetOperandAs<spv::FunctionControlMask>(unsigned long) const
Line
Count
Source
102
28.3k
  T GetOperandAs(size_t index) const {
103
28.3k
    const spv_parsed_operand_t& o = operands_.at(index);
104
28.3k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
28.3k
  }
spv::ExecutionMode spvtools::val::Instruction::GetOperandAs<spv::ExecutionMode>(unsigned long) const
Line
Count
Source
102
14.3k
  T GetOperandAs(size_t index) const {
103
14.3k
    const spv_parsed_operand_t& o = operands_.at(index);
104
14.3k
    assert(o.num_words * 4 >= sizeof(T));
105
0
    assert(o.offset + o.num_words <= inst_.num_words);
106
0
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
14.3k
  }
Unexecuted instantiation: spv::GroupOperation spvtools::val::Instruction::GetOperandAs<spv::GroupOperation>(unsigned long) const
108
109
22.8k
  size_t LineNum() const { return line_num_; }
110
3.27M
  void SetLineNum(size_t pos) { line_num_ = pos; }
111
112
 private:
113
  const std::vector<uint32_t> words_;
114
  const std::vector<spv_parsed_operand_t> operands_;
115
  spv_parsed_instruction_t inst_;
116
  size_t line_num_ = 0;
117
118
  /// The function in which this instruction was declared
119
  Function* function_ = nullptr;
120
121
  /// The basic block in which this instruction was declared
122
  BasicBlock* block_ = nullptr;
123
124
  /// This is a vector of pairs of all references to this instruction's result
125
  /// id. The first element is the instruction in which this result id was
126
  /// referenced and the second is the index of the word in the referencing
127
  /// instruction where this instruction appeared
128
  std::vector<std::pair<const Instruction*, uint32_t>> uses_;
129
};
130
131
bool operator<(const Instruction& lhs, const Instruction& rhs);
132
bool operator<(const Instruction& lhs, uint32_t rhs);
133
bool operator==(const Instruction& lhs, const Instruction& rhs);
134
bool operator==(const Instruction& lhs, uint32_t rhs);
135
136
template <>
137
std::string Instruction::GetOperandAs<std::string>(size_t index) const;
138
139
}  // namespace val
140
}  // namespace spvtools
141
142
// custom specialization of std::hash for Instruction
143
namespace std {
144
template <>
145
struct hash<spvtools::val::Instruction> {
146
  typedef spvtools::val::Instruction argument_type;
147
  typedef std::size_t result_type;
148
0
  result_type operator()(const argument_type& inst) const {
149
0
    return hash<uint32_t>()(inst.id());
150
0
  }
151
};
152
153
}  // namespace std
154
155
#endif  // SOURCE_VAL_INSTRUCTION_H_