MLIR Dialects & Code Generation

How Nyx utilizes Multi-Level Intermediate Representation (MLIR) for high-level domain optimizations, async concurrency lowering, and LLVM machine code generation.

1. Why MLIR?

Traditional compilers lower directly from AST to low-level LLVM IR or C, losing high-level language semantics like regions, async tasks, pattern matching, and tensor operations. Nyx leverages MLIR to perform optimizations at multiple levels of abstraction before emitting final machine instructions.

flowchart TB
    S["Nyx Source (.nyx)"]
    A["AST & Semantic Analysis"]
    HL["Nyx High-Level Dialect (nyx ops)"]
    STD["MLIR Standard Dialects (async, memref, scf, arith)"]
    LLVM["LLVM IR Dialect (llvm ops)"]
    TGT["Target Machine Code (.exe, .so, .dylib, .wasm)"]

    S --> A --> HL
    HL -->|"Region Lifetime Optimization & Async Lowering"| STD
    STD -->|"LLVM Lowering Pass"| LLVM
    LLVM --> TGT

    style A fill:#1e293b,stroke:#38bdf8,stroke-width:2px,color:#f8fafc
    style HL fill:#064e3b,stroke:#10b981,stroke-width:2px,color:#f8fafc
    style STD fill:#312e81,stroke:#818cf8,stroke-width:2px,color:#f8fafc
    style LLVM fill:#1e1b4b,stroke:#6366f1,stroke-width:2px,color:#f8fafc
    style TGT fill:#701a75,stroke:#f472b6,stroke-width:2px,color:#f8fafc
            

2. The Nyx Custom Dialect (`nyx.*`)

The nyx MLIR dialect models Nyx's core language constructs:

Core Operations Table:

OperationSyntaxDescription
nyx.funcnyx.func @name(%arg: !nyx.type) -> !nyx.retFunction declaration with region ownership parameters
nyx.region.alloc%ptr = nyx.region.alloc(%reg, !nyx.struct)Bump-pointer allocation within a designated memory region
nyx.async.spawn%task = nyx.async.spawn { ... }Schedules work onto the work-stealing cooperative thread pool
nyx.async.await%val = nyx.async.await %task : !nyx.futureAsynchronously suspends current task until future completion
nyx.matchnyx.match %enum [case0: ..., case1: ...]Exhaustive pattern matching jump table
nyx.tensor.gemm%res = nyx.tensor.gemm %A, %B : tensor<f32>SIMD/AVX-512 hardware-accelerated matrix multiplication

3. Sample MLIR Output for a Region-Allocated Function

module {
  nyx.func @compute_geospatial_hash(%lat: f64, %lon: f64) -> !nyx.string {
    %reg = nyx.region.create() : !nyx.region
    
    // Allocate temporary vector in local region
    %pt = nyx.struct.create(%lat, %lon) : !nyx.point
    %hash = nyx.call @rt_geohash_encode(%pt, %reg) : (!nyx.point, !nyx.region) -> !nyx.string
    
    // Transfer hash result ownership to caller, release local region
    nyx.region.destroy %reg : !nyx.region
    nyx.return %hash : !nyx.string
  }
}

4. Lowering Pipeline & Passes

  1. nyx-region-inference-pass: Analyzes variable lifetimes and inserts deterministic nyx.region.destroy instructions.
  2. nyx-async-lowering-pass: Lowers nyx.async.spawn into state-machine coroutines conforming to MLIR Async runtime.
  3. nyx-to-llvm-pass: Maps high-level types to native LLVM structures with zero abstraction penalties.