mirror of https://github.com/llvm/torch-mlir
Start defining new IR bindings and cleanup python init.
parent
d3632af675
commit
67d38db1e2
|
@ -27,9 +27,9 @@ set(extension_target NPCOMPNativePyExt)
|
||||||
set(extension_pybind_sources
|
set(extension_pybind_sources
|
||||||
native.cpp
|
native.cpp
|
||||||
mlir_edsc.cpp
|
mlir_edsc.cpp
|
||||||
)
|
|
||||||
set(extension_llvm_sources
|
|
||||||
mlir_init.cpp
|
mlir_init.cpp
|
||||||
|
mlir_ir.cpp
|
||||||
|
pybind_utils.cpp
|
||||||
)
|
)
|
||||||
set_source_files_properties(
|
set_source_files_properties(
|
||||||
${extension_pybind_sources}
|
${extension_pybind_sources}
|
||||||
|
@ -37,7 +37,6 @@ set_source_files_properties(
|
||||||
"-frtti -fexceptions")
|
"-frtti -fexceptions")
|
||||||
add_library(${extension_target} ${NPCOMP_PYEXT_LINK_MODE}
|
add_library(${extension_target} ${NPCOMP_PYEXT_LINK_MODE}
|
||||||
${extension_pybind_sources}
|
${extension_pybind_sources}
|
||||||
${extension_llvm_sources}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
set_target_properties(${extension_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
|
set_target_properties(${extension_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
|
||||||
|
@ -60,7 +59,7 @@ set_target_properties(${extension_target} PROPERTIES LINK_FLAGS
|
||||||
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
|
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
|
||||||
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
|
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
|
||||||
|
|
||||||
llvm_update_compile_flags(${extension_target})
|
# llvm_update_compile_flags(${extension_target})
|
||||||
target_link_libraries(${extension_target}
|
target_link_libraries(${extension_target}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${dialect_libs}
|
${dialect_libs}
|
||||||
|
|
|
@ -62,12 +62,6 @@ struct CustomValueBuilder {
|
||||||
Value value;
|
Value value;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace edsc
|
|
||||||
} // namespace mlir
|
|
||||||
|
|
||||||
namespace npcomp {
|
|
||||||
namespace python {
|
|
||||||
|
|
||||||
struct PythonAttribute;
|
struct PythonAttribute;
|
||||||
struct PythonAttributedType;
|
struct PythonAttributedType;
|
||||||
struct PythonBindable;
|
struct PythonBindable;
|
||||||
|
@ -1178,5 +1172,5 @@ void defineMlirEdscModule(py::module m) {
|
||||||
.def("__str__", &PythonAffineMap::str);
|
.def("__str__", &PythonAffineMap::str);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace python
|
} // namespace edsc
|
||||||
} // namespace npcomp
|
} // namespace mlir
|
||||||
|
|
|
@ -6,14 +6,41 @@
|
||||||
//
|
//
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "mlir/IR/Dialect.h"
|
||||||
#include "mlir/InitAllDialects.h"
|
#include "mlir/InitAllDialects.h"
|
||||||
|
#include "mlir/Pass/PassManager.h"
|
||||||
|
#include "npcomp/Dialect/Numpy/NumpyDialect.h"
|
||||||
|
#include "llvm/Support/CommandLine.h"
|
||||||
|
#include "llvm/Support/PrettyStackTrace.h"
|
||||||
|
#include "llvm/Support/Signals.h"
|
||||||
|
|
||||||
|
namespace mlir {
|
||||||
namespace npcomp {
|
namespace npcomp {
|
||||||
namespace python {
|
namespace python {
|
||||||
|
|
||||||
void npcompMlirInitialize() {
|
bool npcompMlirInitialize() {
|
||||||
|
// Enable LLVM's signal handler to get nice stack traces.
|
||||||
|
llvm::sys::SetOneShotPipeSignalFunction(
|
||||||
|
llvm::sys::DefaultOneShotPipeSignalHandler);
|
||||||
|
llvm::sys::PrintStackTraceOnErrorSignal("npcomp");
|
||||||
|
|
||||||
|
// Register any pass manager command line options.
|
||||||
|
mlir::registerPassManagerCLOptions();
|
||||||
|
mlir::registerMLIRContextCLOptions();
|
||||||
|
|
||||||
|
std::string program_name = "npcomp";
|
||||||
|
std::vector<const char *> default_options = {program_name.c_str(), nullptr};
|
||||||
|
llvm::cl::ParseCommandLineOptions(1, default_options.data());
|
||||||
|
|
||||||
|
// Global registration.
|
||||||
::mlir::registerAllDialects();
|
::mlir::registerAllDialects();
|
||||||
|
|
||||||
|
// Local registration.
|
||||||
|
registerDialect<NPCOMP::Numpy::NumpyDialect>();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace python
|
} // namespace python
|
||||||
} // namesapce npcomp
|
} // namespace npcomp
|
||||||
|
} // namespace mlir
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
//===- mlir_if.cpp - MLIR IR 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 <pybind11/pybind11.h>
|
||||||
|
#include <pybind11/pytypes.h>
|
||||||
|
#include <pybind11/stl.h>
|
||||||
|
|
||||||
|
namespace py = pybind11;
|
||||||
|
|
||||||
|
namespace mlir {
|
||||||
|
|
||||||
|
void defineMlirIrModule(py::module m) {
|
||||||
|
m.doc() = "Python bindings for constructs in the mlir/IR library";
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace mlir
|
|
@ -9,27 +9,70 @@
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include <pybind11/pybind11.h>
|
#include "pybind_utils.h"
|
||||||
#include <pybind11/pytypes.h>
|
|
||||||
#include <pybind11/stl.h>
|
|
||||||
|
|
||||||
namespace py = pybind11;
|
#include "llvm/Support/CommandLine.h"
|
||||||
|
|
||||||
|
namespace mlir {
|
||||||
|
void defineMlirIrModule(py::module m);
|
||||||
|
namespace edsc {
|
||||||
|
void defineMlirEdscModule(py::module m);
|
||||||
|
} // namespace edsc
|
||||||
|
|
||||||
namespace npcomp {
|
namespace npcomp {
|
||||||
namespace python {
|
namespace python {
|
||||||
|
|
||||||
// Externs
|
// Externs
|
||||||
void npcompMlirInitialize();
|
bool npcompMlirInitialize();
|
||||||
void defineMlirEdscModule(py::module m);
|
|
||||||
|
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);
|
||||||
|
throw raiseValueError(message.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
throw raiseValueError(message.c_str());
|
||||||
|
}
|
||||||
|
found_it->getValue()->setDefault();
|
||||||
|
},
|
||||||
|
py::arg("name"));
|
||||||
|
}
|
||||||
|
|
||||||
PYBIND11_MODULE(native, m) {
|
PYBIND11_MODULE(native, m) {
|
||||||
npcompMlirInitialize();
|
// 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);
|
||||||
|
|
||||||
m.doc() = "Npcomp native python bindings";
|
m.doc() = "Npcomp native python bindings";
|
||||||
|
|
||||||
|
auto llvm_m = m.def_submodule("llvm", "LLVM interop");
|
||||||
|
defineLLVMModule(llvm_m);
|
||||||
|
|
||||||
auto mlir_m = m.def_submodule("mlir", "MLIR interop");
|
auto mlir_m = m.def_submodule("mlir", "MLIR interop");
|
||||||
auto mlir_edsc_m = mlir_m.def_submodule("edsc");
|
auto mlir_edsc_m = mlir_m.def_submodule("edsc");
|
||||||
defineMlirEdscModule(mlir_edsc_m);
|
edsc::defineMlirEdscModule(mlir_edsc_m);
|
||||||
|
auto mlir_ir_m = mlir_m.def_submodule("ir");
|
||||||
|
defineMlirIrModule(mlir_ir_m);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace python
|
} // namespace python
|
||||||
} // namespace npcomp
|
} // namespace npcomp
|
||||||
|
} // namespace mlir
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
//===- pybind_utils.cpp - Utilities for interop with python ---------------===//
|
||||||
|
//
|
||||||
|
// 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 "pybind_utils.h"
|
||||||
|
|
||||||
|
namespace mlir {
|
||||||
|
namespace npcomp {
|
||||||
|
namespace python {
|
||||||
|
|
||||||
|
pybind11::error_already_set raisePyError(PyObject *exc_class,
|
||||||
|
const char *message) {
|
||||||
|
PyErr_SetString(exc_class, message);
|
||||||
|
return pybind11::error_already_set();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace python
|
||||||
|
} // namespace npcomp
|
||||||
|
} // namespace mlir
|
|
@ -0,0 +1,54 @@
|
||||||
|
//===- pybind_utils.h - Utilities for interop with python -----------------===//
|
||||||
|
//
|
||||||
|
// 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 <string>
|
||||||
|
|
||||||
|
#include <pybind11/pybind11.h>
|
||||||
|
#include <pybind11/pytypes.h>
|
||||||
|
#include <pybind11/stl.h>
|
||||||
|
|
||||||
|
#include "llvm/ADT/Optional.h"
|
||||||
|
|
||||||
|
namespace py = pybind11;
|
||||||
|
|
||||||
|
namespace pybind11 {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct type_caster<llvm::Optional<T>> : optional_caster<llvm::Optional<T>> {};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
} // namespace pybind11
|
||||||
|
|
||||||
|
namespace mlir {
|
||||||
|
namespace npcomp {
|
||||||
|
namespace python {
|
||||||
|
|
||||||
|
/// Raises a python exception with the given message.
|
||||||
|
/// Correct usage:
|
||||||
|
// throw RaiseValueError(PyExc_ValueError, "Foobar'd");
|
||||||
|
pybind11::error_already_set raisePyError(PyObject *exc_class,
|
||||||
|
const char *message);
|
||||||
|
|
||||||
|
/// Raises a value error with the given message.
|
||||||
|
/// Correct usage:
|
||||||
|
/// throw RaiseValueError("Foobar'd");
|
||||||
|
inline pybind11::error_already_set raiseValueError(const char *message) {
|
||||||
|
return raisePyError(PyExc_ValueError, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Raises a value error with the given message.
|
||||||
|
/// Correct usage:
|
||||||
|
/// throw RaiseValueError(message);
|
||||||
|
inline pybind11::error_already_set raiseValueError(const std::string &message) {
|
||||||
|
return raisePyError(PyExc_ValueError, message.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace python
|
||||||
|
} // namespace npcomp
|
||||||
|
} // namespace mlir
|
|
@ -26,6 +26,8 @@ set -x
|
||||||
cmake -GNinja \
|
cmake -GNinja \
|
||||||
"-H$td" \
|
"-H$td" \
|
||||||
"-B$build_dir" \
|
"-B$build_dir" \
|
||||||
|
"-DCMAKE_BUILD_TYPE=Debug" \
|
||||||
|
"-DCMAKE_CXX_FLAGS_DEBUG=-g3 -gdwarf-2 -Weverything -Werror" \
|
||||||
"-DPYTHON_EXECUTABLE=$python_exe" \
|
"-DPYTHON_EXECUTABLE=$python_exe" \
|
||||||
"-DMLIR_DIR=$install_mlir/lib/cmake/mlir" \
|
"-DMLIR_DIR=$install_mlir/lib/cmake/mlir" \
|
||||||
"-DLLVM_EXTERNAL_LIT=$build_mlir/bin/llvm-lit" \
|
"-DLLVM_EXTERNAL_LIT=$build_mlir/bin/llvm-lit" \
|
||||||
|
|
Loading…
Reference in New Issue