2020-04-01 16:48:34 +08:00
|
|
|
//===- Pass.cpp - MLIR pass registration generator ------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// PassGen uses the description of passes to generate base classes for passes
|
|
|
|
// and command line registration.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "mlir/TableGen/GenInfo.h"
|
|
|
|
#include "mlir/TableGen/Pass.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2020-08-01 04:18:13 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-04-01 16:48:34 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/TableGen/Error.h"
|
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::tblgen;
|
|
|
|
|
2020-08-01 04:18:13 +08:00
|
|
|
static llvm::cl::OptionCategory passGenCat("Options for -gen-pass-decls");
|
|
|
|
static llvm::cl::opt<std::string>
|
|
|
|
groupName("name", llvm::cl::desc("The name of this group of passes"),
|
|
|
|
llvm::cl::cat(passGenCat));
|
|
|
|
|
2020-04-01 16:50:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GEN: Pass base class generation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// The code snippet used to generate the start of a pass base class.
|
|
|
|
///
|
|
|
|
/// {0}: The def name of the pass record.
|
2020-04-08 04:58:12 +08:00
|
|
|
/// {1}: The base class for the pass.
|
|
|
|
/// {2): The command line argument for the pass.
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
/// {3}: The dependent dialects registration.
|
2020-04-01 16:50:29 +08:00
|
|
|
const char *const passDeclBegin = R"(
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// {0}
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-04-08 04:58:12 +08:00
|
|
|
|
|
|
|
template <typename DerivedT>
|
|
|
|
class {0}Base : public {1} {
|
|
|
|
public:
|
2020-04-11 14:46:52 +08:00
|
|
|
{0}Base() : {1}(::mlir::TypeID::get<DerivedT>()) {{}
|
|
|
|
{0}Base(const {0}Base &) : {1}(::mlir::TypeID::get<DerivedT>()) {{}
|
2020-04-08 04:58:12 +08:00
|
|
|
|
2020-04-01 16:50:29 +08:00
|
|
|
/// Returns the command-line argument attached to this pass.
|
2020-06-26 19:20:44 +08:00
|
|
|
::llvm::StringRef getArgument() const override { return "{2}"; }
|
2020-04-01 16:50:29 +08:00
|
|
|
|
2020-04-08 04:58:12 +08:00
|
|
|
/// Returns the derived pass name.
|
2020-06-26 19:20:44 +08:00
|
|
|
::llvm::StringRef getName() const override { return "{0}"; }
|
2020-04-08 04:58:12 +08:00
|
|
|
|
|
|
|
/// Support isa/dyn_cast functionality for the derived pass class.
|
|
|
|
static bool classof(const ::mlir::Pass *pass) {{
|
2020-04-11 14:46:52 +08:00
|
|
|
return pass->getTypeID() == ::mlir::TypeID::get<DerivedT>();
|
2020-04-08 04:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A clone method to create a copy of this pass.
|
[mlir] Avoid pontentially ambiguous class name
Summary: The Pass class exists in both the mlir and the llvm namespaces. Use the fully qualified class name to avoid any ambiguities.
Reviewers: rriddle
Reviewed By: rriddle
Subscribers: mehdi_amini, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D82371
2020-06-24 03:24:04 +08:00
|
|
|
std::unique_ptr<::mlir::Pass> clonePass() const override {{
|
2020-04-08 04:58:12 +08:00
|
|
|
return std::make_unique<DerivedT>(*static_cast<const DerivedT *>(this));
|
|
|
|
}
|
|
|
|
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
/// Return the dialect that must be loaded in the context before this pass.
|
|
|
|
void getDependentDialects(::mlir::DialectRegistry ®istry) const override {
|
|
|
|
{3}
|
|
|
|
}
|
|
|
|
|
2020-04-08 04:58:12 +08:00
|
|
|
protected:
|
2020-04-01 16:50:29 +08:00
|
|
|
)";
|
|
|
|
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
/// Registration for a single dependent dialect, to be inserted for each
|
|
|
|
/// dependent dialect in the `getDependentDialects` above.
|
|
|
|
const char *const dialectRegistrationTemplate = R"(
|
|
|
|
registry.insert<{0}>();
|
|
|
|
)";
|
|
|
|
|
2020-04-01 16:50:29 +08:00
|
|
|
/// Emit the declarations for each of the pass options.
|
|
|
|
static void emitPassOptionDecls(const Pass &pass, raw_ostream &os) {
|
|
|
|
for (const PassOption &opt : pass.getOptions()) {
|
2020-06-26 19:20:44 +08:00
|
|
|
os.indent(2) << "::mlir::Pass::"
|
|
|
|
<< (opt.isListOption() ? "ListOption" : "Option");
|
2020-04-01 16:50:29 +08:00
|
|
|
|
2020-06-26 19:20:44 +08:00
|
|
|
os << llvm::formatv("<{0}> {1}{{*this, \"{2}\", ::llvm::cl::desc(\"{3}\")",
|
2020-04-01 16:50:29 +08:00
|
|
|
opt.getType(), opt.getCppVariableName(),
|
|
|
|
opt.getArgument(), opt.getDescription());
|
|
|
|
if (Optional<StringRef> defaultVal = opt.getDefaultValue())
|
2020-06-26 19:20:44 +08:00
|
|
|
os << ", ::llvm::cl::init(" << defaultVal << ")";
|
2020-04-01 16:50:29 +08:00
|
|
|
if (Optional<StringRef> additionalFlags = opt.getAdditionalFlags())
|
|
|
|
os << ", " << *additionalFlags;
|
|
|
|
os << "};\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the declarations for each of the pass statistics.
|
|
|
|
static void emitPassStatisticDecls(const Pass &pass, raw_ostream &os) {
|
|
|
|
for (const PassStatistic &stat : pass.getStatistics()) {
|
2020-06-26 19:20:44 +08:00
|
|
|
os << llvm::formatv(
|
|
|
|
" ::mlir::Pass::Statistic {0}{{this, \"{1}\", \"{2}\"};\n",
|
|
|
|
stat.getCppVariableName(), stat.getName(), stat.getDescription());
|
2020-04-01 16:50:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emitPassDecl(const Pass &pass, raw_ostream &os) {
|
|
|
|
StringRef defName = pass.getDef()->getName();
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
std::string dependentDialectRegistrations;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream dialectsOs(dependentDialectRegistrations);
|
|
|
|
for (StringRef dependentDialect : pass.getDependentDialects())
|
|
|
|
dialectsOs << llvm::formatv(dialectRegistrationTemplate,
|
|
|
|
dependentDialect);
|
|
|
|
}
|
2020-04-08 04:58:12 +08:00
|
|
|
os << llvm::formatv(passDeclBegin, defName, pass.getBaseClass(),
|
Separate the Registration from Loading dialects in the Context
This changes the behavior of constructing MLIRContext to no longer load globally
registered dialects on construction. Instead Dialects are only loaded explicitly
on demand:
- the Parser is lazily loading Dialects in the context as it encounters them
during parsing. This is the only purpose for registering dialects and not load
them in the context.
- Passes are expected to declare the dialects they will create entity from
(Operations, Attributes, or Types), and the PassManager is loading Dialects into
the Context when starting a pipeline.
This changes simplifies the configuration of the registration: a compiler only
need to load the dialect for the IR it will emit, and the optimizer is
self-contained and load the required Dialects. For example in the Toy tutorial,
the compiler only needs to load the Toy dialect in the Context, all the others
(linalg, affine, std, LLVM, ...) are automatically loaded depending on the
optimization pipeline enabled.
To adjust to this change, stop using the existing dialect registration: the
global registry will be removed soon.
1) For passes, you need to override the method:
virtual void getDependentDialects(DialectRegistry ®istry) const {}
and registery on the provided registry any dialect that this pass can produce.
Passes defined in TableGen can provide this list in the dependentDialects list
field.
2) For dialects, on construction you can register dependent dialects using the
provided MLIRContext: `context.getOrLoadDialect<DialectName>()`
This is useful if a dialect may canonicalize or have interfaces involving
another dialect.
3) For loading IR, dialect that can be in the input file must be explicitly
registered with the context. `MlirOptMain()` is taking an explicit registry for
this purpose. See how the standalone-opt.cpp example is setup:
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
Only operations from these two dialects can be in the input file. To include all
of the dialects in MLIR Core, you can populate the registry this way:
mlir::registerAllDialects(registry);
4) For `mlir-translate` callback, as well as frontend, Dialects can be loaded in
the context before emitting the IR: context.getOrLoadDialect<ToyDialect>()
Differential Revision: https://reviews.llvm.org/D85622
2020-08-19 04:01:19 +08:00
|
|
|
pass.getArgument(), dependentDialectRegistrations);
|
2020-04-01 16:50:29 +08:00
|
|
|
emitPassOptionDecls(pass, os);
|
|
|
|
emitPassStatisticDecls(pass, os);
|
2020-04-08 04:58:12 +08:00
|
|
|
os << "};\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the code for registering each of the given passes with the global
|
|
|
|
/// PassRegistry.
|
|
|
|
static void emitPassDecls(ArrayRef<Pass> passes, raw_ostream &os) {
|
|
|
|
os << "#ifdef GEN_PASS_CLASSES\n";
|
|
|
|
for (const Pass &pass : passes)
|
|
|
|
emitPassDecl(pass, os);
|
|
|
|
os << "#undef GEN_PASS_CLASSES\n";
|
|
|
|
os << "#endif // GEN_PASS_CLASSES\n";
|
2020-04-01 16:50:29 +08:00
|
|
|
}
|
|
|
|
|
2020-04-01 16:48:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GEN: Pass registration generation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-08-01 04:18:13 +08:00
|
|
|
/// The code snippet used to generate the start of a pass base class.
|
|
|
|
///
|
|
|
|
/// {0}: The def name of the pass record.
|
|
|
|
/// {1}: The argument of the pass.
|
|
|
|
/// {2): The summary of the pass.
|
|
|
|
/// {3}: The code for constructing the pass.
|
|
|
|
const char *const passRegistrationCode = R"(
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// {0} Registration
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
inline void register{0}Pass() {{
|
|
|
|
::mlir::registerPass("{1}", "{2}", []() -> std::unique_ptr<::mlir::Pass> {{
|
|
|
|
return {3};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
/// {0}: The name of the pass group.
|
|
|
|
const char *const passGroupRegistrationCode = R"(
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// {0} Registration
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
inline void register{0}Passes() {{
|
|
|
|
)";
|
|
|
|
|
2020-04-01 16:48:34 +08:00
|
|
|
/// Emit the code for registering each of the given passes with the global
|
|
|
|
/// PassRegistry.
|
|
|
|
static void emitRegistration(ArrayRef<Pass> passes, raw_ostream &os) {
|
|
|
|
os << "#ifdef GEN_PASS_REGISTRATION\n";
|
|
|
|
for (const Pass &pass : passes) {
|
2020-08-01 04:18:13 +08:00
|
|
|
os << llvm::formatv(passRegistrationCode, pass.getDef()->getName(),
|
2020-04-01 16:48:34 +08:00
|
|
|
pass.getArgument(), pass.getSummary(),
|
|
|
|
pass.getConstructor());
|
2020-04-03 01:36:11 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 04:18:13 +08:00
|
|
|
os << llvm::formatv(passGroupRegistrationCode, groupName);
|
|
|
|
for (const Pass &pass : passes)
|
|
|
|
os << " register" << pass.getDef()->getName() << "Pass();\n";
|
|
|
|
os << "}\n";
|
2020-04-03 01:36:11 +08:00
|
|
|
os << "#undef GEN_PASS_REGISTRATION\n";
|
2020-08-01 04:18:13 +08:00
|
|
|
os << "#endif // GEN_PASS_REGISTRATION\n";
|
2020-04-01 16:48:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GEN: Registration hooks
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static void emitDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
|
|
|
|
os << "/* Autogenerated by mlir-tblgen; don't manually edit */\n";
|
|
|
|
std::vector<Pass> passes;
|
2020-04-08 04:58:12 +08:00
|
|
|
for (const auto *def : recordKeeper.getAllDerivedDefinitions("PassBase"))
|
|
|
|
passes.push_back(Pass(def));
|
|
|
|
|
|
|
|
emitPassDecls(passes, os);
|
2020-04-01 16:48:34 +08:00
|
|
|
emitRegistration(passes, os);
|
|
|
|
}
|
|
|
|
|
|
|
|
static mlir::GenRegistration
|
|
|
|
genRegister("gen-pass-decls", "Generate operation documentation",
|
|
|
|
[](const llvm::RecordKeeper &records, raw_ostream &os) {
|
|
|
|
emitDecls(records, os);
|
|
|
|
return false;
|
|
|
|
});
|