File: graph.h

package info (click to toggle)
spirv-tools 2025.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,588 kB
  • sloc: cpp: 470,407; javascript: 5,893; python: 3,326; ansic: 488; sh: 450; ruby: 88; makefile: 18; lisp: 9
file content (124 lines) | stat: -rw-r--r-- 4,340 bytes parent folder | download | duplicates (4)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2022-2025 Arm Ltd.
//
// 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.

#ifndef SOURCE_OPT_GRAPH_H_
#define SOURCE_OPT_GRAPH_H_

#include "source/opt/instruction.h"

namespace spvtools {
namespace opt {

struct Graph {
  // Creates a graph instance declared by the given OpGraph instruction
  // |def_inst|.
  inline explicit Graph(std::unique_ptr<Instruction> def_inst);
  explicit Graph(const Graph& f) = delete;

  // Creates a clone of the graph in the given |context|
  //
  // The parent module will default to null and needs to be explicitly set by
  // the user.
  Graph* Clone(IRContext*) const;

  // The OpGraph instruction that begins the definition of this graph.
  Instruction& DefInst() { return *def_inst_; }
  const Instruction& DefInst() const { return *def_inst_; }

  // Appends an input to this graph.
  inline void AddInput(std::unique_ptr<Instruction> inst);

  // Appends an instruction to this graph.
  inline void AddInstruction(std::unique_ptr<Instruction> inst);

  // Appends an output to this graph.
  inline void AddOutput(std::unique_ptr<Instruction> inst);

  // Saves the given graph end instruction.
  void SetGraphEnd(std::unique_ptr<Instruction> end_inst);

  // Returns the given graph end instruction.
  inline Instruction* EndInst() { return end_inst_.get(); }
  inline const Instruction* EndInst() const { return end_inst_.get(); }

  // Returns graph's id
  inline uint32_t result_id() const { return def_inst_->result_id(); }

  // Returns graph's return type id
  inline uint32_t type_id() const { return def_inst_->type_id(); }

  // Return a read-only reference to the instructions that define the body of
  // the graph.
  const std::vector<std::unique_ptr<Instruction>>& instructions() const {
    return insts_;
  }

  // Return a read-only reference to the instructions that define the inputs
  // of the graph.
  const std::vector<std::unique_ptr<Instruction>>& inputs() const {
    return inputs_;
  }

  // Return a read-only reference to the instructions that define the outputs
  // of the graph.
  const std::vector<std::unique_ptr<Instruction>>& outputs() const {
    return outputs_;
  }

  // Runs the given function |f| on instructions in this graph, in order,
  // and optionally on debug line instructions that might precede them and
  // non-semantic instructions that succceed the function.
  void ForEachInst(const std::function<void(Instruction*)>& f,
                   bool run_on_debug_line_insts = false,
                   bool run_on_non_semantic_insts = false);
  void ForEachInst(const std::function<void(const Instruction*)>& f,
                   bool run_on_debug_line_insts = false,
                   bool run_on_non_semantic_insts = false) const;

 private:
  // The OpGraph instruction that begins the definition of this graph.
  std::unique_ptr<Instruction> def_inst_;
  // All inputs to this graph.
  std::vector<std::unique_ptr<Instruction>> inputs_;
  // All instructions describing this graph
  std::vector<std::unique_ptr<Instruction>> insts_;
  // All outputs of this graph.
  std::vector<std::unique_ptr<Instruction>> outputs_;
  // The OpGraphEnd instruction.
  std::unique_ptr<Instruction> end_inst_;
};

inline Graph::Graph(std::unique_ptr<Instruction> def_inst)
    : def_inst_(std::move(def_inst)) {}

inline void Graph::AddInput(std::unique_ptr<Instruction> inst) {
  inputs_.emplace_back(std::move(inst));
}

inline void Graph::AddInstruction(std::unique_ptr<Instruction> inst) {
  insts_.emplace_back(std::move(inst));
}

inline void Graph::AddOutput(std::unique_ptr<Instruction> inst) {
  outputs_.emplace_back(std::move(inst));
}

inline void Graph::SetGraphEnd(std::unique_ptr<Instruction> end_inst) {
  end_inst_ = std::move(end_inst);
}

}  // namespace opt
}  // namespace spvtools

#endif  // SOURCE_OPT_GRAPH_H_