Skip to content
Open
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
4 changes: 4 additions & 0 deletions mlir/examples/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
set(MLIR_BINDINGS_PYTHON_INSTALL_PREFIX "python_packages/standalone/${MLIR_PYTHON_PACKAGE_PREFIX}" CACHE STRING "" FORCE)
endif()
add_subdirectory(python)
option(MLIR_STANDALONE_REALLY "" OFF)
if (MLIR_STANDALONE_REALLY)
add_subdirectory(really_alone)
endif()
endif()
add_subdirectory(test)
add_subdirectory(standalone-opt)
Expand Down
6 changes: 1 addition & 5 deletions mlir/examples/standalone/lib/Standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ add_mlir_dialect_library(MLIRStandalone
DEPENDS
MLIRStandaloneOpsIncGen
MLIRStandalonePassesIncGen

LINK_LIBS PUBLIC
MLIRIR
MLIRInferTypeOpInterface
MLIRFuncDialect
Comment on lines -13 to -17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove so that the code can be "looked up" in libMLIRPythonCAPI.so at runtime.

)
target_link_options(obj.MLIRStandalone PUBLIC --unresolved-symbols=ignore-all)
Copy link
Contributor Author

@makslevental makslevental Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to build without failing to link and instead hope the symbols are found at runtime...

32 changes: 32 additions & 0 deletions mlir/examples/standalone/really_alone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
add_compile_definitions("MLIR_PYTHON_PACKAGE_PREFIX=mlir.")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to set -DMLIR_PYTHON_PACKAGE_PREFIX=mlir so that StandReallyAloneExtensionNanobind.cpp compiles with the right #defines (otherwise APIs with MlirContext and etc. won't work).


declare_mlir_python_sources(StandReallyAlonePythonSources)
declare_mlir_dialect_python_bindings(
ADD_TO_PARENT StandReallyAlonePythonSources
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir_standreallyalone"
SOURCES
dialects/standalonereallyalone.py
dialects/_ods_common.py
)

declare_mlir_python_extension(StandReallyAlonePythonSources.NanobindExtension
MODULE_NAME _standReallyAloneDialectsNanobind
ADD_TO_PARENT StandReallyAlonePythonSources
SOURCES
StandReallyAloneExtensionNanobind.cpp
PRIVATE_LINK_LIBS
StandaloneCAPI
Comment on lines +17 to +18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to fully link StandaloneCAPI instead of EMBED_CAPI_LINK_LIBS because we're not building the aggregate.

PYTHON_BINDINGS_LIBRARY nanobind
)

set(StandReallyAlonePythonModules_ROOT_PREFIX "${MLIR_BINARY_DIR}/${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}")
add_mlir_python_modules(StandReallyAlonePythonModules
ROOT_PREFIX "${StandReallyAlonePythonModules_ROOT_PREFIX}/../mlir_standreallyalone"
INSTALL_PREFIX "${MLIR_BINDINGS_PYTHON_INSTALL_PREFIX}/../mlir_standreallyalone"
DECLARED_SOURCES
StandReallyAlonePythonSources
StandReallyAlonePythonSources.NanobindExtension
StandalonePythonSources.standalone.ops_gen
StandalonePythonSources.standalone.tablegen
MLIRPythonSources.Dialects.builtin
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===- StandaloneExtension.cpp - Extension module -------------------------===//
//
// This is the nanobind version of the example module. There is also a pybind11
// example in StandaloneExtensionPybind11.cpp.
//
// 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 "Standalone-c/Dialects.h"
#include "mlir-c/Dialect/Arith.h"
#include "mlir/Bindings/Python/Nanobind.h"
#include "mlir/Bindings/Python/NanobindAdaptors.h"

namespace nb = nanobind;

NB_MODULE(_standReallyAloneDialectsNanobind, m) {
//===--------------------------------------------------------------------===//
// standalone dialect
//===--------------------------------------------------------------------===//
auto standaloneM = m.def_submodule("standalone");

standaloneM.def(
"register_dialects",
[](MlirContext context, bool load) {
MlirDialectHandle arithHandle = mlirGetDialectHandle__arith__();
MlirDialectHandle standaloneHandle =
mlirGetDialectHandle__standalone__();
mlirDialectHandleRegisterDialect(arithHandle, context);
mlirDialectHandleRegisterDialect(standaloneHandle, context);
if (load) {
mlirDialectHandleLoadDialect(arithHandle, context);
mlirDialectHandleRegisterDialect(standaloneHandle, context);
}
},
nb::arg("context").none() = nb::none(), nb::arg("load") = true,
// clang-format off
nb::sig("def register_dialects(context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") ", load: bool = True) -> None")
// clang-format on
);
}
Loading
Loading