emmtrix ONNX-to-C Code Generator

From emmtrix Wiki
Jump to navigation Jump to search

emmtrix ONNX-to-C Code Generator (emx-onnx-cgen) is an emmtrix-developed, open-source AI frontend compiler that translates ONNX models into deterministic, analyzable C code specifically designed for auto-vectorization and embedded target optimization.

The primary goal of emx-onnx-cgen is not to perform any hardware-specific optimizations itself, but to generate high-quality C code that serves as an ideal input for the emmtrix Vectorizer and subsequent backend toolchains targeting embedded architectures.

emx-onnx-cgen is particularly suited for safety-critical, resource-constrained, and bare-metal environments where transparency, determinism, and static analyzability are mandatory.

Motivation

Typical AI deployment solutions rely on runtime engines or architecture-specific code generators that:

  • hide control flow and memory access patterns,
  • rely on dynamic memory allocation,
  • limit static analysis and certification,
  • restrict the effectiveness of auto-vectorization.

emx-onnx-cgen follows a different approach:

ONNX → Clean C → Vectorizer → Target Architecture

By lowering AI models into a well-structured and explicit C representation, emx-onnx-cgen enables advanced compiler analyses and vectorization passes that would otherwise be impossible or unreliable.

Role in the emmtrix Toolchain

emx-onnx-cgen acts as the frontend in the emmtrix AI compilation pipeline.

Pipeline Overview
  • ONNX ModelImported neural network description
  • emx-onnx-cgenNormalization, lowering, and generation of vectorization-friendly C code
  • emmtrix VectorizerLoop analysis, data-flow analysis, auto-vectorization
  • Backend / ToolchainArchitecture-specific code generation (DSP, MCU, SoC)

Diagram suggestion:

ONNX Model
    |
    v
emx-onnx-cgen
    |
    v
Vectorization-Friendly C Code
    |
    v
emmtrix Vectorizer
    |
    v
Optimized Embedded Binary

Design Goals

emx-onnx-cgen is designed with the following goals:

  • Deterministic code generation
  • Fully static memory layout (no heap usage)
  • Explicit loops and control flow
  • Predictable memory access patterns
  • Readable and auditable C code
  • Maximal compatibility with auto-vectorization tools
  • Suitability for embedded and safety-critical systems

Non-Goals

emx-onnx-cgen explicitly does not aim to:

  • Act as a runtime inference engine
  • Perform hand-written SIMD or target-specific optimizations
  • Hide model execution behind opaque APIs
  • Support training or backpropagation

Code Generation Principles

The generated C code follows strict structural rules to support static analysis and vectorization:

  • Simple, canonical loop forms
  • Linear array accesses
  • No hidden pointer aliasing
  • No dynamic dispatch
  • No recursion
  • No dynamic memory allocation
  • Explicit tensor dimensions and strides

These properties allow the emmtrix Vectorizer to reliably detect vectorization opportunities and apply architecture-specific optimizations.

Example

High-Level Operation

An ONNX operator such as a vector addition is lowered into a simple C loop:

for (i = 0; i < N; ++i) {
    output[i] = input_a[i] + input_b[i];
}

This structure is intentionally chosen to be:

  • easy to analyze,
  • free of side effects,
  • ideal for SIMD vectorization by downstream tools.

Embedded Target Suitability

emx-onnx-cgen is suitable for:

  • Bare-metal systems
  • RTOS-based systems
  • Automotive ECUs
  • Industrial controllers
  • DSP-based platforms

The generated code depends only on standard C headers and can be integrated into existing embedded build systems without requiring a runtime framework.

Verification and Determinism

To ensure correctness, emx-onnx-cgen supports verification against reference ONNX execution.

Key properties:

  • Bit-stable code generation
  • Reproducible builds
  • Deterministic execution behavior

These properties are essential for certification, validation, and long-term maintenance of embedded AI software.

Related Pages

See Also