Skip to content

Commit

Permalink
Fix MLIR Python binding for arith.constant after argument has been ch…
Browse files Browse the repository at this point in the history
…anged to an interface

e179532 removed the Type field from attributes and
arith::ConstantOp argument is now a TypedAttrInterface which isn't
supported by the python generator.
This patch temporarily restore the functionality for arith.constant but
won't generalize: we need to work on the generator instead.

Differential Revision: https://reviews.llvm.org/D130878
  • Loading branch information
joker-eph committed Aug 1, 2022
1 parent 6e1ba62 commit ec5def5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mlir/python/mlir/dialects/_arith_ops_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def create_index(cls, value: int, *, loc=None, ip=None):
def type(self):
return self.results[0].type

@property
def value(self):
return Attribute(self.operation.attributes["value"])

@property
def literal_value(self) -> Union[int, float]:
if _is_integer_like_type(self.type):
Expand Down
19 changes: 19 additions & 0 deletions mlir/test/python/dialects/arith_dialect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# RUN: %PYTHON %s | FileCheck %s

from mlir.ir import *
import mlir.dialects.func as func
import mlir.dialects.arith as arith

def run(f):
print("\nTEST:", f.__name__)
f()

# CHECK-LABEL: TEST: testConstantOp
@run
def testConstantOps():
with Context() as ctx, Location.unknown():
module = Module.create()
with InsertionPoint(module.body):
arith.ConstantOp(value=42.42, result=F32Type.get())
# CHECK: %cst = arith.constant 4.242000e+01 : f32
print(module)

0 comments on commit ec5def5

Please sign in to comment.