Search code examples
embeddedllvmcode-generationjitwebassembly

How can I JIT compile code to run across OSes and for bare metal?


I'm looking to do some realtime data processing within a cross-platform user-facing application, and I need some kind of just-in-time or just-ahead-of-time compilation of dynamically generated code to get the performance I need. I want to use the same library to generate code for a bare-metal microcontroller, but the microcontroller does not need to JIT anything. What's the best solution for embeddable dynamic code execution?

  • I've looked at embedding Clang+LLVM and compiling generated C code on the fly, but couldn't get Clang to compile C from memory.
  • WebAssembly via wasmer would be easier to work with than LLVM, but I believe it needs system libraries and executable memory.
  • I'm embedding this in C++, but the embedded compiler can be written in any language with a C API.
  • Performance is important. I want to do realtime processing without taking more than ~10% CPU time on desktop, and obviously not overloading my microcontroller.
  • I'm okay choosing a microcontroller that can run embedded Linux if necessary, provided it can boot nearly instantly.

Solution

  • I think generating and running LLVM IR using the C++ API will be the best solution, unless another option is presented. Though the code generation might be low-level, it supports all relevant platforms, can cross compile, and has exceptional performance.