Skip to content

[Self-Hosting] Phase 7: Implement Codegen in Gradient #124

@graydeon

Description

@graydeon

Summary

Implement code generation in Gradient. Convert IR to machine code (via Rust kernel).

Background

With IR generation (Phase 6), we can now generate machine code. This phase orchestrates the Rust kernel's code generators.

Target State

Full code generation with:

  • IR to native (via Cranelift in Rust kernel)
  • IR to WASM (via WASM backend in Rust kernel)
  • Linking and output

Implementation Requirements

Architecture

Gradient code generation orchestrates Rust kernel:

fn generate_native(ir: IrModule, options: CompileOptions) -> !{FS} CompiledOutput:
    // Call into Rust kernel to use Cranelift
    ret call_rust_codegen(ir, "native")

fn generate_wasm(ir: IrModule, options: CompileOptions) -> !{FS} CompiledOutput:
    // Call into Rust kernel to use WASM backend
    ret call_rust_codegen(ir, "wasm")

Functions

  1. generate(ir: IrModule, target: Target) -> CompiledOutput

    • Select code generator
    • Call Rust kernel
    • Return result
  2. link(objects: ObjectList, output: String) -> !{FS} ()

    • Link object files
    • Use system linker via Rust kernel
  3. emit_module(ir: IrModule, options: CompileOptions) -> !{FS} ()

    • Full pipeline: generate + link
    • Handle multiple targets

Targets

  • Native (x86_64, ARM64 via Cranelift)
  • WASM (wasm32 via WASM backend)
  • Future: LLVM, other architectures

Acceptance Criteria

  • Can compile Gradient programs to native binaries
  • Can compile Gradient programs to WASM
  • Generated code runs correctly
  • Proper error handling
  • Type-checks successfully
  • Integration tests pass

Part Of

  • Epic: Full Self-Hosting with Rust Kernel
  • Phase: 7 of 7 (FINAL)

Effort

~5 days, ~1,000 lines of Gradient

Dependencies

  • Phase 6 complete (IR Builder)
  • Rust kernel codegen (already exists)

Completion Criteria

When this phase is complete:

  • ✅ Compiler can compile itself
  • gradient-compiler is 95%+ Gradient code
  • ✅ Rust kernel is minimal (~2,000 lines)
  • ✅ Full self-hosting achieved

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