44 lines · cpp
1//===- StandaloneExtension.cpp - Extension module -------------------------===//2//3// This is the nanobind version of the example module. There is also a pybind114// 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-exception9//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 17namespace nb = nanobind;18 19NB_MODULE(_standaloneDialectsNanobind, m) {20 //===--------------------------------------------------------------------===//21 // standalone dialect22 //===--------------------------------------------------------------------===//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 off40 nb::sig("def register_dialects(context: " MAKE_MLIR_PYTHON_QUALNAME("ir.Context") ", load: bool = True) -> None")41 // clang-format on42 );43}44