<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54932>54932</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[MLIR:Python] Improve ergonomics of op constructor APIs in Python bindings
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hawkinsp
</td>
</tr>
</table>
<pre>
We've made extensive use of the MLIR Python bindings to build IR in the MHLO dialect (in the TensorFlow tree) in JAX (https://github.com/google/jax).
The experience has been fairly positive overall, but there are two things that would probably significantly improve the ergonomics of building IR in an MLIR dialect via the Python bindings. While this request comes from experience with the out-of-tree MHLO dialect, I strongly suspect the same is true for any dialect, not just that one.
a) support default arguments to op constructors.
Even if an operator has defaults in its tablegen specification, e.g., via `DefaultValuedAttr`, these defaults aren't available or used by the Python constructors. A natural change would be to make operator arguments that have defaults into Python keyword arguments with default values. This would allow users to omit these attributes, which is important both for ergonomics and for compatibility if the dialect changes. Currently adding a new attribute, even one with a default value, is always a breaking change for Python code.
b) support automatic casts from standard Python types (e.g., integers, lists, ...) into corresponding attributes when passed to op constructors.
If an operator accepts, say, a `BoolAttr` as an argument to its constructor, it would be very nice to pass a Python bool (e.g., `False`) rather than writing out `mlir.ir.BoolAttr.get(False)`. The same thing applies for integer attributes from Python integers, string attributes from Python strings, and dense elements attributes from Python lists. The reason to do this is primarily ergonomics: currently it is very verbose to have to write out attribute types each time.
@stellaraccident
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyFVdFu4zYQ_Br7hYjgSPYlfvCD71KjKa7ooQju-rqSVhITSlRJyj7_fWcp2ZEDtAUUK5KWuzOzw2Vuy_PuBy_ShyOrlkpW_DNw5zUeB8_KVio0rH7_-vyn-nYOje1UrrtSd7VXwap80KZU-Ka7Me7Xr3-oUpPhIqhF-ji9fkFG6w7GnlRwjGpbWfDb_i-JaULo_SLbL9ID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