# onnx/onnx-mlir architecture

> Architecture diagram of the GitHub repository onnx/onnx-mlir, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/onnx/onnx-mlir
- Repository: https://github.com/onnx/onnx-mlir

## Overview

Below are the guidelines and key points to include, tailored to this project:

##### Main Components
- Input – the ONNX model supplied to the compiler.
- Frontend / Parser – the module that reads the ONNX model and establishes an initial representation in an ONNX Dialect.
- Intermediate Representation Layer – where the ONNX model is converted to an MLIR-based representation. This includes the ONNX Dialect defined in the project (visible in the src/Dialect/ONNX directories) and the representation that evolves through various transformation passes.
- Transformation & Conversion Passes – a rich pipeline of conversion stages:
– ONNX-to-MLIR lowering.
– Further conversion passes through the MLIR “Dialect” layer (e.g., ONNX, Krnl, ZHigh, ZLow, and LLVM).
– Specific conversion modules (see src/Conversion/* and src/Transform/*) that implement various optimizations and passes (numerical optimizations, device placement for accelerators, etc.).
- Code Generation / Backend – components that generate the final target code. This includes:
– Emitting LLVM IR.
– Building object files, shared libraries, or JNI libraries (as indicated by the various --Emit flags in the onnx-mlir driver and infrastructure in src/Compiler & src/Runtime).
- Runtime Environment – the provided runtime support (C/C++ runtime, Python wrappers, Java interfaces) that allow the resulting code to execute, including specialized runtime support found in src/Runtime and even the Accelerator wrappers in src/Accelerators.
- Build & Tooling Components – not part of the runtime architecture per se, but crucial to the project:
– CMake files (CMakeLists.txt at various levels).
– Build system configurations and CI/CD pipelines (e.g. .azure-pipelines, .buildbot, .circleci, and .github/workflows directories).
– Docker container files (docker/, docs/docker-example) emphasizing that development and testing often occur in containerized environments.
– Testing and validation infrastructure (test/, utils/, third_party).

##### Relationships and Data Flow
- The onnx-mlir driver parses the model by invoking the ONNX dialect front-end, which then feeds into the series of conversion passes.

##### Important Architectural Patterns or Design Principles
- Layered design: The project is clearly modular with separate layers for frontend parsing (ONNX dialect and model import), IR transformation (MLIR and conversion passes), and backend code generation.
- Plugin-like extensibility: Different conversion passes and dialect definitions (for onnx, krnl, zhigh, zlow) show that the system is designed to be extended for various hardware accelerators and target architectures.
- Separation of concerns: The tool separates compile-time phases from runtime support – with dedicated directories for the compiler (src/Compiler), runtime (src/Runtime), and DSL/IR definitions (src/Dialect, src/Conversion, src/Transform).

##### Relevant Technologies, Frameworks, and Libraries
- LLVM/MLIR – as the core technology for the lowering and intermediate representation.
- ONNX – as the primary input specification.
- CMake – as the build system.
- Docker and various CI/CD systems (Azure Pipelines, Jenkins, GitHub Actions, CircleCI) – for building, testing, and deployment.
- Language runtimes (C/C++, Python, Java) – for the generated outputs and runtime libraries.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Input Layer
    subgraph "Input Layer"
        ONNXInput["ONNX Model Input"]:::input
    end

    %% Driver
    ONNXDriver["ONNX-MLIR Driver"]:::backend

    %% Frontend Layer
    subgraph "Frontend Layer"
        ONNXParserUtils["ONNX Parser Utils"]:::frontend
        ModelInputShaper["Model Input Shaper"]:::frontend
    end

    %% Intermediate Representation Layer
    subgraph "Intermediate Representation Layer"
        ONNXDialect["ONNX Dialect Definition"]:::ir
        MLIRIR["MLIR IR Handling"]:::ir
    end

    %% Transformation & Conversion Passes
    subgraph "Transformation & Conversion Passes"
        ConversionPasses["Conversion Passes"]:::passes
        TransformPasses["Transform Passes"]:::passes
    end

    %% Code Generation / Backend
    CodeGen["Code Generation Module"]:::backend

    %% Runtime Environment
    subgraph "Runtime Environment"
        CPPRuntime["C/C++ Runtime"]:::runtime
        PythonWrappers["Python Wrappers"]:::runtime
        JavaInterface["Java Interface"]:::runtime
    end

    %% Build & Tooling Components
    subgraph "Build & Tooling Components"
        CMakeBuild["CMake Build Config"]:::build
        CICDPipelines["CI/CD Pipelines"]:::build
        DockerConfig["Docker Config"]:::build
        TestingInfra["Testing Infrastructure"]:::build
    end

    %% Data Flow Connections
    ONNXInput --> ONNXDriver
    ONNXDriver --> ONNXParserUtils
    ONNXDriver --> ModelInputShaper
    ONNXParserUtils --> ONNXDialect
    ModelInputShaper --> ONNXDialect
    ONNXDialect --> MLIRIR
    MLIRIR --> ConversionPasses
    MLIRIR --> TransformPasses
    ConversionPasses --> CodeGen
    TransformPasses --> CodeGen
    CodeGen --> CPPRuntime
    CodeGen --> PythonWrappers
    CodeGen --> JavaInterface

    %% Build & Tooling integration (dotted lines)
    CICDPipelines -.-> ONNXDriver
    TestingInfra -.-> ONNXDriver
    DockerConfig -.-> ONNXDriver
    CMakeBuild -.-> CodeGen

    %% Click Events
    click ONNXInput "https://github.com/onnx/onnx-mlir/tree/main/test/mlir/onnx/parse/"
    click ONNXParserUtils "https://github.com/onnx/onnx-mlir/blob/main/src/Builder/ImportONNXUtils.cpp"
    click ModelInputShaper "https://github.com/onnx/onnx-mlir/blob/main/src/Builder/ModelInputShaper.cpp"
    click ONNXDialect "https://github.com/onnx/onnx-mlir/tree/main/src/Dialect/ONNX/"
    click MLIRIR "https://github.com/onnx/onnx-mlir/tree/main/src/Dialect/Mlir/"
    click ConversionPasses "https://github.com/onnx/onnx-mlir/tree/main/src/Conversion/"
    click TransformPasses "https://github.com/onnx/onnx-mlir/tree/main/src/Transform/"
    click CodeGen "https://github.com/onnx/onnx-mlir/tree/main/src/Compiler/"
    click CPPRuntime "https://github.com/onnx/onnx-mlir/tree/main/src/Runtime/"
    click PythonWrappers "https://github.com/onnx/onnx-mlir/tree/main/src/Runtime/python/"
    click JavaInterface "https://github.com/onnx/onnx-mlir/tree/main/src/Runtime/jni/"
    click CMakeBuild "https://github.com/onnx/onnx-mlir/blob/main/CMakeLists.txt"
    click CICDPipelines "https://github.com/onnx/onnx-mlir/tree/main/.github/workflows/"
    click DockerConfig "https://github.com/onnx/onnx-mlir/tree/main/docker/"
    click TestingInfra "https://github.com/onnx/onnx-mlir/tree/main/test/"

    %% Styles
    classDef input fill:#ffeb3b,stroke:#000,stroke-width:1px;
    classDef frontend fill:#bbdefb,stroke:#000,stroke-width:1px;
    classDef ir fill:#c8e6c9,stroke:#000,stroke-width:1px;
    classDef passes fill:#ffe0b2,stroke:#000,stroke-width:1px;
    classDef backend fill:#d1c4e9,stroke:#000,stroke-width:1px;
    classDef runtime fill:#b2dfdb,stroke:#000,stroke-width:1px;
    classDef build fill:#ffcdd2,stroke:#000,stroke-width:1px;
```
