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