2020-04-27 06:50:23 +08:00
|
|
|
//===- native.cpp - MLIR Python bindings ----------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 <cstddef>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2020-06-19 09:02:39 +08:00
|
|
|
#include "npcomp/Python/MlirInit.h"
|
|
|
|
#include "npcomp/Python/NpcompModule.h"
|
|
|
|
#include "npcomp/Python/PybindUtils.h"
|
2020-05-01 07:00:00 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
2020-06-19 14:28:30 +08:00
|
|
|
#ifdef NPCOMP_ENABLE_IREE
|
|
|
|
#include "npcomp/Backend/IREE/PythonModule.h"
|
|
|
|
#endif
|
|
|
|
|
2020-05-01 07:00:00 +08:00
|
|
|
namespace mlir {
|
2020-04-27 06:50:23 +08:00
|
|
|
namespace npcomp {
|
|
|
|
namespace python {
|
|
|
|
|
2020-05-01 07:00:00 +08:00
|
|
|
void defineLLVMModule(pybind11::module m) {
|
|
|
|
m.def("print_help_message", []() { llvm::cl::PrintHelpMessage(); });
|
|
|
|
m.def("add_option",
|
|
|
|
[](std::string name, llvm::Optional<std::string> value) {
|
|
|
|
auto options_map = llvm::cl::getRegisteredOptions();
|
|
|
|
auto found_it = options_map.find(name);
|
|
|
|
if (found_it == options_map.end()) {
|
|
|
|
std::string message = "Unknown LLVM option: ";
|
|
|
|
message.append(name);
|
2020-05-01 08:14:03 +08:00
|
|
|
throw py::raiseValueError(message.c_str());
|
2020-05-01 07:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string value_sr = value ? *value : "";
|
|
|
|
found_it->getValue()->addOccurrence(1, name, value_sr);
|
|
|
|
},
|
|
|
|
py::arg("name"), py::arg("value") = llvm::Optional<std::string>());
|
|
|
|
m.def("reset_option",
|
|
|
|
[](std::string name) {
|
|
|
|
auto options_map = llvm::cl::getRegisteredOptions();
|
|
|
|
auto found_it = options_map.find(name);
|
|
|
|
if (found_it == options_map.end()) {
|
|
|
|
std::string message = "Unknown LLVM option: ";
|
|
|
|
message.append(name);
|
2020-05-01 08:14:03 +08:00
|
|
|
throw py::raiseValueError(message.c_str());
|
2020-05-01 07:00:00 +08:00
|
|
|
}
|
|
|
|
found_it->getValue()->setDefault();
|
|
|
|
},
|
|
|
|
py::arg("name"));
|
|
|
|
}
|
2020-04-27 06:50:23 +08:00
|
|
|
|
2020-05-07 13:44:12 +08:00
|
|
|
PYBIND11_MODULE(_npcomp, m) {
|
2020-05-01 07:00:00 +08:00
|
|
|
// Guard the once init to happen once per process (vs module, which in
|
|
|
|
// mondo builds can happen multiple times).
|
|
|
|
static bool llvm_init_baton = ([]() { return npcompMlirInitialize(); })();
|
|
|
|
(void)(llvm_init_baton);
|
|
|
|
|
2020-04-27 06:50:23 +08:00
|
|
|
m.doc() = "Npcomp native python bindings";
|
|
|
|
|
2020-05-01 07:00:00 +08:00
|
|
|
auto llvm_m = m.def_submodule("llvm", "LLVM interop");
|
|
|
|
defineLLVMModule(llvm_m);
|
|
|
|
|
2020-06-09 07:10:51 +08:00
|
|
|
// "mlir" module.
|
2020-04-27 06:50:23 +08:00
|
|
|
auto mlir_m = m.def_submodule("mlir", "MLIR interop");
|
2020-05-01 07:00:00 +08:00
|
|
|
auto mlir_ir_m = mlir_m.def_submodule("ir");
|
|
|
|
defineMlirIrModule(mlir_ir_m);
|
2020-06-03 16:29:59 +08:00
|
|
|
// Note: not "pass" because it is a reserved word
|
|
|
|
auto mlir_pass_m = mlir_m.def_submodule("passes");
|
|
|
|
defineMlirPassModule(mlir_pass_m);
|
2020-06-09 07:10:51 +08:00
|
|
|
auto mlir_dialect_m = mlir_m.def_submodule("dialect");
|
|
|
|
defineMlirCoreDialects(mlir_dialect_m);
|
2020-05-07 09:24:51 +08:00
|
|
|
|
2020-06-09 07:10:51 +08:00
|
|
|
// Outer "_npcomp" module
|
2020-05-07 09:24:51 +08:00
|
|
|
auto npcomp_dialect = m.def_submodule("dialect", "NPComp custom dialects");
|
|
|
|
defineNpcompDialect(npcomp_dialect);
|
2020-06-19 14:28:30 +08:00
|
|
|
|
|
|
|
// Optional backend modules.
|
|
|
|
auto backend_m = m.def_submodule("backend", "Backend support");
|
|
|
|
(void)backend_m;
|
|
|
|
|
|
|
|
#ifdef NPCOMP_ENABLE_IREE
|
|
|
|
auto iree_m = backend_m.def_submodule("iree", "IREE backend support");
|
|
|
|
defineBackendIREEModule(iree_m);
|
|
|
|
#endif
|
2020-04-27 06:50:23 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 07:00:00 +08:00
|
|
|
} // namespace python
|
|
|
|
} // namespace npcomp
|
|
|
|
} // namespace mlir
|