Coverage Report

Created: 2025-08-28 07:18

/src/shaderc/third_party/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/opcode.h"
26
#include "source/table.h"
27
#include "source/table2.h"
28
#include "spirv-tools/libspirv.h"
29
30
namespace spvtools {
31
namespace val {
32
33
class BasicBlock;
34
class Function;
35
36
/// Wraps the spv_parsed_instruction struct along with use and definition of the
37
/// instruction's result id
38
class Instruction {
39
 public:
40
  explicit Instruction(const spv_parsed_instruction_t* inst);
41
42
  /// Registers the use of the Instruction in instruction \p inst at \p index
43
  void RegisterUse(const Instruction* inst, uint32_t index);
44
45
1.08M
  uint32_t id() const { return inst_.result_id; }
46
3.46M
  uint32_t type_id() const { return inst_.type_id; }
47
19.9M
  spv::Op opcode() const { return static_cast<spv::Op>(inst_.opcode); }
48
49
  /// Returns the Function where the instruction was defined. nullptr if it was
50
  /// defined outside of a Function
51
547k
  const Function* function() const { return function_; }
52
137k
  void set_function(Function* func) { function_ = func; }
53
54
  /// Returns the BasicBlock where the instruction was defined. nullptr if it
55
  /// was defined outside of a BasicBlock
56
346k
  const BasicBlock* block() const { return block_; }
57
137k
  void set_block(BasicBlock* b) { block_ = b; }
58
59
  /// Returns a vector of pairs of all references to this instruction's result
60
  /// id. The first element is the instruction in which this result id was
61
  /// referenced and the second is the index of the word in that instruction
62
  /// where this result id appeared
63
120k
  const std::vector<std::pair<const Instruction*, uint32_t>>& uses() const {
64
120k
    return uses_;
65
120k
  }
66
67
  /// The word used to define the Instruction
68
2.66M
  uint32_t word(size_t index) const { return words_[index]; }
69
70
  /// The words used to define the Instruction
71
770k
  const std::vector<uint32_t>& words() const { return words_; }
72
73
  /// Returns the operand at |idx|.
74
2.62M
  const spv_parsed_operand_t& operand(size_t idx) const {
75
2.62M
    return operands_[idx];
76
2.62M
  }
77
78
  /// The operands of the Instruction
79
4.02M
  const std::vector<spv_parsed_operand_t>& operands() const {
80
4.02M
    return operands_;
81
4.02M
  }
82
83
  /// Provides direct access to the stored C instruction object.
84
1.66k
  const spv_parsed_instruction_t& c_inst() const { return inst_; }
85
86
  /// Provides direct access to instructions spv_ext_inst_type_t object.
87
9.43k
  spv_ext_inst_type_t ext_inst_type() const { return inst_.ext_inst_type; }
88
89
201k
  bool IsNonSemantic() const {
90
201k
    return spvIsExtendedInstruction(opcode()) &&
91
201k
           spvExtInstIsNonSemantic(inst_.ext_inst_type);
92
201k
  }
93
94
  /// True if this is an OpExtInst for debug info extension.
95
27.2k
  bool IsDebugInfo() const {
96
27.2k
    return spvIsExtendedInstruction(opcode()) &&
97
27.2k
           spvExtInstIsDebugInfo(inst_.ext_inst_type);
98
27.2k
  }
99
100
  // Casts the words belonging to the operand under |index| to |T| and returns.
101
  template <typename T>
102
1.42M
  T GetOperandAs(size_t index) const {
103
1.42M
    const spv_parsed_operand_t& o = operands_.at(index);
104
1.42M
    assert(o.num_words * 4 >= sizeof(T));
105
1.42M
    assert(o.offset + o.num_words <= inst_.num_words);
106
1.42M
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
1.42M
  }
unsigned int spvtools::val::Instruction::GetOperandAs<unsigned int>(unsigned long) const
Line
Count
Source
102
1.09M
  T GetOperandAs(size_t index) const {
103
1.09M
    const spv_parsed_operand_t& o = operands_.at(index);
104
1.09M
    assert(o.num_words * 4 >= sizeof(T));
105
1.09M
    assert(o.offset + o.num_words <= inst_.num_words);
106
1.09M
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
1.09M
  }
spv::ExecutionModel spvtools::val::Instruction::GetOperandAs<spv::ExecutionModel>(unsigned long) const
Line
Count
Source
102
6.23k
  T GetOperandAs(size_t index) const {
103
6.23k
    const spv_parsed_operand_t& o = operands_.at(index);
104
6.23k
    assert(o.num_words * 4 >= sizeof(T));
105
6.23k
    assert(o.offset + o.num_words <= inst_.num_words);
106
6.23k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
6.23k
  }
spv::StorageClass spvtools::val::Instruction::GetOperandAs<spv::StorageClass>(unsigned long) const
Line
Count
Source
102
271k
  T GetOperandAs(size_t index) const {
103
271k
    const spv_parsed_operand_t& o = operands_.at(index);
104
271k
    assert(o.num_words * 4 >= sizeof(T));
105
271k
    assert(o.offset + o.num_words <= inst_.num_words);
106
271k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
271k
  }
spv::Decoration spvtools::val::Instruction::GetOperandAs<spv::Decoration>(unsigned long) const
Line
Count
Source
102
20.9k
  T GetOperandAs(size_t index) const {
103
20.9k
    const spv_parsed_operand_t& o = operands_.at(index);
104
20.9k
    assert(o.num_words * 4 >= sizeof(T));
105
20.9k
    assert(o.offset + o.num_words <= inst_.num_words);
106
20.9k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
20.9k
  }
Unexecuted instantiation: spv::FPFastMathModeMask spvtools::val::Instruction::GetOperandAs<spv::FPFastMathModeMask>(unsigned long) const
spv::BuiltIn spvtools::val::Instruction::GetOperandAs<spv::BuiltIn>(unsigned long) const
Line
Count
Source
102
114
  T GetOperandAs(size_t index) const {
103
114
    const spv_parsed_operand_t& o = operands_.at(index);
104
114
    assert(o.num_words * 4 >= sizeof(T));
105
114
    assert(o.offset + o.num_words <= inst_.num_words);
106
114
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
114
  }
unsigned int const spvtools::val::Instruction::GetOperandAs<unsigned int const>(unsigned long) const
Line
Count
Source
102
7.85k
  T GetOperandAs(size_t index) const {
103
7.85k
    const spv_parsed_operand_t& o = operands_.at(index);
104
7.85k
    assert(o.num_words * 4 >= sizeof(T));
105
7.85k
    assert(o.offset + o.num_words <= inst_.num_words);
106
7.85k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
7.85k
  }
spv::LoopControlShift spvtools::val::Instruction::GetOperandAs<spv::LoopControlShift>(unsigned long) const
Line
Count
Source
102
946
  T GetOperandAs(size_t index) const {
103
946
    const spv_parsed_operand_t& o = operands_.at(index);
104
946
    assert(o.num_words * 4 >= sizeof(T));
105
946
    assert(o.offset + o.num_words <= inst_.num_words);
106
946
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
946
  }
Unexecuted instantiation: spv::Op spvtools::val::Instruction::GetOperandAs<spv::Op>(unsigned long) const
Unexecuted instantiation: NonSemanticClspvReflectionInstructions spvtools::val::Instruction::GetOperandAs<NonSemanticClspvReflectionInstructions>(unsigned long) const
int spvtools::val::Instruction::GetOperandAs<int>(unsigned long) const
Line
Count
Source
102
6.29k
  T GetOperandAs(size_t index) const {
103
6.29k
    const spv_parsed_operand_t& o = operands_.at(index);
104
6.29k
    assert(o.num_words * 4 >= sizeof(T));
105
6.29k
    assert(o.offset + o.num_words <= inst_.num_words);
106
6.29k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
6.29k
  }
Unexecuted instantiation: spv::FPEncoding spvtools::val::Instruction::GetOperandAs<spv::FPEncoding>(unsigned long) const
spv::Capability spvtools::val::Instruction::GetOperandAs<spv::Capability>(unsigned long) const
Line
Count
Source
102
4.53k
  T GetOperandAs(size_t index) const {
103
4.53k
    const spv_parsed_operand_t& o = operands_.at(index);
104
4.53k
    assert(o.num_words * 4 >= sizeof(T));
105
4.53k
    assert(o.offset + o.num_words <= inst_.num_words);
106
4.53k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
4.53k
  }
spv::AddressingModel spvtools::val::Instruction::GetOperandAs<spv::AddressingModel>(unsigned long) const
Line
Count
Source
102
1.47k
  T GetOperandAs(size_t index) const {
103
1.47k
    const spv_parsed_operand_t& o = operands_.at(index);
104
1.47k
    assert(o.num_words * 4 >= sizeof(T));
105
1.47k
    assert(o.offset + o.num_words <= inst_.num_words);
106
1.47k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
1.47k
  }
spv::MemoryModel spvtools::val::Instruction::GetOperandAs<spv::MemoryModel>(unsigned long) const
Line
Count
Source
102
1.47k
  T GetOperandAs(size_t index) const {
103
1.47k
    const spv_parsed_operand_t& o = operands_.at(index);
104
1.47k
    assert(o.num_words * 4 >= sizeof(T));
105
1.47k
    assert(o.offset + o.num_words <= inst_.num_words);
106
1.47k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
1.47k
  }
spv::ExecutionMode spvtools::val::Instruction::GetOperandAs<spv::ExecutionMode>(unsigned long) const
Line
Count
Source
102
810
  T GetOperandAs(size_t index) const {
103
810
    const spv_parsed_operand_t& o = operands_.at(index);
104
810
    assert(o.num_words * 4 >= sizeof(T));
105
810
    assert(o.offset + o.num_words <= inst_.num_words);
106
810
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
810
  }
spv::FunctionControlMask spvtools::val::Instruction::GetOperandAs<spv::FunctionControlMask>(unsigned long) const
Line
Count
Source
102
2.21k
  T GetOperandAs(size_t index) const {
103
2.21k
    const spv_parsed_operand_t& o = operands_.at(index);
104
2.21k
    assert(o.num_words * 4 >= sizeof(T));
105
2.21k
    assert(o.offset + o.num_words <= inst_.num_words);
106
2.21k
    return *reinterpret_cast<const T*>(&words_[o.offset]);
107
2.21k
  }
Unexecuted instantiation: spv::TensorAddressingOperandsMask spvtools::val::Instruction::GetOperandAs<spv::TensorAddressingOperandsMask>(unsigned long) const
Unexecuted instantiation: spv::GroupOperation spvtools::val::Instruction::GetOperandAs<spv::GroupOperation>(unsigned long) const
108
109
1.42k
  size_t LineNum() const { return line_num_; }
110
235k
  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
  const 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_