1 | // Copyright (c) 2016 Google 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_OPT_PASS_H_ |
16 | #define SOURCE_OPT_PASS_H_ |
17 | |
18 | #include <algorithm> |
19 | #include <map> |
20 | #include <unordered_map> |
21 | #include <unordered_set> |
22 | #include <utility> |
23 | |
24 | #include "source/opt/basic_block.h" |
25 | #include "source/opt/def_use_manager.h" |
26 | #include "source/opt/ir_context.h" |
27 | #include "source/opt/module.h" |
28 | #include "spirv-tools/libspirv.hpp" |
29 | #include "types.h" |
30 | |
31 | namespace spvtools { |
32 | namespace opt { |
33 | |
34 | // Abstract class of a pass. All passes should implement this abstract class |
35 | // and all analysis and transformation is done via the Process() method. |
36 | class Pass { |
37 | public: |
38 | // The status of processing a module using a pass. |
39 | // |
40 | // The numbers for the cases are assigned to make sure that Failure & anything |
41 | // is Failure, SuccessWithChange & any success is SuccessWithChange. |
42 | enum class Status { |
43 | Failure = 0x00, |
44 | SuccessWithChange = 0x10, |
45 | SuccessWithoutChange = 0x11, |
46 | }; |
47 | |
48 | using ProcessFunction = std::function<bool(Function*)>; |
49 | |
50 | // Destructs the pass. |
51 | virtual ~Pass() = default; |
52 | |
53 | // Returns a descriptive name for this pass. |
54 | // |
55 | // NOTE: When deriving a new pass class, make sure you make the name |
56 | // compatible with the corresponding spirv-opt command-line flag. For example, |
57 | // if you add the flag --my-pass to spirv-opt, make this function return |
58 | // "my-pass" (no leading hyphens). |
59 | virtual const char* name() const = 0; |
60 | |
61 | // Sets the message consumer to the given |consumer|. |consumer| which will be |
62 | // invoked every time there is a message to be communicated to the outside. |
63 | void SetMessageConsumer(MessageConsumer c) { consumer_ = std::move(c); } |
64 | |
65 | // Returns the reference to the message consumer for this pass. |
66 | const MessageConsumer& consumer() const { return consumer_; } |
67 | |
68 | // Returns the def-use manager used for this pass. TODO(dnovillo): This should |
69 | // be handled by the pass manager. |
70 | analysis::DefUseManager* get_def_use_mgr() const { |
71 | return context()->get_def_use_mgr(); |
72 | } |
73 | |
74 | analysis::DecorationManager* get_decoration_mgr() const { |
75 | return context()->get_decoration_mgr(); |
76 | } |
77 | |
78 | FeatureManager* get_feature_mgr() const { |
79 | return context()->get_feature_mgr(); |
80 | } |
81 | |
82 | // Returns a pointer to the current module for this pass. |
83 | Module* get_module() const { return context_->module(); } |
84 | |
85 | // Sets the pointer to the current context for this pass. |
86 | void SetContextForTesting(IRContext* ctx) { context_ = ctx; } |
87 | |
88 | // Returns a pointer to the current context for this pass. |
89 | IRContext* context() const { return context_; } |
90 | |
91 | // Returns a pointer to the CFG for current module. |
92 | CFG* cfg() const { return context()->cfg(); } |
93 | |
94 | // Run the pass on the given |module|. Returns Status::Failure if errors occur |
95 | // when processing. Returns the corresponding Status::Success if processing is |
96 | // successful to indicate whether changes are made to the module. If there |
97 | // were any changes it will also invalidate the analyses in the IRContext |
98 | // that are not preserved. |
99 | // |
100 | // It is an error if |Run| is called twice with the same instance of the pass. |
101 | // If this happens the return value will be |Failure|. |
102 | Status Run(IRContext* ctx); |
103 | |
104 | // Returns the set of analyses that the pass is guaranteed to preserve. |
105 | virtual IRContext::Analysis GetPreservedAnalyses() { |
106 | return IRContext::kAnalysisNone; |
107 | } |
108 | |
109 | // Return type id for |ptrInst|'s pointee |
110 | uint32_t GetPointeeTypeId(const Instruction* ptrInst) const; |
111 | |
112 | // Return base type of |ty_id| type |
113 | Instruction* GetBaseType(uint32_t ty_id); |
114 | |
115 | // Return true if |inst| returns scalar, vector or matrix type with base |
116 | // float and |width| |
117 | bool IsFloat(uint32_t ty_id, uint32_t width); |
118 | |
119 | // Return the id of OpConstantNull of type |type_id|. Create if necessary. |
120 | uint32_t GetNullId(uint32_t type_id); |
121 | |
122 | protected: |
123 | // Constructs a new pass. |
124 | // |
125 | // The constructed instance will have an empty message consumer, which just |
126 | // ignores all messages from the library. Use SetMessageConsumer() to supply |
127 | // one if messages are of concern. |
128 | Pass(); |
129 | |
130 | // Processes the given |module|. Returns Status::Failure if errors occur when |
131 | // processing. Returns the corresponding Status::Success if processing is |
132 | // succesful to indicate whether changes are made to the module. |
133 | virtual Status Process() = 0; |
134 | |
135 | // Return the next available SSA id and increment it. |
136 | // TODO(1841): Handle id overflow. |
137 | uint32_t TakeNextId() { return context_->TakeNextId(); } |
138 | |
139 | // Returns the id whose value is the same as |object_to_copy| except its type |
140 | // is |new_type_id|. Any instructions needed to generate this value will be |
141 | // inserted before |insertion_position|. |
142 | uint32_t GenerateCopy(Instruction* object_to_copy, uint32_t new_type_id, |
143 | Instruction* insertion_position); |
144 | |
145 | private: |
146 | MessageConsumer consumer_; // Message consumer. |
147 | |
148 | // The context that this pass belongs to. |
149 | IRContext* context_; |
150 | |
151 | // An instance of a pass can only be run once because it is too hard to |
152 | // enforce proper resetting of internal state for each instance. This member |
153 | // is used to check that we do not run the same instance twice. |
154 | bool already_run_; |
155 | }; |
156 | |
157 | inline Pass::Status CombineStatus(Pass::Status a, Pass::Status b) { |
158 | return std::min(a, b); |
159 | } |
160 | |
161 | } // namespace opt |
162 | } // namespace spvtools |
163 | |
164 | #endif // SOURCE_OPT_PASS_H_ |
165 | |