-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR][Python] Add python bindings for IRDL dialect #158488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+236
−6
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8cbbb6b
[MLIR][Python] Add python bindings for IRDL dialect
PragmaTwice 9937529
format
PragmaTwice 5bafd42
format
PragmaTwice fc7728d
refine test case
PragmaTwice deb2db1
Merge branch 'main' into mlir-python-irdl
PragmaTwice df14b40
add class for typeop and attributeop
PragmaTwice 4357853
apply review suggestions
PragmaTwice 0cbf520
remove useless using namespace
PragmaTwice e76fa65
make it 80 characters
PragmaTwice 0c7af71
format
PragmaTwice 4be1ac3
add type annotations
PragmaTwice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
//===--- DialectIRDL.cpp - Pybind module for IRDL dialect API support ---===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir-c/Dialect/IRDL.h" | ||
#include "mlir-c/IR.h" | ||
#include "mlir-c/Support.h" | ||
#include "mlir/Bindings/Python/Nanobind.h" | ||
#include "mlir/Bindings/Python/NanobindAdaptors.h" | ||
|
||
namespace nb = nanobind; | ||
using namespace mlir; | ||
using namespace mlir::python; | ||
using namespace mlir::python::nanobind_adaptors; | ||
|
||
static void populateDialectIRDLSubmodule(nb::module_ &m) { | ||
m.def( | ||
"load_dialects", | ||
[](MlirModule module) { | ||
if (mlirLogicalResultIsFailure(mlirLoadIRDLDialects(module))) | ||
throw std::runtime_error( | ||
"failed to load IRDL dialects from the input module"); | ||
}, | ||
nb::arg("module"), "Load IRDL dialects from the given module."); | ||
} | ||
|
||
NB_MODULE(_mlirDialectsIRDL, m) { | ||
m.doc() = "MLIR IRDL dialect."; | ||
|
||
populateDialectIRDLSubmodule(m); | ||
} |
This file contains hidden or 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 hidden or 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,14 @@ | ||
//===-- IRDLOps.td - Entry point for IRDL binding ----------*- 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_BINDINGS_IRDL_OPS | ||
#define PYTHON_BINDINGS_IRDL_OPS | ||
|
||
include "mlir/Dialect/IRDL/IR/IRDLOps.td" | ||
|
||
#endif |
This file contains hidden or 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,92 @@ | ||
# 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 | ||
|
||
from ._irdl_ops_gen import * | ||
from ._irdl_ops_gen import _Dialect | ||
from ._irdl_enum_gen import * | ||
from .._mlir_libs._mlirDialectsIRDL import * | ||
from ..ir import register_attribute_builder | ||
from ._ods_common import _cext as _ods_cext | ||
from typing import Union, Sequence | ||
|
||
_ods_ir = _ods_cext.ir | ||
|
||
|
||
@_ods_cext.register_operation(_Dialect, replace=True) | ||
class DialectOp(DialectOp): | ||
__doc__ = DialectOp.__doc__ | ||
|
||
def __init__(self, sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None): | ||
super().__init__(sym_name, loc=loc, ip=ip) | ||
self.regions[0].blocks.append() | ||
|
||
@property | ||
def body(self) -> _ods_ir.Block: | ||
return self.regions[0].blocks[0] | ||
|
||
|
||
def dialect(sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None) -> DialectOp: | ||
return DialectOp(sym_name=sym_name, loc=loc, ip=ip) | ||
|
||
|
||
@_ods_cext.register_operation(_Dialect, replace=True) | ||
class OperationOp(OperationOp): | ||
__doc__ = OperationOp.__doc__ | ||
|
||
def __init__(self, sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None): | ||
super().__init__(sym_name, loc=loc, ip=ip) | ||
self.regions[0].blocks.append() | ||
|
||
@property | ||
def body(self) -> _ods_ir.Block: | ||
return self.regions[0].blocks[0] | ||
|
||
|
||
def operation_( | ||
sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None | ||
) -> OperationOp: | ||
return OperationOp(sym_name=sym_name, loc=loc, ip=ip) | ||
|
||
|
||
@_ods_cext.register_operation(_Dialect, replace=True) | ||
class TypeOp(TypeOp): | ||
__doc__ = TypeOp.__doc__ | ||
|
||
def __init__(self, sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None): | ||
super().__init__(sym_name, loc=loc, ip=ip) | ||
self.regions[0].blocks.append() | ||
|
||
@property | ||
def body(self) -> _ods_ir.Block: | ||
return self.regions[0].blocks[0] | ||
|
||
|
||
def type_(sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None) -> TypeOp: | ||
return TypeOp(sym_name=sym_name, loc=loc, ip=ip) | ||
|
||
|
||
@_ods_cext.register_operation(_Dialect, replace=True) | ||
class AttributeOp(AttributeOp): | ||
__doc__ = AttributeOp.__doc__ | ||
|
||
def __init__(self, sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None): | ||
super().__init__(sym_name, loc=loc, ip=ip) | ||
self.regions[0].blocks.append() | ||
|
||
@property | ||
def body(self) -> _ods_ir.Block: | ||
return self.regions[0].blocks[0] | ||
|
||
|
||
def attribute( | ||
sym_name: Union[str, _ods_ir.Attribute], *, loc=None, ip=None | ||
) -> AttributeOp: | ||
return AttributeOp(sym_name=sym_name, loc=loc, ip=ip) | ||
|
||
|
||
@register_attribute_builder("VariadicityArrayAttr") | ||
def _variadicity_array_attr(x: Sequence[Variadicity], context) -> _ods_ir.Attribute: | ||
return _ods_ir.Attribute.parse( | ||
f"#irdl<variadicity_array [{', '.join(str(i) for i in x)}]>", context | ||
) |
This file contains hidden or 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,66 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
from mlir.ir import * | ||
from mlir.dialects.irdl import * | ||
import sys | ||
|
||
|
||
def run(f): | ||
print("\nTEST:", f.__name__, file=sys.stderr) | ||
f() | ||
|
||
|
||
# CHECK: TEST: testIRDL | ||
@run | ||
def testIRDL(): | ||
with Context() as ctx, Location.unknown(): | ||
module = Module.create() | ||
with InsertionPoint(module.body): | ||
irdl_test = dialect("irdl_test") | ||
with InsertionPoint(irdl_test.body): | ||
op = operation_("test_op") | ||
with InsertionPoint(op.body): | ||
f32 = is_(TypeAttr.get(F32Type.get())) | ||
operands_([f32], ["input"], [Variadicity.single]) | ||
type1 = type_("type1") | ||
with InsertionPoint(type1.body): | ||
f32 = is_(TypeAttr.get(F32Type.get())) | ||
parameters([f32], ["val"]) | ||
attr1 = attribute("attr1") | ||
with InsertionPoint(attr1.body): | ||
test = is_(StringAttr.get("test")) | ||
parameters([test], ["val"]) | ||
|
||
# CHECK: module { | ||
# CHECK: irdl.dialect @irdl_test { | ||
# CHECK: irdl.operation @test_op { | ||
# CHECK: %0 = irdl.is f32 | ||
# CHECK: irdl.operands(input: %0) | ||
# CHECK: } | ||
# CHECK: irdl.type @type1 { | ||
# CHECK: %0 = irdl.is f32 | ||
# CHECK: irdl.parameters(val: %0) | ||
# CHECK: } | ||
# CHECK: irdl.attribute @attr1 { | ||
# CHECK: %0 = irdl.is "test" | ||
# CHECK: irdl.parameters(val: %0) | ||
# CHECK: } | ||
# CHECK: } | ||
# CHECK: } | ||
module.operation.verify() | ||
module.dump() | ||
|
||
load_dialects(module) | ||
|
||
m = Module.parse( | ||
""" | ||
module { | ||
%a = arith.constant 1.0 : f32 | ||
"irdl_test.test_op"(%a) : (f32) -> () | ||
} | ||
""" | ||
) | ||
# CHECK: module { | ||
# CHECK: "irdl_test.test_op"(%cst) : (f32) -> () | ||
# CHECK: } | ||
m.dump() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.