# hqu-little-boy/ncnn-compiler architecture

> Architecture diagram of the GitHub repository hqu-little-boy/ncnn-compiler, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hqu-little-boy/ncnn-compiler
- Repository: https://github.com/hqu-little-boy/ncnn-compiler

## Overview

ncnn-compiler turns an ncnn `.param` network and `.bin` weights into a Linux shared library and model-specific C ABI header. The main user workflow runs through `ncnn-compile`, which orchestrates import and staged MLIR lowering before code generation, linking, and execution checks. The graph also preserves the documented debug-driver path and the execution-plan and profiling capabilities. Stage ordering is based on the README and pipeline excerpts; unsampled implementation details are not expanded into additional call relationships.

Main entry points: [`tools/CompileSession.cpp`](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/CompileSession.cpp) (Compile session), [`tools/CompileSession.hpp`](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/CompileSession.hpp) (Header and shared library), [`tools/ncnn-mlir-driver.cpp`](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/ncnn-mlir-driver.cpp) (MLIR debug driver), [`lib/Pipelines/NCNNPipelines.cpp`](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Pipelines/NCNNPipelines.cpp) (NCNN to TOSA)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Model frontend"]
  node_compile_cli["ncnn-compile<br/>[ncnn-compile.cpp]"]
  node_compile_session["Compile session<br/>[CompileSession.cpp]"]
  node_graph_parser["Graph parser<br/>[parser.cpp]"]
  node_graph_model["Parsed graph<br/>[graph.cpp]"]
  node_importer["NCNN importer<br/>[NCNNImporter.cpp]"]
  node_debug_driver["MLIR debug driver"]
end

subgraph group_ir["IR lowering"]
  node_ncnn_dialect["NCNN dialect<br/>[NCNNDialect.cpp]"]
  node_conversion_pipeline["NCNN to TOSA<br/>[NCNNPipelines.cpp]"]
  node_tosa_lowering["TOSA conversion<br/>[NCNNToTosa.cpp]"]
  node_linalg_stage["Linalg optimization<br/>[StrategyNCNN.cpp]"]
  node_memref_stage["Bufferization<br/>[BufferizeNCNN.cpp]"]
end

subgraph group_backend["Code generation"]
  node_llvm_stage["LLVM lowering<br/>[NCNNPipelines.cpp]"]
  node_abi["C ABI generation"]
  node_native_artifacts["Header and shared library<br/>[CompileSession.hpp]"]
end

subgraph group_observability["Plans and profiling"]
  node_execution_plan["Execution plan<br/>[EmitModelPlan.cpp]"]
  node_profile_instrumentation["Profile instrumentation"]
  node_profile_runtime["Profile runtime<br/>[profile_runtime.c]"]
  node_profile_reports["Attribution reports<br/>[attr_report.py]"]
end

node_model_files["ncnn model files"]
node_compile_user(("C/C++ consumer"))

node_model_files -->|"provides inputs"| node_compile_cli
node_compile_cli -->|"starts session"| node_compile_session
node_model_files -.->|"provides inputs"| node_debug_driver
node_debug_driver -->|"parses parameters"| node_graph_parser
node_compile_session -->|"imports model"| node_graph_parser
node_graph_parser -->|"builds graph"| node_graph_model
node_graph_model -->|"supplies layers"| node_importer
node_debug_driver -->|"runs frontend"| node_importer
node_importer -->|"builds IR"| node_ncnn_dialect
node_compile_session -->|"runs pipeline"| node_conversion_pipeline
node_conversion_pipeline -->|"lowers NCNN"| node_tosa_lowering
node_tosa_lowering -->|"produces Linalg"| node_linalg_stage
node_linalg_stage -->|"lowers to buffers"| node_memref_stage
node_memref_stage -->|"lowers to LLVM"| node_llvm_stage
node_compile_session -->|"emits ABI"| node_abi
node_compile_session -.->|"requests plan"| node_execution_plan
node_compile_session -->|"publishes outputs"| node_native_artifacts
node_abi -->|"emits header"| node_native_artifacts
node_llvm_stage -->|"feeds native link"| node_native_artifacts
node_compile_user -->|"calls model API"| node_native_artifacts
node_conversion_pipeline -.->|"adds instrumentation"| node_profile_instrumentation
node_profile_instrumentation -.->|"emits callbacks"| node_profile_runtime
node_profile_runtime -.->|"provides measurements"| node_profile_reports
node_execution_plan -.->|"provides plan data"| node_profile_reports

