Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions mlir/include/mlir-c/Dialect/SPIRV.h
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions mlir/lib/CAPI/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions mlir/lib/CAPI/Dialect/SPIRV.cpp
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions mlir/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions mlir/python/mlir/dialects/SPIRVOps.td
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions mlir/python/mlir/dialects/spirv.py
Original file line number Diff line number Diff line change
@@ -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 *
21 changes: 21 additions & 0 deletions mlir/test/python/dialects/spirv_dialect.py
Original file line number Diff line number Diff line change
@@ -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
@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)