1// Copyright (c) 2016 Google Inc.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and/or associated documentation files (the
5// "Materials"), to deal in the Materials without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Materials, and to
8// permit persons to whom the Materials are furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Materials.
13//
14// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17//    https://www.khronos.org/registry/
18//
19// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
26
27#ifndef LIBSPIRV_OPT_PASS_MANAGER_H_
28#define LIBSPIRV_OPT_PASS_MANAGER_H_
29
30#include <cassert>
31#include <memory>
32#include <vector>
33
34#include "module.h"
35#include "passes.h"
36
37namespace spvtools {
38namespace opt {
39
40// The pass manager, responsible for tracking and running passes.
41// Clients should first call AddPass() to add passes and then call Run()
42// to run on a module. Passes are executed in the exact order of added.
43//
44// TODO(antiagainst): The pass manager is fairly simple right now. Eventually it
45// should support pass dependency, common functionality (like def-use analysis)
46// sharing, etc.
47class PassManager {
48 public:
49  // Adds a pass.
50  void AddPass(std::unique_ptr<Pass> pass) {
51    passes_.push_back(std::move(pass));
52  }
53  template <typename PassT>
54  void AddPass() {
55    passes_.emplace_back(new PassT);
56  }
57
58  // Returns the number of passes added.
59  uint32_t NumPasses() const { return static_cast<uint32_t>(passes_.size()); }
60  // Returns a pointer to the |index|th pass added.
61  Pass* GetPass(uint32_t index) const {
62    assert(index < passes_.size() && "index out of bound");
63    return passes_[index].get();
64  }
65
66  // Runs all passes on the given |module|.
67  void Run(ir::Module* module) {
68    for (const auto& pass : passes_) {
69      // TODO(antiagainst): Currently we ignore the return value of the pass,
70      // which indicates whether the module has been modified, since there is
71      // nothing shared between passes right now.
72      pass->Process(module);
73    }
74  }
75
76 private:
77  // A vector of passes. Order matters.
78  std::vector<std::unique_ptr<Pass>> passes_;
79};
80
81}  // namespace opt
82}  // namespace spvtools
83
84#endif  // LIBSPIRV_OPT_PASS_MANAGER_H_
85