Skip to content

[Self-Hosting] Phase 6: Implement IR Builder in Gradient #123

@graydeon

Description

@graydeon

Summary

Implement the IR builder in Gradient. Convert typed AST to Intermediate Representation for code generation.

Background

File: compiler/ir_builder.gr (~489 lines currently, stubs). Expand to ~2,000 lines with actual IR generation.

Target State

Full IR generation with:

  • Expression lowering
  • Statement lowering
  • Function lowering
  • SSA form construction
  • Type conversion

Implementation Requirements

Core Functions

  1. build_module(typed: TypedModule) -> IrModule

    • Lower entire module to IR
    • Generate all functions
  2. build_function(func: TypedFunction) -> IrFunction

    • Lower function to IR
    • Create entry block
    • Generate parameters
  3. build_expr(builder: IrBuilder, expr: TypedExpr) -> (IrBuilder, IrValue)

    • Lower expression to IR instructions
    • Handle all expression kinds
    • Return generated value
  4. build_stmt(builder: IrBuilder, stmt: TypedStmt) -> IrBuilder

    • Lower statement to IR
    • Update builder state
  5. create_block(builder: IrBuilder, name: String) -> (IrBuilder, BlockId)

    • Create new basic block
    • For control flow

IR Instructions to Generate

  • Literals: const_int, const_float, const_bool
  • Arithmetic: add, sub, mul, div
  • Memory: alloca, load, store
  • Control: br, br_cond, ret
  • Calls: call, call_indirect
  • Conversions: trunc, zext, inttoptr

Acceptance Criteria

  • Can lower complete Gradient programs to IR
  • Correct SSA form
  • All expression types lowered
  • All statement types lowered
  • Proper block/branch structure
  • Type-checks successfully
  • Tests for lowering all constructs

Part Of

  • Epic: Full Self-Hosting with Rust Kernel
  • Phase: 6 of 7
  • Blocks: Phase 7 (Codegen)

Effort

~5 days, ~1,500 lines of Gradient

Dependencies

  • Phase 5 complete (LSP)
  • May need memory allocation primitives in Rust

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions