[clang] [llvm] [WebAssembly,llvm] Add llvm.wasm.ref.test.func intrinsic (PR #147076)
Hood Chatham via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 8 01:01:24 PDT 2025
hoodmane wrote:
The goal is as an alternative to `EMULATE_FUNCTION_POINTER_CASTS` for projects like Python and GLib that do dispatch with sometimes-incorrect function pointers. In particular, rather than having to `call_indirect` a function pointer and find out whether or not we trap, I want a way to query the function pointer ahead of time for whether the call will succeed.
In the ideal case the solution should:
1. work on wasm32-unknown-unknown so it's portable between Emscripten and wasi
2. not require dropping down to assembly or JS
My idea is to add a clang builtin `__builtin_wasm_test_function_pointer_signature` so I can replace this whole file:
https://github.com/python/cpython/blob/main/Python/emscripten_trampoline.c
with:
```C
typedef PyObject* (*zero_arg)(void);
typedef PyObject* (*one_arg)(PyObject*);
typedef PyObject* (*two_arg)(PyObject*, PyObject*);
typedef PyObject* (*three_arg)(PyObject*, PyObject*, PyObject*);
PyObject*
_PyEM_TrampolineCall(PyCFunctionWithKeywords func,
PyObject* self,
PyObject* args,
PyObject* kw)
{
if (__builtin_wasm_test_function_pointer_signature((zero_arg)func))
return ((zero_arg)func)();
if (__builtin_wasm_test_function_pointer_signature((one_arg)func))
return ((one_arg)func)(self);
if (__builtin_wasm_test_function_pointer_signature((two_arg)func))
return ((two_arg)func)(self, args);
if (__builtin_wasm_test_function_pointer_signature((three_arg)func))
return ((three_arg)func)(self, args, kw);
PyErr_SetString(PyExc_SystemError, "Handler has unexpected signature");
return NULL;
}
```
https://github.com/llvm/llvm-project/pull/147076
More information about the llvm-commits
mailing list