|
| 1 | +# Spidir |
| 2 | + |
| 3 | +WIP JIT library written in Rust with the following goals: |
| 4 | + |
| 5 | +- Embeddability: Spidir is completely freestanding and requires only a memory allocator |
| 6 | +- Simple C API: The intended way to embed/consume Spidir, though the unstable internal Rust APIs can be used as well |
| 7 | +- Reasonable optimization quality: Higher-level language runtimes using Spidir as a backend should eventually be able to expect decent codegen |
| 8 | + |
| 9 | +Spidir was originally created for use in [TomatoDotNet](https://github.com/TomatOrg/TomatoDotNet/), so feature development is guided primarily by the needs of that project. |
| 10 | + |
| 11 | +## Using Spidir |
| 12 | + |
| 13 | +See the `c-api-tests` directory for example programs embedding the C API. |
| 14 | + |
| 15 | +All important functionality is exposed via the CLI binary `spidirtool`, which can be used to: |
| 16 | + |
| 17 | +- Graph the IR using graphviz |
| 18 | +- Inspect internal state |
| 19 | +- Generate/execute code for a given IR module |
| 20 | + |
| 21 | +See |
| 22 | + |
| 23 | +``` |
| 24 | +cargo run -p spidirtool help |
| 25 | +``` |
| 26 | + |
| 27 | +for more information. |
| 28 | + |
| 29 | +## IR |
| 30 | + |
| 31 | +Spidir uses two intermediate representations internally: |
| 32 | + |
| 33 | +- Spidir: target-independent IR for high-level optimizations |
| 34 | + - SSA-based sea-of-nodes representation |
| 35 | + - Uses phi nodes to model dataflow merges (easier to integrate in a sea-of-nodes setup) |
| 36 | +- LIR (linear IR): target-dependent IR used after instruction selection |
| 37 | + - Flat list of instructions with CFG information stored separately |
| 38 | + - Still in SSA form, but uses block parameters instead of phi nodes (more elegant for block-based IRs) |
| 39 | + - Used by register allocator and final code emission |
| 40 | + |
| 41 | +These are abstracted away by the C construction API, which provides a simple block-based facade over the sea-of-nodes graph. |
| 42 | + |
| 43 | +## Implementation Status |
| 44 | + |
| 45 | +### Implemented |
| 46 | + |
| 47 | +- Miscellaneous: |
| 48 | + |
| 49 | + - C bindings: IR construction/dumping/optimization/codegen |
| 50 | + - IR text format/parser for testing and debugging |
| 51 | + - IR verifier |
| 52 | + |
| 53 | +- IR features: |
| 54 | + |
| 55 | + - Basic integer arithmetic/bitwise instructions |
| 56 | + - Loads/stores |
| 57 | + - Basic control flow (conditional branches) |
| 58 | + - Internal/external/indirect calls |
| 59 | + - External functions |
| 60 | + - Stack slots |
| 61 | + |
| 62 | +- Codegen: |
| 63 | + |
| 64 | + - Basic x64 backend |
| 65 | + - Hopefully-intelligent register allocator |
| 66 | + - Live range splitting |
| 67 | + - Rematerialization |
| 68 | + - Symbolic verifier |
| 69 | + |
| 70 | +- Optimization: |
| 71 | + |
| 72 | + - Global code motion |
| 73 | + - Simple canonicalization/GVN |
| 74 | + |
| 75 | +### Planned |
| 76 | + |
| 77 | +- IR: |
| 78 | + |
| 79 | + - Memory operations (`memset`, `memcpy`, etc.) |
| 80 | + - Bit-counting operations |
| 81 | + - External globals |
| 82 | + - Cold block annotation |
| 83 | + - Floating point support |
| 84 | + - Unwinding support |
| 85 | + - Jump tables |
| 86 | + - Static branch/patchpoint support |
| 87 | + |
| 88 | +- Codegen: |
| 89 | + |
| 90 | + - Improved x64 instruction selection |
| 91 | + - Minor register allocator improvements |
| 92 | + - Use 3-address instructions instead of copies where possible |
| 93 | + - Improved redundant copy elimination? |
| 94 | + |
| 95 | +- Optimization: |
| 96 | + - Improved local block scheduling |
| 97 | + - SROA |
| 98 | + - SCCP |
| 99 | + - Jump threading |
| 100 | + - Range check elimination |
| 101 | + |
| 102 | +## Projects Using Spidir |
| 103 | + |
| 104 | +- [TomatoDotNet](https://github.com/TomatOrg/TomatoDotNet/), a freestanding C# runtime (original project motivation) |
| 105 | +- [spidir-wasm](https://github.com/Itay2805/spidir-wasm/), a small WASM implementation showcasing the C API |
0 commit comments