Skip to content

Commit

Permalink
Separate the Registration from Loading dialects in the Context
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joker-eph committed Aug 19, 2020
1 parent e75bc5c commit f9dc2b7
Show file tree
Hide file tree
Showing 95 changed files with 761 additions and 239 deletions.
13 changes: 10 additions & 3 deletions mlir/examples/standalone/standalone-opt/standalone-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
int main(int argc, char **argv) {
mlir::registerAllDialects();
mlir::registerAllPasses();

mlir::registerDialect<mlir::standalone::StandaloneDialect>();
// TODO: Register standalone passes here.

return failed(mlir::MlirOptMain(argc, argv, "Standalone optimizer driver\n"));
mlir::DialectRegistry registry;
registry.insert<mlir::standalone::StandaloneDialect>();
registry.insert<mlir::StandardOpsDialect>();
// Add the following to include *all* MLIR Core dialects, or selectively
// include what you need like above. You only need to register dialects that
// will be *parsed* by the tool, not the one generated
// registerAllDialects(registry);

return failed(
mlir::MlirOptMain(argc, argv, "Standalone optimizer driver\n", registry));
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// RUN: standalone-opt --show-dialects | FileCheck %s
// CHECK: Registered Dialects:
// CHECK: Available Dialects:
// CHECK: standalone
7 changes: 3 additions & 4 deletions mlir/examples/toy/Ch2/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
}

int dumpMLIR() {
// Register our Dialect with MLIR.
mlir::registerDialect<mlir::toy::ToyDialect>();

mlir::MLIRContext context;
mlir::MLIRContext context(/*loadAllDialects=*/false);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();

// Handle '.toy' input to the compiler.
if (inputType != InputType::MLIR &&
Expand Down
6 changes: 3 additions & 3 deletions mlir/examples/toy/Ch3/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
}

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

mlir::MLIRContext context;
mlir::OwningModuleRef module;
llvm::SourceMgr sourceMgr;
mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
Expand Down
6 changes: 3 additions & 3 deletions mlir/examples/toy/Ch4/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
}

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

mlir::MLIRContext context;
mlir::OwningModuleRef module;
llvm::SourceMgr sourceMgr;
mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
Expand Down
3 changes: 3 additions & 0 deletions mlir/examples/toy/Ch5/mlir/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ struct TransposeOpLowering : public ConversionPattern {
namespace {
struct ToyToAffineLoweringPass
: public PassWrapper<ToyToAffineLoweringPass, FunctionPass> {
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<AffineDialect, StandardOpsDialect>();
}
void runOnFunction() final;
};
} // end anonymous namespace.
Expand Down
6 changes: 3 additions & 3 deletions mlir/examples/toy/Ch5/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
}

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

