# microsoft/triton-shared architecture

> Architecture diagram of the GitHub repository microsoft/triton-shared, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/microsoft/triton-shared
- Repository: https://github.com/microsoft/triton-shared

## Overview

##### Project type and purpose
- This is a compiler‐related project: a shared middle‐layer for the Triton compiler that lowers Triton IR into MLIR core dialects (Linalg, Tensor, Memref), enabling reuse of analyses and backends across Triton targets and other languages.
- Primary use cases:
- Stand‐alone conversion tool (`triton-shared-opt`) for Triton→MLIR transformations
- Out-of-tree backend component for Triton (e.g., reference CPU backend via Python driver)
- Integration in Triton’s compilation pipeline to emit hardware‐specific IR

##### File structure
- Top‐level build and governance: `CMakeLists.txt`, `.github/`, `LICENSE`, `CONTRIBUTING.md`
- Core middle layer source:
- `include/triton-shared/` – public headers (subdirs: Analysis, Conversion, Dialect, Transform, Utils)
- `lib/` – implementation of analyses, conversion passes, dialects, transforms, utils
- Backend glue:
- `backend/compiler.py` & `backend/driver.py` – Python bindings and driver for the reference CPU backend
- `backend/include/ExecutionEngine` – CRunner utilities, sanitizer helpers
- Tools:
- `tools/triton-shared-opt/` – standalone CLI executable that wires MLIR passes and registration of Triton‐shared dialects
- Python integration: `python/examples/` – sample Triton programs and pytest harness
- Scripts: build and sanitizer setup under `scripts/`
- Tests:
- `test/` – lit tests covering conversion scenarios, Sanitizer tests
- Python example tests under `python/examples` run with pytest

##### README for additional insights
- Describes compiler pipeline:
[Triton IR] → [Middle Layer (MLIR Linalg & Memref)] → [HW-specific IR]
- Key analyses: Pointer analysis, Use analysis, Mask analysis
- Conversion passes: TritonToLinalg, bufferization, lowering of loads/stores to memref ops, translation of elementwise ops to linalg.generic, dot→linalg.matmul, reduce→linalg.reduce
- Build & usage instructions: CMake + Ninja + pybind11 for Python driver, plugin registration via `TRITON_PLUGIN_DIRS`

1. Triton Frontend (external)
- Triton IR generator (from Python/CLI)
2. Middle Layer (this project)
- Public API (headers in `include/triton-shared/`)
- Implementation libraries (`lib/*`)
- Analyses (Pointer, Use, Mask)
- Conversion Passes (TritonToLinalg, StructuredToMemref, TritonArithToLinalg, etc.)
- Dialect definitions (TritonStructured, TPtr, TritonTilingExt)
- Transforms (AddLLVMDebugInfo)
3. Tools & Drivers
- `triton-shared-opt` CLI (in `tools/`)
- Python driver (`backend/driver.py`, `backend/compiler.py`)
4. Backend / Hardware‐specific IR (external)
5. Tests and Examples
- lit tests (`test/`)
- Python examples & pytest (`python/examples/`)
6. Build System & Scripts
- CMake, Ninja targets, sanitizer scripts (`scripts/`)
1. Triton Frontend → invokes triton-shared‐opt OR Triton plugin registration
2. triton-shared‐opt / Python driver → MLIR Pass Manager →
a. Analyses → Conversion Passes → Bufferization → HW‐specific IR
3. Conversion Passes ↔ Dialect Definitions (they register ops/types)
4. Tests/Examples → Python driver / CLI tool → Middle Layer → validate IR dumps
c. Architectural patterns and principles
- Compiler pipeline / pass‐manager architecture
- Modular layering: clear separation of Analysis, Conversion, Dialect, Transform, Utils
- Plugin pattern for dialect registration (MLIR dialect registry)
- Out-of-tree backend integration via Python driver and CMake targets
d. Relevant technologies & frameworks
- MLIR (Linalg, Tensor, Memref dialects)
- LLVM (for code generation, sanitizers)
- CMake & Ninja (build)
- Python (pybind11 bindings, pytest)
- Triton compiler (as submodule)

##### Detail without overthinking
- Directory’s role as a separate component if it has unique functionality
- Detail both C++ and Python integration surfaces
- Build & test subsystems to give a full picture of how the project fits into Triton’s end-to-end pipeline

## Diagram (Mermaid)

