Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Aug 3, 2024
1 parent b535e75 commit a749220
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
8 changes: 5 additions & 3 deletions example/parse_ir_assmebly.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
for f in m.functions:
print(f'Function | name: "{f.name}", type: "{f.type}"')
module = f.parent
# assert m == module
assert m == module # point to the same module object
assert f.parent is not module # but python objects are not the same
assert f.kind == core.ValueKind.Function
for i, a in enumerate(f.args, 1):
print(f'\tArgument | name: "{a.name}", type: "{a.type}"')
attrs = f.get_attributes_at_index(i)
print(f"\t\tattrs: {attrs}")
for b in f.basic_blocks:
print(b)


print("\n----------------------------\n")
# print("\n----------------------------\n")

2 changes: 1 addition & 1 deletion src/llvm/Core/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace nb::literals;

void bindIterators(nb::module_ &m) {
BIND_ITERATOR_CLASS(PyUseIterator, "UseIterator")
BIND_ITERATOR_CLASS(PyBasicBlockIterator, "BasicBlockIterator")
// BIND_ITERATOR_CLASS(PyBasicBlockIterator, "BasicBlockIterator")
// BIND_ITERATOR_CLASS(PyArgumentIterator, "ArgumentIterator")
BIND_ITERATOR_CLASS(PyInstructionIterator, "InstructionIterator")
BIND_ITERATOR_CLASS(PyGlobalVariableIterator, "GlobalVariableIterator")
Expand Down
41 changes: 26 additions & 15 deletions src/llvm/Core/miscClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using optional = std::optional<T>;

void bindOtherClasses(nb::module_ &m) {
auto ContextClass =
nb::class_<PyContext>
nb::class_<PyContext, PyLLVMObject<PyContext, LLVMContextRef>>
(m, "Context",
"Contexts are execution states for the core LLVM IR system.\n\n"
"Most types are tied to a context instance. Multiple contexts can"
Expand Down Expand Up @@ -47,15 +47,19 @@ void bindOtherClasses(nb::module_ &m) {
auto NamedMDNodeClass =
nb::class_<PyNamedMDNode, PyLLVMObject<PyNamedMDNode, LLVMNamedMDNodeRef>>
(m, "NamedMDNode", "NamedMDNode");

auto ModuleClass =
nb::class_<PyModule>
nb::class_<PyModule, PyLLVMObject<PyModule, LLVMModuleRef>>
(m, "Module",
"Modules represent the top-level structure in an LLVM program. An LLVM"
"module is effectively a translation unit or a collection of translation "
"units merged together.");

auto ModuleFlagEntriesClass = nb::class_<PyModuleFlagEntries>
(m, "ModuleFlagEntry", "ModuleFlagEntry");
auto ModuleFlagEntriesClass =
nb::class_<PyModuleFlagEntries, PyLLVMObject<PyModuleFlagEntries, LLVMModuleFlagEntries>>
(m, "ModuleFlagEntry", "ModuleFlagEntry");


auto MetadataClass =
nb::class_<PyMetadata, PyLLVMObject<PyMetadata, LLVMMetadataRef>>
(m, "Metadata", "Metadata");
Expand All @@ -65,29 +69,35 @@ void bindOtherClasses(nb::module_ &m) {
(m, "ValueAsMetadata", "ValueAsMetadata");


auto MetadataEntriesClass = nb::class_<PyMetadataEntries>
(m, "MetadataEntry", "MetadataEntry");
auto MetadataEntriesClass =
nb::class_<PyMetadataEntries, PyLLVMObject<PyMetadataEntries, LLVMValueMetadataEntries>>
(m, "MetadataEntry", "MetadataEntry");

auto UseClass =
nb::class_<PyUse, PyLLVMObject<PyUse, LLVMUseRef>>
(m, "Use", "Use");

auto IntrinsicClass =
// nb::class_<PyIntrinsic, PyLLVMObject<PyIntrinsic, unsigned>>
nb::class_<PyIntrinsic, PyLLVMObject<PyIntrinsic, unsigned>>
(m, "Intrinsic", "Intrinsic");
auto OperandBundleClass = nb::class_<PyOperandBundle>(m, "OperandBundle",
"OperandBundle");
auto OperandBundleClass =
nb::class_<PyOperandBundle, PyLLVMObject<PyOperandBundle, LLVMOperandBundleRef>>
(m, "OperandBundle", "OperandBundle");
auto BuilderClass =
nb::class_<PyBuilder, PyLLVMObject<PyBuilder, LLVMBuilderRef>>
(m, "Builder", "Builder");
auto ModuleProviderClass = nb::class_<PyModuleProvider>
(m, "ModuleProvider", "ModuleProvider");
auto MemoryBufferClass = nb::class_<PyMemoryBuffer>
(m, "MemoryBuffer", "MemoryBuffer");
auto ModuleProviderClass =
nb::class_<PyModuleProvider, PyLLVMObject<PyModuleProvider, LLVMModuleProviderRef>>
(m, "ModuleProvider", "ModuleProvider");
auto MemoryBufferClass =
nb::class_<PyMemoryBuffer, PyLLVMObject<PyMemoryBuffer, LLVMMemoryBufferRef>>
(m, "MemoryBuffer", "MemoryBuffer");

// no need to create PyPassManagerBase binding
auto PassManagerClass = nb::class_<PyPassManager>
(m, "PassManager", "PassManager");
auto PassManagerClass =
nb::class_<PyPassManager, PyLLVMObject<PyPassManagerBase, LLVMPassManagerRef>>
(m, "PassManager", "PassManager");
auto FunctionPassManagerClass = nb::class_<PyFunctionPassManager>
(m, "FunctionPassManager", "FunctionPassManager");

Expand Down Expand Up @@ -1382,7 +1392,8 @@ void bindOtherClasses(nb::module_ &m) {
return &self;
})
.def("__exit__",
[](PyModule &self, nb::args args, nb::kwargs kwargs) {})

[](PyModule &self, nb::args args, nb::kwargs kwargs) {})
.def_prop_ro("first_global_variable",
[](PyModule &m) -> optional<PyGlobalVariable> {
auto res = LLVMGetFirstGlobal(m.get());
Expand Down
5 changes: 0 additions & 5 deletions src/llvm/Core/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,11 +1029,6 @@ void bindValueClasses(nb::module_ &m) {
auto res = LLVMGetLastBasicBlock(self.get());
WRAP_OPTIONAL_RETURN(res, PyBasicBlock);
})
.def_prop_ro("basic_blocks",
[](PyFunction &self) {
auto res = LLVMGetFirstBasicBlock(self.get());
return PyBasicBlockIterator(PyBasicBlock(res));
})
.def_prop_ro("entry_basic_block",
[](PyFunction &self) {
return PyBasicBlock(LLVMGetEntryBasicBlock(self.get()));
Expand Down
9 changes: 6 additions & 3 deletions src/llvm/_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "_types/PyMemoryBuffer.h"
#include "_types/PyModuleProvider.h"
#include "_types/PyLLVMObject.h"
#include "_types/PyLLVMObject.h"


#define DEFINE_PY_WRAPPER_CLASS(ClassName, UnderlyingType) \
Expand Down Expand Up @@ -258,8 +259,10 @@ enum class PyLLVMFastMathFlags {
All = LLVMFastMathAll
};


// NOTE the `__bool__` method of PyIntrinsic is overridden
/*
* Check three places: here, class inheritance, binding class inheritance
* NOTE the `__bool__` method of PyIntrinsic is overridden
*/
#define BIND_PYLLVMOBJECT() \
BIND_PYLLVMOBJECT_(PyValue, LLVMValueRef, PyValueObject) \
BIND_PYLLVMOBJECT_(PyType, LLVMTypeRef, PyTypeObject) \
Expand Down Expand Up @@ -346,7 +349,7 @@ DEFINE_DIRECT_SUB_CLASS(PyPassManagerBase, PyFunctionPassManager);


DEFINE_ITERATOR_CLASS(PyUseIterator, PyUse, LLVMGetNextUse)
DEFINE_ITERATOR_CLASS(PyBasicBlockIterator, PyBasicBlock, LLVMGetNextBasicBlock)
// DEFINE_ITERATOR_CLASS(PyBasicBlockIterator, PyBasicBlock, LLVMGetNextBasicBlock)
// DEFINE_ITERATOR_CLASS(PyArgumentIterator, PyArgument, LLVMGetNextParam)
DEFINE_ITERATOR_CLASS(PyInstructionIterator, PyInstruction, LLVMGetNextInstruction)
DEFINE_ITERATOR_CLASS(PyGlobalVariableIterator, PyGlobalVariable, LLVMGetNextGlobal)
Expand Down
7 changes: 5 additions & 2 deletions src/llvm/_types/PyLLVMObject.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef PYLLVMOBJECT_H
#define PYLLVMOBJECT_H

#include <iostream>
#include <memory>

template <typename Derived, typename UnderlyingType>
class PyLLVMObject {
public:
Expand All @@ -21,9 +24,9 @@ class PyLLVMObject {
return this->get() == other.get();
}

std::size_t __hash__() const {
std::size_t __hash__() const {
return std::hash<UnderlyingType>{}(this->get());
}
}
};


Expand Down

0 comments on commit a749220

Please sign in to comment.