How notlate-cn/ascend-mlir works
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: Conversion Conversion passes · Execution Execution core · python Python bindings · CannKernel CANN kernel emit
Components 19
Compiler IR
MLIR dialects, canonicalization, and lowering toward target code.
- AFIR · Core Ascend Frontend IR dialect and semantic boundary.
include/Dialect/AFIR - AFIR transforms · Shape inference, canonicalization, and local IR rewrites.
lib/Dialect/AFIR/Transforms - AFIR transform ops · Transform IR operations used to drive AFIR rewrites.
include/Dialect/AFIR/TransformOps - Conversion passes · Main lowering and rewrite pipeline from AFIR to backend-ready IR.
lib/Conversion - AFIR→ASCIR · Converts AFIR into ASC-IR and text form.
lib/Conversion/AFIRToASCIR - Linalg→AscendC · Lowers structured/linalg operations into AscendC-oriented IR.
lib/Conversion/LinalgToAscendC - CANN kernel emit · Emitter and translation layer for hardware-targeted kernel generation.
lib/Target/CannKernel
Runtime
Artifact packaging, execution sessions, scheduling, and backends.
- Mix compiler · Matmul-focused compilation, tiling, and artifact generation path.
lib/Runtime/Mix - Artifact model · Compiler outputs, manifests, and runtime session request packaging.
lib/Runtime/Artifact - Execution core · Execution sessions, runner orchestration, and backend abstraction.
lib/Runtime/Execution - Schedulers · Global and resource scheduling for sessions and launches.
lib/Runtime/Execution - Profiling · Launch tracing and profile utilities for runtime validation.
lib/Runtime/Profile - Runtime support · Shared runtime utilities such as schema, tiling packs, paths, and IO.
lib/Runtime/Support
Python
Bindings and higher-level integration layers.
- Python bindings · CAPI and Python module bindings for AFIR and runtime.
python - Torch bridge · Torch-facing conversion and pipeline helpers.
python/torch
Tools
Command-line entrypoints into the compiler and runtime.
- CLI tools · User-facing command line entrypoints for optimization, translation, compilation, tiling, and sessions.
tools
Connections 24
- 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.