Skip to content

Commit

Permalink
fix: Module.context invalid pointer error
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziqi-Yang committed Sep 6, 2024
1 parent 09705c0 commit 0b6219d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
33 changes: 31 additions & 2 deletions example/el_psy_congaroo.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
from llvmpym import error_handling
error_handling.install_fatal_error_handler(lambda s: print(s))
from llvmpym import core, utils

asm_str = r'''
; ModuleID = '<string>'
target triple = "unknown-unknown-unknown"
%struct.glob_type = type { i64, [2 x i64] }
@glob = global i32 0
@glob_b = global i8 0
@glob_f = global float 1.5
@glob_struct = global %struct.glob_type {i64 0, [2 x i64] [i64 0, i64 0]}
define zeroext i32 @sum(i32 nocapture %.1, i32 %.2) cold {
%.3 = add i32 %.1, %.2
%.4 = add i32 0, %.3
ret i32 %.4
}
define void @foo() "frame-pointer"="all" {
call void asm sideeffect "nop", ""()
ret void
}
declare void @a_readonly_func(i8 *) readonly
'''

m = utils.parse_assembly(asm_str)

print(m.context) # FIXME
# with core.Context.get_global_context() as ctx:
# print(ctx)
1 change: 0 additions & 1 deletion example/parse_ir_assmebly.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

m = utils.parse_assembly(asm_str)


for f in m.functions:
print(f'Function | name: "{f.name}", type: "{f.type}"')
module = f.parent
Expand Down
5 changes: 4 additions & 1 deletion src/llvm/Core/miscClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,10 @@ void bindOtherClasses(nb::module_ &m) {
"Obtain an iterator to the first NamedMDNode in a Module.")
.def_prop_ro("context",
[](PymModule &m) {
return PymContext(LLVMGetModuleContext(m.get()));
// here we assume that the context got is global context
// so that it won't be automatically destroyed, which
// will cause an invalid pointer error.
return PymContext(LLVMGetModuleContext(m.get()), true);
},
"Obtain the context to which this module is associated.")
.def_prop_rw("id",
Expand Down
1 change: 0 additions & 1 deletion src/llvm/types_priv/PymContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ PymContext::PymContext(LLVMContextRef ctx, bool is_global_context) {
}

PymContext::PymContext(LLVMContextRef ctx) {
std::cout << 3 << std::endl;
LLVMContextRef global_context = LLVMGetGlobalContext();
is_global_context = global_context == context.get();

Expand Down

0 comments on commit 0b6219d

Please sign in to comment.