From ec5def5e20f6ae9fe8cc30e5ee152d4b239e1e95 Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Mon, 1 Aug 2022 08:52:41 +0000 Subject: [PATCH] Fix MLIR Python binding for arith.constant after argument has been changed to an interface e1795322844c 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 --- mlir/python/mlir/dialects/_arith_ops_ext.py | 4 ++++ mlir/test/python/dialects/arith_dialect.py | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 mlir/test/python/dialects/arith_dialect.py diff --git a/mlir/python/mlir/dialects/_arith_ops_ext.py b/mlir/python/mlir/dialects/_arith_ops_ext.py index c755df2..2408593 100644 --- a/mlir/python/mlir/dialects/_arith_ops_ext.py +++ b/mlir/python/mlir/dialects/_arith_ops_ext.py @@ -61,6 +61,10 @@ class ConstantOp: 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): return IntegerAttr(self.value).value diff --git a/mlir/test/python/dialects/arith_dialect.py b/mlir/test/python/dialects/arith_dialect.py new file mode 100644 index 0000000..acae9b6 --- /dev/null +++ b/mlir/test/python/dialects/arith_dialect.py @@ -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) -- 2.7.4