From 3757a9dbe8e06a0ae7f1c6cf7b6f9e63db659ef9 Mon Sep 17 00:00:00 2001 From: TGMM Date: Tue, 10 Dec 2024 16:02:30 -0800 Subject: [PATCH 1/3] [mlir][emitc] Add support for C-API/python binding to EmitC dialect --- mlir/include/mlir-c/Dialect/EmitC.h | 26 ++++++++++++++++ mlir/lib/CAPI/Dialect/CMakeLists.txt | 9 ++++++ mlir/lib/CAPI/Dialect/EmitC.cpp | 13 ++++++++ mlir/python/CMakeLists.txt | 7 +++++ mlir/python/mlir/dialects/EmitC.td | 14 +++++++++ mlir/python/mlir/dialects/emitc.py | 5 +++ mlir/test/python/dialects/emitc_dialect.py | 31 +++++++++++++++++++ .../mlir/python/BUILD.bazel | 31 +++++++++++++++++++ 8 files changed, 136 insertions(+) create mode 100644 mlir/include/mlir-c/Dialect/EmitC.h create mode 100644 mlir/lib/CAPI/Dialect/EmitC.cpp create mode 100644 mlir/python/mlir/dialects/EmitC.td create mode 100644 mlir/python/mlir/dialects/emitc.py create mode 100644 mlir/test/python/dialects/emitc_dialect.py diff --git a/mlir/include/mlir-c/Dialect/EmitC.h b/mlir/include/mlir-c/Dialect/EmitC.h new file mode 100644 index 0000000000000..82e698344bf1e --- /dev/null +++ b/mlir/include/mlir-c/Dialect/EmitC.h @@ -0,0 +1,26 @@ +//===-- mlir-c/Dialect/EmitC.h - C API for EmitC dialect ----------*- C -*-===// +// +// 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 MLIR_C_DIALECT_EmitC_H +#define MLIR_C_DIALECT_EmitC_H + +#include "mlir-c/IR.h" +#include "mlir-c/Support.h" + +#ifdef __cplusplus +extern "C" { +#endif + +MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(EmitC, emitc); + +#ifdef __cplusplus +} +#endif + +#endif // MLIR_C_DIALECT_EmitC_H diff --git a/mlir/lib/CAPI/Dialect/CMakeLists.txt b/mlir/lib/CAPI/Dialect/CMakeLists.txt index 4e141b60ff8cc..5ad4bafedf6c4 100644 --- a/mlir/lib/CAPI/Dialect/CMakeLists.txt +++ b/mlir/lib/CAPI/Dialect/CMakeLists.txt @@ -40,6 +40,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIControlFlow MLIRControlFlowDialect ) +add_mlir_upstream_c_api_library(MLIRCAPIEmitC + EmitC.cpp + + PARTIAL_SOURCES_INTENDED + LINK_LIBS PUBLIC + MLIRCAPIIR + MLIREmitCDialect +) + add_mlir_upstream_c_api_library(MLIRCAPIMath Math.cpp diff --git a/mlir/lib/CAPI/Dialect/EmitC.cpp b/mlir/lib/CAPI/Dialect/EmitC.cpp new file mode 100644 index 0000000000000..3dcb7038a5798 --- /dev/null +++ b/mlir/lib/CAPI/Dialect/EmitC.cpp @@ -0,0 +1,13 @@ +//===- EmitC.cpp - C Interface for EmitC dialect --------------------------===// +// +// 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/EmitC.h" +#include "mlir/CAPI/Registration.h" +#include "mlir/Dialect/EmitC/IR/EmitC.h" + +MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(EmitC, emitc, mlir::emitc::EmitCDialect) diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt index e1b870b53ad25..6af5433fe946b 100644 --- a/mlir/python/CMakeLists.txt +++ b/mlir/python/CMakeLists.txt @@ -352,6 +352,13 @@ declare_mlir_python_sources( dialects/quant.py _mlir_libs/_mlir/dialects/quant.pyi) +declare_mlir_dialect_python_bindings( + ADD_TO_PARENT MLIRPythonSources.Dialects + ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir" + TD_FILE dialects/EmitC.td + SOURCES dialects/emitc.py + DIALECT_NAME emitc) + declare_mlir_dialect_python_bindings( ADD_TO_PARENT MLIRPythonSources.Dialects ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir" diff --git a/mlir/python/mlir/dialects/EmitC.td b/mlir/python/mlir/dialects/EmitC.td new file mode 100644 index 0000000000000..ff0a56d155014 --- /dev/null +++ b/mlir/python/mlir/dialects/EmitC.td @@ -0,0 +1,14 @@ +//===-- EmitC.td - Entry point for EmitC 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_EMITC +#define PYTHON_BINDINGS_EMITC + +include "mlir/Dialect/EmitC/IR/EmitC.td" + +#endif diff --git a/mlir/python/mlir/dialects/emitc.py b/mlir/python/mlir/dialects/emitc.py new file mode 100644 index 0000000000000..99c3286e576f1 --- /dev/null +++ b/mlir/python/mlir/dialects/emitc.py @@ -0,0 +1,5 @@ +# 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 ._emitc_ops_gen import * diff --git a/mlir/test/python/dialects/emitc_dialect.py b/mlir/test/python/dialects/emitc_dialect.py new file mode 100644 index 0000000000000..0c42c2d4084f1 --- /dev/null +++ b/mlir/test/python/dialects/emitc_dialect.py @@ -0,0 +1,31 @@ +# RUN: %PYTHON %s | FileCheck %s + +from mlir.ir import * +import mlir.dialects.emitc as emitc + + +def run(f): + print("\nTEST:", f.__name__) + with Context() as ctx, Location.unknown(): + module = Module.create() + with InsertionPoint(module.body): + f(ctx) + print(module) + + +# CHECK-LABEL: TEST: testConstantOp +@run +def testConstantOp(ctx): + i32 = IntegerType.get_signless(32) + a = emitc.ConstantOp(result=i32, value=IntegerAttr.get(i32, 42)) + # CHECK: %{{.*}} = "emitc.constant"() <{value = 42 : i32}> : () -> i32 + + +# CHECK-LABEL: TEST: testAddOp +@run +def testAddOp(ctx): + i32 = IntegerType.get_signless(32) + lhs = emitc.ConstantOp(result=i32, value=IntegerAttr.get(i32, 0)) + rhs = emitc.ConstantOp(result=i32, value=IntegerAttr.get(i32, 0)) + a = emitc.AddOp(i32, lhs, rhs) + # CHECK: %{{.*}} = emitc.add %{{.*}}, %{{.*}} : (i32, i32) -> i32 diff --git a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel index 254cab0db4a5d..2b3971a508804 100644 --- a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel @@ -227,6 +227,37 @@ filegroup( ], ) +##---------------------------------------------------------------------------## +# EmitC dialect. +##---------------------------------------------------------------------------## + +gentbl_filegroup( + name = "EmitCPyGen", + tbl_outs = [ + ( + [ + "-gen-python-op-bindings", + "-bind-dialect=emitc", + ], + "mlir/dialects/_emitc_ops_gen.py", + ), + ], + tblgen = "//mlir:mlir-tblgen", + td_file = "mlir/dialects/EmitC.td", + deps = [ + "//mlir:OpBaseTdFiles", + "//mlir:EmitCTdFiles", + ], +) + +filegroup( + name = "EmitCPyFiles", + srcs = [ + "mlir/dialects/emitc.py", + ":EmitCPyGen", + ], +) + ##---------------------------------------------------------------------------## # Linalg dialect. ##---------------------------------------------------------------------------## From 83b6647544dcf6758a1cbc31ba0beb2a34bb9415 Mon Sep 17 00:00:00 2001 From: TGMM Date: Tue, 10 Dec 2024 23:09:40 -0800 Subject: [PATCH 2/3] Removed emitc bazel config --- .../mlir/python/BUILD.bazel | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel index 2b3971a508804..254cab0db4a5d 100644 --- a/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/mlir/python/BUILD.bazel @@ -227,37 +227,6 @@ filegroup( ], ) -##---------------------------------------------------------------------------## -# EmitC dialect. -##---------------------------------------------------------------------------## - -gentbl_filegroup( - name = "EmitCPyGen", - tbl_outs = [ - ( - [ - "-gen-python-op-bindings", - "-bind-dialect=emitc", - ], - "mlir/dialects/_emitc_ops_gen.py", - ), - ], - tblgen = "//mlir:mlir-tblgen", - td_file = "mlir/dialects/EmitC.td", - deps = [ - "//mlir:OpBaseTdFiles", - "//mlir:EmitCTdFiles", - ], -) - -filegroup( - name = "EmitCPyFiles", - srcs = [ - "mlir/dialects/emitc.py", - ":EmitCPyGen", - ], -) - ##---------------------------------------------------------------------------## # Linalg dialect. ##---------------------------------------------------------------------------## From c40110898a7d0b0bdac18cab3b6c90edbdf40493 Mon Sep 17 00:00:00 2001 From: TGMM Date: Tue, 10 Dec 2024 23:11:46 -0800 Subject: [PATCH 3/3] Fixed indenting --- mlir/python/CMakeLists.txt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt index 6af5433fe946b..10866c11bdb71 100644 --- a/mlir/python/CMakeLists.txt +++ b/mlir/python/CMakeLists.txt @@ -353,11 +353,12 @@ declare_mlir_python_sources( _mlir_libs/_mlir/dialects/quant.pyi) declare_mlir_dialect_python_bindings( - ADD_TO_PARENT MLIRPythonSources.Dialects - ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir" - TD_FILE dialects/EmitC.td - SOURCES dialects/emitc.py - DIALECT_NAME emitc) + ADD_TO_PARENT MLIRPythonSources.Dialects + ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir" + TD_FILE dialects/EmitC.td + SOURCES + dialects/emitc.py + DIALECT_NAME emitc) declare_mlir_dialect_python_bindings( ADD_TO_PARENT MLIRPythonSources.Dialects