From 2479c85b45bc73c45dd0c535bb02e2c87033f911 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Wed, 3 Apr 2024 01:15:13 +0900 Subject: [PATCH] make `jl_lookup_code_address` return `CodeInstance` instead of `MethodInstance` --- base/stacktraces.jl | 5 ++++- src/codegen-stubs.c | 2 +- src/debug-registry.h | 6 +++--- src/debuginfo.cpp | 18 ++++++++---------- src/julia_internal.h | 4 ++-- src/staticdata.c | 4 ++-- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/base/stacktraces.jl b/base/stacktraces.jl index 84d280973c28ab..8b5befa0eefeec 100644 --- a/base/stacktraces.jl +++ b/base/stacktraces.jl @@ -106,7 +106,7 @@ up stack frame context information. Returns an array of frame information for al inlined at that point, innermost function first. """ Base.@constprop :none function lookup(pointer::Ptr{Cvoid}) - infos = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint), pointer, false)::Core.SimpleVector + infos = @ccall jl_lookup_code_address(pointer::Ptr{Cvoid}, false::Cint)::Core.SimpleVector pointer = convert(UInt64, pointer) isempty(infos) && return [StackFrame(empty_sym, empty_sym, -1, nothing, true, false, pointer)] # this is equal to UNKNOWN res = Vector{StackFrame}(undef, length(infos)) @@ -117,6 +117,9 @@ Base.@constprop :none function lookup(pointer::Ptr{Cvoid}) file = info[2]::Symbol linenum = info[3]::Int linfo = info[4] + if linfo isa Core.CodeInstance + linfo = linfo.def + end res[i] = StackFrame(func, file, linenum, linfo, info[5]::Bool, info[6]::Bool, pointer) end return res diff --git a/src/codegen-stubs.c b/src/codegen-stubs.c index e67ae3b75ea764..50e9790e1c3f60 100644 --- a/src/codegen-stubs.c +++ b/src/codegen-stubs.c @@ -33,7 +33,7 @@ JL_DLLEXPORT int jl_getFunctionInfo_fallback(jl_frame_t **frames, uintptr_t poin } JL_DLLEXPORT void jl_register_fptrs_fallback(uint64_t image_base, const struct _jl_image_fptrs_t *fptrs, - jl_method_instance_t **linfos, size_t n) + jl_code_instance_t **linfos, size_t n) { (void)image_base; (void)fptrs; (void)linfos; (void)n; } diff --git a/src/debug-registry.h b/src/debug-registry.h index f30049eb5b2104..5c38b681aaa388 100644 --- a/src/debug-registry.h +++ b/src/debug-registry.h @@ -82,7 +82,7 @@ class JITDebugInfoRegistry struct image_info_t { uint64_t base; jl_image_fptrs_t fptrs; - jl_method_instance_t **fvars_linfo; + jl_code_instance_t **fvars_linfo; size_t fvars_n; }; @@ -114,7 +114,7 @@ class JITDebugInfoRegistry typedef rev_map objfilemap_t; objectmap_t objectmap{}; - rev_map> linfomap{}; + rev_map> linfomap{}; // Maintain a mapping of unrealized function names -> linfo objects // so that when we see it get emitted, we can add a link back to the linfo @@ -135,7 +135,7 @@ class JITDebugInfoRegistry libc_frames_t libc_frames{}; void add_code_in_flight(llvm::StringRef name, jl_code_instance_t *codeinst, const llvm::DataLayout &DL) JL_NOTSAFEPOINT; - jl_method_instance_t *lookupLinfo(size_t pointer) JL_NOTSAFEPOINT; + jl_code_instance_t *lookupLinfo(size_t pointer) JL_NOTSAFEPOINT; void registerJITObject(const llvm::object::ObjectFile &Object, std::function getLoadAddress, std::function lookupWriteAddress); diff --git a/src/debuginfo.cpp b/src/debuginfo.cpp index 5cb7401a036d47..a22e90840fdd20 100644 --- a/src/debuginfo.cpp +++ b/src/debuginfo.cpp @@ -93,11 +93,11 @@ void JITDebugInfoRegistry::add_code_in_flight(StringRef name, jl_code_instance_t (**codeinst_in_flight)[mangle(name, DL)] = codeinst; } -jl_method_instance_t *JITDebugInfoRegistry::lookupLinfo(size_t pointer) +jl_code_instance_t *JITDebugInfoRegistry::lookupLinfo(size_t pointer) { jl_lock_profile(); auto region = linfomap.lower_bound(pointer); - jl_method_instance_t *linfo = NULL; + jl_code_instance_t *linfo = NULL; if (region != linfomap.end() && pointer < region->first + region->second.first) linfo = region->second.second; jl_unlock_profile(); @@ -372,19 +372,17 @@ void JITDebugInfoRegistry::registerJITObject(const object::ObjectFile &Object, codeinst_in_flight.erase(codeinst_it); } } - jl_method_instance_t *mi = NULL; if (codeinst) { JL_GC_PROMISE_ROOTED(codeinst); - mi = codeinst->def; // Non-opaque-closure MethodInstances are considered globally rooted // through their methods, but for OC, we need to create a global root // here. - if (jl_is_method(mi->def.value) && mi->def.method->is_for_opaque_closure) - mi = (jl_method_instance_t*)jl_as_global_root((jl_value_t*)mi, 1); + if (jl_is_method(codeinst->def->def.value) && codeinst->def->def.method->is_for_opaque_closure) + codeinst = (jl_code_instance_t*)jl_as_global_root((jl_value_t*)codeinst, 1); } jl_profile_atomic([&]() JL_NOTSAFEPOINT { - if (mi) - linfomap[Addr] = std::make_pair(Size, mi); + if (codeinst) + linfomap[Addr] = std::make_pair(Size, codeinst); if (first) { objectmap[SectionLoadAddr] = {&Object, (size_t)SectionSize, @@ -699,7 +697,7 @@ openDebugInfo(StringRef debuginfopath, const debug_link_info &info) JL_NOTSAFEPO } extern "C" JL_DLLEXPORT_CODEGEN void jl_register_fptrs_impl(uint64_t image_base, const jl_image_fptrs_t *fptrs, - jl_method_instance_t **linfos, size_t n) + jl_code_instance_t **linfos, size_t n) { getJITDebugRegistry().add_image_info({(uintptr_t) image_base, *fptrs, linfos, n}); } @@ -1249,7 +1247,7 @@ extern "C" JL_DLLEXPORT_CODEGEN int jl_getFunctionInfo_impl(jl_frame_t **frames_ extern "C" jl_method_instance_t *jl_gdblookuplinfo(void *p) JL_NOTSAFEPOINT { - return getJITDebugRegistry().lookupLinfo((size_t)p); + return getJITDebugRegistry().lookupLinfo((size_t)p)->def; } #if defined(_OS_DARWIN_) && defined(LLVM_SHLIB) diff --git a/src/julia_internal.h b/src/julia_internal.h index 7df996d888909a..7363b60cf737f0 100644 --- a/src/julia_internal.h +++ b/src/julia_internal.h @@ -1157,7 +1157,7 @@ typedef struct { char *func_name; char *file_name; int line; - jl_method_instance_t *linfo; + jl_code_instance_t *linfo; int fromC; int inlined; } jl_frame_t; @@ -1751,7 +1751,7 @@ JL_DLLIMPORT void jl_get_llvm_external_fns(void *native_code, arraylist_t *gvs); JL_DLLIMPORT void jl_get_function_id(void *native_code, jl_code_instance_t *ncode, int32_t *func_idx, int32_t *specfunc_idx); JL_DLLIMPORT void jl_register_fptrs(uint64_t image_base, const struct _jl_image_fptrs_t *fptrs, - jl_method_instance_t **linfos, size_t n); + jl_code_instance_t **linfos, size_t n); JL_DLLIMPORT void jl_init_codegen(void); JL_DLLIMPORT void jl_teardown_codegen(void) JL_NOTSAFEPOINT; diff --git a/src/staticdata.c b/src/staticdata.c index 3e2aaf44d8a0d7..8df0cbd0f5ed61 100644 --- a/src/staticdata.c +++ b/src/staticdata.c @@ -2162,7 +2162,7 @@ static void jl_update_all_fptrs(jl_serializer_state *s, jl_image_t *image) uintptr_t base = (uintptr_t)&s->s->buf[0]; // These will become MethodInstance references, but they start out as a list of // offsets into `s` for CodeInstances - jl_method_instance_t **linfos = (jl_method_instance_t**)&s->fptr_record->buf[0]; + jl_code_instance_t **linfos = (jl_code_instance_t**)&s->fptr_record->buf[0]; uint32_t clone_idx = 0; for (i = 0; i < img_fvars_max; i++) { reloc_t offset = *(reloc_t*)&linfos[i]; @@ -2177,7 +2177,7 @@ static void jl_update_all_fptrs(jl_serializer_state *s, jl_image_t *image) jl_code_instance_t *codeinst = (jl_code_instance_t*)(base + offset); assert(jl_is_method(codeinst->def->def.method) && jl_atomic_load_relaxed(&codeinst->invoke) != jl_fptr_const_return); assert(specfunc ? jl_atomic_load_relaxed(&codeinst->invoke) != NULL : jl_atomic_load_relaxed(&codeinst->invoke) == NULL); - linfos[i] = codeinst->def; // now it's a MethodInstance + linfos[i] = codeinst; // now it's a MethodInstance void *fptr = fvars.ptrs[i]; for (; clone_idx < fvars.nclones; clone_idx++) { uint32_t idx = fvars.clone_idxs[clone_idx] & jl_sysimg_val_mask;