From adca41f9fe801e0a15a105230d685c1c37d4c372 Mon Sep 17 00:00:00 2001 From: Andre Masella Date: Tue, 27 Jun 2023 15:33:40 -0400 Subject: [PATCH 1/2] Expose library name in OrcJIT tracker Add a `name` property with the OrcJIT library name in the tracker object. --- llvmlite/binding/orcjit.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/llvmlite/binding/orcjit.py b/llvmlite/binding/orcjit.py index 16dbcadf7..82996f586 100644 --- a/llvmlite/binding/orcjit.py +++ b/llvmlite/binding/orcjit.py @@ -144,8 +144,8 @@ def link(self, lljit, library_name): however, the name of the library cannot be reused. """ assert not lljit.closed, "Cannot add to closed JIT" - library_name = str(library_name).encode('utf-8') - assert len(library_name) > 0, "Library cannot be empty" + encoded_library_name = str(library_name).encode('utf-8') + assert len(encoded_library_name) > 0, "Library cannot be empty" elements = (_LinkElement * len(self.__entries))() for idx, (kind, value) in enumerate(self.__entries): elements[idx].element_kind = c_uint8(kind) @@ -163,7 +163,7 @@ def link(self, lljit, library_name): with ffi.OutputString() as outerr: tracker = lljit._capi.LLVMPY_LLJIT_Link( lljit._ptr, - library_name, + encoded_library_name, elements, len(self.__entries), imports, @@ -173,9 +173,10 @@ def link(self, lljit, library_name): outerr) if not tracker: raise RuntimeError(str(outerr)) - return ResourceTracker(tracker, {name: exports[idx].address - for idx, name in - enumerate(self.__exports)}) + return ResourceTracker(tracker, + library_name, + {name: exports[idx].address + for idx, name in enumerate(self.__exports)}) class ResourceTracker(ffi.ObjectRef): @@ -195,8 +196,9 @@ class ResourceTracker(ffi.ObjectRef): LLVM internally tracks references between different libraries, so only "leaf" libraries need to be tracked. """ - def __init__(self, ptr, addresses): + def __init__(self, ptr, name, addresses): self.__addresses = addresses + self.__name = name ffi.ObjectRef.__init__(self, ptr) def __getitem__(self, item): @@ -205,6 +207,10 @@ def __getitem__(self, item): """ return self.__addresses[item] + @property + def name(self): + return self.__name + def _dispose(self): with ffi.OutputString() as outerr: if self._capi.LLVMPY_LLJIT_Dylib_Tracker_Dispose(self, outerr): @@ -252,7 +258,7 @@ def lookup(self, dylib, fn): if not tracker: raise RuntimeError(str(outerr)) - return ResourceTracker(tracker, {fn: address.value}) + return ResourceTracker(tracker, dylib, {fn: address.value}) @property def target_data(self): From 68b355dd0707988ecc7153dad2a3b803b057f0d1 Mon Sep 17 00:00:00 2001 From: Andre Masella Date: Wed, 5 Jul 2023 13:53:54 -0400 Subject: [PATCH 2/2] Add test for OrcJIT library name As titled. --- llvmlite/tests/test_binding.py | 1 + 1 file changed, 1 insertion(+) diff --git a/llvmlite/tests/test_binding.py b/llvmlite/tests/test_binding.py index 91497321e..0c3f2a179 100644 --- a/llvmlite/tests/test_binding.py +++ b/llvmlite/tests/test_binding.py @@ -1183,6 +1183,7 @@ def jit(self, asm=asm_sum, func_name="sum", target_machine=None, .link(lljit, func_name) cfptr = rt[func_name] self.assertTrue(cfptr) + self.assertEqual(func_name, rt.name) return lljit, rt, func_type(cfptr) # From test_dylib_symbols