# llvm/torch-mlir architecture

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

- Interactive diagram: https://gitdiagram.com/llvm/torch-mlir
- Repository: https://github.com/llvm/torch-mlir

## Overview

Torch-MLIR bridges PyTorch-family inputs into MLIR: documented entry routes include FX and ONNX, with a Torch dialect and conversion passes forming the compiler core. The tree also shows a JIT importer, a lazy-tensor integration, C/Python APIs, and several lowering targets. The principal path is input import → Torch IR → target lowering; the reference backend provides a separate execution-oriented component. Some lowerings and subsystem links are inferred from the tree rather than sampled call sites, so those links are dashed and unverified.

Main entry points: [`lib/Dialect/Torch/IR/TorchOps.cpp`](https://github.com/llvm/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchOps.cpp) (Torch dialect), [`lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp`](https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp) (ONNX conversion), [`lib/InitAll.cpp`](https://github.com/llvm/torch-mlir/blob/main/lib/InitAll.cpp) (Pass registration), [`projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp`](https://github.com/llvm/torch-mlir/blob/main/projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp) (JIT importer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontends["Input frontends"]
  node_fx["FX importer<br/>[fx_importer.py]"]
  node_fx_api["FX API<br/>[fx.py]"]
  node_onnx_import["ONNX importer<br/>[OnnxImporter.cpp]"]
  node_onnx_convert["ONNX conversion"]
  node_jit_import["JIT importer"]
end

subgraph group_ir["Torch IR"]
  node_torch_dialect["Torch dialect<br/>[TorchOps.cpp]"]
  node_torch_types["Torch types<br/>[TorchTypes.cpp]"]
end

subgraph group_compiler["Compiler passes"]
  node_torch_transforms["Torch transforms"]
  node_conversion["Pass registration<br/>[InitAll.cpp]"]
end

subgraph group_targets["Targets and integrations"]
  node_linalg["Linalg lowering<br/>[Uncategorized.cpp]"]
  node_stablehlo["StableHLO lowering"]
  node_tosa["TOSA lowering<br/>[TorchToTosa.cpp]"]
  node_tmtensor["TMTensor lowering"]
  node_refbackend["Reference backend<br/>[RefBackend.cpp]"]
  node_ltc["Lazy backend"]
  node_api["C API<br/>[TorchOps.cpp]"]
  node_opt["MLIR optimizer<br/>[torch-mlir-opt.cpp]"]
end

node_pytorch(("PyTorch"))
node_onnx["ONNX models"]

node_pytorch -->|"provides FX"| node_fx_api
node_fx_api -->|"uses importer"| node_fx
node_onnx -.->|"provides model"| node_onnx_import
node_onnx_import -.->|"imports for conversion"| node_onnx_convert
node_onnx -->|"ONNX entry"| node_onnx_convert
node_pytorch -.->|"provides JIT input"| node_jit_import
node_fx -->|"emits Torch IR"| node_torch_dialect
node_onnx_convert -->|"rewrites to Torch ops"| node_torch_dialect
node_jit_import -.->|"imports Torch IR"| node_torch_dialect
node_torch_dialect -->|"uses types"| node_torch_types
node_torch_transforms -->|"rewrites ops"| node_torch_dialect
node_conversion -->|"registers passes"| node_torch_transforms
node_conversion -->|"registers passes"| node_onnx_convert
node_torch_dialect -.->|"lowering input"| node_linalg
node_torch_dialect -.->|"lowering input"| node_stablehlo
node_torch_dialect -.->|"lowering input"| node_tosa
node_torch_dialect -->|"lowering input"| node_tmtensor
node_ltc -->|"imports functions"| node_jit_import
node_opt -->|"registers dialects"| node_conversion
node_api -->|"exposes ops"| node_torch_dialect
node_refbackend -->|"uses conversion ops"| node_torch_dialect
node_linalg -.->|"backend path"| node_refbackend

click node_fx "https://github.com/llvm/torch-mlir/blob/main/python/torch_mlir/extras/fx_importer.py"
click node_fx_api "https://github.com/llvm/torch-mlir/blob/main/python/torch_mlir/fx.py"
click node_onnx_import "https://github.com/llvm/torch-mlir/blob/main/projects/onnx_c_importer/OnnxImporter.cpp"
click node_onnx_convert "https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp"
click node_jit_import "https://github.com/llvm/torch-mlir/blob/main/projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp"
click node_torch_dialect "https://github.com/llvm/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchOps.cpp"
click node_torch_types "https://github.com/llvm/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchTypes.cpp"
click node_torch_transforms "https://github.com/llvm/torch-mlir/blob/main/lib/Dialect/Torch/Transforms/ScalarizeShapes.cpp"
click node_conversion "https://github.com/llvm/torch-mlir/blob/main/lib/InitAll.cpp"
click node_linalg "https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToLinalg/Uncategorized.cpp"
click node_stablehlo "https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToStablehlo/TorchToStablehlo.cpp"
click node_tosa "https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToTosa/TorchToTosa.cpp"
click node_tmtensor "https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp"
click node_refbackend "https://github.com/llvm/torch-mlir/blob/main/lib/RefBackend/RefBackend.cpp"
click node_ltc "https://github.com/llvm/torch-mlir/blob/main/projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp"
click node_api "https://github.com/llvm/torch-mlir/blob/main/lib/CAPI/TorchOps.cpp"
click node_opt "https://github.com/llvm/torch-mlir/blob/main/tools/torch-mlir-opt/torch-mlir-opt.cpp"

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_fx,node_fx_api,node_onnx_import,node_onnx_convert,node_jit_import toneBlue
class node_torch_dialect,node_torch_types toneAmber
class node_torch_transforms,node_conversion toneMint
class node_linalg,node_stablehlo,node_tosa,node_tmtensor,node_refbackend,node_ltc,node_api,node_opt toneRose
class node_pytorch,node_onnx toneIndigo
```

## Components

### Input frontends

- [FX importer](https://github.com/llvm/torch-mlir/blob/main/python/torch_mlir/extras/fx_importer.py): `python/torch_mlir/extras/fx_importer.py`
- [FX API](https://github.com/llvm/torch-mlir/blob/main/python/torch_mlir/fx.py): `python/torch_mlir/fx.py`
- [ONNX importer](https://github.com/llvm/torch-mlir/blob/main/projects/onnx_c_importer/OnnxImporter.cpp): `projects/onnx_c_importer/OnnxImporter.cpp`
- [ONNX conversion](https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp): `lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp`
- [JIT importer](https://github.com/llvm/torch-mlir/blob/main/projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp): `projects/jit_ir_common/csrc/jit_ir_importer/function_importer.cpp`

### Torch IR

- [Torch dialect](https://github.com/llvm/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchOps.cpp): `lib/Dialect/Torch/IR/TorchOps.cpp`
- [Torch types](https://github.com/llvm/torch-mlir/blob/main/lib/Dialect/Torch/IR/TorchTypes.cpp): `lib/Dialect/Torch/IR/TorchTypes.cpp`

### Compiler passes

- [Torch transforms](https://github.com/llvm/torch-mlir/blob/main/lib/Dialect/Torch/Transforms/ScalarizeShapes.cpp): `lib/Dialect/Torch/Transforms/ScalarizeShapes.cpp`
- [Pass registration](https://github.com/llvm/torch-mlir/blob/main/lib/InitAll.cpp): `lib/InitAll.cpp`

### Targets and integrations

- [Linalg lowering](https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToLinalg/Uncategorized.cpp): `lib/Conversion/TorchToLinalg/Uncategorized.cpp`
- [StableHLO lowering](https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToStablehlo/TorchToStablehlo.cpp): `lib/Conversion/TorchToStablehlo/TorchToStablehlo.cpp`
- [TOSA lowering](https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToTosa/TorchToTosa.cpp): `lib/Conversion/TorchToTosa/TorchToTosa.cpp`
- [TMTensor lowering](https://github.com/llvm/torch-mlir/blob/main/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp): `lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp`
- [Reference backend](https://github.com/llvm/torch-mlir/blob/main/lib/RefBackend/RefBackend.cpp): `lib/RefBackend/RefBackend.cpp`
- [Lazy backend](https://github.com/llvm/torch-mlir/blob/main/projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp): `projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp`
- [C API](https://github.com/llvm/torch-mlir/blob/main/lib/CAPI/TorchOps.cpp): `lib/CAPI/TorchOps.cpp`
- [MLIR optimizer](https://github.com/llvm/torch-mlir/blob/main/tools/torch-mlir-opt/torch-mlir-opt.cpp): `tools/torch-mlir-opt/torch-mlir-opt.cpp`

### Other components

- **PyTorch**
- **ONNX models**

## Connections

- PyTorch → FX API: provides FX (evidence: `README.md`)
- FX API → FX importer: uses importer (evidence: `python/torch_mlir/fx.py`)
- ONNX models → ONNX importer: provides model (evidence: `README.md`)
- ONNX importer → ONNX conversion: imports for conversion
- ONNX models → ONNX conversion: ONNX entry (evidence: `README.md`)
- PyTorch → JIT importer: provides JIT input
- FX importer → Torch dialect: emits Torch IR (evidence: `README.md`)
- ONNX conversion → Torch dialect: rewrites to Torch ops (evidence: `lib/Conversion/TorchOnnxToTorch/DefaultDomainQtoZ.cpp`)
- JIT importer → Torch dialect: imports Torch IR
- Torch dialect → Torch types: uses types (evidence: `lib/Dialect/Torch/IR/TorchOps.cpp`)
- Torch transforms → Torch dialect: rewrites ops (evidence: `lib/Dialect/Torch/Transforms/ScalarizeShapes.cpp`)
- Pass registration → Torch transforms: registers passes (evidence: `lib/InitAll.cpp`)
- Pass registration → ONNX conversion: registers passes (evidence: `lib/InitAll.cpp`)
- Torch dialect → Linalg lowering: lowering input
- Torch dialect → StableHLO lowering: lowering input
- Torch dialect → TOSA lowering: lowering input
- Torch dialect → TMTensor lowering: lowering input (evidence: `lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp`)
- Lazy backend → JIT importer: imports functions (evidence: `projects/ltc/csrc/base_lazy_backend/mlir_lowering_context.cpp`)
- MLIR optimizer → Pass registration: registers dialects (evidence: `tools/torch-mlir-opt/torch-mlir-opt.cpp`)
- C API → Torch dialect: exposes ops (evidence: `lib/CAPI/TorchOps.cpp`)
- Reference backend → Torch dialect: uses conversion ops (evidence: `lib/RefBackend/RefBackend.cpp`)
- Linalg lowering → Reference backend: backend path
