# notlate-cn/ascend-mlir architecture

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

- Interactive diagram: https://gitdiagram.com/notlate-cn/ascend-mlir
- Repository: https://github.com/notlate-cn/ascend-mlir

## Overview

1. **Overall shape**
Ascend-MLIR is an MLIR-based compiler/runtime stack for Ascend hardware. The repository is split into compiler IR and lowering logic, runtime artifact/execution logic, Python frontends, CLI tools, and a broad test/examples layer. External compiler dependencies are brought in as submodules: LLVM/MLIR, StableHLO, and PyAsc/ASC-IR.

2. **Primary IR layer: AFIR**
The core internal dialect is AFIR (Ascend Frontend IR). It defines high-level tensor ops such as elementwise math, broadcast, concat, reduce, cast, and condition ops. AFIR also has transform ops and shape-inference/normalization passes, so it acts as the repository’s main semantic boundary before target-specific lowering.

3. **Dialect transforms and canonicalization**
AFIR is not used as-is; it is rewritten by a set of dialect-local transforms in `lib/Dialect/AFIR/Transforms` and conversion passes in `lib/Conversion`. These passes handle shape inference, canonicalization, broadcast handling, linalg marking, and other rewrites that prepare the IR for backend lowering.

4. **Lowering pipeline**
The main lowering path appears to be:
StableHLO or other structured input → AFIR → ASC-IR / AscendC / target-specific kernel code.
The conversion layer includes AFIR-to-ASCIR conversion, text export, Linalg-to-AscendC lowering, gather-elementwise fusion, canonicalization of CANN signatures, buffer placement, parallelization, and preparation-for-emit passes. This is the main compile-time data flow in the repo.

5. **Target code generation**
`lib/Target/CannKernel` and `include/Target/CannKernel` hold the target-facing emitters and translation layer for CANN/Ascend kernel generation. This is the final codegen boundary for producing hardware-executable kernel artifacts from lowered IR.

6. **Mix subsystem for matmul specialization**
A major architectural subsystem is `Runtime/Mix`, which is focused on matmul-oriented compilation and tiling. It contains ABI extraction, source analysis, tiling backends, tiling generators, compile metadata, direct compile pipelines, stub templates, and a native matmul planner. This looks like a specialized compile path for generating optimized matmul artifacts, separate from the more general IR lowering path.

7. **Runtime artifact model**
The runtime is organized around artifacts, manifests, and session requests. `ArtifactCompiler`, `VecCubeArtifactBackend`, `Mix` artifact support, `RunManifest`, and `RuntimeSessionRequestBuilder` form the compile-and-package boundary. The runtime does not just execute raw kernels; it packages them into backend-specific artifacts with metadata and launch parameters.

8. **Execution stack and backends**
Execution is layered through `ExecutionSession`, `ExecutionRunner`, `RuntimeFrontendCore`, and backend implementations such as `SimBackend`, `NativeExecutionRunner`, and `NpuBackend`. `ExecutionBackend`, `BackendCapabilities`, and `DynamicLibraryArtifactEnv` define the abstraction surface. This makes the runtime backend-pluggable: simulation, native host execution, and real NPU execution are distinct runtime targets.

9. **Scheduling, resources, and observability**
The runtime has explicit resource management via `GlobalScheduler` and `ResourceScheduler`, plus launch tracing and profile utilities under `lib/Runtime/Profile` and `lib/Runtime/Execution`. The presence of scheduler, trace, and comparator components suggests the runtime is designed to manage multi-session execution, admission/resource constraints, and correctness/performance validation.

10. **Python and higher-level integration**
Python is a first-class integration layer. There are Python bindings for AFIR, runtime helpers, compiler/executor utilities, tiling schema helpers, and Torch-facing conversion/pipeline code under `python/torch`. This is the main bridge for model-level workflows and higher-level automation above the C++ core.

11. **CLI and tooling boundary**
The repository ships several user-facing tools: `afir-opt`, `afir-translate`, `mix-compiler`, `mix-tiling-helper`, and `runtime-session`. These are thin entrypoints into the compiler/runtime subsystems, exposing optimization, translation, compilation, tiling selection, and session execution as separate command-line surfaces.

