From 7e69e7b23e5670d3b8df493adbbca5b6aaffa899 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 27 Oct 2026 21:05:23 +0530 Subject: [PATCH] Tests: Let host_jit read a function's result as any ctypes type run_function took the return type from the IR and read only i32 and i64, as signed. It now takes an optional ctypes type, so a caller can read an unsigned or narrow result the way C would see it. Without one, it does what it did before. Co-Authored-By: Claude Opus 5.5 --- tests/framework/host_jit.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/framework/host_jit.py b/tests/framework/host_jit.py index e865c1d..e193e0c 100644 --- a/tests/framework/host_jit.py +++ b/tests/framework/host_jit.py @@ -17,7 +17,9 @@ from pathlib import Path import llvmlite.binding as llvm -def run_function(ll_text: str, func_name: str) -> int: +def run_function(ll_text: str, func_name: str, restype=None) -> int: + """Call func_name(NULL) and return its result. restype is the ctypes type + to read the result as; by default the IR's i32 or i64, read as signed.""" ll_text = re.sub(r'^target (triple|datalayout) = ".*"$', "", ll_text, flags=re.M) llvm.initialize_native_target() llvm.initialize_native_asmprinter() @@ -27,12 +29,15 @@ def run_function(ll_text: str, func_name: str) -> int: engine = llvm.create_mcjit_compiler(mod, tm) engine.finalize_object() - ret = re.search(rf'define [^\n]*?\bi(\d+) @"?{re.escape(func_name)}"?\(', ll_text) - ret_ty = {64: ctypes.c_int64, 32: ctypes.c_int32}[int(ret.group(1))] - func = ctypes.CFUNCTYPE(ret_ty, ctypes.c_void_p)( + if restype is None: + ret = re.search( + rf'define [^\n]*?\bi(\d+) @"?{re.escape(func_name)}"?\(', ll_text + ) + restype = {64: ctypes.c_int64, 32: ctypes.c_int32}[int(ret.group(1))] + func = ctypes.CFUNCTYPE(restype, ctypes.c_void_p)( engine.get_function_address(func_name) ) - return func(None) + return int(func(None)) def run_in_subprocess(ll_path, func_name: str, timeout: float) -> int: