diff --git a/mlir/lib/Bindings/Python/BuiltinOps.td b/mlir/lib/Bindings/Python/BuiltinOps.td new file mode 100644 --- /dev/null +++ b/mlir/lib/Bindings/Python/BuiltinOps.td @@ -0,0 +1,15 @@ +//===-- LinalgOps.td - Entry point for linalg bind ---------*- 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_BUILTIN_OPS +#define PYTHON_BINDINGS_BUILTIN_OPS + +include "mlir/Bindings/Python/Attributes.td" +include "mlir/IR/BuiltinOps.td" + +#endif diff --git a/mlir/lib/Bindings/Python/CMakeLists.txt b/mlir/lib/Bindings/Python/CMakeLists.txt --- a/mlir/lib/Bindings/Python/CMakeLists.txt +++ b/mlir/lib/Bindings/Python/CMakeLists.txt @@ -11,6 +11,7 @@ mlir/ir.py mlir/dialects/__init__.py mlir/dialects/_linalg.py + mlir/dialects/_builtin.py mlir/ir.py mlir/passmanager.py mlir/transforms/__init__.py @@ -36,6 +37,11 @@ # Generate dialect-specific bindings. ################################################################################ +add_mlir_dialect_python_bindings(MLIRBindingsPythonBuiltinOps + TD_FILE BuiltinOps.td + DIALECT_NAME builtin) +add_dependencies(MLIRBindingsPythonSources MLIRBindingsPythonBuiltinOps) + add_mlir_dialect_python_bindings(MLIRBindingsPythonLinalgOps TD_FILE LinalgOps.td DIALECT_NAME linalg diff --git a/mlir/lib/Bindings/Python/mlir/dialects/_builtin.py b/mlir/lib/Bindings/Python/mlir/dialects/_builtin.py new file mode 100644 --- /dev/null +++ b/mlir/lib/Bindings/Python/mlir/dialects/_builtin.py @@ -0,0 +1,41 @@ +# 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 mlir.ir import * + +class ModuleOp: + """Specialization for the module op class.""" + + def __init__(self, loc=None, ip=None): + super().__init__( + self._ods_build_default(operands=[], + results=[], + loc=loc, + ip=ip)) + body = self.regions[0].blocks.append() + with InsertionPoint(body): + Operation.create("module_terminator") + + @property + def body(self): + return self.regions[0].blocks[0] + +class FuncOp: + """Specialization for the func op class.""" + + def __init__(self, name, type, visibility, loc=None, ip=None): + sym_name = StringAttr.get(str(name)) + type = TypeAttr.get(type) + sym_visibility = None + if visibility is not None: sym_visibility = StringAttr.get(str(visibility)) + super().__init__(sym_name, type, sym_visibility, loc, ip) + + @property + def body(self): + return self.regions[0].blocks[0] + +def select_opview_mixin(parent_opview_cls): + if (parent_opview_cls.OPERATION_NAME == "module"): + return ModuleOp + if (parent_opview_cls.OPERATION_NAME == "func"): + return FuncOp diff --git a/mlir/test/Bindings/Python/dialects/builtin.py b/mlir/test/Bindings/Python/dialects/builtin.py new file mode 100644 --- /dev/null +++ b/mlir/test/Bindings/Python/dialects/builtin.py @@ -0,0 +1,35 @@ +# RUN: %PYTHON %s | FileCheck %s + +from mlir.ir import * +import mlir.dialects.builtin as builtin +import mlir.dialects.std as std + +def run(f): + print("\nTEST:", f.__name__) + f() + +# CHECK-LABEL: TEST: testBuildFuncOp +def testBuildFuncOp(): + ctx = Context() + with Location.unknown(ctx) as loc: + m = builtin.ModuleOp() + + f32 = F32Type.get() + tensor_type = RankedTensorType.get((2, 3, 4), f32) + with InsertionPoint.at_block_begin(m.body): + func_name = StringAttr.get("some_func") + func = builtin.FuncOp(name="some_func", + type=FunctionType.get(inputs=[tensor_type, tensor_type], results=[tensor_type]), + visibility="nested") + func.regions[0].blocks.append(tensor_type, tensor_type) + with InsertionPoint(func.body): + std.ReturnOp([func.body.arguments[0]]) + pass + # CHECK: module { + # CHECK: func nested @some_func(%arg0: tensor<2x3x4xf32>, %arg1: tensor<2x3x4xf32>) -> tensor<2x3x4xf32> { + # CHECK: return %arg0 : tensor<2x3x4xf32> + # CHECK: } + print(m) + + +run(testBuildFuncOp) diff --git a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp --- a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp +++ b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp @@ -716,6 +716,10 @@ os << llvm::formatv(fileHeader, clDialectName.getValue()); os << llvm::formatv(dialectClassTemplate, clDialectName.getValue()); + + if (clDialectName == "builtin") + clDialectName = ""; + for (const llvm::Record *rec : records.getAllDerivedDefinitions("Op")) { Operator op(rec); if (op.getDialectName() == clDialectName.getValue())