Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mlir] Make attributes mutable in Python bindings
Attributes represent additional data about an operation and are intended to be modifiable during the lifetime of the operation. In the dialect-specific Python bindings, attributes are exposed as properties on the operation class. Allow for assigning values to these properties. Also support creating new and deleting existing attributes through the generic "attributes" property of an operation. Any validity checking must be performed by the op verifier after the mutation, similarly to C++. Operations are not invalidated in the process: no dangling pointers can be created as all attributes are owned by the context and will remain live even if they are not used in any operation. Introduce a Python Test dialect by analogy with the Test dialect and to avoid polluting the latter with Python-specific constructs. Use this dialect to implement a test for the attribute access and mutation API. Reviewed By: stellaraccident, mehdi_amini Differential Revision: https://reviews.llvm.org/D91652
- Loading branch information
Showing
8 changed files
with
253 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
if(MLIR_BINDINGS_PYTHON_ENABLED) | ||
add_subdirectory(Python) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
include(AddMLIRPythonExtension) | ||
add_mlir_dialect_python_bindings(MLIRBindingsPythonTestOps | ||
python_test_ops.td | ||
python_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# RUN: %PYTHON %s | FileCheck %s | ||
|
||
from mlir.ir import * | ||
import mlir.dialects.python_test as test | ||
|
||
def run(f): | ||
print("\nTEST:", f.__name__) | ||
f() | ||
|
||
# CHECK-LABEL: TEST: testAttributes | ||
def testAttributes(): | ||
with Context(), Location.unknown(): | ||
# | ||
# Check op construction with attributes. | ||
# | ||
|
||
i32 = IntegerType.get_signless(32) | ||
one = IntegerAttr.get(i32, 1) | ||
two = IntegerAttr.get(i32, 2) | ||
unit = UnitAttr.get() | ||
|
||
# CHECK: "python_test.attributed_op"() { | ||
# CHECK-DAG: mandatory_i32 = 1 : i32 | ||
# CHECK-DAG: optional_i32 = 2 : i32 | ||
# CHECK-DAG: unit | ||
# CHECK: } | ||
op = test.AttributedOp(one, two, unit) | ||
print(f"{op}") | ||
|
||
# CHECK: "python_test.attributed_op"() { | ||
# CHECK: mandatory_i32 = 2 : i32 | ||
# CHECK: } | ||
op2 = test.AttributedOp(two, None, None) | ||
print(f"{op2}") | ||
|
||
# | ||
# Check generic "attributes" access and mutation. | ||
# | ||
|
||
assert "additional" not in op.attributes | ||
|
||
# CHECK: "python_test.attributed_op"() { | ||
# CHECK-DAG: additional = 1 : i32 | ||
# CHECK-DAG: mandatory_i32 = 2 : i32 | ||
# CHECK: } | ||
op2.attributes["additional"] = one | ||
print(f"{op2}") | ||
|
||
# CHECK: "python_test.attributed_op"() { | ||
# CHECK-DAG: additional = 2 : i32 | ||
# CHECK-DAG: mandatory_i32 = 2 : i32 | ||
# CHECK: } | ||
op2.attributes["additional"] = two | ||
print(f"{op2}") | ||
|
||
# CHECK: "python_test.attributed_op"() { | ||
# CHECK-NOT: additional = 2 : i32 | ||
# CHECK: mandatory_i32 = 2 : i32 | ||
# CHECK: } | ||
del op2.attributes["additional"] | ||
print(f"{op2}") | ||
|
||
try: | ||
print(op.attributes["additional"]) | ||
except KeyError: | ||
pass | ||
else: | ||
assert False, "expected KeyError on unknown attribute key" | ||
|
||
# | ||
# Check accessors to defined attributes. | ||
# | ||
|
||
# CHECK: Mandatory: 1 | ||
# CHECK: Optional: 2 | ||
# CHECK: Unit: True | ||
print(f"Mandatory: {op.mandatory_i32.value}") | ||
print(f"Optional: {op.optional_i32.value}") | ||
print(f"Unit: {op.unit}") | ||
|
||
# CHECK: Mandatory: 2 | ||
# CHECK: Optional: None | ||
# CHECK: Unit: False | ||
print(f"Mandatory: {op2.mandatory_i32.value}") | ||
print(f"Optional: {op2.optional_i32}") | ||
print(f"Unit: {op2.unit}") | ||
|
||
# CHECK: Mandatory: 2 | ||
# CHECK: Optional: None | ||
# CHECK: Unit: False | ||
op.mandatory_i32 = two | ||
op.optional_i32 = None | ||
op.unit = False | ||
print(f"Mandatory: {op.mandatory_i32.value}") | ||
print(f"Optional: {op.optional_i32}") | ||
print(f"Unit: {op.unit}") | ||
assert "optional_i32" not in op.attributes | ||
assert "unit" not in op.attributes | ||
|
||
try: | ||
op.mandatory_i32 = None | ||
except ValueError: | ||
pass | ||
else: | ||
assert False, "expected ValueError on setting a mandatory attribute to None" | ||
|
||
# CHECK: Optional: 2 | ||
op.optional_i32 = two | ||
print(f"Optional: {op.optional_i32.value}") | ||
|
||
# CHECK: Optional: None | ||
del op.optional_i32 | ||
print(f"Optional: {op.optional_i32}") | ||
|
||
# CHECK: Unit: False | ||
op.unit = None | ||
print(f"Unit: {op.unit}") | ||
assert "unit" not in op.attributes | ||
|
||
# CHECK: Unit: True | ||
op.unit = True | ||
print(f"Unit: {op.unit}") | ||
|
||
# CHECK: Unit: False | ||
del op.unit | ||
print(f"Unit: {op.unit}") | ||
|
||
run(testAttributes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
if not config.enable_bindings_python: | ||
config.unsupported = True | ||
config.excludes.add('python_test_ops.td') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===-- python_test_ops.td - Python test Op definitions ----*- tablegen -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef PYTHON_TEST_OPS | ||
#define PYTHON_TEST_OPS | ||
|
||
include "mlir/IR/OpBase.td" | ||
include "../../../lib/Bindings/Python/Attributes.td" | ||
|
||
def Python_Test_Dialect : Dialect { | ||
let name = "python_test"; | ||
let cppNamespace = "PythonTest"; | ||
} | ||
class TestOp<string mnemonic, list<OpTrait> traits = []> | ||
: Op<Python_Test_Dialect, mnemonic, traits>; | ||
|
||
def AttributedOp : TestOp<"attributed_op"> { | ||
let arguments = (ins I32Attr:$mandatory_i32, | ||
OptionalAttr<I32Attr>:$optional_i32, | ||
UnitAttr:$unit); | ||
} | ||
|
||
#endif // PYTHON_TEST_OPS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters