mirror of
https://github.com/intel/llvm.git
synced 2026-01-13 11:02:04 +08:00
In this PR we add basic python bindings for IRDL dialect, so that python
users can create and load IRDL dialects in python. This allows users, to
some extent, to define dialects in Python without having to modify
MLIR’s CMake/TableGen/C++ code and rebuild, making prototyping more
convenient.
A basic example is shown below (and also in the added test case):
```python
# create a module with IRDL dialects
module = Module.create()
with InsertionPoint(module.body):
dialect = irdl.DialectOp("irdl_test")
with InsertionPoint(dialect.body):
op = irdl.OperationOp("test_op")
with InsertionPoint(op.body):
f32 = irdl.is_(TypeAttr.get(F32Type.get()))
irdl.operands_([f32], ["input"], [irdl.Variadicity.single])
# load the module
irdl.load_dialects(module)
# use the op defined in IRDL
m = Module.parse("""
module {
%a = arith.constant 1.0 : f32
"irdl_test.test_op"(%a) : (f32) -> ()
}
""")
```
36 lines
1.1 KiB
C++
36 lines
1.1 KiB
C++
//===--- 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);
|
|
}
|