Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Aug 5, 2024
1 parent acb28b2 commit 7be19e0
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 11 deletions.
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ add_definitions(${LLVM_DEFINITIONS_LIST})
nanobind_add_module(
llvmpym_ext

# Target the stable ABI for Python 3.12+, which reduces
# the number of binary wheels that must be built. This
# does nothing on older Python versions
STABLE_ABI

NB_STATIC
Expand All @@ -80,6 +77,7 @@ nanobind_add_module(
src/llvm/Utils.cpp
src/llvm/Support.cpp
src/llvm/Analysis.cpp
src/llvm/Target.cpp
src/llvm/TargetMachine.cpp

src/llvm/types_priv/PyModule.cpp
Expand All @@ -90,7 +88,9 @@ nanobind_add_module(
src/llvm/types_priv/PyPassManagerBase.cpp
src/llvm/types_priv/PyMemoryBuffer.cpp
src/llvm/types_priv/PyModuleProvider.cpp
src/llvm/types_priv/PyTargetData.cpp
src/llvm/types_priv/PyTargetMachine.cpp
src/llvm/types_priv/PyTargetMachineOptions.cpp

${LLVM_INCLUDE_DIRS}
)
Expand Down Expand Up @@ -161,6 +161,15 @@ nanobind_add_stub(
DEPENDS llvmpym_ext
)

nanobind_add_stub(
llvmpym_ext_stub_target
MODULE llvmpym_ext.target
OUTPUT target.pyi
PYTHON_PATH $<TARGET_FILE_DIR:llvmpym_ext>
DEPENDS llvmpym_ext
)


nanobind_add_stub(
llvmpym_ext_stub_target_machine
MODULE llvmpym_ext.target_machine
Expand All @@ -181,6 +190,7 @@ install(FILES
${CMAKE_BINARY_DIR}/support.pyi
${CMAKE_BINARY_DIR}/utils.pyi
${CMAKE_BINARY_DIR}/analysis.pyi
${CMAKE_BINARY_DIR}/target.pyi
${CMAKE_BINARY_DIR}/target_machine.pyi

DESTINATION ${SKBUILD_PROJECT_NAME}/llvmpym_ext)
12 changes: 12 additions & 0 deletions src/llvm/Target.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "Target.h"

#include <llvm-c/Target.h>
#include <nanobind/nanobind.h>

namespace nb = nanobind;
using namespace nb::literals;

void populateTarget(nb::module_ &m) {

}

9 changes: 9 additions & 0 deletions src/llvm/Target.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef LLVMPYM_TARGET_H
#define LLVMPYM_TARGET_H

#include <nanobind/nanobind.h>

void populateTarget(nanobind::module_ &m);


#endif
96 changes: 92 additions & 4 deletions src/llvm/TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ using namespace nb::literals;
void populateTargetMachine(nanobind::module_ &m) {
BIND_ITERATOR_CLASS(PyTargetIterator, "TargetIterator")

auto TargetMachineClass =
nb::class_<PyTargetMachine, PyLLVMObject<PyTargetMachine, LLVMTargetMachineRef>>
(m, "TargetMachine", "TargetMachine");

auto TargetMachineOptionsClass =
nb::class_<PyTargetMachineOptions,
PyLLVMObject<PyTargetMachineOptions, LLVMTargetMachineOptionsRef>>
(m, "TargetMachineOptions", "TargetMachineOptions");

auto TargetClass =
nb::class_<PyTarget, PyLLVMObject<PyTarget, LLVMTargetRef>>(m, "Target", "Target");


nb::enum_<LLVMCodeGenOptLevel>(m, "CodeGenOptLevel", "CodeGenOptLevel")
// None is a python keyword
.value("Null", LLVMCodeGenOptLevel::LLVMCodeGenLevelNone)
Expand All @@ -41,7 +54,7 @@ void populateTargetMachine(nanobind::module_ &m) {
.value("Medium", LLVMCodeModel::LLVMCodeModelMedium)
.value("Large", LLVMCodeModel::LLVMCodeModelLarge);

nb::class_<PyTarget, PyLLVMObject<PyTarget, LLVMTargetRef>>(m, "Target", "Target")
TargetClass
.def("__iter__", [](PyTarget &self) { return PyTargetIterator(self); })
.def_static("get_first",
[]() {
Expand Down Expand Up @@ -90,8 +103,83 @@ void populateTargetMachine(nanobind::module_ &m) {
auto res = LLVMGetNextTarget(self.get());
WRAP_OPTIONAL_RETURN(res, PyTarget);
},
"Returns the next llvm::Target given a previous one (or null if there's none)");
"Returns the next llvm::Target given a previous one (or null "
"if there's none)");
TargetMachineOptionsClass
.def("__init__",
[](PyTargetMachineOptions *tmo) {
new (tmo) PyTargetMachineOptions(LLVMCreateTargetMachineOptions());
})
.def("set_cpu",
[](PyTargetMachineOptions &self, const char *cpu) {
return LLVMTargetMachineOptionsSetCPU(self.get(), cpu);
},
"cpu"_a)
.def("set_features",
[](PyTargetMachineOptions &self, const char *features) {
return LLVMTargetMachineOptionsSetFeatures(self.get(), features);
},
"features"_a)
.def("set_abi",
[](PyTargetMachineOptions &self, const char *abi) {
return LLVMTargetMachineOptionsSetABI(self.get(), abi);
},
"abi"_a)
.def("set_code_gen_opt_level",
[](PyTargetMachineOptions &self, LLVMCodeGenOptLevel level) {
return LLVMTargetMachineOptionsSetCodeGenOptLevel(self.get(), level);
},
"level"_a)
.def("set_reloc_mode",
[](PyTargetMachineOptions &self, LLVMRelocMode reloc) {
return LLVMTargetMachineOptionsSetRelocMode(self.get(), reloc);
},
"reloc"_a)
.def("set_code_model",
[](PyTargetMachineOptions &self, LLVMCodeModel codeModel) {
return LLVMTargetMachineOptionsSetCodeModel(self.get(), codeModel);
},
"code_model"_a);

nb::class_<PyTargetMachine, PyLLVMObject<PyTargetMachine, LLVMTargetMachineRef>>
(m, "TargetMachine", "TargetMachine");
TargetMachineClass
.def("__init__",
[](PyTargetMachine *tm, PyTarget &T, const char *triple,
PyTargetMachineOptions options) {
auto res = LLVMCreateTargetMachineWithOptions
(T.get(), triple, options.get());
new (tm) PyTargetMachine(res);
},
"target"_a, "triple"_a, "options"_a)
.def("__init__",
[](PyTargetMachine *tm, PyTarget &T, const char *triple, const char *cpu,
const char *features, LLVMCodeGenOptLevel level, LLVMRelocMode reloc,
LLVMCodeModel codeModel) {
auto res = LLVMCreateTargetMachine
(T.get(), triple, cpu, features, level, reloc, codeModel);
new (tm) PyTargetMachine(res);
})
.def_prop_ro("target",
[](PyTargetMachine &self) {
return PyTarget(LLVMGetTargetMachineTarget(self.get()));
})
.def_prop_ro("triple",
[](PyTargetMachine &self) {
auto res = LLVMGetTargetMachineTriple(self.get());
RETURN_MESSAGE(res);
})
.def_prop_ro("cpu",
[](PyTargetMachine &self) {
auto res = LLVMGetTargetMachineCPU(self.get());
RETURN_MESSAGE(res);
})
.def_prop_ro("feature_string",
[](PyTargetMachine &self) {
auto res = LLVMGetTargetMachineFeatureString(self.get());
RETURN_MESSAGE(res);
});
// TODO nedd
// .def_prop_ro("data_layout",
// [](PyTargetMachine &self) {
// return LLVMCreateTargetDataLayout(self.get());
// });
}
9 changes: 7 additions & 2 deletions src/llvm/types_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include "types_priv/PyPassManagerBase.h"
#include "types_priv/PyMemoryBuffer.h"
#include "types_priv/PyModuleProvider.h"
#include "types_priv/PyTargetData.h"
#include "types_priv/PyTargetMachine.h"
#include "types_priv/PyTargetMachineOptions.h"
#include "types_priv/PyLLVMObject.h"


Expand Down Expand Up @@ -286,8 +288,11 @@ enum class PyLLVMFastMathFlags {
BIND_PYLLVMOBJECT_(PyPassManagerBase, LLVMPassManagerRef, PyPassManagerBaseObject) \
\
BIND_PYLLVMOBJECT_(PyTarget, LLVMTargetRef, PyTargetObject) \
BIND_PYLLVMOBJECT_(PyTargetMachine, LLVMTargetMachineRef, PyTargetMachineObject)

BIND_PYLLVMOBJECT_(PyTargetMachine, LLVMTargetMachineRef, PyTargetMachineObject) \
BIND_PYLLVMOBJECT_(PyTargetMachineOptions, LLVMTargetMachineOptionsRef, \
PyTargetMachineOptionsObject) \
\
BIND_PYLLVMOBJECT_(PyTargetData, LLVMTargetDataRef, PyTargetDataObject)


// Core --------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions src/llvm/types_priv/PyTargetData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "PyTargetData.h"

PyTargetData::PyTargetData(LLVMTargetDataRef data)
: obj(get_shared_obj(data)) {}

LLVMTargetDataRef PyTargetData::get() const {
return obj.get();
}

SHARED_POINTER_IMPL(PyTargetData, LLVMTargetDataRef, LLVMOpaqueTargetData,
LLVMDisposeTargetData)
21 changes: 21 additions & 0 deletions src/llvm/types_priv/PyTargetData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef LLVMPYM_TYPES_PRIV_PYTARGETDATA_H
#define LLVMPYM_TYPES_PRIV_PYTARGETDATA_H

#include <llvm-c/Target.h>
#include <memory>
#include <unordered_map>
#include <mutex>
#include "PyLLVMObject.h"
#include "utils.h"

class PyTargetData : public PyLLVMObject<PyTargetData, LLVMTargetDataRef> {
public:
explicit PyTargetData(LLVMTargetDataRef data);
LLVMTargetDataRef get() const;

private:
SHARED_POINTER_DEF(LLVMTargetDataRef, LLVMOpaqueTargetData);
};


#endif
12 changes: 12 additions & 0 deletions src/llvm/types_priv/PyTargetMachineOptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "PyTargetMachineOptions.h"

PyTargetMachineOptions::PyTargetMachineOptions(LLVMTargetMachineOptionsRef obj)
: obj(get_shared_obj(obj)) {}

LLVMTargetMachineOptionsRef PyTargetMachineOptions::get() const {
return obj.get();
}


SHARED_POINTER_IMPL(PyTargetMachineOptions, LLVMTargetMachineOptionsRef,
LLVMOpaqueTargetMachineOptions, LLVMDisposeTargetMachineOptions)
23 changes: 23 additions & 0 deletions src/llvm/types_priv/PyTargetMachineOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef LLVMPYM_TYPES_PRIV_PYTARGETMACHINEOPTIONS_H
#define LLVMPYM_TYPES_PRIV_PYTARGETMACHINEOPTIONS_H

#include <llvm-c/TargetMachine.h>
#include <memory>
#include <unordered_map>
#include <mutex>
#include "PyLLVMObject.h"
#include "utils.h"

class PyTargetMachineOptions : public PyLLVMObject<PyTargetMachineOptions, LLVMTargetMachineOptionsRef> {
public:
explicit PyTargetMachineOptions(LLVMTargetMachineOptionsRef machine);
LLVMTargetMachineOptionsRef get() const;

private:
SHARED_POINTER_DEF(LLVMTargetMachineOptionsRef, LLVMOpaqueTargetMachineOptions);
};




#endif
5 changes: 5 additions & 0 deletions src/llvm/utils_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@
} \
return WRAP_FN(T);

#define RETURN_MESSAGE(RES) \
std::string _str(RES); \
LLVMDisposeMessage(RES); \
return _str; \


#endif
1 change: 1 addition & 0 deletions src/llvmpym/target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .llvmpym_ext.target import *
8 changes: 6 additions & 2 deletions src/llvmpym_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "llvm/Support.h"
#include "llvm/types_priv.h"
#include "llvm/Analysis.h"
#include "llvm/Target.h"
#include "llvm/TargetMachine.h"

namespace nb = nanobind;
Expand All @@ -30,7 +31,10 @@ NB_MODULE(llvmpym_ext, m) {

auto analysisModule = m.def_submodule("analysis", "Analysis");
populateAnalysis(analysisModule);

auto targetMachineModule = m.def_submodule("target_machine", "Target");

auto targetModule = m.def_submodule("target", "Target");
populateTarget(targetModule);

auto targetMachineModule = m.def_submodule("target_machine", "Target Machine");
populateTargetMachine(targetMachineModule);
}

0 comments on commit 7be19e0

Please sign in to comment.