How megengine/megcc works
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: mgb-to-tinynn.cpp MGB compiler CLI · KernelGen.h Kernel selection · network_c.h Lite C API · parse.c Runtime parser
Components 24
Model ingestion
- MGB compiler CLI
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - MGB importer
compiler/lib/Target/MGB/importer.cpp - ONNX compiler CLI
compiler/tools/onnx-to-tinynn/onnx-to-tinynn.cpp - ONNX importer
compiler/lib/Target/onnx/importer.cpp
Compiler pipeline
- MGB dialect
compiler/lib/Dialect/MGB/IR/MGBDialect.cpp - Kernel dialect
compiler/lib/Dialect/Kernel/IR/KernelDialect.cpp - Graph fusion
compiler/lib/Dialect/MGB/Transforms/KernelFuse.cpp - MGB-to-kernel lowering
compiler/lib/Conversion/MGBToKernel/MGBToKernel.cpp - Memory forwarding
compiler/lib/Dialect/Kernel/Transforms/MemoryForwarding.cpp - Kernel materialization
compiler/lib/Dialect/Kernel/Transforms/KernelMaterialization.cpp - Static memory planning
compiler/lib/Dialect/Kernel/Transforms/StaticMemoryPlanning.cpp - TinyNN exporter
compiler/lib/Target/TinyNN/exporter.cpp
Kernel generation
- Kernel selection
compiler/include/compiler/KernelGen/KernelGen.h - Arm kernels
compiler/lib/KernelGen/Arm/Arm64/KernelPack.cpp - Bare-metal kernels
compiler/lib/KernelGen/BareMetal/KernelPack.cpp - CV kernels
compiler/lib/KernelGen/BareMetal/GaussianBlur.cpp
Runtime inference
- Lite C API
runtime/include/lite-c/network_c.h - Model reader
runtime/src/cheader/model_reader.h - Runtime parser
runtime/src/parse.c - Inference VM
runtime/src/vm/extern_opr.c - Tensor API
runtime/include/lite-c/tensor_c.h
Other components
- Model provider
- Compiled model
- Inference caller
Connections 21
- Model provider → MGB compiler CLI: provides model
- MGB compiler CLI → MGB importer: imports graph
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - ONNX compiler CLI → ONNX importer: imports graph
compiler/tools/onnx-to-tinynn/onnx-to-tinynn.cpp - MGB compiler CLI → Graph fusion: runs fusion
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - MGB compiler CLI → MGB-to-kernel lowering: runs lowering
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - MGB compiler CLI → Memory forwarding: runs forwarding
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - MGB compiler CLI → Kernel materialization: runs materialization
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - MGB compiler CLI → Static memory planning: runs planning
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - MGB compiler CLI → Kernel selection: selects kernels
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
compiler/tools/mgb-to-tinynn/mgb-to-tinynn.cpp - MGB compiler CLI → TinyNN exporter: exports model
- TinyNN exporter → Compiled model: writes artifact
README.md - Compiled model → Inference caller: supplies model
README.md - Inference caller → Lite C API: invokes inference
runtime/include/lite-c/network_c.h - Lite C API → Tensor API: exposes tensors
runtime/include/lite-c/network_c.h - Compiled model → Model reader: reads schema
- Runtime parser → Model reader: uses model definitions
runtime/src/parse.c - Runtime parser → Inference VM: dispatches execution
runtime/src/parse.c - Inference VM → Lite C API: serves inference