Skip to content

Commit f9dc2b7

Browse files
committed
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 &registry) 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
1 parent e75bc5c commit f9dc2b7

File tree

95 files changed

+761
-239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+761
-239
lines changed

mlir/examples/standalone/standalone-opt/standalone-opt.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424
int main(int argc, char **argv) {
2525
mlir::registerAllDialects();
2626
mlir::registerAllPasses();
27-
28-
mlir::registerDialect<mlir::standalone::StandaloneDialect>();
2927
// TODO: Register standalone passes here.
3028

31-
return failed(mlir::MlirOptMain(argc, argv, "Standalone optimizer driver\n"));
29+
mlir::DialectRegistry registry;
30+
registry.insert<mlir::standalone::StandaloneDialect>();
31+
registry.insert<mlir::StandardOpsDialect>();
32+
// Add the following to include *all* MLIR Core dialects, or selectively
33+
// include what you need like above. You only need to register dialects that
34+
// will be *parsed* by the tool, not the one generated
35+
// registerAllDialects(registry);
36+
37+
return failed(
38+
mlir::MlirOptMain(argc, argv, "Standalone optimizer driver\n", registry));
3239
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// RUN: standalone-opt --show-dialects | FileCheck %s
2-
// CHECK: Registered Dialects:
2+
// CHECK: Available Dialects:
33
// CHECK: standalone

mlir/examples/toy/Ch2/toyc.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
6868
}
6969

7070
int dumpMLIR() {
71-
// Register our Dialect with MLIR.
72-
mlir::registerDialect<mlir::toy::ToyDialect>();
73-
74-
mlir::MLIRContext context;
71+
mlir::MLIRContext context(/*loadAllDialects=*/false);
72+
// Load our Dialect in this MLIR Context.
73+
context.getOrLoadDialect<mlir::toy::ToyDialect>();
7574

7675
// Handle '.toy' input to the compiler.
7776
if (inputType != InputType::MLIR &&

mlir/examples/toy/Ch3/toyc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
102102
}
103103

104104
int dumpMLIR() {
105-
// Register our Dialect with MLIR.
106-
mlir::registerDialect<mlir::toy::ToyDialect>();
105+
mlir::MLIRContext context(/*loadAllDialects=*/false);
106+
// Load our Dialect in this MLIR Context.
107+
context.getOrLoadDialect<mlir::toy::ToyDialect>();
107108

108-
mlir::MLIRContext context;
109109
mlir::OwningModuleRef module;
110110
llvm::SourceMgr sourceMgr;
111111
mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);

mlir/examples/toy/Ch4/toyc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
103103
}
104104

105105
int dumpMLIR() {
106-
// Register our Dialect with MLIR.
107-
mlir::registerDialect<mlir::toy::ToyDialect>();
106+
mlir::MLIRContext context(/*loadAllDialects=*/false);
107+
// Load our Dialect in this MLIR Context.
108+
context.getOrLoadDialect<mlir::toy::ToyDialect>();
108109

109-
mlir::MLIRContext context;
110110
mlir::OwningModuleRef module;
111111
llvm::SourceMgr sourceMgr;
112112
mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);

mlir/examples/toy/Ch5/mlir/LowerToAffineLoops.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ struct TransposeOpLowering : public ConversionPattern {
256256
namespace {
257257
struct ToyToAffineLoweringPass
258258
: public PassWrapper<ToyToAffineLoweringPass, FunctionPass> {
259+
void getDependentDialects(DialectRegistry &registry) const override {
260+
registry.insert<AffineDialect, StandardOpsDialect>();
261+
}
259262
void runOnFunction() final;
260263
};
261264
} // end anonymous namespace.

mlir/examples/toy/Ch5/toyc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
106106
}
107107

108108
int dumpMLIR() {
109-
// Register our Dialect with MLIR.
110-
mlir::registerDialect<mlir::toy::ToyDialect>();
109+
mlir::MLIRContext context(/*loadAllDialects=*/false);
110+
// Load our Dialect in this MLIR Context.
111+
context.getOrLoadDialect<mlir::toy::ToyDialect>();
111112

112-
mlir::MLIRContext context;
113113
mlir::OwningModuleRef module;
114114
llvm::SourceMgr sourceMgr;
115115
mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);

mlir/examples/toy/Ch6/mlir/LowerToAffineLoops.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ struct TransposeOpLowering : public ConversionPattern {
255255
namespace {
256256
struct ToyToAffineLoweringPass
257257
: public PassWrapper<ToyToAffineLoweringPass, FunctionPass> {
258+
void getDependentDialects(DialectRegistry &registry) const override {
259+
registry.insert<AffineDialect, StandardOpsDialect>();
260+
}
258261
void runOnFunction() final;
259262
};
260263
} // end anonymous namespace.

mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ class PrintOpLowering : public ConversionPattern {
159159
namespace {
160160
struct ToyToLLVMLoweringPass
161161
: public PassWrapper<ToyToLLVMLoweringPass, OperationPass<ModuleOp>> {
162+
void getDependentDialects(DialectRegistry &registry) const override {
163+
registry.insert<LLVM::LLVMDialect, scf::SCFDialect>();
164+
}
162165
void runOnOperation() final;
163166
};
164167
} // end anonymous namespace

mlir/examples/toy/Ch6/toyc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ int main(int argc, char **argv) {
255255

256256
// If we aren't dumping the AST, then we are compiling with/to MLIR.
257257

258-
// Register our Dialect with MLIR.
259-
mlir::registerDialect<mlir::toy::ToyDialect>();
258+
mlir::MLIRContext context(/*loadAllDialects=*/false);
259+
// Load our Dialect in this MLIR Context.
260+
context.getOrLoadDialect<mlir::toy::ToyDialect>();
260261

261-
mlir::MLIRContext context;
262262
mlir::OwningModuleRef module;
263263
if (int error = loadAndProcessMLIR(context, module))
264264
return error;

0 commit comments

Comments
 (0)