mlir::MLIRContext context;
mlir::OwningModuleRef module;
llvm::SourceMgr sourceMgr;
mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
Expand Down
3 changes: 3 additions & 0 deletions mlir/examples/toy/Ch6/mlir/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ struct TransposeOpLowering : public ConversionPattern {
namespace {
struct ToyToAffineLoweringPass
: public PassWrapper<ToyToAffineLoweringPass, FunctionPass> {
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<AffineDialect, StandardOpsDialect>();
}
void runOnFunction() final;
};
} // end anonymous namespace.
Expand Down
3 changes: 3 additions & 0 deletions mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class PrintOpLowering : public ConversionPattern {
namespace {
struct ToyToLLVMLoweringPass
: public PassWrapper<ToyToLLVMLoweringPass, OperationPass<ModuleOp>> {
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<LLVM::LLVMDialect, scf::SCFDialect>();
}
void runOnOperation() final;
};
} // end anonymous namespace
Expand Down
6 changes: 3 additions & 3 deletions mlir/examples/toy/Ch6/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ int main(int argc, char **argv) {

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

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

mlir::MLIRContext context;
mlir::OwningModuleRef module;
if (int error = loadAndProcessMLIR(context, module))
return error;
Expand Down
3 changes: 3 additions & 0 deletions mlir/examples/toy/Ch7/mlir/LowerToAffineLoops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ struct TransposeOpLowering : public ConversionPattern {
namespace {
struct ToyToAffineLoweringPass
: public PassWrapper<ToyToAffineLoweringPass, FunctionPass> {
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<AffineDialect, StandardOpsDialect>();
}
void runOnFunction() final;
};
} // end anonymous namespace.
Expand Down
3 changes: 3 additions & 0 deletions mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class PrintOpLowering : public ConversionPattern {
namespace {
struct ToyToLLVMLoweringPass
: public PassWrapper<ToyToLLVMLoweringPass, OperationPass<ModuleOp>> {
void getDependentDialects(DialectRegistry &registry) const override {
registry.insert<LLVM::LLVMDialect, scf::SCFDialect>();
}
void runOnOperation() final;
};
} // end anonymous namespace
Expand Down
6 changes: 3 additions & 3 deletions mlir/examples/toy/Ch7/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ int main(int argc, char **argv) {

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

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

mlir::MLIRContext context;
mlir::OwningModuleRef module;
if (int error = loadAndProcessMLIR(context, module))
return error;
Expand Down
10 changes: 6 additions & 4 deletions mlir/include/mlir-c/Registration.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
#ifndef MLIR_C_REGISTRATION_H
#define MLIR_C_REGISTRATION_H

#include "mlir-c/IR.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Registers all dialects known to core MLIR with the system. This must be
* called before creating an MlirContext if it needs access to the registered
* dialects. */
void mlirRegisterAllDialects();
/** Registers all dialects known to core MLIR with the provided Context.
* This is needed before creating IR for these Dialects.
*/
void mlirRegisterAllDialects(MlirContext context);

#ifdef __cplusplus
}
Expand Down
26 changes: 26 additions & 0 deletions mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def ConvertAffineToStandard : Pass<"lower-affine"> {
`affine.apply`.
}];
let constructor = "mlir::createLowerAffinePass()";
let dependentDialects = [
"scf::SCFDialect",
"StandardOpsDialect",
"vector::VectorDialect"
];
}

