This is an archive of the discontinued LLVM Phabricator instance.

[mlir] avoid exposing mutable DialectRegistry from MLIRContext
ClosedPublic

Authored by ftynse on Feb 9 2021, 4:23 AM.

Details

Summary

MLIRContext allows its users to access directly to the DialectRegistry it
contains. While sometimes useful for registering additional dialects on an
already existing context, this breaks the encapsulation by essentially giving
raw accesses to a part of the context's internal state. Remove this mutable
access and instead provide a method to append a given DialectRegistry to the
one already contained in the context. Also provide a shortcut mechanism to
construct a context from an already existing registry, which seems to be a
common use case in the wild. Keep read-only access to the registry contained in
the context in case it needs to be copied or used for constructing another
context.

With this change, DialectRegistry is no longer concerned with loading the
dialects and deciding whether to invoke delayed interface registration. Loading
is concentrated in the MLIRContext, and the functionality of the registry
better reflects its name.

Depends On D96137

Diff Detail

Event Timeline

ftynse created this revision.Feb 9 2021, 4:23 AM
ftynse requested review of this revision.Feb 9 2021, 4:23 AM
mehdi_amini accepted this revision.Feb 9 2021, 9:59 AM
mehdi_amini added inline comments.
mlir/include/mlir/IR/MLIRContext.h
49

Append for consistency with everywhere else here (this isn't google3)

92
mlir/lib/CAPI/Registration/Registration.cpp
17

Could we provide a registerAllDialects(MLIRContext *) helper that replace these three lines? I suspect this'll be a common pattern

mlir/lib/IR/MLIRContext.cpp
330

Trying to follow all the diff is really made harder by the order of the patches, you're undoing the change made in the parent in too many places.

mlir/lib/Target/SPIRV/TranslateRegistration.cpp
140

Can you add a TODO, we shouldn't need this and only load the dialect we need.

This revision is now accepted and ready to land.Feb 9 2021, 9:59 AM
ftynse updated this revision to Diff 322434.Feb 9 2021, 10:18 AM
ftynse marked 4 inline comments as done.

Address review

ftynse added inline comments.Feb 9 2021, 10:19 AM
mlir/lib/IR/MLIRContext.cpp
330

Sorry about that.

rriddle added inline comments.Feb 9 2021, 3:05 PM
mlir/include/mlir/IR/Dialect.h
271

Do we need to return this by-value?

mlir/include/mlir/IR/MLIRContext.h
47–51

What is this function used for now? Can we remove it?

mlir/lib/Pass/Pass.cpp
870

nit: Spell out auto here. It isn't clear if this is copying something expensive or not.

ftynse updated this revision to Diff 322614.Feb 10 2021, 1:12 AM
ftynse marked an inline comment as done.

Address review

ftynse added inline comments.Feb 10 2021, 1:12 AM
mlir/include/mlir/IR/Dialect.h
271

Good point, changed to function_ref.

mlir/include/mlir/IR/MLIRContext.h
47–51

SPIR-V translation constructs a new context with the same registry as the old context, so it needs readonly access to the registry. An alternative could be to provide a function to append the registry of one context into another context without exposing it.