```mermaid
graph TB
    classDef core fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef tool fill:#90EE90,stroke:#333,stroke-width:1px
    classDef test fill:#FFA500,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px

    TritonFE["Triton Frontend"]:::external
    HWBackend["HW-specific Backend"]:::external

    subgraph "Middle Layer"
        PublicAPI["Public API (headers)"]:::core
        Implementation["Core Implementation (lib/)"]:::core
        PassMgr["MLIR Pass Manager"]:::core

        subgraph "Analyses"
            AnalysesMod["Analyses Module"]:::core
            StructAnalyses["Structured Analyses"]:::core
        end

        subgraph "Conversion Passes"
            T2L["TritonToLinalg Pass"]:::core
            TA2L["TritonArithToLinalg Pass"]:::core
            S2M["StructuredToMemref Pass"]:::core
            TP2M["TritonPtrToMemref Pass"]:::core
            T2S["TritonToStructured Pass"]:::core
            T2U["TritonToUnstructured Pass"]:::core
            U2M["UnstructuredToMemref Pass"]:::core
            T2LEX["TritonToLinalgExperimental Pass"]:::core
        end

        subgraph "Dialects"
            TPtr["TPtr Dialect"]:::core
            TStruct["TritonStructured Dialect"]:::core
            TTiling["TritonTilingExt Dialect"]:::core
        end

        Transform["AddLLVMDebugInfo"]:::core
        Utils["Utilities Module"]:::core
    end

    subgraph "Tools & Drivers"
        CLI["triton-shared-opt CLI"]:::tool
        PyDriver["Python Driver"]:::tool
        CompilerBind["Python Compiler Binding"]:::tool
        ExecEngine["Backend ExecutionEngine Utils"]:::tool
    end

    subgraph "Tests & Examples"
        MLIRTests["MLIR lit Tests"]:::test
        PyExamples["Python Examples & Pytest"]:::test
    end

    subgraph "Build & Scripts"
        Sanitizer["Sanitizer Support"]:::core
        Scripts["Scripts (build & sanitizer)"]:::core
        BuildSys["Build System & Governance"]:::external
    end

    %% Data Flow
    TritonFE --> CLI
    TritonFE --> PyDriver
    CLI --> PassMgr
    PyDriver --> PassMgr
    PassMgr --> AnalysesMod
    PassMgr --> StructAnalyses
    PassMgr --> T2L
    PassMgr --> TA2L
    PassMgr --> S2M
    PassMgr --> TP2M
    PassMgr --> T2S
    PassMgr --> T2U
    PassMgr --> U2M
    PassMgr --> T2LEX
    PassMgr --> Transform
    AnalysesMod --> T2L
    StructAnalyses --> S2M
    Utils --> AnalysesMod
    Utils --> T2L
    Utils --> TA2L
    Utils --> S2M
    Utils --> TP2M
    Utils --> T2S
    Utils --> T2U
    Utils --> U2M
    Utils --> T2LEX
    TPtr --> PassMgr
    TStruct --> PassMgr
    TTiling --> PassMgr
    Transform --> HWBackend
    T2L --> HWBackend
    TA2L --> HWBackend
    S2M --> HWBackend
    TP2M --> HWBackend
    T2S --> HWBackend
    T2U --> HWBackend
    U2M --> HWBackend
    T2LEX --> HWBackend

    MLIRTests --> CLI
    MLIRTests --> PassMgr
    PyExamples --> PyDriver

    Scripts --> Sanitizer
    Scripts --> BuildSys

    %% Click Events
    click PublicAPI "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/"
    click Implementation "https://github.com/microsoft/triton-shared/tree/main/lib/"
    click AnalysesMod "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Analysis/"
    click AnalysesMod "https://github.com/microsoft/triton-shared/tree/main/lib/Analysis/"
    click StructAnalyses "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/AnalysisStructured/"
    click StructAnalyses "https://github.com/microsoft/triton-shared/tree/main/lib/AnalysisStructured/"
    click T2L "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/TritonToLinalg/"
    click T2L "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/TritonToLinalg/"
    click TA2L "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/TritonArithToLinalg/"
    click TA2L "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/TritonArithToLinalg/"
    click S2M "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/StructuredToMemref/"
    click S2M "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/StructuredToMemref/"
    click TP2M "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/TritonPtrToMemref/"
    click TP2M "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/TritonPtrToMemref/"
    click T2S "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/TritonToStructured/"
    click T2S "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/TritonToStructured/"
    click T2U "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/TritonToUnstructured/"
    click T2U "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/TritonToUnstructured/"
    click U2M "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/UnstructuredToMemref/"
    click U2M "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/UnstructuredToMemref/"
    click T2LEX "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Conversion/TritonToLinalgExperimental/"
    click T2LEX "https://github.com/microsoft/triton-shared/tree/main/lib/Conversion/TritonToLinalgExperimental/"
    click TPtr "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Dialect/TPtr/"
    click TPtr "https://github.com/microsoft/triton-shared/tree/main/lib/Dialect/TPtr/"
    click TStruct "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Dialect/TritonStructured/"
    click TStruct "https://github.com/microsoft/triton-shared/tree/main/lib/Dialect/TritonStructured/"
    click TTiling "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Dialect/TritonTilingExt/"
    click TTiling "https://github.com/microsoft/triton-shared/tree/main/lib/Dialect/TritonTilingExt/"
    click Transform "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Transform/AddLLVMDebugInfo/"
    click Transform "https://github.com/microsoft/triton-shared/tree/main/lib/Transform/AddLLVMDebugInfo/"
    click Utils "https://github.com/microsoft/triton-shared/tree/main/include/triton-shared/Utils/"
    click Utils "https://github.com/microsoft/triton-shared/tree/main/lib/Utils/"
    click CLI "https://github.com/microsoft/triton-shared/tree/main/tools/triton-shared-opt/"
    click PyDriver "https://github.com/microsoft/triton-shared/blob/main/backend/driver.py"
    click CompilerBind "https://github.com/microsoft/triton-shared/blob/main/backend/compiler.py"
    click ExecEngine "https://github.com/microsoft/triton-shared/tree/main/backend/include/ExecutionEngine/"
    click MLIRTests "https://github.com/microsoft/triton-shared/tree/main/test/"
    click PyExamples "https://github.com/microsoft/triton-shared/tree/main/python/examples/"
    click Sanitizer "https://github.com/microsoft/triton-shared/tree/main/lib/Sanitizer/"
    click Scripts "https://github.com/microsoft/triton-shared/tree/main/scripts/"
```
