xo-pyjit: initial commit

This commit is contained in:
Roland Conybeare 2024-06-14 15:00:57 -04:00
commit 3cc8e73ab4
8 changed files with 171 additions and 0 deletions

9
src/pyjit/CMakeLists.txt Normal file
View file

@ -0,0 +1,9 @@
# xo-pyjit/src/pyjit/CMakeLists.txt
set(SELF_LIB xo_pyjit)
set(SELF_SRCS pyjit.cpp)
xo_pybind11_library(${SELF_LIB} ${PROJECT_NAME}Targets ${SELF_SRCS})
xo_pybind11_dependency(${SELF_LIB} xo_jit)
xo_pybind11_dependency(${SELF_LIB} xo_pyexpression)
xo_dependency(${SELF_LIB} refcnt)

60
src/pyjit/pyjit.cpp Normal file
View file

@ -0,0 +1,60 @@
/* @file pyjit.cpp */
#include "pyjit.hpp"
#include "xo/pyexpression/pyexpression.hpp"
#include "xo/jit/Jit.hpp"
#include "xo/pyutil/pyutil.hpp"
namespace xo {
namespace jit {
using xo::ast::Expression;
using xo::ref::rp;
using xo::ref::unowned_ptr;
namespace py = pybind11;
PYBIND11_MODULE(XO_PYJIT_MODULE_NAME(), m) {
// e.g. for xo::ast::Expression
XO_PYEXPRESSION_IMPORT_MODULE(); // py::module_::import("pyexpression");
m.doc() = "pybind11 plugin for xo-jit";
py::class_<Jit, rp<Jit>>(m, "Jit")
.def_static("make", &Jit::make,
py::doc("create Jit instance. Not threadsafe,"
" but does not share resources with any other Jit instance"))
.def("codegen",
[](Jit & jit, const rp<Expression> & expr) {
return jit.codegen(expr.borrow());
},
py::arg("x"),
py::doc("generate llvm (IR) code for Expression x"),
/* we're assuming llvm-generated code lives for as long as the Jit
* instance that created it.
*
* RC 14jun2024 - I think this is true modulo use of llvm resource trackers.
*/
py::return_value_policy::reference_internal)
;
py::class_<llvm::Value,
unowned_ptr<llvm::Value>>(m, "llvm_Value")
.def("print",
[](llvm::Value & x) {
std::string buf;
llvm::raw_string_ostream ss(buf);
x.print(ss);
return buf;
})
.def("__repr__",
&Jit::display_string)
;
}
} /*namespace jit*/
} /*namespace xo*/
/* end pyjit.cpp */

25
src/pyjit/pyjit.hpp.in Normal file
View file

@ -0,0 +1,25 @@
/* @file pyjit.hpp
*
* automatically generated from src/xo_pyjit/pyjit.hpp.in
* see src/xo_pyjit/CMakeLists.txt
*/
/* python requires module name = library name
* example:
* PYBIND11_MODULE(XO_PYJIT_MODULE_NAME(), m) { ... }
*/
#define XO_PYJIT_MODULE_NAME() @SELF_LIB@
/* example:
* py::module_::import(XO_PYJIT_MODULE_NAME_STR)
*/
#define XO_PYJIT_MODULE_NAME_STR "@SELF_LIB@"
/* example:
* XO_PYJIT_IMPORT_MODULE()
* replaces
* py::module_::import("pyjit")
*/
#define XO_PYJIT_IMPORT_MODULE() py::module_::import("@SELF_LIB@")
/* end pyjit.hpp */