Magpie is an experimental programming language and compiler toolchain.
It is built for:
- deterministic source and machine output
- explicit ownership/borrowing rules with ARC-managed heap lifetimes
- strong diagnostics for fast fix loops
- CLI workflows that are friendly to automation and LLM agents
This repository is a Rust workspace for Magpie v0.1. It includes:
- a
magpieCLI - lexer/parser/semantic analysis/type checking
- ownership checking
- MPIR lowering + verification
- monomorphization (BLAKE3-keyed specialization)
- ARC insertion/optimization passes
- LLVM-text, WASM, and multi-backend GPU codegen paths (SPIR-V, MSL, PTX, HIP, WGSL)
- MLX host API integration (Apple Silicon ML acceleration)
- GPU profiling system (Chrome trace export, allocation tracking)
- runtime with real GPU dispatch via
dlopen(Metal > CUDA > HIP > Vulkan > WebGPU) bf16(bfloat16) primitive type- runtime, package, memory-index, graph, web, and MCP tooling
High-level crates (29 total):
crates/magpie_cli— command-line entrypoint (magpie)crates/magpie_driver— compiler orchestration pipelinecrates/magpie_lex,magpie_parse,magpie_ast— frontendcrates/magpie_sema,magpie_hir,magpie_types— semantic + type layerscrates/magpie_own— ownership/borrow checkercrates/magpie_mpir,magpie_mono,magpie_arc— mid-level IR and lowering passescrates/magpie_codegen_llvm,magpie_codegen_wasm— CPU backend codegencrates/magpie_gpu— GPU codegen core (BackendEmitter trait, CFG structurizer, kernel registry)crates/magpie_gpu_spirv— SPIR-V backend (Vulkan)crates/magpie_gpu_msl— Metal Shading Language backend (Apple)crates/magpie_gpu_ptx— PTX/NVVM backend (NVIDIA CUDA)crates/magpie_gpu_hip— HIP/HSACO backend (AMD ROCm)crates/magpie_gpu_wgsl— WGSL backend (WebGPU)crates/magpie_mlx— MLX host API integration (Apple Silicon ML)crates/magpie_rt— runtime library (ARC, GPU dispatch, profiling)crates/magpie_diag— diagnostics + envelopescrates/magpie_csnf— canonical formatter/digest handlingcrates/magpie_pkg,magpie_memory,magpie_ctx,magpie_web— tooling and platform subsystems
Other important paths:
tests/fixtures/— language fixture programs, includingfeature_harness.mpandtresult_parse_json.mpstd/— standard library modules used by Magpie projectsDOCUMENTATION.md— full technical documentationDOCUMENTATION_QUICKSTART.md— fast command referenceSKILL.md— detailed coding/diagnostic guide for agent workflows
Required:
- Rust 1.80+
- Cargo
Optional but recommended (for execution/link workflows):
lli(run LLVM IR viamagpie runin dev workflows)llc+clang+ system linker (native executable emission/linking)
Optional (for GPU backend compilation):
llcwith NVPTX target (PTX backend) or AMDGPU target (HIP backend)ld.lld(HIP HSACO linking)- Metal.framework (MSL backend, macOS only — auto-detected via
dlopen) - Vulkan SDK (SPIR-V backend)
- MLX framework (Apple Silicon ML — auto-detected via
dlopen)
From repo root:
cargo build -p magpie_cliBuild the full workspace:
cargo build --workspaceCheck CLI help:
cargo run -p magpie_cli -- --helpOptional: install local magpie binary:
cargo install --path crates/magpie_cli --force
magpie --helpIf you do not install the binary, use:
cargo run -p magpie_cli -- <GLOBAL_FLAGS> <SUBCOMMAND> ...magpie uses global flags, so put them before the subcommand.
Correct:
magpie --entry src/main.mp --emit mpir,llvm-ir --output json buildNot correct:
magpie build --entry src/main.mpmagpie new demo
cd demo
magpie --output json --emit mpir,llvm-ir buildThis generates artifacts like:
target/<triple>/<profile>/main.mpirtarget/<triple>/<profile>/main.ll.magpie/memory/main.mms_index.json
Magpie source files use a strict module header and explicit basic blocks:
module demo.main
exports { @main }
imports { }
digest "0000000000000000"
fn @main() -> i64 {
bb0:
ret const.i64 0
}
This structure is intentionally regular so formatting, parsing, diagnostics, and automated edits stay predictable.
Format source files:
magpie fmt --fix-metaParse only:
magpie --entry src/main.mp --output json parseBuild with debug artifacts:
magpie --entry src/main.mp --emit mpir,llvm-ir,mpdbg --output json buildExplain a diagnostic code:
magpie explain MPT2014Run tests in this repository:
cargo testTop-level commands in magpie:
newbuildrunreplfmtparselinttestdocmpir verifyexplainpkg(resolve,add,remove,why)web(dev,build,serve)mcp servememory(build,query)ctx packffi importgraph(symbols,deps,ownership,cfg)
Magpie supports 5 GPU compute backends via a unified BackendEmitter trait:
| Backend | Emit kind | Target | Crate |
|---|---|---|---|
| SPIR-V | spv |
Vulkan | magpie_gpu_spirv |
| MSL | msl |
Metal (Apple) | magpie_gpu_msl |
| PTX | ptx |
CUDA (NVIDIA) | magpie_gpu_ptx |
| HIP | hip |
ROCm (AMD) | magpie_gpu_hip |
| WGSL | wgsl |
WebGPU | magpie_gpu_wgsl |
The runtime probes backends at startup via dlopen in priority order: Metal > CUDA > HIP > Vulkan > WebGPU. Falls back to CPU simulation if no GPU is available.
Configure GPU behavior in Magpie.toml:
[gpu]
backend = "auto" # auto | spirv | msl | ptx | hip | wgsl
fallback = "cpu" # cpu | error
llc_path = "/usr/local/bin/llc" # optional
lld_path = "/usr/local/bin/ld.lld" # optional (HIP)The magpie_mlx crate provides full MLX host API integration via dlopen/dlsym dispatch tables (~40 function pointers). This enables ML workloads (array ops, neural network layers, optimizers, automatic differentiation) on Apple Silicon without requiring MLX to be a build-time dependency.
The magpie_mono crate implements generic specialization using BLAKE3-keyed instance hashing. Generic functions are duplicated and specialized per concrete type argument set, with deterministic SID (Symbol ID) assignment.
Parse and JSON runtime ABI now has dual APIs:
- Preferred: fallible
*_try_*symbols (mp_rt_str_try_parse_*,mp_rt_json_try_*) that return status codes. - Legacy:
mp_rt_str_parse_*,mp_rt_json_encode,mp_rt_json_decodeare deprecated compatibility wrappers.
At source level, compatibility value-style ops still exist, and TResult parse/json fixtures are included for end-to-end coverage.
A broad language feature harness is included at:
tests/fixtures/feature_harness.mptests/fixtures/tresult_parse_json.mp
Build it:
magpie --entry tests/fixtures/feature_harness.mp --emit mpir,llvm-ir --output json buildTry execution paths:
- LLVM IR path (requires
lli):magpie --entry tests/fixtures/feature_harness.mp --emit llvm-ir run
- Native binary path (requires full native toolchain):
magpie --entry tests/fixtures/feature_harness.mp --emit exe build
Global --output supports:
textjsonjsonl
Use --output json for machine-readable automation.
- Language and semantics:
DOCUMENTATION.md - Fast command cheatsheet:
DOCUMENTATION_QUICKSTART.md - Deep compiler/diagnostic examples:
SKILL.md - GPU expansion specification:
SPEC_GPU_UPGRADE.md - GPU interoperability contracts:
GPU_INTEROP_SPEC.md