Skip to content

Commit f40e433

Browse files
authored
[Binding] enable mlir python binding for graph compiler (#91)
* enable gc mlir python binding * fix * add gc passes binding * add readme * fix license * fix license * add option * fix style * update llvm license path * fix words * update readme * add context init hook * fix license * add __init__.py
1 parent fce4118 commit f40e433

26 files changed

+444
-10
lines changed

CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
################################################################################
2+
# Copyright (C) 2024 Intel Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing,
11+
# software distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions
14+
# and limitations under the License.
15+
# SPDX-License-Identifier: Apache-2.0
16+
################################################################################
17+
118
cmake_minimum_required(VERSION 3.20)
219
project(GraphCompiler VERSION "0.1.0" LANGUAGES C CXX)
320

@@ -10,6 +27,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
1027

1128
option(GC_LEGACY_ENABLE ON)
1229
option(GC_TEST_ENABLE "Build the tests" ON)
30+
option(GC_ENABLE_BINDINGS_PYTHON "Enable Graph Complier Python Binding" ON)
1331
option(GC_DEV_LINK_LLVM_DYLIB "Link dynamic libraries of LLVM and MLIR. For developers only. Do not use it in packing the library." OFF)
1432

1533
if(GC_LEGACY_ENABLE)
@@ -33,6 +51,16 @@ include(AddLLVM)
3351
include(AddMLIR)
3452
include(HandleLLVMOptions)
3553

54+
if(GC_ENABLE_BINDINGS_PYTHON AND NOT MLIR_ENABLE_BINDINGS_PYTHON)
55+
message(STATUS "Failed to enable Python API due to the 'MLIR_ENABLE_BINDINGS_PYTHON' for LLVM is not ON.")
56+
set(GC_ENABLE_BINDINGS_PYTHON OFF CACHE BOOL "" FORCE)
57+
endif()
58+
59+
if(GC_ENABLE_BINDINGS_PYTHON)
60+
include(MLIRDetectPythonEnv)
61+
mlir_configure_python_dev_packages()
62+
endif()
63+
3664
include_directories(
3765
${LLVM_INCLUDE_DIRS}
3866
${MLIR_INCLUDE_DIRS}
@@ -61,6 +89,10 @@ include("cmake/version.cmake")
6189
add_subdirectory(include)
6290
add_subdirectory(lib)
6391
add_subdirectory(src)
92+
if(GC_ENABLE_BINDINGS_PYTHON)
93+
message(STATUS "Enabling Python API")
94+
add_subdirectory(python)
95+
endif()
6496

6597
set(GC_LIB_LINKED_LIBS
6698
MLIRLinalgx

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ Graph Compiler supports the following build-time options.
6363
| GC_LEGACY_ENABLE | **ON**, OFF | Controls building the legacy graph-compiler component |
6464
| GC_TEST_ENABLE | **ON**, OFF | Controls building the tests |
6565
| GC_DEV_LINK_LLVM_DYLIB | ON, **OFF** | Controls dynamic link LLVM/MLIR libraries, mainly for developer |
66+
| GC_ENABLE_BINDINGS_PYTHON | **ON**, OFF | Controls building the Python API |
6667

include/gc-c/Dialects.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions
15+
* and limitations under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
#ifndef GC_MLIR_C_DIALECTS_H
21+
#define GC_MLIR_C_DIALECTS_H
22+
23+
#include "mlir-c/IR.h"
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(OneDNNGraph, onednn_graph);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
#endif // GC_MLIR_C_DIALECTS_H

include/gc-c/Passes.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions
15+
* and limitations under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
20+
#ifndef GCEXT_MLIR_C_PASSES_H
21+
#define GCEXT_MLIR_C_PASSES_H
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
#include "gc/Transforms/Passes.capi.h.inc"
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+
#endif // GCEXT_MLIR_C_PASSES_H
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
add_mlir_dialect(OneDNNGraphOps onednn_graph)
2-
add_mlir_doc(OneDNNGraphOps OneDNNGraphOps gc/Dialect/OneDNNGraph/ -gen-op-doc)
3-
add_mlir_doc(OneDNNGraphDialect OneDNNGraphDialect gc/Dialect/OneDNNGraph/ -gen-dialect-doc)
2+
# add_mlir_doc(OneDNNGraphOps OneDNNGraphOps gc/Dialect/OneDNNGraph/ -gen-op-doc)
3+
# add_mlir_doc(OneDNNGraphDialect OneDNNGraphDialect gc/Dialect/OneDNNGraph/ -gen-dialect-doc)

include/gc/Dialect/OneDNNGraph/OneDNNGraphOps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ include "mlir/Interfaces/SideEffectInterfaces.td"
1414
include "mlir/Interfaces/DestinationStyleOpInterface.td"
1515
include "mlir/IR/AttrTypeBase.td"
1616
include "mlir/IR/OpBase.td"
17-
include "OneDNNGraphDialect.td"
18-
include "OneDNNGraphTypes.td"
17+
include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.td"
18+
include "gc/Dialect/OneDNNGraph/OneDNNGraphTypes.td"
1919

2020
//===----------------------------------------------------------------------===//
2121
// Base OneDNNGraph operation definition.

include/gc/Dialect/OneDNNGraph/OneDNNGraphTypes.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
//===- OneDNNGraphTypes.h - OneDNN input dialect types -----*- tablegen -*-===//
2-
//
1+
//===-- OneDNNGraphTypes.td - DESC -------------------------*- tablegen -*-===//
2+
//
33
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
//
6+
//
77
//===----------------------------------------------------------------------===//
88

99
#ifndef ONEDNNGRAPH_TYPES
1010
#define ONEDNNGRAPH_TYPES
1111

1212
include "mlir/IR/BuiltinTypes.td"
1313
include "mlir/IR/AttrTypeBase.td"
14-
include "OneDNNGraphDialect.td"
14+
include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.td"
1515

1616
//===----------------------------------------------------------------------===//
1717
// OneDNNGraph type definitions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
set(LLVM_TARGET_DEFINITIONS Passes.td)
22
mlir_tablegen(Passes.h.inc -gen-pass-decls -name GraphCompiler)
3+
mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix GraphCompiler)
4+
mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix GraphCompiler)
35
add_public_tablegen_target(GraphCompilerPassIncGen)
46
add_mlir_doc(Passes GraphCompilerPasses ./ -gen-pass-doc)

lib/gc/CAPI/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_mlir_public_c_api_library(GcCAPI
2+
Dialects.cpp
3+
Passes.cpp
4+
LINK_LIBS PUBLIC
5+
MLIROneDNNGraph
6+
GCPasses
7+
)

lib/gc/CAPI/Dialects.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Dialects.cpp - DESC -------------------------------------*- C++ -*-===//
2+
//
3+
// This file is licensed 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 "gc-c/Dialects.h"
10+
#include "gc/Dialect/OneDNNGraph/OneDNNGraphDialect.h"
11+
#include "mlir/CAPI/Registration.h"
12+
13+
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(OneDNNGraph, onednn_graph,
14+
mlir::onednn_graph::OneDNNGraphDialect)

0 commit comments

Comments
 (0)