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 build
to emit LLVM IR, retarget for BPF, and linkbuild/program.so
.make test
to 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-linker
Ensure 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.
swiftc
emits optimized LLVM IR (build/entrypoint.ll
) forsrc/entrypoint.swift
.- The pass in
tooling/llvm/swift_bpf_prepare_pass.cpp
is built and run to pin literals and reset Swift calling conventions. opt -mtriple=bpfel
retargets the module for the Solana BPF ABI andllvm-as
produces bitcode.sbpf-linker --cpu v3
links the bitcode intobuild/program.so
, suitable for deployment.
make
make test
Note
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-linker
cannot find LLVM libs, exportLLVM_LIBDIR=/path/to/llvm/lib
- Re-run
make clean
when switching Swift toolchains to avoid stale IR - Pass
SWIFTC=/custom/swiftc
orOPT=/custom/opt
tomake
when using non-default binaries