Write Solana BPF programs in Swift using upstream Rust toolchain, LLVM, and sbpf-linker.
- End-to-end Swift to BPF compilation pipeline driven with upstream toolings
- Custom LLVM pass to adapt Swift calling conventions and normalizes Swift literals
- Rust integration tests exercise the program under Mollusk SVM
- Direct Solana syscalls by hash with zero Solana SDK dependencies
- Ensure prerequisites are installed (see below).
make buildto emit LLVM IR, retarget for BPF, and linkbuild/program.so.make testto rebuild and run the Rust integration suite against Mollusk.- Inspect generated IR or artifacts under
build/as needed for debugging.
The toolchain expects a recent Swift compiler plus LLVM utilities
(opt, llvm-as, llvm-config, clang++) and sbpf-linker.
# Fedora / RHEL example
sudo dnf install swift-lang llvm llvm-devel clang
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install sbpf-linker
# macOS example
brew install swift llvm
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install sbpf-linkerEnsure llvm-config is on PATH; override with LLVM_CONFIG=/path/to/bin/llvm-config
if necessary. The build also uses clang++ to compile the custom LLVM pass.
swiftcemits optimized LLVM IR (build/entrypoint.ll) forsrc/entrypoint.swift.- The pass in
tooling/llvm/swift_bpf_prepare_pass.cppis built and run to pin literals and reset Swift calling conventions. opt -mtriple=bpfelretargets the module for the Solana BPF ABI andllvm-asproduces bitcode.sbpf-linker --cpu v3links the bitcode intobuild/program.so, suitable for deployment.
makemake testNote
This will build the program first. To only run tests, use cargo test.
- Install matching Swift and LLVM versions; stale toolchains often fail during IR emission
- If
sbpf-linkercannot find LLVM libs, exportLLVM_LIBDIR=/path/to/llvm/lib - Re-run
make cleanwhen switching Swift toolchains to avoid stale IR - Pass
SWIFTC=/custom/swiftcorOPT=/custom/opttomakewhen using non-default binaries