ABI for i256 in MCJIT


I have a small JIT project based on MCJIT. The generated LLVM IR code uses the i256 type. Also, the jitted code has to call back the host application from time to time. E.g. it calls a function i256 @callback(i256).

  1. Can the callback function be implemented on the host application side (C/C++) to match the ABI used for the call by MCJIT? Or maybe the i256 has be to be casted to any struct type on LLVM side to make that possible?
  2. Should that work correctly on different host OSes?
    So far I used pointer arguments for both arguments and return types. But I’m afraid that using pointers blocks a lots of optimizations.
  • Paweł

Hi Pawel,

This should be fine. MCJIT doesn’t use any special calling conventions. As long as your JIT’d code has the same calling convention as the callback in the host program this should just work. If it doesn’t please file a bug and assign it to me. :slight_smile:

  • Lang.

Thank you for the response.

The other part of the question is how to match i256 type on the C/C++ side?

For a single i256, I think anything with the same size will do
("struct { uint64_t vals[4]; }" for example).

If other arguments are around you get into how the target decides to
split values between registers and the stack which is complicated
business. For the cases I'm aware of a similar struct with the largest
valid type is *probably* OK (so an array of "unsigned __int128" if
it's legal on the C side, otherwise uint64_t, ...).

It would be safest to do the munging on the LLVM side though (i.e.
split up your i256 and actually do the call as a "{ [4 x i64] }").