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_MANAGER_H_
16 #define SOURCE_OPT_PASS_MANAGER_H_
17
18 #include <memory>
19 #include <ostream>
20 #include <utility>
21 #include <vector>
22
23 #include "source/opt/log.h"
24 #include "source/opt/module.h"
25 #include "source/opt/pass.h"
26
27 #include "source/opt/ir_context.h"
28 #include "spirv-tools/libspirv.hpp"
29
30 namespace spvtools {
31 namespace opt {
32
33 // The pass manager, responsible for tracking and running passes.
34 // Clients should first call AddPass() to add passes and then call Run()
35 // to run on a module. Passes are executed in the exact order of addition.
36 class PassManager {
37 public:
38 // Constructs a pass manager.
39 //
40 // The constructed instance will have an empty message consumer, which just
41 // ignores all messages from the library. Use SetMessageConsumer() to supply
42 // one if messages are of concern.
PassManager()43 PassManager()
44 : consumer_(nullptr),
45 print_all_stream_(nullptr),
46 time_report_stream_(nullptr),
47 target_env_(SPV_ENV_UNIVERSAL_1_2),
48 val_options_(nullptr),
49 validate_after_all_(false) {}
50
51 // Sets the message consumer to the given |consumer|.
SetMessageConsumer(MessageConsumer c)52 void SetMessageConsumer(MessageConsumer c) { consumer_ = std::move(c); }
53
54 // Adds an externally constructed pass.
55 void AddPass(std::unique_ptr<Pass> pass);
56 // Uses the argument |args| to construct a pass instance of type |T|, and adds
57 // the pass instance to this pass manger. The pass added will use this pass
58 // manager's message consumer.
59 template <typename T, typename... Args>
60 void AddPass(Args&&... args);
61
62 // Returns the number of passes added.
63 uint32_t NumPasses() const;
64 // Returns a pointer to the |index|th pass added.
65 inline Pass* GetPass(uint32_t index) const;
66
67 // Returns the message consumer.
68 inline const MessageConsumer& consumer() const;
69
70 // Runs all passes on the given |module|. Returns Status::Failure if errors
71 // occur when processing using one of the registered passes. All passes
72 // registered after the error-reporting pass will be skipped. Returns the
73 // corresponding Status::Success if processing is succesful to indicate
74 // whether changes are made to the module.
75 //
76 // After running all the passes, they are removed from the list.
77 Pass::Status Run(IRContext* context);
78
79 // Sets the option to print the disassembly before each pass and after the
80 // last pass. Output is written to |out| if that is not null. No output
81 // is generated if |out| is null.
SetPrintAll(std::ostream * out)82 PassManager& SetPrintAll(std::ostream* out) {
83 print_all_stream_ = out;
84 return *this;
85 }
86
87 // Sets the option to print the resource utilization of each pass. Output is
88 // written to |out| if that is not null. No output is generated if |out| is
89 // null.
SetTimeReport(std::ostream * out)90 PassManager& SetTimeReport(std::ostream* out) {
91 time_report_stream_ = out;
92 return *this;
93 }
94
95 // Sets the target environment for validation.
SetTargetEnv(spv_target_env env)96 PassManager& SetTargetEnv(spv_target_env env) {
97 target_env_ = env;
98 return *this;
99 }
100
101 // Sets the validation options.
SetValidatorOptions(spv_validator_options options)102 PassManager& SetValidatorOptions(spv_validator_options options) {
103 val_options_ = options;
104 return *this;
105 }
106
107 // Sets the option to validate after each pass.
SetValidateAfterAll(bool validate)108 PassManager& SetValidateAfterAll(bool validate) {
109 validate_after_all_ = validate;
110 return *this;
111 }
112
113 private:
114 // Consumer for messages.
115 MessageConsumer consumer_;
116 // A vector of passes. Order matters.
117 std::vector<std::unique_ptr<Pass>> passes_;
118 // The output stream to write disassembly to before each pass, and after
119 // the last pass. If this is null, no output is generated.
120 std::ostream* print_all_stream_;
121 // The output stream to write the resource utilization of each pass. If this
122 // is null, no output is generated.
123 std::ostream* time_report_stream_;
124 // The target environment.
125 spv_target_env target_env_;
126 // The validator options (used when validating each pass).
127 spv_validator_options val_options_;
128 // Controls whether validation occurs after every pass.
129 bool validate_after_all_;
130 };
131
AddPass(std::unique_ptr<Pass> pass)132 inline void PassManager::AddPass(std::unique_ptr<Pass> pass) {
133 passes_.push_back(std::move(pass));
134 }
135
136 template <typename T, typename... Args>
AddPass(Args &&...args)137 inline void PassManager::AddPass(Args&&... args) {
138 passes_.emplace_back(new T(std::forward<Args>(args)...));
139 passes_.back()->SetMessageConsumer(consumer_);
140 }
141
NumPasses()142 inline uint32_t PassManager::NumPasses() const {
143 return static_cast<uint32_t>(passes_.size());
144 }
145
GetPass(uint32_t index)146 inline Pass* PassManager::GetPass(uint32_t index) const {
147 SPIRV_ASSERT(consumer_, index < passes_.size(), "index out of bound");
148 return passes_[index].get();
149 }
150
consumer()151 inline const MessageConsumer& PassManager::consumer() const {
152 return consumer_;
153 }
154
155 } // namespace opt
156 } // namespace spvtools
157
158 #endif // SOURCE_OPT_PASS_MANAGER_H_
159