[Mlir-commits] [mlir] 2292fd0 - [mlir][spirv] Add support for C-API/python binding to SPIR-V dialect (#76055)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Jan 2 08:11:48 PST 2024


Author: Jungwook Park
Date: 2024-01-02T08:11:44-08:00
New Revision: 2292fd0129362865d07777329fa38850d7a642a3

URL: https://github.com/llvm/llvm-project/commit/2292fd0129362865d07777329fa38850d7a642a3
DIFF: https://github.com/llvm/llvm-project/commit/2292fd0129362865d07777329fa38850d7a642a3.diff

LOG: [mlir][spirv] Add support for C-API/python binding to SPIR-V dialect (#76055)

Enable bindings.

---------

Co-authored-by: jungpark-mlir <jungwook at jungwook-22.04>

Added: 
    mlir/include/mlir-c/Dialect/SPIRV.h
    mlir/lib/CAPI/Dialect/SPIRV.cpp
    mlir/python/mlir/dialects/SPIRVOps.td
    mlir/python/mlir/dialects/spirv.py
    mlir/test/python/dialects/spirv_dialect.py

Modified: 
    mlir/lib/CAPI/Dialect/CMakeLists.txt
    mlir/python/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir-c/Dialect/SPIRV.h b/mlir/include/mlir-c/Dialect/SPIRV.h
new file mode 100644
index 00000000000000..f22708c9db0433
--- /dev/null
+++ b/mlir/include/mlir-c/Dialect/SPIRV.h
@@ -0,0 +1,26 @@
+//===-- mlir-c/Dialect/SPIRV.h - C API for SPIRV 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_SPIRV_H
+#define MLIR_C_DIALECT_SPIRV_H
+
+#include "mlir-c/IR.h"
+#include "mlir-c/Support.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(SPIRV, spirv);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // MLIR_C_DIALECT_SPIRV_H

diff  --git a/mlir/lib/CAPI/Dialect/CMakeLists.txt b/mlir/lib/CAPI/Dialect/CMakeLists.txt
index d815eba48d9b9d..b2952da17a41c0 100644
--- a/mlir/lib/CAPI/Dialect/CMakeLists.txt
+++ b/mlir/lib/CAPI/Dialect/CMakeLists.txt
@@ -171,6 +171,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIFunc
   MLIRFuncDialect
 )
 
+add_mlir_upstream_c_api_library(MLIRCAPISPIRV
+  SPIRV.cpp
+
+  PARTIAL_SOURCES_INTENDED
+  LINK_LIBS PUBLIC
+  MLIRCAPIIR
+  MLIRSPIRVDialect
+)
+
 add_mlir_upstream_c_api_library(MLIRCAPITensor
   Tensor.cpp
 

diff  --git a/mlir/lib/CAPI/Dialect/SPIRV.cpp b/mlir/lib/CAPI/Dialect/SPIRV.cpp
new file mode 100644
index 00000000000000..9bfe26b9598e23
--- /dev/null
+++ b/mlir/lib/CAPI/Dialect/SPIRV.cpp
@@ -0,0 +1,13 @@
+//===- SPIRV.cpp - C Interface for SPIRV 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/SPIRV.h"
+#include "mlir/CAPI/Registration.h"
+#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
+
+MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(SPIRV, spirv, mlir::spirv::SPIRVDialect)

diff  --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt
index 3c9cf304d88a27..266b86090fe174 100644
--- a/mlir/python/CMakeLists.txt
+++ b/mlir/python/CMakeLists.txt
@@ -378,6 +378,13 @@ declare_mlir_dialect_python_bindings(
     "../../include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td"
 )
 
+declare_mlir_dialect_python_bindings(
+    ADD_TO_PARENT MLIRPythonSources.Dialects
+    ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
+    TD_FILE dialects/SPIRVOps.td
+    SOURCES dialects/spirv.py
+    DIALECT_NAME spirv)
+
 declare_mlir_dialect_python_bindings(
   ADD_TO_PARENT MLIRPythonSources.Dialects
   ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"

diff  --git a/mlir/python/mlir/dialects/SPIRVOps.td b/mlir/python/mlir/dialects/SPIRVOps.td
new file mode 100644
index 00000000000000..eaae0e609d623c
--- /dev/null
+++ b/mlir/python/mlir/dialects/SPIRVOps.td
@@ -0,0 +1,14 @@
+//===-- SPIRVOps.td - Entry point for SPIRVOps 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_SPIRV_OPS
+#define PYTHON_BINDINGS_SPIRV_OPS
+
+include "mlir/Dialect/SPIRV/IR/SPIRVOps.td"
+
+#endif

diff  --git a/mlir/python/mlir/dialects/spirv.py b/mlir/python/mlir/dialects/spirv.py
new file mode 100644
index 00000000000000..269678a2032eb9
--- /dev/null
+++ b/mlir/python/mlir/dialects/spirv.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 ._spirv_ops_gen import *

diff  --git a/mlir/test/python/dialects/spirv_dialect.py b/mlir/test/python/dialects/spirv_dialect.py
new file mode 100644
index 00000000000000..d5b9e6cedb5d31
--- /dev/null
+++ b/mlir/test/python/dialects/spirv_dialect.py
@@ -0,0 +1,21 @@
+# RUN: %PYTHON %s | FileCheck %s
+
+from mlir.ir import *
+import mlir.dialects.spirv as spirv
+
+
+def run(f):
+    print("\nTEST:", f.__name__)
+    f()
+
+
+# CHECK-LABEL: TEST: testConstantOp
+ at run
+def testConstantOps():
+    with Context() as ctx, Location.unknown():
+        module = Module.create()
+        with InsertionPoint(module.body):
+            i32 = IntegerType.get_signless(32)
+            spirv.ConstantOp(value=IntegerAttr.get(i32, 42), constant=i32)
+        # CHECK: spirv.Constant 42 : i32
+        print(module)


        


More information about the Mlir-commits mailing list