[Mlir-commits] [mlir] [MLIR][Python] Add python-side adaptor class codegen in mlir-tblgen (PR #176640)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Jan 18 03:08:03 PST 2026


PragmaTwice wrote:

An example to show what is generated after this patch:
```python
class AddFOpAdaptor(_ods_OpAdaptor):
  OPERATION_NAME = "arith.addf"

  @builtins.property
  def lhs(self) -> _ods_ir.Value:
    return self.operands[0]

  @builtins.property
  def rhs(self) -> _ods_ir.Value:
    return self.operands[1]

@_ods_cext.register_operation(_Dialect)
class AddFOp(_ods_ir.OpView):
  r"""
  The `addf` operation takes two operands and returns one result, each of
  these is required to be the same type. This type may be a floating point
  scalar type, a vector whose element type is a floating point type, or a
  floating point tensor.
  
  Example:
  
  ```mlir
  // Scalar addition.
  %a = arith.addf %b, %c : f64
  
  // SIMD vector addition, e.g. for Intel SSE.
  %f = arith.addf %g, %h : vector<4xf32>
  
  // Tensor addition.
  %x = arith.addf %y, %z : tensor<4x?xbf16>
  ```
  
  TODO: In the distant future, this will accept optional attributes for fast
  math, contraction, rounding mode, and other controls.
  """

  OPERATION_NAME = "arith.addf"
  Adaptor = AddFOpAdaptor

  _ODS_REGIONS = (0, True)

  def __init__(self, lhs, rhs, *, fastmath=None, results=None, loc=None, ip=None):
    operands = []
    attributes = {}
    regions = None
    operands.append(lhs)
    operands.append(rhs)
    _ods_context = _ods_get_default_loc_context(loc)
    if fastmath is not None: attributes["fastmath"] = (fastmath if (
        isinstance(fastmath, _ods_ir.Attribute) or
        not _ods_ir.AttrBuilder.contains('Arith_FastMathAttr')) else
          _ods_ir.AttrBuilder.get('Arith_FastMathAttr')(fastmath, context=_ods_context))
    if results is None: results = [operands[0].type] * 1
    _ods_successors = None
    super().__init__(self.OPERATION_NAME, self._ODS_REGIONS, self._ODS_OPERAND_SEGMENTS, self._ODS_RESULT_SEGMENTS, attributes=attributes, results=results, operands=operands, successors=_ods_successors, regions=regions, loc=loc, ip=ip)

  @builtins.property
  def lhs(self) -> _ods_ir.Value:
    return self.operation.operands[0]

  @builtins.property
  def rhs(self) -> _ods_ir.Value:
    return self.operation.operands[1]

  @builtins.property
  def fastmath(self) -> _ods_ir.Attribute:
    return self.operation.attributes["fastmath"]

  @fastmath.setter
  def fastmath(self, value: _ods_ir.Attribute):
    if value is None:
      raise ValueError("'None' not allowed as value for mandatory attributes")
    self.operation.attributes["fastmath"] = value

  @builtins.property
  def result(self) -> _ods_ir.OpResult:
    return self.operation.results[0]
```

https://github.com/llvm/llvm-project/pull/176640


More information about the Mlir-commits mailing list