HideIron/compiler-env
0
IRis Compiler Optimization Environment
An OpenEnv environment where an LLM agent selects LLVM optimization passes to minimize execution time of C programs compiled for RISC-V architecture. The agent builds a sequence of compiler passes, then compiles and measures performance against standard optimization level baselines (O0-O3).
Quick Start
from compiler_env import CompilerAction, CompilerEnv
with CompilerEnv.from_docker_image("compiler_env-env:latest") as env:
# Start episode - picks a C program and computes baselines
result = env.reset()
print(f"Program: {result.observation.data['program']}")
print(f"O3 baseline: {result.observation.data['baselines']['O3']}s")
# Build a pass sequence
for pass_name in ["mem2reg", "simplifycfg", "instcombine", "licm", "gvn"]:
result = env.step(CompilerAction(action=f"add_pass:{pass_name}"))
print(f"Added {pass_name}, reward so far: {result.reward}")
# Compile and measure
result = env.step(CompilerAction(action="compile_and_measure"))
print(f"Execution time: {result.observation.data['execution_time']}s")
print(f"Final reward: {result.reward}")How It Works
- reset() picks a C program (random from 200 training programs, or a fixed eval task)
- The environment compiles it with
-O0,-O1,-O2,-O3and measures each on QEMU -> baselines - The agent calls tools to build an optimization pass sequence
- compile_and_measure runs the full pipeline:
clang -> opt (agent's passes) -> llc -> gcc -> qemu - Reward is based on how the agent's execution time compares to baselines
Action Space
Observation
CompilerObservation fields:
- data (dict) - Action-specific data (baselines, sequences, execution times, etc.)
- status (str) -
"success","error","invalid_pass","failed" - message (str) - Human-readable description
- reward (float) - Cumulative episode reward
- done (bool) - Whether the episode has ended
Reward Structure (Partial Progress)
Evaluation Tasks
System Requirements
The Docker image includes all needed tools:
- clang + llvm (opt, llc) - LLVM compilation toolchain
- gcc-riscv64-linux-gnu - RISC-V cross-compiler
- qemu-user (qemu-riscv64) - RISC-V binary emulation
Building & Deploying
# Build Docker image
docker build -t compiler_env-env:latest -f server/Dockerfile .
# Deploy to HF Spaces
openenv pushRunning Locally
# Start the server
uvicorn server.app:app --reload --host 0.0.0.0 --port 8000Project Structure
compiler_env/
|-- __init__.py # Module exports
|-- README.md # This file
|-- openenv.yaml # OpenEnv manifest
|-- pyproject.toml # Dependencies
|-- client.py # CompilerEnv WebSocket client
|-- models.py # CompilerAction & CompilerObservation
|-- inference.py # Baseline script (OpenAI API)
|-- training_programs/ # 200 C programs
| |-- 01_insertion_sort.c
| |-- 08_strassen_matrix.c
| |-- 114_polynomial_multiply_fft.c
| +-- ... (197 more)
+-- server/
|-- __init__.py
|-- compiler_env_environment.py # Core environment logic
|-- app.py # FastAPI app (HTTP + WebSocket)
|-- requirements.txt
+-- Dockerfile