# reactorlabs/rir architecture

> Architecture diagram of the GitHub repository reactorlabs/rir, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/reactorlabs/rir
- Repository: https://github.com/reactorlabs/rir

## Overview

##### Main Components

a. Core Components:
- RIR (R Intermediate Representation) Compiler
- PIR (Platform Independent Representation) Optimizer
- LLVM Backend
- GNU R Integration Layer
- Runtime System

b. Key Processing Stages:
- R Code Input
- RIR Compilation
- PIR Optimization
- LLVM Code Generation
- Native Code Execution

**[R Source Code Layer]**
- ↓
**[GNU R Integration Layer]**
- ↓
**[RIR Compiler Layer]**
- ↓
**[PIR Optimizer Layer]**
- ↓
**[LLVM Backend Layer]**
- ↓
**[Native Execution Layer]**

##### Component Details

a. RIR Compiler Layer:
- Bytecode compilation components (/rir/src/bc/)
- Interpreter components (/rir/src/interpreter/)
- Runtime components (/rir/src/runtime/)

b. PIR Optimizer Layer (most complex part):
- Analysis components (/rir/src/compiler/analysis/)
- Optimization passes (/rir/src/compiler/opt/)
- PIR representation (/rir/src/compiler/pir/)

c. LLVM Backend:
- Native code generation (/rir/src/compiler/native/)
- LLVM interface components

##### Special Features to Highlight

- JIT compilation trigger (after 3rd execution)
- Optimization feedback loops
- Type feedback system
- Deoptimization paths
- Context specialization

##### Interactions to Emphasize

- How the PIR optimizer interacts with both RIR and LLVM layers
- Feedback mechanisms for JIT compilation
- Demonstrate the integration points with GNU R
- Optimization pass scheduling

##### Additional Elements

- Version/build type indicators
- Major configuration points
- Performance monitoring components

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "R Source Code Layer"
        RSource[R Source Code]
    end

    subgraph "GNU R Integration Layer"
        GNURCore["Core Integration"]:::gnur
        Symbols["Symbol Handler"]:::gnur
        RListOps["R List Operations"]:::gnur
    end

    subgraph "RIR Compiler Layer"
        RIRComp["RIR Compiler"]:::rir
        ByteCode["Bytecode Definitions"]:::rir
        Interp["Interpreter"]:::rir
        Builtins["Builtin Functions"]:::rir
        Prof["Profiler"]:::rir
    end

    subgraph "PIR Optimizer Layer"
        Analysis["Analysis Components"]:::pir
        OptPasses["Optimization Passes"]:::pir
        PIRCore["PIR Core"]:::pir
        Scheduler["Pass Scheduler"]:::pir
    end

    subgraph "LLVM Backend Layer"
        JITComp["JIT Compiler"]:::llvm
        LLVMTypes["LLVM Types"]:::llvm
        FuncLower["Function Lowering"]:::llvm
    end

    subgraph "Runtime Layer"
        Runtime["Core Runtime"]:::runtime
        TypeSys["Type System"]:::runtime
        Deopt["Deoptimization"]:::runtime
    end

    %% Connections
    RSource --> GNURCore
    GNURCore --> Symbols
    GNURCore --> RListOps
    
    GNURCore --> RIRComp
    RIRComp --> ByteCode
    ByteCode --> Interp
    Interp --> Builtins
    Interp --> Prof

    Interp --> Analysis
    Analysis --> OptPasses
    OptPasses --> PIRCore
    PIRCore --> Scheduler
    
    Scheduler --> JITComp
    JITComp --> LLVMTypes
    JITComp --> FuncLower
    
    FuncLower --> Runtime
    Runtime --> TypeSys
    Runtime --> Deopt
    Deopt -.-> Interp

    %% Click events
    click RIRComp "https://github.com/reactorlabs/rir/blob/master/rir/src/bc/Compiler.cpp"
    click ByteCode "https://github.com/reactorlabs/rir/blob/master/rir/src/bc/BC.h"
    click Analysis "https://github.com/reactorlabs/rir/tree/master/rir/src/compiler/analysis/"
    click OptPasses "https://github.com/reactorlabs/rir/tree/master/rir/src/compiler/opt/"
    click PIRCore "https://github.com/reactorlabs/rir/tree/master/rir/src/compiler/pir/"
    click Scheduler "https://github.com/reactorlabs/rir/blob/master/rir/src/compiler/opt/pass_scheduler.cpp"
    click JITComp "https://github.com/reactorlabs/rir/blob/master/rir/src/compiler/native/pir_jit_llvm.cpp"
    click LLVMTypes "https://github.com/reactorlabs/rir/blob/master/rir/src/compiler/native/types_llvm.h"
    click FuncLower "https://github.com/reactorlabs/rir/blob/master/rir/src/compiler/native/lower_function_llvm.cpp"
    click GNURCore "https://github.com/reactorlabs/rir/blob/master/rir/src/R/r.h"
    click Symbols "https://github.com/reactorlabs/rir/blob/master/rir/src/R/Symbols.cpp"
    click RListOps "https://github.com/reactorlabs/rir/blob/master/rir/src/R/RList.cpp"
    click Runtime "https://github.com/reactorlabs/rir/blob/master/rir/src/runtime/Function.cpp"
    click TypeSys "https://github.com/reactorlabs/rir/blob/master/rir/src/runtime/TypeFeedback.cpp"
    click Deopt "https://github.com/reactorlabs/rir/blob/master/rir/src/runtime/Deoptimization.cpp"
    click Interp "https://github.com/reactorlabs/rir/blob/master/rir/src/interpreter/interp.cpp"
    click Builtins "https://github.com/reactorlabs/rir/blob/master/rir/src/interpreter/builtins.cpp"
    click Prof "https://github.com/reactorlabs/rir/blob/master/rir/src/interpreter/profiler.cpp"

    %% Styles
    classDef gnur fill:#4B8BBE,stroke:#000,color:#fff
    classDef rir fill:#4CAF50,stroke:#000,color:#fff
    classDef pir fill:#FF9800,stroke:#000,color:#fff
    classDef llvm fill:#F44336,stroke:#000,color:#fff
    classDef runtime fill:#9C27B0,stroke:#000,color:#fff
```
