Table of Contents
Fetching ...

C codegen considered unnecessary: go directly to binary, do not pass C. Compilation of Julia code for deployment in model-based engineering

Fredrik Bagge Carlson, Cody Tapscott, Gabriel Baraldi, Chris Rackauckas

TL;DR

This work questions the necessity of C-code generation for embedded deployment by showing how Julia, via ModelingToolkit and LLVM-based code generation, can produce executable binaries and C-callable libraries from high-level models. It introduces trimming and emulation techniques to enable ahead-of-time compilation suitable for real-time systems, avoiding the restrictions of traditional code-generation workflows. The authors demonstrate an end-to-end pipeline with a model-based state estimator deployed on a Raspberry Pi (producing a 3.5MB binary and achieving ~93× faster execution than a standard Julia run) and a Julia-based PID controller compiled as a shared library for use from C. While limitations remain (e.g., reliance on unreleased Julia v1.12 tooling and cross-compilation via emulation), the approach preserves high-level expressiveness and ecosystem access, offering a practical path for accelerated model-based engineering workflows.

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, and instead 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 which is compiled into a program that performs state estimation for deployment onto a Raspberry Pi, as well as a PID controller library implemented in Julia and compiled into a shared library callable from a C program.

C codegen considered unnecessary: go directly to binary, do not pass C. Compilation of Julia code for deployment in model-based engineering

TL;DR

This work questions the necessity of C-code generation for embedded deployment by showing how Julia, via ModelingToolkit and LLVM-based code generation, can produce executable binaries and C-callable libraries from high-level models. It introduces trimming and emulation techniques to enable ahead-of-time compilation suitable for real-time systems, avoiding the restrictions of traditional code-generation workflows. The authors demonstrate an end-to-end pipeline with a model-based state estimator deployed on a Raspberry Pi (producing a 3.5MB binary and achieving ~93× faster execution than a standard Julia run) and a Julia-based PID controller compiled as a shared library for use from C. While limitations remain (e.g., reliance on unreleased Julia v1.12 tooling and cross-compilation via emulation), the approach preserves high-level expressiveness and ecosystem access, offering a practical path for accelerated model-based engineering workflows.

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, and instead 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 which is compiled into a program that performs state estimation for deployment onto a Raspberry Pi, as well as a PID controller library implemented in Julia and compiled into a shared library callable from a C program.

Paper Structure

This paper contains 9 sections.