Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hydrogen602/math-go-brr
Python and LLVM experiments
https://github.com/hydrogen602/math-go-brr
compiler jit llvm python
Last synced: 23 days ago
JSON representation
Python and LLVM experiments
- Host: GitHub
- URL: https://github.com/hydrogen602/math-go-brr
- Owner: hydrogen602
- Created: 2024-06-07T07:08:15.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-09-30T16:39:46.000Z (about 1 month ago)
- Last Synced: 2024-10-02T07:42:33.972Z (about 1 month ago)
- Topics: compiler, jit, llvm, python
- Language: Rust
- Homepage:
- Size: 160 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# JIT Python function to LLVM IR for fast numeric operations POC
This is a simple POC to show the concept of taking a python, analyzing its AST, and generating LLVM IR code that does what the python code does, and then making a callable with LLVM's JIT feature all at runtime, using only a decorator.
The idea is that when a function is just basic math, there's no real need to deal with the overhead of python and its objects, and so instead we can translate it to basic LLVM IR `add` and `mul` instructions that have almost no overhead.
Now Cyton and maturin/PyO3 already do this, but the idea is to not require any compilation step or make the user interact with anything that isn't python, so that it is near seamless to the user (well there is always the restriction that llvm is statically typed, plus any such function can only contain integer / float operations).
---
```python
@brrr
def bar(a, b):
return a + b
```Here, `brrr` takes `bar`, gets and parses its source, and then generates an llvm function that does the `add` instructions on two i64s, and then creates a python-callable to replace `bar` that calls the llvm JIT engine.
---
```python
@brrr
def foo(a: int) -> int:
b = 0
i = 0
while i < a:
b = b + i
i = i + 1return b
start = time()
llvm_out = foo(10_000_000)
print(f"LLVM JIT Compiled |", time() - start)# foo.original_func is our plain-old Python function
start = time()
python_out = foo.original_func(10_000_000)
print(f"Regular Python |", time() - start)
```
Outputs: (On an M1Pro MacBookPro)
```
LLVM JIT Compiled | 0.026 sec
Regular Python | 0.383 sec
```---
# Direction
Priority:
- [ ] All basic math operations for integers (as i64)
- [x] `+`, `-`, `*`
- [ ] Division ops: `/`, `//`, `%`
- [ ] Power & bitwise: `**`, `<<`, `>>`, `&`, `|`, `^`, `~`
- [ ] Augmented assignment for all: e.g. `+=`, `-=`, `*=`, etc.
- [ ] All basic math operations for floats (as f64)
- [x] Basic control flow that doesn't rely on python objects (if, while)
- [x] Booleans, logic operations, and comparisons
- [ ] Interop with numpy arrays, numpy C-APISecondary:
- [ ] Recursion support
- [ ] Nice error messagesTertiary:
- [ ] JIT Functions calling other JIT functions
- [ ] closures: capturing of numeric variables