Commit be870dcf
be870dcfa82eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified
Description
Update README with ao_render results and full feature list
Both benchmarks now compile to standalone binaries (no mruby). 57-59x faster than CRuby, 16-21KB binary sizes.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- be870dcfa82e
- Parent
- c83a43b7b491
README.mdmodifiedView file
@@ -1,9 +1,10 @@
11
# Spinel — AOT Compiler for Ruby22
3-
Spinel compiles Ruby source code to C via [Prism](https://github.com/ruby/prism)4-
parsing and type inference, then links against [mruby](https://github.com/mruby/mruby)5-
for the runtime. For proven types (Integer, Float, Boolean), it generates direct6-
C operations with no dynamic dispatch.3+
Spinel compiles Ruby source code to standalone C executables via4+
[Prism](https://github.com/ruby/prism) parsing and whole-program type inference.5+
Classes become C structs, methods become direct function calls, and numeric6+
operations compile to native C arithmetic with zero dynamic dispatch overhead.7+
Generated binaries have no runtime dependencies — no mruby, no GC, just libc and libm.78
89
## Quick Start910
@@ -14,76 +15,87 @@
1415
# 2. Build the spinel compiler1516
make1617
17-
# 3. Compile a Ruby program to C18-
./spinel --source=app.rb --output=app_aot.c19-
20-
# 4. Build the final binary (adjust mruby path as needed)21-
cc -O2 app_aot.c \22-
$(~/work/mruby/build/host/bin/mruby-config --cflags) \23-
$(~/work/mruby/build/host/bin/mruby-config --ldflags --libs) \24-
-o app18+
# 3. Compile a Ruby program to C, then to a native binary19+
./spinel --source=app.rb --output=app.c20+
cc -O2 app.c -lm -o app2521
```2622
27-
## Benchmark23+
## Benchmarks2824
29-
First target: `bm_so_mandelbrot.rb` (Mandelbrot set, 600x600 PBM output).25+
| Benchmark | CRuby 3.2 | mruby | **Spinel AOT** | Speedup |26+
|-----------|-----------|-------|----------------|---------|27+
| mandelbrot (600x600 PBM) | 1.14s | 3.18s | **0.02s** | **57x** |28+
| ao_render (64x64 AO raytracer) | 3.55s | 13.69s | **0.06s** | **59x** |3029
30+
Binary sizes: mandelbrot 16KB, ao_render 21KB (stripped).31+
3132
```bash32-
make test # compile, run, and verify output matches CRuby33+
make test # compile mandelbrot, run, verify output matches CRuby3334
```34-
35-
| Runtime | Time | Speedup |36-
|---------|------|---------|37-
| CRuby 3.2 | 1.14s | 1x |38-
| mruby interpreter | 3.18s | 0.36x |39-
| **Spinel AOT** | **0.06s** | **19x** |40-
41-
Output is bit-identical to CRuby.4235
4336
## How It Works4437
4538
```46-
Ruby Source39+
Ruby Source (.rb)4740
|4841
v49-
Prism (libprism) -- parse to AST42+
Prism (libprism) -- parse to AST5043
|5144
v52-
Type Inference (Pass 1) -- infer Integer/Float/Boolean/String per variable45+
Pass 1: Class Analysis -- find classes, methods, instance variables5346
|5447
v55-
C Code Generation (Pass 2)56-
| - Proven numeric types -> direct C arithmetic57-
| - String operations -> mruby API calls58-
| - I/O (puts/print) -> stdio or mruby48+
Pass 2: Type Inference -- infer types for all variables, ivars, params49+
| (Integer, Float, Boolean, Object types)50+
v51+
Pass 3: Struct/Method Emit -- classes -> C structs52+
| methods -> C functions (direct calls)53+
| getters/setters -> inline field access5954
v60-
Generated C file55+
Pass 4: Main Codegen -- top-level code -> main()56+
| while/for/times -> C loops57+
| arithmetic -> C operators58+
| puts/print/printf -> stdio59+
v60+
Standalone C file6161
|6262
v63-
cc + libmruby -> executable63+
cc -O2 -lm -> native binary -- no mruby, no GC, just libc6464
```6565
66-
For `bm_so_mandelbrot.rb`, the inner loop compiles to pure C float/int67-
arithmetic with zero mruby dispatch. Only the PBM header string interpolation68-
uses the mruby runtime.66+
For `bm_ao_render.rb`, the compiler:67+
- Converts 6 Ruby classes (Vec, Sphere, Plane, Ray, Isect, Scene) into C structs68+
- Vec (3 floats) is passed/returned by value — no heap allocation69+
- All method calls are devirtualized to direct C function calls70+
- `Integer#times` blocks become C for loops71+
- `Math.sqrt/cos/sin` map directly to C math functions72+
- The Rand module's xorshift PRNG compiles to inline integer arithmetic6973
7074
## Supported Language Features7175
72-
Currently handles the subset needed for mandelbrot:73-
74-
- Local variables (assignment, read, operator-assignment like `+=`, `<<=`)75-
- `while` loops, `for..in` with Range76-
- `if` / `elsif` / `else`, ternary operator77-
- Integer, Float, Boolean, String, nil literals78-
- Binary integer literals (`0b0`, `0b1`)79-
- Arithmetic (`+`, `-`, `*`, `/`, `%`)80-
- Comparison (`>`, `<`, `>=`, `<=`, `==`, `!=`)81-
- Bitwise (`<<`, `>>`, `|`, `&`, `^`)82-
- String interpolation (`"hello #{name}"`)83-
- `puts`, `print`84-
- `Integer#chr`85-
- `break`86-
- Parallel assignment (`a, b = c, d`)76+
| Feature | Example |77+
|---------|---------|78+
| Classes with instance variables | `class Vec; def initialize(x,y,z); @x=x; end; end` |79+
| Method definitions | `def vadd(b); Vec.new(@x+b.x, ...); end` |80+
| Getters/setters (inlined) | `def x; @x; end` / `def x=(v); @x=v; end` |81+
| Object construction | `Vec.new(1.0, 2.0, 3.0)` |82+
| Method calls on typed objects | `ray.org.vsub(@center)` |83+
| Modules with state | `module Rand; @x = 123; def self.rand; ...; end; end` |84+
| Local variables, constants | `size = 600`, `ITER = 49` |85+
| `while` loops | `while x <= count_size` |86+
| `Integer#times` with block | `n.times do \|i\| ... end` |87+
| `if` / `elsif` / `else` | conditional branching |88+
| Ternary operator | `escape ? 0 : 1` |89+
| `and` / `or` | `t > 0.0 and t < isect.t` |90+
| Arithmetic, comparison, bitwise | `+`, `-`, `*`, `/`, `%`, `<`, `>`, `==`, `<<`, `\|`, `^` |91+
| Unary minus | `-b`, `-(expr)` |92+
| `Math.sqrt`, `Math.cos`, `Math.sin` | C math functions |93+
| String interpolation | `"P4\n#{size} #{size}"` → printf |94+
| `puts`, `print`, `printf` | stdio calls |95+
| `Integer#chr` | `print byte_acc.chr` → putchar |96+
| `break`, `return` | loop/method exit |97+
| Parallel assignment | `zr, zi = tr, ti` |98+
| Array indexing | `basis[0].x`, `@spheres[1].intersect(...)` |8799
88100
## Project Structure89101
@@ -91,11 +103,12 @@
91103
spinel/92104
├── src/93105
│ ├── main.c # CLI, file reading, Prism parsing94-
│ ├── codegen.h # Type definitions, codegen API95-
│ └── codegen.c # Type inference + C code generation106+
│ ├── codegen.h # Type system, class/method/module info structs107+
│ └── codegen.c # Multi-pass code generator (~2000 lines)96108
├── prototype/97109
│ └── tools/ # Step 0 prototype (RBS extraction, LumiTrace, etc.)98-
├── bm_so_mandelbrot.rb # First compilation target110+
├── bm_so_mandelbrot.rb # Benchmark: Mandelbrot set renderer111+
├── bm_ao_render.rb # Benchmark: Ambient occlusion raytracer99112
├── Makefile100113
├── PLAN.md # Implementation roadmap101114
└── ruby_aot_compiler_design.md # Detailed design document@@ -104,7 +117,7 @@
104117
## Dependencies105118
106119
- **Build time**: [Prism](https://github.com/ruby/prism) (fetched automatically by `make deps`)107-
- **Link time**: [mruby](https://github.com/mruby/mruby) (set `MRUBY_DIR` if not at `~/work/mruby`)120+
- **Run time**: None. Generated binaries are standalone (libc + libm only).108121
109122
## License110123