click node_compile_cli "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/ncnn-compile.cpp"
click node_compile_session "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/CompileSession.cpp"
click node_graph_parser "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Graph/parser.cpp"
click node_graph_model "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Graph/graph.cpp"
click node_importer "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Importer/NCNNImporter.cpp"
click node_debug_driver "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/ncnn-mlir-driver.cpp"
click node_ncnn_dialect "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Dialect/NCNN/IR/NCNNDialect.cpp"
click node_conversion_pipeline "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Pipelines/NCNNPipelines.cpp"
click node_tosa_lowering "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Conversion/NCNNToTosa/NCNNToTosa.cpp"
click node_linalg_stage "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/StrategyNCNN/StrategyNCNN.cpp"
click node_memref_stage "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/BufferizeNCNN/BufferizeNCNN.cpp"
click node_execution_plan "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/EmitModelPlan/EmitModelPlan.cpp"
click node_llvm_stage "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Pipelines/NCNNPipelines.cpp"
click node_abi "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/GenerateCAPI/GenerateCAPIEmitWrapper.cpp"
click node_native_artifacts "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/CompileSession.hpp"
click node_profile_instrumentation "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/InstrumentNCNNProfile/InstrumentNCNNProfile.cpp"
click node_profile_runtime "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/ProfileRuntime/profile_runtime.c"
click node_profile_reports "https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/attr/attr_report.py"

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_compile_cli,node_compile_session,node_graph_parser,node_graph_model,node_importer,node_debug_driver toneBlue
class node_ncnn_dialect,node_conversion_pipeline,node_tosa_lowering,node_linalg_stage,node_memref_stage toneAmber
class node_llvm_stage,node_abi,node_native_artifacts toneMint
class node_execution_plan,node_profile_instrumentation,node_profile_runtime,node_profile_reports toneRose
class node_model_files,node_compile_user toneIndigo
```

## Components

### Model frontend

- [ncnn-compile](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/ncnn-compile.cpp): `tools/ncnn-compile.cpp`
- [Compile session](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/CompileSession.cpp): `tools/CompileSession.cpp`
- [Graph parser](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Graph/parser.cpp): `lib/Graph/parser.cpp`
- [Parsed graph](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Graph/graph.cpp): `lib/Graph/graph.cpp`
- [NCNN importer](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Importer/NCNNImporter.cpp): `lib/Importer/NCNNImporter.cpp`
- [MLIR debug driver](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/ncnn-mlir-driver.cpp): `tools/ncnn-mlir-driver.cpp`

### IR lowering

- [NCNN dialect](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Dialect/NCNN/IR/NCNNDialect.cpp): `lib/Dialect/NCNN/IR/NCNNDialect.cpp`
- [NCNN to TOSA](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Pipelines/NCNNPipelines.cpp): `lib/Pipelines/NCNNPipelines.cpp`
- [TOSA conversion](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Conversion/NCNNToTosa/NCNNToTosa.cpp): `lib/Conversion/NCNNToTosa/NCNNToTosa.cpp`
- [Linalg optimization](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/StrategyNCNN/StrategyNCNN.cpp): `lib/Transforms/StrategyNCNN/StrategyNCNN.cpp`
- [Bufferization](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/BufferizeNCNN/BufferizeNCNN.cpp): `lib/Transforms/BufferizeNCNN/BufferizeNCNN.cpp`

### Code generation

- [LLVM lowering](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Pipelines/NCNNPipelines.cpp): `lib/Pipelines/NCNNPipelines.cpp`
- [C ABI generation](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/GenerateCAPI/GenerateCAPIEmitWrapper.cpp): `lib/Transforms/GenerateCAPI/GenerateCAPIEmitWrapper.cpp`
- [Header and shared library](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/CompileSession.hpp): `tools/CompileSession.hpp`

### Plans and profiling

- [Execution plan](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/EmitModelPlan/EmitModelPlan.cpp): `lib/Transforms/EmitModelPlan/EmitModelPlan.cpp`
- [Profile instrumentation](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/Transforms/InstrumentNCNNProfile/InstrumentNCNNProfile.cpp): `lib/Transforms/InstrumentNCNNProfile/InstrumentNCNNProfile.cpp`
- [Profile runtime](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/lib/ProfileRuntime/profile_runtime.c): `lib/ProfileRuntime/profile_runtime.c`
- [Attribution reports](https://github.com/hqu-little-boy/ncnn-compiler/blob/master/tools/attr/attr_report.py): `tools/attr/attr_report.py`

### Other components

- **ncnn model files**
- **C/C++ consumer**

## Connections

- ncnn model files → ncnn-compile: provides inputs
- ncnn-compile → Compile session: starts session
- ncnn model files → MLIR debug driver: provides inputs
- MLIR debug driver → Graph parser: parses parameters
- Compile session → Graph parser: imports model
- Graph parser → Parsed graph: builds graph
- Parsed graph → NCNN importer: supplies layers
- MLIR debug driver → NCNN importer: runs frontend
- NCNN importer → NCNN dialect: builds IR
- Compile session → NCNN to TOSA: runs pipeline
- NCNN to TOSA → TOSA conversion: lowers NCNN
- TOSA conversion → Linalg optimization: produces Linalg
- Linalg optimization → Bufferization: lowers to buffers
- Bufferization → LLVM lowering: lowers to LLVM
- Compile session → C ABI generation: emits ABI
- Compile session → Execution plan: requests plan
- Compile session → Header and shared library: publishes outputs
- C ABI generation → Header and shared library: emits header
- LLVM lowering → Header and shared library: feeds native link
- C/C++ consumer → Header and shared library: calls model API
- NCNN to TOSA → Profile instrumentation: adds instrumentation
- Profile instrumentation → Profile runtime: emits callbacks
- Profile runtime → Attribution reports: provides measurements
- Execution plan → Attribution reports: provides plan data