//===----------------------------------------------------------------------===//
Expand All @@ -76,6 +81,7 @@ def ConvertAVX512ToLLVM : Pass<"convert-avx512-to-llvm", "ModuleOp"> {
let summary = "Convert the operations from the avx512 dialect into the LLVM "
"dialect";
let constructor = "mlir::createConvertAVX512ToLLVMPass()";
let dependentDialects = ["LLVM::LLVMDialect", "LLVM::LLVMAVX512Dialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -98,6 +104,7 @@ def GpuToLLVMConversionPass : Pass<"gpu-to-llvm", "ModuleOp"> {
def ConvertGpuOpsToNVVMOps : Pass<"convert-gpu-to-nvvm", "gpu::GPUModuleOp"> {
let summary = "Generate NVVM operations for gpu operations";
let constructor = "mlir::createLowerGpuOpsToNVVMOpsPass()";
let dependentDialects = ["NVVM::NVVMDialect"];
let options = [
Option<"indexBitwidth", "index-bitwidth", "unsigned",
/*default=kDeriveIndexBitwidthFromDataLayout*/"0",
Expand All @@ -112,6 +119,7 @@ def ConvertGpuOpsToNVVMOps : Pass<"convert-gpu-to-nvvm", "gpu::GPUModuleOp"> {
def ConvertGpuOpsToROCDLOps : Pass<"convert-gpu-to-rocdl", "gpu::GPUModuleOp"> {
let summary = "Generate ROCDL operations for gpu operations";
let constructor = "mlir::createLowerGpuOpsToROCDLOpsPass()";
let dependentDialects = ["ROCDL::ROCDLDialect"];
let options = [
Option<"indexBitwidth", "index-bitwidth", "unsigned",
/*default=kDeriveIndexBitwidthFromDataLayout*/"0",
Expand All @@ -126,6 +134,7 @@ def ConvertGpuOpsToROCDLOps : Pass<"convert-gpu-to-rocdl", "gpu::GPUModuleOp"> {
def ConvertGPUToSPIRV : Pass<"convert-gpu-to-spirv", "ModuleOp"> {
let summary = "Convert GPU dialect to SPIR-V dialect";
let constructor = "mlir::createConvertGPUToSPIRVPass()";
let dependentDialects = ["spirv::SPIRVDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -136,13 +145,15 @@ def ConvertGpuLaunchFuncToVulkanLaunchFunc
: Pass<"convert-gpu-launch-to-vulkan-launch", "ModuleOp"> {
let summary = "Convert gpu.launch_func to vulkanLaunch external call";
let constructor = "mlir::createConvertGpuLaunchFuncToVulkanLaunchFuncPass()";
let dependentDialects = ["spirv::SPIRVDialect"];
}

def ConvertVulkanLaunchFuncToVulkanCalls
: Pass<"launch-func-to-vulkan", "ModuleOp"> {
let summary = "Convert vulkanLaunch external call to Vulkan runtime external "
"calls";
let constructor = "mlir::createConvertVulkanLaunchFuncToVulkanCallsPass()";
let dependentDialects = ["LLVM::LLVMDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -153,6 +164,7 @@ def ConvertLinalgToLLVM : Pass<"convert-linalg-to-llvm", "ModuleOp"> {
let summary = "Convert the operations from the linalg dialect into the LLVM "
"dialect";
let constructor = "mlir::createConvertLinalgToLLVMPass()";
let dependentDialects = ["scf::SCFDialect", "LLVM::LLVMDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -163,6 +175,7 @@ def ConvertLinalgToStandard : Pass<"convert-linalg-to-std", "ModuleOp"> {
let summary = "Convert the operations from the linalg dialect into the "
"Standard dialect";
let constructor = "mlir::createConvertLinalgToStandardPass()";
let dependentDialects = ["StandardOpsDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -172,6 +185,7 @@ def ConvertLinalgToStandard : Pass<"convert-linalg-to-std", "ModuleOp"> {
def ConvertLinalgToSPIRV : Pass<"convert-linalg-to-spirv", "ModuleOp"> {
let summary = "Convert Linalg ops to SPIR-V ops";
let constructor = "mlir::createLinalgToSPIRVPass()";
let dependentDialects = ["spirv::SPIRVDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -182,6 +196,7 @@ def SCFToStandard : Pass<"convert-scf-to-std"> {
let summary = "Convert SCF dialect to Standard dialect, replacing structured"
" control flow with a CFG";
let constructor = "mlir::createLowerToCFGPass()";
let dependentDialects = ["StandardOpsDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -191,6 +206,7 @@ def SCFToStandard : Pass<"convert-scf-to-std"> {
def ConvertAffineForToGPU : FunctionPass<"convert-affine-for-to-gpu"> {
let summary = "Convert top-level AffineFor Ops to GPU kernels";
let constructor = "mlir::createAffineForToGPUPass()";
let dependentDialects = ["gpu::GPUDialect"];
let options = [
Option<"numBlockDims", "gpu-block-dims", "unsigned", /*default=*/"1u",
"Number of GPU block dimensions for mapping">,
Expand All @@ -202,6 +218,7 @@ def ConvertAffineForToGPU : FunctionPass<"convert-affine-for-to-gpu"> {
def ConvertParallelLoopToGpu : Pass<"convert-parallel-loops-to-gpu"> {
let summary = "Convert mapped scf.parallel ops to gpu launch operations";
let constructor = "mlir::createParallelLoopToGpuPass()";
let dependentDialects = ["AffineDialect", "gpu::GPUDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -212,6 +229,7 @@ def ConvertShapeToStandard : Pass<"convert-shape-to-std", "ModuleOp"> {
let summary = "Convert operations from the shape dialect into the standard "
"dialect";
let constructor = "mlir::createConvertShapeToStandardPass()";
let dependentDialects = ["StandardOpsDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -221,6 +239,7 @@ def ConvertShapeToStandard : Pass<"convert-shape-to-std", "ModuleOp"> {
def ConvertShapeToSCF : FunctionPass<"convert-shape-to-scf"> {
let summary = "Convert operations from the shape dialect to the SCF dialect";
let constructor = "mlir::createConvertShapeToSCFPass()";
let dependentDialects = ["scf::SCFDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -230,6 +249,7 @@ def ConvertShapeToSCF : FunctionPass<"convert-shape-to-scf"> {
def ConvertSPIRVToLLVM : Pass<"convert-spirv-to-llvm", "ModuleOp"> {
let summary = "Convert SPIR-V dialect to LLVM dialect";
let constructor = "mlir::createConvertSPIRVToLLVMPass()";
let dependentDialects = ["LLVM::LLVMDialect"];
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -264,6 +284,7 @@ def ConvertStandardToLLVM : Pass<"convert-std-to-llvm", "ModuleOp"> {
LLVM IR types.
}];
let constructor = "mlir::createLowerToLLVMPass()";
let dependentDialects = ["LLVM::LLVMDialect"];
let options = [
Option<"useAlignedAlloc", "use-aligned-alloc", "bool", /*default=*/"false",
"Use aligned_alloc in place of malloc for heap allocations">,
Expand Down Expand Up @@ -291,11 +312,13 @@ def ConvertStandardToLLVM : Pass<"convert-std-to-llvm", "ModuleOp"> {
def LegalizeStandardForSPIRV : Pass<"legalize-std-for-spirv"> {
let summary = "Legalize standard ops for SPIR-V lowering";
let constructor = "mlir::createLegalizeStdOpsForSPIRVLoweringPass()";
let dependentDialects = ["spirv::SPIRVDialect"];
}

def ConvertStandardToSPIRV : Pass<"convert-std-to-spirv", "ModuleOp"> {
let summary = "Convert Standard Ops to SPIR-V dialect";
let constructor = "mlir::createConvertStandardToSPIRVPass()";
let dependentDialects = ["spirv::SPIRVDialect"];
}

//===----------------------------------------------------------------------===//
Expand All @@ -306,6 +329,7 @@ def ConvertVectorToSCF : FunctionPass<"convert-vector-to-scf"> {
let summary = "Lower the operations from the vector dialect into the SCF "
"dialect";
let constructor = "mlir::createConvertVectorToSCFPass()";
let dependentDialects = ["AffineDialect", "scf::SCFDialect"];
let options = [
Option<"fullUnroll", "full-unroll", "bool", /*default=*/"false",
"Perform full unrolling when converting vector transfers to SCF">,
Expand All @@ -320,6 +344,7 @@ def ConvertVectorToLLVM : Pass<"convert-vector-to-llvm", "ModuleOp"> {
let summary = "Lower the operations from the vector dialect into the LLVM "
"dialect";
let constructor = "mlir::createConvertVectorToLLVMPass()";
let dependentDialects = ["LLVM::LLVMDialect"];
let options = [
Option<"reassociateFPReductions", "reassociate-fp-reductions",
"bool", /*default=*/"false",
Expand All @@ -335,6 +360,7 @@ def ConvertVectorToROCDL : Pass<"convert-vector-to-rocdl", "ModuleOp"> {
let summary = "Lower the operations from the vector dialect into the ROCDL "
"dialect";
let constructor = "mlir::createConvertVectorToROCDLPass()";
let dependentDialects = ["ROCDL::ROCDLDialect"];
}

#endif // MLIR_CONVERSION_PASSES
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/Affine/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def AffineLoopUnrollAndJam : FunctionPass<"affine-loop-unroll-jam"> {
def AffineVectorize : FunctionPass<"affine-super-vectorize"> {
let summary = "Vectorize to a target independent n-D vector abstraction";
let constructor = "mlir::createSuperVectorizePass()";
let dependentDialects = ["vector::VectorDialect"];
let options = [
ListOption<"vectorSizes", "virtual-vector-size", "int64_t",
"Specify an n-D virtual vector size for vectorization",
Expand Down
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define MLIR_DIALECT_LLVMIR_LLVMDIALECT_H_

#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Function.h"
#include "mlir/IR/OpDefinition.h"
Expand Down
5 changes: 5 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ include "mlir/IR/OpBase.td"
def LLVM_Dialect : Dialect {
let name = "llvm";
let cppNamespace = "LLVM";

/// FIXME: at the moment this is a dependency of the translation to LLVM IR,
/// not really one of this dialect per-se.
let dependentDialects = ["omp::OpenMPDialect"];

let hasRegionArgAttrVerify = 1;
let hasOperationAttrVerify = 1;
let extraClassDeclaration = [{
Expand Down
1 change: 1 addition & 0 deletions mlir/include/mlir/Dialect/LLVMIR/NVVMDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#ifndef MLIR_DIALECT_LLVMIR_NVVMDIALECT_H_
#define MLIR_DIALECT_LLVMIR_NVVMDIALECT_H_

#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
Expand Down
Loading

0 comments on commit f9dc2b7

Please sign in to comment.