My Project
PassRegistry.h
Go to the documentation of this file.
1 //===- PassRegistry.h - Pass Registration Utilities -------------*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains utilities for registering information about compiler
10 // passes.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_PASS_PASSREGISTRY_H_
15 #define MLIR_PASS_PASSREGISTRY_H_
16 
17 #include "mlir/Pass/PassOptions.h"
18 #include <functional>
19 
20 namespace mlir {
21 class OpPassManager;
22 class Pass;
23 
27  std::function<LogicalResult(OpPassManager &, StringRef options)>;
28 using PassAllocatorFunction = std::function<std::unique_ptr<Pass>()>;
29 
32 using PassID = ClassID;
33 
34 //===----------------------------------------------------------------------===//
35 // PassRegistry
36 //===----------------------------------------------------------------------===//
37 
41 public:
45  LogicalResult addToPipeline(OpPassManager &pm, StringRef options) const {
46  assert(builder &&
47  "cannot call addToPipeline on PassRegistryEntry without builder");
48  return builder(pm, options);
49  }
50 
53  StringRef getPassArgument() const { return arg; }
54 
56  StringRef getPassDescription() const { return description; }
57 
58 protected:
59  PassRegistryEntry(StringRef arg, StringRef description,
60  const PassRegistryFunction &builder)
61  : arg(arg), description(description), builder(builder) {}
62 
63 private:
64  // The argument with which to invoke the pass via mlir-opt.
65  StringRef arg;
66 
67  // Description of the pass.
68  StringRef description;
69 
70  // Function to register this entry to a pass manager pipeline.
71  PassRegistryFunction builder;
72 };
73 
76 public:
77  PassPipelineInfo(StringRef arg, StringRef description,
78  const PassRegistryFunction &builder)
79  : PassRegistryEntry(arg, description, builder) {}
80 };
81 
83 class PassInfo : public PassRegistryEntry {
84 public:
87  PassInfo(StringRef arg, StringRef description, const PassID *passID,
88  const PassAllocatorFunction &allocator);
89 };
90 
91 //===----------------------------------------------------------------------===//
92 // PassRegistration
93 //===----------------------------------------------------------------------===//
94 
97 void registerPassPipeline(StringRef arg, StringRef description,
98  const PassRegistryFunction &function);
99 
102 void registerPass(StringRef arg, StringRef description, const PassID *passID,
103  const PassAllocatorFunction &function);
104 
115 template <typename ConcretePass> struct PassRegistration {
116 
117  PassRegistration(StringRef arg, StringRef description,
118  const PassAllocatorFunction &constructor) {
119  registerPass(arg, description, PassID::getID<ConcretePass>(), constructor);
120  }
121 
122  PassRegistration(StringRef arg, StringRef description)
123  : PassRegistration(arg, description,
124  [] { return std::make_unique<ConcretePass>(); }) {}
125 };
126 
140 template <typename Options = EmptyPipelineOptions>
143  StringRef arg, StringRef description,
144  std::function<void(OpPassManager &, const Options &options)> builder) {
145  registerPassPipeline(arg, description,
146  [builder](OpPassManager &pm, StringRef optionsStr) {
147  Options options;
148  if (failed(options.parseFromString(optionsStr)))
149  return failure();
150  builder(pm, options);
151  return success();
152  });
153  }
154 };
155 
159  PassPipelineRegistration(StringRef arg, StringRef description,
160  std::function<void(OpPassManager &)> builder) {
161  registerPassPipeline(arg, description,
162  [builder](OpPassManager &pm, StringRef optionsStr) {
163  if (!optionsStr.empty())
164  return failure();
165  builder(pm);
166  return success();
167  });
168  }
169 };
170 
175 LogicalResult parsePassPipeline(StringRef pipeline, OpPassManager &pm,
176  raw_ostream &errorStream = llvm::errs());
177 
178 //===----------------------------------------------------------------------===//
179 // PassPipelineCLParser
180 //===----------------------------------------------------------------------===//
181 
182 namespace detail {
183 struct PassPipelineCLParserImpl;
184 } // end namespace detail
185 
193 public:
195  PassPipelineCLParser(StringRef arg, StringRef description);
197 
199  bool hasAnyOccurrences() const;
200 
203  bool contains(const PassRegistryEntry *entry) const;
204 
209 
210 private:
211  std::unique_ptr<detail::PassPipelineCLParserImpl> impl;
212 };
213 
214 } // end namespace mlir
215 
216 #endif // MLIR_PASS_PASSREGISTRY_H_
Definition: InferTypeOpInterface.cpp:20
Definition: STLExtras.h:95
void registerPassPipeline(StringRef arg, StringRef description, const PassRegistryFunction &function)
Definition: PassRegistry.cpp:42
std::function< LogicalResult(OpPassManager &, StringRef options)> PassRegistryFunction
Definition: PassRegistry.h:27
bool failed(LogicalResult result)
Definition: LogicalResult.h:45
StringRef getPassArgument() const
Definition: PassRegistry.h:53
PassPipelineInfo(StringRef arg, StringRef description, const PassRegistryFunction &builder)
Definition: PassRegistry.h:77
LogicalResult success(bool isSuccess=true)
Definition: LogicalResult.h:25
Definition: LogicalResult.h:18
LogicalResult failure(bool isFailure=true)
Definition: LogicalResult.h:32
PassRegistryEntry(StringRef arg, StringRef description, const PassRegistryFunction &builder)
Definition: PassRegistry.h:59
Definition: PassOptions.h:234
PassRegistration(StringRef arg, StringRef description, const PassAllocatorFunction &constructor)
Definition: PassRegistry.h:117
LogicalResult addToPipeline(OpPassManager &pm, StringRef options) const
Definition: PassRegistry.h:45
std::function< std::unique_ptr< Pass >()> PassAllocatorFunction
Definition: PassRegistry.h:28
PassRegistration(StringRef arg, StringRef description)
Definition: PassRegistry.h:122
A structure to represent the information of a registered pass pipeline.
Definition: PassRegistry.h:75
void registerPass(StringRef arg, StringRef description, const PassID *passID, const PassAllocatorFunction &function)
Definition: PassRegistry.cpp:58
Definition: PassRegistry.h:141
A structure to represent the information for a derived pass class.
Definition: PassRegistry.h:83
LogicalResult parsePassPipeline(StringRef pipeline, OpPassManager &pm, raw_ostream &errorStream=llvm::errs())
Definition: PassRegistry.cpp:377
Definition: PassRegistry.h:115
PassPipelineRegistration(StringRef arg, StringRef description, std::function< void(OpPassManager &)> builder)
Definition: PassRegistry.h:159
Definition: PassRegistry.h:40
PassPipelineRegistration(StringRef arg, StringRef description, std::function< void(OpPassManager &, const Options &options)> builder)
Definition: PassRegistry.h:142
StringRef getPassDescription() const
Returns a description for the pass, this never returns null.
Definition: PassRegistry.h:56
Definition: PassManager.h:44
Definition: PassRegistry.h:192