Abstract
Since time immemorial, an old adage has always seemed to ring true: you cannot use a high-level productive programming language like Python or R for real-time control and embedded-systems programming; you must rewrite your program in C. We present a counterexample to this mantra by demonstrating how recent compiler developments in the Julia programming language allow users of Julia and the equation-based modeling language ModelingToolkit to compile and deploy binaries for real-time model-based estimation and control. Contrary to the approach taken by a majority of modeling and simulation tools, we do not generate C code. Instead, we demonstrate how we may use the native Julia code-generation pipeline through LLVM to compile architecture-specific binaries from high-level code. This approach avoids many of the restrictions typically placed on high-level languages to enable C-code generation. As case studies, we include a nonlinear state estimator derived from an equation-based model compiled for a Raspberry Pi, as well as a PID controller library implemented in Julia and compiled into a shared library callable from a C program.
Introduction: The Two-Language Problem in Engineering
In Model-Based Engineering (MBE), designers often face a "two-language problem." Complex systems are designed and simulated in high-level environments like Simulink, Python, or Julia, but must be manually translated into low-level C code for deployment on embedded hardware. This workflow is fraught with friction: commercial code-generation tools are expensive, and the "C-sieve" often strips away high-level features like operator overloading or complex data structures to ensure compatibility. Furthermore, the manual rewrite process is error-prone and slows the iteration cycle between design and deployment.
This paper demonstrates that Julia’s native LLVM-based compiler pipeline can bypass C entirely, moving directly from high-level mathematical descriptions to architecture-specific machine code without sacrificing expressiveness or performance.
Methodology
To achieve standalone deployment, the authors leverage several recent advancements in the Julia ecosystem designed to enable Ahead-of-Time (AOT) compilation for resource-constrained environments.
The juliac Tool and Trimming Mode
The core of this approach is juliac, a new compilation tool that performs static analysis on user-defined entry points. It utilizes a "trimming" mode to prune unused code and enforce "sufficient specialization." This ensures the compiler can infer concrete types for every function call, effectively eliminating the overhead of Julia's standard Just-In-Time (JIT) compiler and the need for a full development environment at runtime.
Modeling and Verification
System dynamics are described using ModelingToolkit.jl (MTK), an acausal modeling framework. MTK symbolically transforms these equations into optimized Julia functions. To ensure these functions are suitable for real-time execution, the authors employ two critical static verification tools:
- JET.jl: Used for static type-checking to ensure type stability.
- AllocCheck.jl: Used to verify that the compiled code performs no heap allocations (Garbage Collection-managed memory) during execution, a prerequisite for deterministic real-time stability.
Cross-Compilation
As Julia currently lacks a native cross-compiler, the authors utilized qemu to emulate the target architecture (AArch64) on a host machine (x86-64) during the build process, allowing for the creation of binaries specifically for the Raspberry Pi.
Case Studies and Results
The effectiveness of the direct-to-binary pipeline was evaluated through two primary functional case studies.
Case Study 1: CSTR State Estimator
A nonlinear state estimator for a Continuously Stirred Tank Reactor (CSTR) was implemented using an Unscented Kalman Filter (UKF). The estimator was compiled and successfully deployed to a Raspberry Pi 4.
- Performance: The compiled executable completed its task in 26.6 ms, compared to 2.48 seconds for the standard Julia execution model. This 93x reduction in latency is primarily due to the elimination of the "startup tax" associated with JIT compilation and the loading of heavy modeling libraries.
- Binary Size: The standalone application logic was reduced to approximately 3.5 MB.
Case Study 2: PID Controller Library
A standard PID controller library (DiscretePIDs.jl) was compiled into a 1.7 MB shared library. This library was then called directly from a C program, demonstrating that Julia can be integrated into existing C-based ecosystems as a high-performance component rather than a total replacement.
Discussion and Limitations
While the results prove the viability of bypassing C-codegen, several hurdles remain for broader embedded adoption:
- Runtime Overhead: While the application logic is small, the current Julia runtime adds a ~48 MB overhead (largely due to OpenBLAS). This is manageable for a Raspberry Pi but remains too large for smaller microcontrollers.
- OS Dependency: The current workflow requires a traditional Operating System (Linux, Windows, or macOS). It does not yet support bare-metal deployment or Real-Time Operating Systems (RTOS).
- Tooling Maturity: The workflow relies on experimental features in Julia v1.12 and the evolving
juliactool.
Conclusion
By leveraging LLVM and new static analysis tools, Julia can now produce small, efficient binaries that rival the performance of C while maintaining the productivity of a high-level language. This approach eliminates the need for restrictive C-code generation, offering a more streamlined path from mathematical modeling to real-world embedded deployment. Future work will focus on "trimming" the Julia runtime itself and enabling native cross-compilation to further reduce binary footprints and deployment complexity.
Summary updated