Skip to content

Commit 58fa15d

Browse files
committed
[MLIR][Python] demo building bindings with no CAPI aggregate and using mlir aggregate
1 parent c0a2bea commit 58fa15d

File tree

12 files changed

+496
-9
lines changed

12 files changed

+496
-9
lines changed

mlir/examples/standalone/CMakeLists.txt

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
2525
include(TableGen)
2626
include(AddLLVM)
2727
include(AddMLIR)
28-
include(HandleLLVMOptions)
28+
# include(HandleLLVMOptions)
2929
else()
3030
set(EXTERNAL_PROJECT_BUILD TRUE)
3131
# Build via external projects mechanism
@@ -42,6 +42,40 @@ else()
4242
list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
4343
endif()
4444

45+
include(CheckCCompilerFlag)
46+
include(CheckCXXCompilerFlag)
47+
48+
function(append_if condition value)
49+
if (${condition})
50+
foreach(variable ${ARGN})
51+
set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
52+
endforeach(variable)
53+
endif()
54+
endfunction()
55+
56+
macro(add_flag_if_supported flag name)
57+
check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
58+
append_if("C_SUPPORTS_${name}" "${flag}" CMAKE_C_FLAGS)
59+
check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
60+
append_if("CXX_SUPPORTS_${name}" "${flag}" CMAKE_CXX_FLAGS)
61+
endmacro()
62+
63+
function(add_flag_or_print_warning flag name)
64+
check_c_compiler_flag("-Werror ${flag}" "C_SUPPORTS_${name}")
65+
check_cxx_compiler_flag("-Werror ${flag}" "CXX_SUPPORTS_${name}")
66+
if (C_SUPPORTS_${name} AND CXX_SUPPORTS_${name})
67+
message(STATUS "Building with ${flag}")
68+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" PARENT_SCOPE)
69+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" PARENT_SCOPE)
70+
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${flag}" PARENT_SCOPE)
71+
else()
72+
message(WARNING "${flag} is not supported.")
73+
endif()
74+
endfunction()
75+
76+
add_flag_or_print_warning("-fPIC" FPIC)
77+
add_flag_if_supported("-fno-semantic-interposition" FNO_SEMANTIC_INTERPOSITION)
78+
4579
if(MLIR_ENABLE_BINDINGS_PYTHON)
4680
include(MLIRDetectPythonEnv)
4781
mlir_configure_python_dev_packages()
@@ -70,6 +104,10 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
70104
set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
71105
endif()
72106
add_subdirectory(python)
107+
option(MLIR_STANDALONE_REALLY "" OFF)
108+
if (MLIR_STANDALONE_REALLY)
109+
add_subdirectory(really_alone)
110+
endif()
73111
endif()
74112
add_subdirectory(test)
75113
add_subdirectory(standalone-opt)

mlir/examples/standalone/lib/Standalone/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ add_mlir_dialect_library(MLIRStandalone
1010
DEPENDS
1111
MLIRStandaloneOpsIncGen
1212
MLIRStandalonePassesIncGen
13-
14-
LINK_LIBS PUBLIC
15-
MLIRIR
16-
MLIRInferTypeOpInterface
17-
MLIRFuncDialect
1813
)
14+
target_link_options(obj.MLIRStandalone PUBLIC --unresolved-symbols=ignore-all)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=mlir.")
2+
3+
declare_mlir_python_sources(StandReallyAlonePythonSources)
4+
declare_mlir_dialect_python_bindings(
5+
ADD_TO_PARENT StandReallyAlonePythonSources
6+
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir_standreallyalone"
7+
SOURCES
8+
dialects/standalonereallyalone.py
9+
dialects/_ods_common.py
10+
)
11+
12+
declare_mlir_python_extension(StandReallyAlonePythonSources.NanobindExtension
13+
MODULE_NAME _standReallyAloneDialectsNanobind
14+
ADD_TO_PARENT StandReallyAlonePythonSources
15+
SOURCES
16+
StandReallyAloneExtensionNanobind.cpp
17+
PRIVATE_LINK_LIBS
18+
StandaloneCAPI
19+
PYTHON_BINDINGS_LIBRARY nanobind
20+
)
21+
22+
set(StandReallyAlonePythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
23+
add_mlir_python_modules(StandReallyAlonePythonModules
24+
ROOT_PREFIX "${StandReallyAlonePythonModules_ROOT_PREFIX}/../mlir_standreallyalone"
25+
INSTALL_PREFIX "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/../mlir_standreallyalone"
26+
DECLARED_SOURCES
27+
StandReallyAlonePythonSources
28+
StandReallyAlonePythonSources.NanobindExtension
29+
StandalonePythonSources.standalone.ops_gen
30+
StandalonePythonSources.standalone.tablegen
31+
MLIRPythonSources.Dialects.builtin
32+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===- StandaloneExtension.cpp - Extension module -------------------------===//
2+
//
3+
// This is the nanobind version of the example module. There is also a pybind11
4+
// example in StandaloneExtensionPybind11.cpp.
5+
//
6+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
7+
// See https://llvm.org/LICENSE.txt for license information.
8+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#include "Standalone-c/Dialects.h"
13+
#include "mlir-c/Dialect/Arith.h"
14+
#include "mlir/Bindings/Python/Nanobind.h"
15+
#include "mlir/Bindings/Python/NanobindAdaptors.h"
16+
17+
namespace nb = nanobind;
18+
19+
NB_MODULE(_standReallyAloneDialectsNanobind, m) {
20+
//===--------------------------------------------------------------------===//
21+
// standalone dialect
22+
//===--------------------------------------------------------------------===//
23+
auto standaloneM = m.def_submodule("standalone");
24+
25+
standaloneM.def(
26+
"register_dialects",
27+
[](MlirContext context, bool load) {
28+
MlirDialectHandle arithHandle = mlirGetDialectHandle__arith__();
29+
MlirDialectHandle standaloneHandle =
30+
mlirGetDialectHandle__standalone__();
31+
mlirDialectHandleRegisterDialect(arithHandle, context);
32+
mlirDialectHandleRegisterDialect(standaloneHandle, context);
33+
if (load) {
34+
mlirDialectHandleLoadDialect(arithHandle, context);
35+
mlirDialectHandleRegisterDialect(standaloneHandle, context);
36+
}
37+
},
38+
nb::arg("context").none() = nb::none(), nb::arg("load") = true,
39+
// clang-format off
40+
nb::sig("def register_dialects(context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") ", load: bool = True) -> None")
41+
// clang-format on
42+
);
43+
}

0 commit comments

Comments
 (0)