1- import weakref
2-
3- from mlir import ir
41from mlir .dialects import ext
52
63
@@ -26,35 +23,11 @@ def load(cls, *args, **kwargs):
2623 dialect stays loaded in every context it was loaded into.
2724 - Never loaded: perform a fresh load.
2825 """
29- # Per-subclass registry of the contexts this dialect is loaded in. A
30- # single "last context" is insufficient: a dialect stays loaded in every
31- # context it was loaded into, so interleaving contexts requires tracking
32- # all of them. Contexts are held weakly so they remain garbage
33- # collectable.
34- #
35- # FIXME: It is a workaround to simplify dialect loading.
36- # Ideally, the `ext.Dialect` base class would handle this automatically.
37- # See issue: #229
38- loaded_contexts = cls .__dict__ .get ("_loaded_contexts" )
39- if loaded_contexts is None :
40- loaded_contexts = weakref .WeakSet ()
41- cls ._loaded_contexts = loaded_contexts
42-
43- current = ir .Context .current
44- if current in loaded_contexts :
45- # Already present in this context; MLIR cannot load it again here.
26+ try :
27+ super ().load (* args , ** kwargs )
28+ except ext .DialectAlreadyLoadedError :
4629 return
4730
48- # If the dialect was loaded before (in any context), its process-global,
49- # context-independent registrations already exist and must be replaced
50- # rather than added again.
51- globals_registered = hasattr (cls , "_mlir_module" )
52-
53- # Registers the dialect and its op classes and loads them into the context.
54- kwargs ["reload" ] = globals_registered
55- super ().load (* args , ** kwargs )
56- loaded_contexts .add (current )
57-
5831 # Attach interfaces to just registered/loaded operations.
5932 for op_cls in cls .operations :
6033 if hasattr (op_cls , "attach_interface_impls" ):
0 commit comments