2
0

pass_manager.h 5.2 KB

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