Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/Bindings/Python/mlir/dialects/_ods_common.py
Show All 11 Lines | __all__ = [ | ||||
"segmented_accessor", | "segmented_accessor", | ||||
] | ] | ||||
def extend_opview_class(ext_module): | def extend_opview_class(ext_module): | ||||
"""Decorator to extend an OpView class from an extension module. | """Decorator to extend an OpView class from an extension module. | ||||
Extension modules can expose various entry-points: | Extension modules can expose various entry-points: | ||||
Stand-alone class with the same name as a parent OpView class (i.e. | |||||
"ReturnOp"). A name-based match is attempted first before falling back | |||||
to a below mechanism. | |||||
def select_opview_mixin(parent_opview_cls): | def select_opview_mixin(parent_opview_cls): | ||||
If defined, allows an appropriate mixin class to be selected dynamically | If defined, allows an appropriate mixin class to be selected dynamically | ||||
based on the parent OpView class. Should return NotImplemented if a | based on the parent OpView class. Should return NotImplemented if a | ||||
decision is not made. | decision is not made. | ||||
Stand-alone class with the same name as a parent OpView class (i.e. | |||||
"ReturnOp"). | |||||
Args: | Args: | ||||
ext_module: A module from which to locate extensions. Can be None if not | ext_module: A module from which to locate extensions. Can be None if not | ||||
available. | available. | ||||
Returns: | Returns: | ||||
A decorator that takes an OpView subclass and further extends it as | A decorator that takes an OpView subclass and further extends it as | ||||
needed. | needed. | ||||
""" | """ | ||||
def class_decorator(parent_opview_cls: type): | def class_decorator(parent_opview_cls: type): | ||||
if ext_module is None: | if ext_module is None: | ||||
return parent_opview_cls | return parent_opview_cls | ||||
mixin_cls = NotImplemented | mixin_cls = NotImplemented | ||||
# First try to resolve by name. | |||||
try: | try: | ||||
select_mixin = getattr(ext_module, "select_opview_mixin") | mixin_cls = getattr(ext_module, parent_opview_cls.__name__) | ||||
except AttributeError: | except AttributeError: | ||||
# Try to default resolve it. | # Fall back to a select_opview_mixin hook. | ||||
try: | try: | ||||
mixin_cls = getattr(ext_module, parent_opview_cls.__name__) | select_mixin = getattr(ext_module, "select_opview_mixin") | ||||
except AttributeError: | except AttributeError: | ||||
pass | pass | ||||
else: | else: | ||||
mixin_cls = select_mixin(parent_opview_cls) | mixin_cls = select_mixin(parent_opview_cls) | ||||
if mixin_cls is NotImplemented or mixin_cls is None: | if mixin_cls is NotImplemented or mixin_cls is None: | ||||
return parent_opview_cls | return parent_opview_cls | ||||
# Have a mixin_cls. Create an appropriate subclass. | # Have a mixin_cls. Create an appropriate subclass. | ||||
try: | try: | ||||
class LocalOpView(mixin_cls, parent_opview_cls): | class LocalOpView(mixin_cls, parent_opview_cls): | ||||
pass | pass | ||||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |