/src/shaderc/third_party/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 | | // Add a non-semantic instruction that succeeds this graph in the module. |
49 | | // These instructions are maintained in the order they are added. |
50 | | inline void AddNonSemanticInstruction(std::unique_ptr<Instruction> inst); |
51 | | |
52 | | // Saves the given graph end instruction. |
53 | | void SetGraphEnd(std::unique_ptr<Instruction> end_inst); |
54 | | |
55 | | // Returns the given graph end instruction. |
56 | 0 | inline Instruction* EndInst() { return end_inst_.get(); } |
57 | 0 | inline const Instruction* EndInst() const { return end_inst_.get(); } |
58 | | |
59 | | // Returns graph's id |
60 | 0 | inline uint32_t result_id() const { return def_inst_->result_id(); } |
61 | | |
62 | | // Returns graph's return type id |
63 | 0 | inline uint32_t type_id() const { return def_inst_->type_id(); } |
64 | | |
65 | | // Return a read-only reference to the instructions that define the body of |
66 | | // the graph. |
67 | 0 | const std::vector<std::unique_ptr<Instruction>>& instructions() const { |
68 | 0 | return insts_; |
69 | 0 | } |
70 | | |
71 | | // Return a read-only reference to the instructions that define the inputs |
72 | | // of the graph. |
73 | 0 | const std::vector<std::unique_ptr<Instruction>>& inputs() const { |
74 | 0 | return inputs_; |
75 | 0 | } |
76 | | |
77 | | // Return a read-only reference to the instructions that define the outputs |
78 | | // of the graph. |
79 | 0 | const std::vector<std::unique_ptr<Instruction>>& outputs() const { |
80 | 0 | return outputs_; |
81 | 0 | } |
82 | | |
83 | | // Runs the given function |f| on instructions in this graph, in order, |
84 | | // and optionally on debug line instructions that might precede them and |
85 | | // non-semantic instructions that succeed the graph. |
86 | | void ForEachInst(const std::function<void(Instruction*)>& f, |
87 | | bool run_on_debug_line_insts = false, |
88 | | bool run_on_non_semantic_insts = false); |
89 | | void ForEachInst(const std::function<void(const Instruction*)>& f, |
90 | | bool run_on_debug_line_insts = false, |
91 | | bool run_on_non_semantic_insts = false) const; |
92 | | |
93 | | private: |
94 | | // The OpGraph instruction that begins the definition of this graph. |
95 | | std::unique_ptr<Instruction> def_inst_; |
96 | | // All inputs to this graph. |
97 | | std::vector<std::unique_ptr<Instruction>> inputs_; |
98 | | // All instructions describing this graph |
99 | | std::vector<std::unique_ptr<Instruction>> insts_; |
100 | | // All outputs of this graph. |
101 | | std::vector<std::unique_ptr<Instruction>> outputs_; |
102 | | // The OpGraphEnd instruction. |
103 | | std::unique_ptr<Instruction> end_inst_; |
104 | | // Non-semantic instructions that succeed this graph. |
105 | | std::vector<std::unique_ptr<Instruction>> non_semantic_; |
106 | | }; |
107 | | |
108 | | inline Graph::Graph(std::unique_ptr<Instruction> def_inst) |
109 | 0 | : def_inst_(std::move(def_inst)) {} |
110 | | |
111 | 0 | inline void Graph::AddInput(std::unique_ptr<Instruction> inst) { |
112 | 0 | inputs_.emplace_back(std::move(inst)); |
113 | 0 | } |
114 | | |
115 | 0 | inline void Graph::AddInstruction(std::unique_ptr<Instruction> inst) { |
116 | 0 | insts_.emplace_back(std::move(inst)); |
117 | 0 | } |
118 | | |
119 | 0 | inline void Graph::AddOutput(std::unique_ptr<Instruction> inst) { |
120 | 0 | outputs_.emplace_back(std::move(inst)); |
121 | 0 | } |
122 | | |
123 | | inline void Graph::AddNonSemanticInstruction( |
124 | 0 | std::unique_ptr<Instruction> inst) { |
125 | 0 | non_semantic_.emplace_back(std::move(inst)); |
126 | 0 | } |
127 | | |
128 | 0 | inline void Graph::SetGraphEnd(std::unique_ptr<Instruction> end_inst) { |
129 | 0 | end_inst_ = std::move(end_inst); |
130 | 0 | } |
131 | | |
132 | | } // namespace opt |
133 | | } // namespace spvtools |
134 | | |
135 | | #endif // SOURCE_OPT_GRAPH_H_ |