Skip to content

Commit 2292fd0

Browse files
Jungwook Parkjungpark-mlir
andauthored
[mlir][spirv] Add support for C-API/python binding to SPIR-V dialect (#76055)
Enable bindings. --------- Co-authored-by: jungpark-mlir <[email protected]>
1 parent 1bb85fa commit 2292fd0

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed

mlir/include/mlir-c/Dialect/SPIRV.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- mlir-c/Dialect/SPIRV.h - C API for SPIRV dialect ----------*- C -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef MLIR_C_DIALECT_SPIRV_H
11+
#define MLIR_C_DIALECT_SPIRV_H
12+
13+
#include "mlir-c/IR.h"
14+
#include "mlir-c/Support.h"
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(SPIRV, spirv);
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif
25+
26+
#endif // MLIR_C_DIALECT_SPIRV_H

mlir/lib/CAPI/Dialect/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIFunc
171171
MLIRFuncDialect
172172
)
173173

174+
add_mlir_upstream_c_api_library(MLIRCAPISPIRV
175+
SPIRV.cpp
176+
177+
PARTIAL_SOURCES_INTENDED
178+
LINK_LIBS PUBLIC
179+
MLIRCAPIIR
180+
MLIRSPIRVDialect
181+
)
182+
174183
add_mlir_upstream_c_api_library(MLIRCAPITensor
175184
Tensor.cpp
176185

mlir/lib/CAPI/Dialect/SPIRV.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===- SPIRV.cpp - C Interface for SPIRV dialect --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir-c/Dialect/SPIRV.h"
10+
#include "mlir/CAPI/Registration.h"
11+
#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
12+
13+
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(SPIRV, spirv, mlir::spirv::SPIRVDialect)

mlir/python/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,13 @@ declare_mlir_dialect_python_bindings(
378378
"../../include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td"
379379
)
380380

381+
declare_mlir_dialect_python_bindings(
382+
ADD_TO_PARENT MLIRPythonSources.Dialects
383+
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
384+
TD_FILE dialects/SPIRVOps.td
385+
SOURCES dialects/spirv.py
386+
DIALECT_NAME spirv)
387+
381388
declare_mlir_dialect_python_bindings(
382389
ADD_TO_PARENT MLIRPythonSources.Dialects
383390
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"

mlir/python/mlir/dialects/SPIRVOps.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- SPIRVOps.td - Entry point for SPIRVOps bind --------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef PYTHON_BINDINGS_SPIRV_OPS
10+
#define PYTHON_BINDINGS_SPIRV_OPS
11+
12+
include "mlir/Dialect/SPIRV/IR/SPIRVOps.td"
13+
14+
#endif

mlir/python/mlir/dialects/spirv.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
from ._spirv_ops_gen import *
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# RUN: %PYTHON %s | FileCheck %s
2+
3+
from mlir.ir import *
4+
import mlir.dialects.spirv as spirv
5+
6+
7+
def run(f):
8+
print("\nTEST:", f.__name__)
9+
f()
10+
11+
12+
# CHECK-LABEL: TEST: testConstantOp
13+
@run
14+
def testConstantOps():
15+
with Context() as ctx, Location.unknown():
16+
module = Module.create()
17+
with InsertionPoint(module.body):
18+
i32 = IntegerType.get_signless(32)
19+
spirv.ConstantOp(value=IntegerAttr.get(i32, 42), constant=i32)
20+
# CHECK: spirv.Constant 42 : i32
21+
print(module)

0 commit comments

Comments
 (0)