This is an archive of the discontinued LLVM Phabricator instance.

Revisit Dialect registration: require and store a TypeID on dialects
ClosedPublic

Authored by mehdi_amini on Aug 6 2020, 8:35 PM.

Details

Summary

This patch moves the registration to a method in the MLIRContext: getOrCreateDialect<ConcreteDialect>()

This method requires dialect to provide a static getDialectNamespace()
and store a TypeID on the Dialect itself, which allows to lazyily
create a dialect when not yet loaded in the context.
As a side effect, it means that duplicated registration of the same
dialect is not an issue anymore.

To limit the boilerplate, TableGen dialect generation is modified to
emit the constructor entirely and invoke separately a "init()" method
that the user implements.

Diff Detail

Event Timeline

mehdi_amini created this revision.Aug 6 2020, 8:35 PM
mehdi_amini requested review of this revision.Aug 6 2020, 8:35 PM

Rename init to initialize

rriddle added inline comments.Aug 6 2020, 9:56 PM
mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
30–32

Why this change?

mlir/include/mlir/IR/MLIRContext.h
59

nit: new T(this)?

mlir/lib/IR/MLIRContext.cpp
476–477

nit:

auto it  = impl.dialects.insert(insertPt, ctor());
return &**it;?
mlir/lib/Transforms/LoopFusion.cpp
1456 ↗(On Diff #283809)

Seems unrelated.

mehdi_amini marked 2 inline comments as done.

Address comments

mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
30–32

Constructor is not longer out-of-line and the forward declaration + the unique_ptr is the usual issue...

mlir/lib/Transforms/LoopFusion.cpp
1456 ↗(On Diff #283809)

Ahah my sed caught a bit wide apparently, reverted.

rriddle accepted this revision.Aug 7 2020, 12:28 AM
rriddle added inline comments.
mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td
30–32

Right, forgot that we emit the constructor in the .h now. Can you add a comment here describing why we use a raw pointer? This could no longer be an issue if we generated dialect defs too at some point.

mlir/tools/mlir-tblgen/DialectGen.cpp
67

nit: ::mlir::TypeID::

This revision is now accepted and ready to land.Aug 7 2020, 12:28 AM
This revision was landed with ongoing or failed builds.Aug 7 2020, 8:57 AM
This revision was automatically updated to reflect the committed changes.

This broke the flang build:

../../flang/lib/Optimizer/Dialect/FIRDialect.cpp:17:7: error: no matching constructor for initialization of 'mlir::Dialect'
    : mlir::Dialect("fir", ctx) {
      ^             ~~~~~~~~~~

This broke the flang build:

../../flang/lib/Optimizer/Dialect/FIRDialect.cpp:17:7: error: no matching constructor for initialization of 'mlir::Dialect'
    : mlir::Dialect("fir", ctx) {
      ^             ~~~~~~~~~~

Should be fixed in 82fd1392016984c81c6037e147ee2dd36cf91f4c

This broke the flang build:

../../flang/lib/Optimizer/Dialect/FIRDialect.cpp:17:7: error: no matching constructor for initialization of 'mlir::Dialect'
    : mlir::Dialect("fir", ctx) {
      ^             ~~~~~~~~~~

Sorry, I should build flang when refactoring mlir. Thanks @rriddle for the quick fix!