diff options
| author | Jeremy Furtek <jfurtek@nvidia.com> | 2022-05-21 21:12:11 -0700 |
|---|---|---|
| committer | Stella Laurenzo <stellaraccident@gmail.com> | 2022-05-21 21:18:53 -0700 |
| commit | 9b79f50b59c671f3a7b0c7654cdd6d59ab6a6bbc (patch) | |
| tree | c6df8c7d79504662bc068424494e00f7bb4ae149 /mlir/python | |
| parent | b86440ecde5c1dec5b898a3f1bc08ab9853d5ed9 (diff) | |
[mlir][tblgen][ods][python] Use keyword-only arguments for optional builder arguments in generated Python bindings
This diff modifies `mlir-tblgen` to generate Python Operation class `__init__()`
functions that use Python keyword-only arguments.
Previously, all `__init__()` function arguments were positional. Python code to
create MLIR Operations was required to provide values for ALL builder arguments,
including optional arguments (attributes and operands). Callers that did not
provide, for example, an optional attribute would be forced to provide `None`
as an argument for EACH optional attribute. Proposed changes in this diff use
`tblgen` record information (as provided by ODS) to generate keyword arguments
for:
- optional operands
- optional attributes (which includes unit attributes)
- default-valued attributes
These `__init__()` function keyword arguments have default `None` values (i.e.
the argument form is `optionalAttr=None`), allowing callers to create Operations
more easily.
Note that since optional arguments become keyword-only arguments (since they are
placed after the bare `*` argument), this diff will require ALL optional
operands and attributes to be provided using explicit keyword syntax. This may,
in the short term, break any out-of-tree Python code that provided values via
positional arguments. However, in the long term, it seems that requiring
keywords for optional arguments will be more robust to operation changes that
add arguments.
Tests were modified to reflect the updated Operation builder calling convention.
This diff partially addresses the requests made in the github issue below.
https://github.com/llvm/llvm-project/issues/54932
Reviewed By: stellaraccident, mikeurbach
Differential Revision: https://reviews.llvm.org/D124717
Diffstat (limited to 'mlir/python')
| -rw-r--r-- | mlir/python/mlir/dialects/_func_ops_ext.py | 2 | ||||
| -rw-r--r-- | mlir/python/mlir/dialects/_ml_program_ops_ext.py | 2 | ||||
| -rw-r--r-- | mlir/python/mlir/dialects/_pdl_ops_ext.py | 20 |
3 files changed, 12 insertions, 12 deletions
diff --git a/mlir/python/mlir/dialects/_func_ops_ext.py b/mlir/python/mlir/dialects/_func_ops_ext.py index 6fe3ff5302e2..79577463d919 100644 --- a/mlir/python/mlir/dialects/_func_ops_ext.py +++ b/mlir/python/mlir/dialects/_func_ops_ext.py @@ -58,7 +58,7 @@ class FuncOp: type = TypeAttr.get(type) sym_visibility = StringAttr.get( str(visibility)) if visibility is not None else None - super().__init__(sym_name, type, sym_visibility, loc=loc, ip=ip) + super().__init__(sym_name, type, sym_visibility=sym_visibility, loc=loc, ip=ip) if body_builder: entry_block = self.add_entry_block() with InsertionPoint(entry_block): diff --git a/mlir/python/mlir/dialects/_ml_program_ops_ext.py b/mlir/python/mlir/dialects/_ml_program_ops_ext.py index a3df7ff03360..8db82cf81c67 100644 --- a/mlir/python/mlir/dialects/_ml_program_ops_ext.py +++ b/mlir/python/mlir/dialects/_ml_program_ops_ext.py @@ -48,7 +48,7 @@ class FuncOp: type = TypeAttr.get(type) sym_visibility = StringAttr.get( str(visibility)) if visibility is not None else None - super().__init__(sym_name, type, sym_visibility, loc=loc, ip=ip) + super().__init__(sym_name, type, sym_visibility=sym_visibility, loc=loc, ip=ip) if body_builder: entry_block = self.add_entry_block() with InsertionPoint(entry_block): diff --git a/mlir/python/mlir/dialects/_pdl_ops_ext.py b/mlir/python/mlir/dialects/_pdl_ops_ext.py index fb5b519c7c02..bb63fe64dd03 100644 --- a/mlir/python/mlir/dialects/_pdl_ops_ext.py +++ b/mlir/python/mlir/dialects/_pdl_ops_ext.py @@ -93,7 +93,7 @@ class AttributeOp: ip=None): type = type if type is None else _get_value(type) result = pdl.AttributeType.get() - super().__init__(result, type, value, loc=loc, ip=ip) + super().__init__(result, type=type, value=value, loc=loc, ip=ip) class EraseOp: @@ -118,7 +118,7 @@ class OperandOp: ip=None): type = type if type is None else _get_value(type) result = pdl.ValueType.get() - super().__init__(result, type, loc=loc, ip=ip) + super().__init__(result, type=type, loc=loc, ip=ip) class OperandsOp: @@ -131,7 +131,7 @@ class OperandsOp: ip=None): types = types if types is None else _get_value(types) result = pdl.RangeType.get(pdl.ValueType.get()) - super().__init__(result, types, loc=loc, ip=ip) + super().__init__(result, type=types, loc=loc, ip=ip) class OperationOp: @@ -155,7 +155,7 @@ class OperationOp: attributeNames = ArrayAttr.get(attributeNames) types = _get_values(types) result = pdl.OperationType.get() - super().__init__(result, name, args, attributeValues, attributeNames, types, loc=loc, ip=ip) + super().__init__(result, args, attributeValues, attributeNames, types, name=name, loc=loc, ip=ip) class PatternOp: @@ -170,7 +170,7 @@ class PatternOp: """Creates an PDL `pattern` operation.""" name_attr = None if name is None else _get_str_attr(name) benefit_attr = _get_int_attr(16, benefit) - super().__init__(benefit_attr, name_attr, loc=loc, ip=ip) + super().__init__(benefit_attr, sym_name=name_attr, loc=loc, ip=ip) self.regions[0].blocks.append() @property @@ -192,7 +192,7 @@ class ReplaceOp: op = _get_value(op) with_op = with_op if with_op is None else _get_value(with_op) with_values = _get_values(with_values) - super().__init__(op, with_op, with_values, loc=loc, ip=ip) + super().__init__(op, with_values, replOperation=with_op, loc=loc, ip=ip) class ResultOp: @@ -222,7 +222,7 @@ class ResultsOp: ip=None): parent = _get_value(parent) index = index if index is None else _get_int_attr(32, index) - super().__init__(result, parent, index, loc=loc, ip=ip) + super().__init__(result, parent, index=index, loc=loc, ip=ip) class RewriteOp: @@ -238,7 +238,7 @@ class RewriteOp: root = root if root is None else _get_value(root) name = name if name is None else _get_str_attr(name) args = _get_values(args) - super().__init__(root, name, args, loc=loc, ip=ip) + super().__init__(args, root=root,name=name, loc=loc, ip=ip) def add_body(self): """Add body (block) to the rewrite.""" @@ -261,7 +261,7 @@ class TypeOp: ip=None): type = type if type is None else _get_type_attr(type) result = pdl.TypeType.get() - super().__init__(result, type, loc=loc, ip=ip) + super().__init__(result, type=type, loc=loc, ip=ip) class TypesOp: @@ -275,4 +275,4 @@ class TypesOp: types = _get_array_attr([_get_type_attr(ty) for ty in types]) types = None if not types else types result = pdl.RangeType.get(pdl.TypeType.get()) - super().__init__(result, types, loc=loc, ip=ip) + super().__init__(result, types=types, loc=loc, ip=ip) |
