[Mlir-commits] [mlir] [mlir][spirv] Add support for C-API/python binding to SPIR-V dialect (PR #76055)
Jungwook Park
llvmlistbot at llvm.org
Thu Dec 21 02:21:10 PST 2023
https://github.com/jungpark-mlir updated https://github.com/llvm/llvm-project/pull/76055
>From 00683b356d214fd80eb640e58d8e15a8755e6685 Mon Sep 17 00:00:00 2001
From: Jungwook Park <jungwook.park at innosilicon.com>
Date: Wed, 20 Dec 2023 13:39:49 +0000
Subject: [PATCH 1/4] [mlir][spirv] Add support for C-API and python binding to
SPIR-V dialect (#5)
Enable bindings.
---
mlir/include/mlir-c/Dialect/SPIRV.h | 26 ++++++++++++++++++++++
mlir/lib/CAPI/Dialect/CMakeLists.txt | 9 ++++++++
mlir/lib/CAPI/Dialect/SPIRV.cpp | 13 +++++++++++
mlir/python/CMakeLists.txt | 7 ++++++
mlir/python/mlir/dialects/SPIRVOps.td | 14 ++++++++++++
mlir/python/mlir/dialects/spirv.py | 5 +++++
mlir/test/python/dialects/spirv_dialect.py | 22 ++++++++++++++++++
7 files changed, 96 insertions(+)
create mode 100644 mlir/include/mlir-c/Dialect/SPIRV.h
create mode 100644 mlir/lib/CAPI/Dialect/SPIRV.cpp
create mode 100644 mlir/python/mlir/dialects/SPIRVOps.td
create mode 100644 mlir/python/mlir/dialects/spirv.py
create mode 100644 mlir/test/python/dialects/spirv_dialect.py
diff --git a/mlir/include/mlir-c/Dialect/SPIRV.h b/mlir/include/mlir-c/Dialect/SPIRV.h
new file mode 100644
index 00000000000000..79d2cdc44fb0f4
--- /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..650c04e439158a
--- /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 41d91cf6778338..28782fe2c46657 100644
--- a/mlir/python/CMakeLists.txt
+++ b/mlir/python/CMakeLists.txt
@@ -371,6 +371,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..f724a1ca880012
--- /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..f00e35494db717
--- /dev/null
+++ b/mlir/test/python/dialects/spirv_dialect.py
@@ -0,0 +1,22 @@
+# 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):
+ spirv.ConstantOp(
+ value=FloatAttr.get(F32Type.get(), 42.42), constant=F32Type.get()
+ )
+ # CHECK: %cst_f32 = spirv.Constant 4.242000e+01 : f32
+ print(module)
>From ec2490438b65889f083bdeb01b2e8e1e4a2194dd Mon Sep 17 00:00:00 2001
From: jungpark-mlir <jungwook at jungwook-22.04>
Date: Wed, 20 Dec 2023 15:57:53 +0000
Subject: [PATCH 2/4] test integer
---
mlir/test/python/dialects/spirv_dialect.py | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/mlir/test/python/dialects/spirv_dialect.py b/mlir/test/python/dialects/spirv_dialect.py
index f00e35494db717..d5b9e6cedb5d31 100644
--- a/mlir/test/python/dialects/spirv_dialect.py
+++ b/mlir/test/python/dialects/spirv_dialect.py
@@ -15,8 +15,7 @@ def testConstantOps():
with Context() as ctx, Location.unknown():
module = Module.create()
with InsertionPoint(module.body):
- spirv.ConstantOp(
- value=FloatAttr.get(F32Type.get(), 42.42), constant=F32Type.get()
- )
- # CHECK: %cst_f32 = spirv.Constant 4.242000e+01 : f32
+ i32 = IntegerType.get_signless(32)
+ spirv.ConstantOp(value=IntegerAttr.get(i32, 42), constant=i32)
+ # CHECK: spirv.Constant 42 : i32
print(module)
>From 5502eebd69eb10949853ba455e7228f92c525ac2 Mon Sep 17 00:00:00 2001
From: jungpark-mlir <jungwook at jungwook-22.04>
Date: Thu, 21 Dec 2023 09:04:09 +0000
Subject: [PATCH 3/4] style fix.
---
mlir/include/mlir-c/Dialect/SPIRV.h | 4 ++--
mlir/lib/CAPI/Dialect/SPIRV.cpp | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/mlir/include/mlir-c/Dialect/SPIRV.h b/mlir/include/mlir-c/Dialect/SPIRV.h
index 79d2cdc44fb0f4..f22708c9db0433 100644
--- a/mlir/include/mlir-c/Dialect/SPIRV.h
+++ b/mlir/include/mlir-c/Dialect/SPIRV.h
@@ -1,11 +1,11 @@
-//===-- mlir-c/Dialect/SPIRV.h - C API for SPIRV dialect -------*- C -*-===//
+//===-- 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
diff --git a/mlir/lib/CAPI/Dialect/SPIRV.cpp b/mlir/lib/CAPI/Dialect/SPIRV.cpp
index 650c04e439158a..9bfe26b9598e23 100644
--- a/mlir/lib/CAPI/Dialect/SPIRV.cpp
+++ b/mlir/lib/CAPI/Dialect/SPIRV.cpp
@@ -1,4 +1,4 @@
-//===- SPIRV.cpp - C Interface for SPIRV dialect ------------------------===//
+//===- 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.
>From 72cd785b61e885bc5d6a34f6a9099122f3ab9d5d Mon Sep 17 00:00:00 2001
From: jungpark-mlir <jungwook at jungwook-22.04>
Date: Thu, 21 Dec 2023 10:20:55 +0000
Subject: [PATCH 4/4] Last style fix
---
mlir/python/mlir/dialects/SPIRVOps.td | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mlir/python/mlir/dialects/SPIRVOps.td b/mlir/python/mlir/dialects/SPIRVOps.td
index f724a1ca880012..eaae0e609d623c 100644
--- a/mlir/python/mlir/dialects/SPIRVOps.td
+++ b/mlir/python/mlir/dialects/SPIRVOps.td
@@ -1,4 +1,4 @@
-//===-- SPIRVOps.td - Entry point for SPIRVOps bind ------*- tablegen -*-===//
+//===-- 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.
More information about the Mlir-commits
mailing list