# megengine/megcc architecture

> Architecture diagram of the GitHub repository megengine/megcc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/megengine/megcc
- Repository: https://github.com/megengine/megcc

## Overview

MegCC compiles neural-network graphs into compact, target-specific model artifacts, then runs those artifacts with a lightweight C runtime. The main workflow is model import, graph-to-kernel conversion and optimization, kernel selection, model export, and inference. The README also documents generated CV kernels, static memory planning, dynamic or static tensor shapes, and Arm, x86, and bare-metal targets. Some runtime and importer implementations were not excerpted, so their internal call relationships are omitted rather than inferred.

Main entry points: [`compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`](https://github.com/megengine/megcc/blob/main/compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp) (MGB compiler CLI), [`compiler/include/compiler/KernelGen/KernelGen.h`](https://github.com/megengine/megcc/blob/main/compiler/include/compiler/KernelGen/KernelGen.h) (Kernel selection), [`runtime/include/lite-c/network_c.h`](https://github.com/megengine/megcc/blob/main/runtime/include/lite-c/network_c.h) (Lite C API), [`runtime/src/parse.c`](https://github.com/megengine/megcc/blob/main/runtime/src/parse.c) (Runtime parser)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Model ingestion"]
  node_mgb_cli["MGB compiler CLI<br/>[mgb-to-tinynn.cpp]"]
  node_mgb_importer["MGB importer<br/>[importer.cpp]"]
  node_onnx_cli["ONNX compiler CLI<br/>[onnx-to-tinynn.cpp]"]
  node_onnx_importer["ONNX importer<br/>[importer.cpp]"]
end

subgraph group_compile["Compiler pipeline"]
  node_mgb_dialect["MGB dialect<br/>[MGBDialect.cpp]"]
  node_kernel_dialect["Kernel dialect<br/>[KernelDialect.cpp]"]
  node_graph_fusion["Graph fusion<br/>[KernelFuse.cpp]"]
  node_lowering["MGB-to-kernel lowering<br/>[MGBToKernel.cpp]"]
  node_memory_forwarding["Memory forwarding"]
  node_materialization["Kernel materialization"]
  node_memory_planning["Static memory planning"]
  node_exporter["TinyNN exporter<br/>[exporter.cpp]"]
end

subgraph group_kernels["Kernel generation"]
  node_kernel_selection["Kernel selection<br/>[KernelGen.h]"]
  node_arm_kernels["Arm kernels<br/>[KernelPack.cpp]"]
  node_baremetal_kernels["Bare-metal kernels<br/>[KernelPack.cpp]"]
  node_cv_kernels["CV kernels<br/>[GaussianBlur.cpp]"]
end

subgraph group_runtime["Runtime inference"]
  node_c_api["Lite C API<br/>[network_c.h]"]
  node_model_reader["Model reader<br/>[model_reader.h]"]
  node_runtime_parser["Runtime parser<br/>[parse.c]"]
  node_runtime_vm["Inference VM<br/>[extern_opr.c]"]
  node_tensor_api["Tensor API<br/>[tensor_c.h]"]
end

node_model_author(("Model provider"))
node_model_artifact["Compiled model"]
node_runtime_user(("Inference caller"))

node_model_author -.->|"provides model"| node_mgb_cli
node_mgb_cli -->|"imports graph"| node_mgb_importer
node_onnx_cli -->|"imports graph"| node_onnx_importer
node_mgb_cli -->|"runs fusion"| node_graph_fusion
node_mgb_cli -->|"runs lowering"| node_lowering
node_mgb_cli -->|"runs forwarding"| node_memory_forwarding
node_mgb_cli -->|"runs materialization"| node_materialization
node_mgb_cli -->|"runs planning"| node_memory_planning
node_mgb_cli -->|"selects kernels"| node_kernel_selection
node_kernel_selection -.->|"selects target kernels"| node_arm_kernels
node_kernel_selection -.->|"selects target kernels"| node_baremetal_kernels
node_mgb_cli -->|"includes CV kernels"| node_cv_kernels
node_mgb_cli -.->|"exports model"| node_exporter
node_exporter -->|"writes artifact"| node_model_artifact
node_model_artifact -->|"supplies model"| node_runtime_user
node_runtime_user -->|"invokes inference"| node_c_api
node_c_api -->|"exposes tensors"| node_tensor_api
node_model_artifact -.->|"reads schema"| node_model_reader
node_runtime_parser -->|"uses model definitions"| node_model_reader
node_runtime_parser -->|"dispatches execution"| node_runtime_vm
node_runtime_vm -.->|"serves inference"| node_c_api

click node_mgb_cli "https://github.com/megengine/megcc/blob/main/compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp"
click node_mgb_importer "https://github.com/megengine/megcc/blob/main/compiler/lib/Target/MGB/importer.cpp"
click node_onnx_cli "https://github.com/megengine/megcc/blob/main/compiler/tools/onnx-to-tinynn/onnx-to-tinynn.cpp"
click node_onnx_importer "https://github.com/megengine/megcc/blob/main/compiler/lib/Target/onnx/importer.cpp"
click node_mgb_dialect "https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/MGB/IR/MGBDialect.cpp"
click node_kernel_dialect "https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/IR/KernelDialect.cpp"
click node_graph_fusion "https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/MGB/Transforms/KernelFuse.cpp"
click node_lowering "https://github.com/megengine/megcc/blob/main/compiler/lib/Conversion/MGBToKernel/MGBToKernel.cpp"
click node_memory_forwarding "https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/Transforms/MemoryForwarding.cpp"
click node_materialization "https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/Transforms/KernelMaterialization.cpp"
click node_memory_planning "https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/Transforms/StaticMemoryPlanning.cpp"
click node_kernel_selection "https://github.com/megengine/megcc/blob/main/compiler/include/compiler/KernelGen/KernelGen.h"
click node_arm_kernels "https://github.com/megengine/megcc/blob/main/compiler/lib/KernelGen/Arm/Arm64/KernelPack.cpp"
click node_baremetal_kernels "https://github.com/megengine/megcc/blob/main/compiler/lib/KernelGen/BareMetal/KernelPack.cpp"
click node_cv_kernels "https://github.com/megengine/megcc/blob/main/compiler/lib/KernelGen/BareMetal/GaussianBlur.cpp"
click node_exporter "https://github.com/megengine/megcc/blob/main/compiler/lib/Target/TinyNN/exporter.cpp"
click node_c_api "https://github.com/megengine/megcc/blob/main/runtime/include/lite-c/network_c.h"
click node_model_reader "https://github.com/megengine/megcc/blob/main/runtime/src/cheader/model_reader.h"
click node_runtime_parser "https://github.com/megengine/megcc/blob/main/runtime/src/parse.c"
click node_runtime_vm "https://github.com/megengine/megcc/blob/main/runtime/src/vm/extern_opr.c"
click node_tensor_api "https://github.com/megengine/megcc/blob/main/runtime/include/lite-c/tensor_c.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_mgb_cli,node_mgb_importer,node_onnx_cli,node_onnx_importer toneBlue
class node_mgb_dialect,node_kernel_dialect,node_graph_fusion,node_lowering,node_memory_forwarding,node_materialization,node_memory_planning,node_exporter toneAmber
class node_kernel_selection,node_arm_kernels,node_baremetal_kernels,node_cv_kernels toneMint
class node_c_api,node_model_reader,node_runtime_parser,node_runtime_vm,node_tensor_api toneRose
class node_model_author,node_model_artifact,node_runtime_user toneIndigo
```

## Components

### Model ingestion

- [MGB compiler CLI](https://github.com/megengine/megcc/blob/main/compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp): `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`
- [MGB importer](https://github.com/megengine/megcc/blob/main/compiler/lib/Target/MGB/importer.cpp): `compiler/lib/Target/MGB/importer.cpp`
- [ONNX compiler CLI](https://github.com/megengine/megcc/blob/main/compiler/tools/onnx-to-tinynn/onnx-to-tinynn.cpp): `compiler/tools/onnx-to-tinynn/onnx-to-tinynn.cpp`
- [ONNX importer](https://github.com/megengine/megcc/blob/main/compiler/lib/Target/onnx/importer.cpp): `compiler/lib/Target/onnx/importer.cpp`

### Compiler pipeline

- [MGB dialect](https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/MGB/IR/MGBDialect.cpp): `compiler/lib/Dialect/MGB/IR/MGBDialect.cpp`
- [Kernel dialect](https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/IR/KernelDialect.cpp): `compiler/lib/Dialect/Kernel/IR/KernelDialect.cpp`
- [Graph fusion](https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/MGB/Transforms/KernelFuse.cpp): `compiler/lib/Dialect/MGB/Transforms/KernelFuse.cpp`
- [MGB-to-kernel lowering](https://github.com/megengine/megcc/blob/main/compiler/lib/Conversion/MGBToKernel/MGBToKernel.cpp): `compiler/lib/Conversion/MGBToKernel/MGBToKernel.cpp`
- [Memory forwarding](https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/Transforms/MemoryForwarding.cpp): `compiler/lib/Dialect/Kernel/Transforms/MemoryForwarding.cpp`
- [Kernel materialization](https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/Transforms/KernelMaterialization.cpp): `compiler/lib/Dialect/Kernel/Transforms/KernelMaterialization.cpp`
- [Static memory planning](https://github.com/megengine/megcc/blob/main/compiler/lib/Dialect/Kernel/Transforms/StaticMemoryPlanning.cpp): `compiler/lib/Dialect/Kernel/Transforms/StaticMemoryPlanning.cpp`
- [TinyNN exporter](https://github.com/megengine/megcc/blob/main/compiler/lib/Target/TinyNN/exporter.cpp): `compiler/lib/Target/TinyNN/exporter.cpp`

### Kernel generation

- [Kernel selection](https://github.com/megengine/megcc/blob/main/compiler/include/compiler/KernelGen/KernelGen.h): `compiler/include/compiler/KernelGen/KernelGen.h`
- [Arm kernels](https://github.com/megengine/megcc/blob/main/compiler/lib/KernelGen/Arm/Arm64/KernelPack.cpp): `compiler/lib/KernelGen/Arm/Arm64/KernelPack.cpp`
- [Bare-metal kernels](https://github.com/megengine/megcc/blob/main/compiler/lib/KernelGen/BareMetal/KernelPack.cpp): `compiler/lib/KernelGen/BareMetal/KernelPack.cpp`
- [CV kernels](https://github.com/megengine/megcc/blob/main/compiler/lib/KernelGen/BareMetal/GaussianBlur.cpp): `compiler/lib/KernelGen/BareMetal/GaussianBlur.cpp`

### Runtime inference

- [Lite C API](https://github.com/megengine/megcc/blob/main/runtime/include/lite-c/network_c.h): `runtime/include/lite-c/network_c.h`
- [Model reader](https://github.com/megengine/megcc/blob/main/runtime/src/cheader/model_reader.h): `runtime/src/cheader/model_reader.h`
- [Runtime parser](https://github.com/megengine/megcc/blob/main/runtime/src/parse.c): `runtime/src/parse.c`
- [Inference VM](https://github.com/megengine/megcc/blob/main/runtime/src/vm/extern_opr.c): `runtime/src/vm/extern_opr.c`
- [Tensor API](https://github.com/megengine/megcc/blob/main/runtime/include/lite-c/tensor_c.h): `runtime/include/lite-c/tensor_c.h`

### Other components

- **Model provider**
- **Compiled model**
- **Inference caller**

## Connections

- Model provider → MGB compiler CLI: provides model
- MGB compiler CLI → MGB importer: imports graph (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- ONNX compiler CLI → ONNX importer: imports graph (evidence: `compiler/tools/onnx-to-tinynn/onnx-to-tinynn.cpp`)
- MGB compiler CLI → Graph fusion: runs fusion (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- MGB compiler CLI → MGB-to-kernel lowering: runs lowering (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- MGB compiler CLI → Memory forwarding: runs forwarding (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- MGB compiler CLI → Kernel materialization: runs materialization (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- MGB compiler CLI → Static memory planning: runs planning (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- MGB compiler CLI → Kernel selection: selects kernels (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- Kernel selection → Arm kernels: selects target kernels
- Kernel selection → Bare-metal kernels: selects target kernels
- MGB compiler CLI → CV kernels: includes CV kernels (evidence: `compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp`)
- MGB compiler CLI → TinyNN exporter: exports model
- TinyNN exporter → Compiled model: writes artifact (evidence: `README.md`)
- Compiled model → Inference caller: supplies model (evidence: `README.md`)
- Inference caller → Lite C API: invokes inference (evidence: `runtime/include/lite-c/network_c.h`)
- Lite C API → Tensor API: exposes tensors (evidence: `runtime/include/lite-c/network_c.h`)
- Compiled model → Model reader: reads schema
- Runtime parser → Model reader: uses model definitions (evidence: `runtime/src/parse.c`)
- Runtime parser → Inference VM: dispatches execution (evidence: `runtime/src/parse.c`)
- Inference VM → Lite C API: serves inference
