Coverage Report

Created: 2026-02-14 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/opt/graph.h
Line
Count
Source
1
// Copyright (c) 2022-2025 Arm Ltd.
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_OPT_GRAPH_H_
16
#define SOURCE_OPT_GRAPH_H_
17
18
#include "source/opt/instruction.h"
19
20
namespace spvtools {
21
namespace opt {
22
23
struct Graph {
24
  // Creates a graph instance declared by the given OpGraph instruction
25
  // |def_inst|.
26
  inline explicit Graph(std::unique_ptr<Instruction> def_inst);
27
  explicit Graph(const Graph& f) = delete;
28
29
  // Creates a clone of the graph in the given |context|
30
  //
31
  // The parent module will default to null and needs to be explicitly set by
32
  // the user.
33
  Graph* Clone(IRContext*) const;
34
35
  // The OpGraph instruction that begins the definition of this graph.
36
0
  Instruction& DefInst() { return *def_inst_; }
37
0
  const Instruction& DefInst() const { return *def_inst_; }
38
39
  // Appends an input to this graph.
40
  inline void AddInput(std::unique_ptr<Instruction> inst);
41
42
  // Appends an instruction to this graph.
43
  inline void AddInstruction(std::unique_ptr<Instruction> inst);
44
45
  // Appends an output to this graph.
46
  inline void AddOutput(std::unique_ptr<Instruction> inst);
47
48
  // Saves the given graph end instruction.
49
  void SetGraphEnd(std::unique_ptr<Instruction> end_inst);
50
51
  // Returns the given graph end instruction.
52
0
  inline Instruction* EndInst() { return end_inst_.get(); }
53
0
  inline const Instruction* EndInst() const { return end_inst_.get(); }
54
55
  // Returns graph's id
56
0
  inline uint32_t result_id() const { return def_inst_->result_id(); }
57
58
  // Returns graph's return type id
59
0
  inline uint32_t type_id() const { return def_inst_->type_id(); }
60
61
  // Return a read-only reference to the instructions that define the body of
62
  // the graph.
63
0
  const std::vector<std::unique_ptr<Instruction>>& instructions() const {
64
0
    return insts_;
65
0
  }
66
67
  // Return a read-only reference to the instructions that define the inputs
68
  // of the graph.
69
0
  const std::vector<std::unique_ptr<Instruction>>& inputs() const {
70
0
    return inputs_;
71
0
  }
72
73
  // Return a read-only reference to the instructions that define the outputs
74
  // of the graph.
75
0
  const std::vector<std::unique_ptr<Instruction>>& outputs() const {
76
0
    return outputs_;
77
0
  }
78
79
  // Runs the given function |f| on instructions in this graph, in order,
80
  // and optionally on debug line instructions that might precede them and
81
  // non-semantic instructions that succceed the function.
82
  void ForEachInst(const std::function<void(Instruction*)>& f,
83
                   bool run_on_debug_line_insts = false,
84
                   bool run_on_non_semantic_insts = false);
85
  void ForEachInst(const std::function<void(const Instruction*)>& f,
86
                   bool run_on_debug_line_insts = false,
87
                   bool run_on_non_semantic_insts = false) const;
88
89
 private:
90
  // The OpGraph instruction that begins the definition of this graph.
91
  std::unique_ptr<Instruction> def_inst_;
92
  // All inputs to this graph.
93
  std::vector<std::unique_ptr<Instruction>> inputs_;
94
  // All instructions describing this graph
95
  std::vector<std::unique_ptr<Instruction>> insts_;
96
  // All outputs of this graph.
97
  std::vector<std::unique_ptr<Instruction>> outputs_;
98
  // The OpGraphEnd instruction.
99
  std::unique_ptr<Instruction> end_inst_;
100
};
101
102
inline Graph::Graph(std::unique_ptr<Instruction> def_inst)
103
11
    : def_inst_(std::move(def_inst)) {}
104
105
0
inline void Graph::AddInput(std::unique_ptr<Instruction> inst) {
106
0
  inputs_.emplace_back(std::move(inst));
107
0
}
108
109
0
inline void Graph::AddInstruction(std::unique_ptr<Instruction> inst) {
110
0
  insts_.emplace_back(std::move(inst));
111
0
}
112
113
1
inline void Graph::AddOutput(std::unique_ptr<Instruction> inst) {
114
1
  outputs_.emplace_back(std::move(inst));
115
1
}
116
117
0
inline void Graph::SetGraphEnd(std::unique_ptr<Instruction> end_inst) {
118
0
  end_inst_ = std::move(end_inst);
119
0
}
120
121
}  // namespace opt
122
}  // namespace spvtools
123
124
#endif  // SOURCE_OPT_GRAPH_H_