12. **Examples, tests, and validation**
`examples/` demonstrates the lowering/runtime pipeline on small MLIR programs, Torch end-to-end flows, and real NPU microcases. `test/` and `test/tools/runtime` cover dialect, conversion, target translation, runtime behavior, tiling schema, and Mix-specific logic. The docs directory also shows a strong emphasis on runtime design reviews, scheduler policy, and NPU validation, indicating that runtime correctness and deployment behavior are major architectural concerns.

Main entry points: [`lib/Conversion`](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Conversion) (Conversion passes), [`lib/Runtime/Execution`](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Execution) (Execution core), [`python`](https://github.com/notlate-cn/ascend-mlir/tree/main/python) (Python bindings), [`lib/Target/CannKernel`](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Target/CannKernel) (CANN kernel emit)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_compiler["Compiler IR"]
  node_dialect_afir{{"AFIR<br/>dialect"}}
  node_afir_transforms["AFIR transforms<br/>pass suite"]
  node_afir_transform_ops["AFIR transform ops<br/>transform dialect"]
  node_conversion_core["Conversion passes<br/>lowering passes"]
  node_afir_to_ascir["AFIR→ASCIR<br/>converter"]
  node_linalg_to_ascendc["Linalg→AscendC<br/>converter"]
  node_cann_target["CANN kernel emit<br/>target backend"]
end

subgraph group_runtime["Runtime"]
  node_mix_runtime["Mix compiler<br/>specialized pipeline"]
  node_artifact_model["Artifact model<br/>artifact layer"]
  node_execution_core["Execution core<br/>session/execution"]
  node_schedulers["Schedulers<br/>resource control"]
  node_profile_support["Profiling<br/>observability"]
  node_runtime_support["Runtime support<br/>utilities"]
end

subgraph group_python["Python"]
  node_python_bindings["Python bindings"]
  node_python_torch["Torch bridge<br/>integration"]
end

subgraph group_tools["Tools"]
  node_cli_tools["CLI tools<br/>entrypoints"]
end

subgraph group_externals["Externals"]
  node_externals_llvm_mlir["LLVM/MLIR<br/>submodule"]
  node_externals_stablehlo["StableHLO<br/>submodule"]
  node_externals_pyasc["PyAsc<br/>submodule"]
end

node_externals_llvm_mlir -->|"MLIR base"| node_dialect_afir
node_externals_stablehlo -->|"structured input"| node_conversion_core
node_externals_pyasc -->|"frontend dep"| node_python_bindings
node_dialect_afir -->|"rewritten by"| node_afir_transforms
node_afir_transform_ops -->|"drives"| node_afir_transforms
node_afir_transforms -->|"prepares"| node_conversion_core
node_conversion_core -->|"includes"| node_afir_to_ascir
node_conversion_core -->|"includes"| node_linalg_to_ascendc
node_conversion_core -->|"emits to"| node_cann_target
node_afir_to_ascir -->|"feeds"| node_cann_target
node_linalg_to_ascendc -->|"feeds"| node_cann_target
node_mix_runtime -->|"packages"| node_artifact_model
node_mix_runtime -->|"specializes"| node_cann_target
node_artifact_model -->|"launched by"| node_execution_core
node_execution_core -->|"uses"| node_schedulers
node_execution_core -->|"traced by"| node_profile_support
node_execution_core -->|"depends on"| node_runtime_support
node_python_bindings -->|"binds"| node_dialect_afir
node_python_bindings -->|"binds"| node_artifact_model
node_python_torch -->|"invokes"| node_conversion_core
node_python_torch -->|"builds on"| node_python_bindings
node_cli_tools -->|"fronts"| node_conversion_core
node_cli_tools -->|"fronts"| node_execution_core
node_cli_tools -->|"fronts"| node_mix_runtime

click node_externals_llvm_mlir "https://github.com/notlate-cn/ascend-mlir/tree/main/externals/llvm-project"
click node_externals_stablehlo "https://github.com/notlate-cn/ascend-mlir/tree/main/externals/stablehlo"
click node_externals_pyasc "https://github.com/notlate-cn/ascend-mlir/tree/main/externals/pyasc"
click node_dialect_afir "https://github.com/notlate-cn/ascend-mlir/tree/main/include/Dialect/AFIR"
click node_afir_transforms "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Dialect/AFIR/Transforms"
click node_afir_transform_ops "https://github.com/notlate-cn/ascend-mlir/tree/main/include/Dialect/AFIR/TransformOps"
click node_conversion_core "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Conversion"
click node_afir_to_ascir "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Conversion/AFIRToASCIR"
click node_linalg_to_ascendc "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Conversion/LinalgToAscendC"
click node_cann_target "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Target/CannKernel"
click node_mix_runtime "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Mix"
click node_artifact_model "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Artifact"
click node_execution_core "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Execution"
click node_schedulers "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Execution"
click node_profile_support "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Profile"
click node_runtime_support "https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Support"
click node_python_bindings "https://github.com/notlate-cn/ascend-mlir/tree/main/python"
click node_python_torch "https://github.com/notlate-cn/ascend-mlir/tree/main/python/torch"
click node_cli_tools "https://github.com/notlate-cn/ascend-mlir/tree/main/tools"

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_dialect_afir,node_afir_transforms,node_afir_transform_ops,node_conversion_core,node_afir_to_ascir,node_linalg_to_ascendc,node_cann_target toneBlue
class node_mix_runtime,node_artifact_model,node_execution_core,node_schedulers,node_profile_support,node_runtime_support toneAmber
class node_python_bindings,node_python_torch toneMint
class node_cli_tools toneRose
class node_externals_llvm_mlir,node_externals_stablehlo,node_externals_pyasc toneIndigo
```

## Components

### Compiler IR

MLIR dialects, canonicalization, and lowering toward target code.

- [AFIR](https://github.com/notlate-cn/ascend-mlir/tree/main/include/Dialect/AFIR): dialect · Core Ascend Frontend IR dialect and semantic boundary. · `include/Dialect/AFIR`
- [AFIR transforms](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Dialect/AFIR/Transforms): pass suite · Shape inference, canonicalization, and local IR rewrites. · `lib/Dialect/AFIR/Transforms`
- [AFIR transform ops](https://github.com/notlate-cn/ascend-mlir/tree/main/include/Dialect/AFIR/TransformOps): transform dialect · Transform IR operations used to drive AFIR rewrites. · `include/Dialect/AFIR/TransformOps`
- [Conversion passes](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Conversion): lowering passes · Main lowering and rewrite pipeline from AFIR to backend-ready IR. · `lib/Conversion`
- [AFIR→ASCIR](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Conversion/AFIRToASCIR): converter · Converts AFIR into ASC-IR and text form. · `lib/Conversion/AFIRToASCIR`
- [Linalg→AscendC](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Conversion/LinalgToAscendC): converter · Lowers structured/linalg operations into AscendC-oriented IR. · `lib/Conversion/LinalgToAscendC`
- [CANN kernel emit](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Target/CannKernel): target backend · Emitter and translation layer for hardware-targeted kernel generation. · `lib/Target/CannKernel`

### Runtime

Artifact packaging, execution sessions, scheduling, and backends.

- [Mix compiler](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Mix): specialized pipeline · Matmul-focused compilation, tiling, and artifact generation path. · `lib/Runtime/Mix`
- [Artifact model](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Artifact): artifact layer · Compiler outputs, manifests, and runtime session request packaging. · `lib/Runtime/Artifact`
- [Execution core](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Execution): session/execution · Execution sessions, runner orchestration, and backend abstraction. · `lib/Runtime/Execution`
- [Schedulers](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Execution): resource control · Global and resource scheduling for sessions and launches. · `lib/Runtime/Execution`
- [Profiling](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Profile): observability · Launch tracing and profile utilities for runtime validation. · `lib/Runtime/Profile`
- [Runtime support](https://github.com/notlate-cn/ascend-mlir/tree/main/lib/Runtime/Support): utilities · Shared runtime utilities such as schema, tiling packs, paths, and IO. · `lib/Runtime/Support`

### Python

Bindings and higher-level integration layers.

- [Python bindings](https://github.com/notlate-cn/ascend-mlir/tree/main/python): bindings · CAPI and Python module bindings for AFIR and runtime. · `python`
- [Torch bridge](https://github.com/notlate-cn/ascend-mlir/tree/main/python/torch): integration · Torch-facing conversion and pipeline helpers. · `python/torch`

### Tools

Command-line entrypoints into the compiler and runtime.

- [CLI tools](https://github.com/notlate-cn/ascend-mlir/tree/main/tools): entrypoints · User-facing command line entrypoints for optimization, translation, compilation, tiling, and sessions. · `tools`

### Externals

Vendored compiler and frontend dependencies.

- [LLVM/MLIR](https://github.com/notlate-cn/ascend-mlir/tree/main/externals/llvm-project): submodule · Vendored LLVM and MLIR toolchain dependency. · `externals/llvm-project`
- [StableHLO](https://github.com/notlate-cn/ascend-mlir/tree/main/externals/stablehlo): submodule · Vendored StableHLO input dialect dependency. · `externals/stablehlo`
- [PyAsc](https://github.com/notlate-cn/ascend-mlir/tree/main/externals/pyasc): submodule · Vendored Ascend/Python frontend dependency. · `externals/pyasc`

## Connections

- LLVM/MLIR → AFIR: MLIR base · AFIR is built on the MLIR infrastructure.
- StableHLO → Conversion passes: structured input · StableHLO and similar structured inputs feed the lowering pipeline.
- PyAsc → Python bindings: frontend dep · Python integration depends on Ascend/Python frontend components.
- AFIR → AFIR transforms: rewritten by · AFIR is normalized and simplified by dialect-local passes.
- AFIR transform ops → AFIR transforms: drives · Transform ops guide AFIR rewrite workflows.
- AFIR transforms → Conversion passes: prepares · Canonicalized AFIR flows into backend lowering passes.
- Conversion passes → AFIR→ASCIR: includes · AFIR-to-ASCIR conversion is a primary lowering stage.
- Conversion passes → Linalg→AscendC: includes · Structured lowering to AscendC is handled here.
- Conversion passes → CANN kernel emit: emits to · Lowered IR is translated into target kernel artifacts.
- AFIR→ASCIR → CANN kernel emit: feeds · ASCIR conversion is a direct path toward target emission.
- Linalg→AscendC → CANN kernel emit: feeds · AscendC lowering feeds the kernel translation boundary.
- Mix compiler → Artifact model: packages · Mix generates matmul-specific artifacts and metadata.
- Mix compiler → CANN kernel emit: specializes · Mix is a specialized compile path for target-oriented matmul codegen.
- Artifact model → Execution core: launched by · Artifacts are consumed by runtime execution sessions and runners.
- Execution core → Schedulers: uses · Execution is mediated by global and resource schedulers.
- Execution core → Profiling: traced by · Launch tracing and profile utilities observe execution behavior.
- Execution core → Runtime support: depends on · Execution uses shared runtime utilities and schemas.
- Python bindings → AFIR: binds · Python exposes AFIR constructs and helpers.
- Python bindings → Artifact model: binds · Python exposes runtime artifact and session helpers.
- Torch bridge → Conversion passes: invokes · Torch integration drives compiler pipelines and conversion flows.
- Torch bridge → Python bindings: builds on · Torch helpers sit above the lower-level Python bindings.
- CLI tools → Conversion passes: fronts · afir-opt and related tools expose compiler passes.
- CLI tools → Execution core: fronts · runtime-session and related tools expose runtime execution.
- CLI tools → Mix compiler: fronts · mix-compiler and tiling helper expose the Mix pipeline.
