How it works
Architecture
What the build pipeline produces, why each stage is necessary, and the evidence that it works. The exact mechanics are at the end of the page.
What it produces
bflat-riscv64 compiles an ordinary C# program into a single, fully static RISC-V ELF. The ELF needs no operating system, no dynamic libraries and nothing installed on the target. The same source compiles without changes for:
- a zkVM prover, which executes and proves the binary. bflat supports
five platforms: ZisK (
zisk), SP1 (sp1) and OpenVM (openvm) on riscv64, and ZKsync Airbender (airbender) on riscv32. For Airbender, the build also writes the flat ROM images that Airbender loads; qemu-riscv64or any RISC-V64 Linux host, for debugging (zisk_sim);- real RISC-V64 hardware.
One command, bflat build, does all the steps. bflat also keeps the stock
linux-x64 target, which builds an ordinary NativeAOT program.
Targets describes each platform.
Why the extra stages exist
Microsoft’s .NET NativeAOT compiles C# to a native binary. That binary needs a kernel, dynamic linking, floating-point hardware, atomics and compressed instructions. A zkVM gives none of them.
bflat is a compiler driver. It contains no code generator of its own.
Roslyn compiles C# into IL, the ILCompiler of dotnet-riscv compiles IL into
a RISC-V object, and lld links it. bflat adds these stages around them:
- ILC-stage substitutions for the single hart;
- RyuJIT options that keep the code inside the ISA of the zkVM;
- a link step that replaces unsupported OS and runtime calls with calls that are safe on a zkVM;
- post-link steps that make the image acceptable to the loader of the prover;
- target-specific memory layouts.
These stages run only for a platform build (--libc zisk, zisk_sim,
sp1, openvm or airbender). For all other targets, bflat operates as
upstream bflat does. bflat knows none of these targets by name. Each one is
a platform that its bflat-* package defines (bflat-libziskos, bflat-sp1,
bflat-openvm, bflat-airbender), and --extlib gives the package. The
manifest format page describes what a platform declares.
These stages are outside .NET. What they cannot do goes into the patch series of dotnet-riscv: the riscv64 ABI and ISA patches, and the RyuJIT and NativeAOT changes for zkVM guests (see Runtime). The pipeline takes ordinary C#. Exceptions, allocation, interface dispatch and generics all work. The same source builds for the prover, for QEMU and for real hardware.
How it works, at a glance
| Stage | What happens | Unique to this fork? |
|---|---|---|
| 1 · Compile | The NativeAOT compiler (ILC) of dotnet-riscv compiles C# to a RISC-V object, with the RyuJIT options of the platform | Partly: .NET 11 with the patch series of dotnet-riscv |
| 1.5 · Substitute | The platform’s modules give ILC its single-hart ILLink substitutions and feature switches (guest_subst) |
Yes |
| 2 · Link | ld.lld links the object statically and replaces unsupported OS and runtime calls with the link-time modules |
Yes |
| 3 · Post-link | The post-link steps of the platform change the ELF in place, or write the flat images, so that the loader of the prover accepts it (zisk, sp1, airbender) |
Yes |
| 4 · Check | The ISA checks decode the linked image: no F, D, C or A instruction (opt-in; on by default for airbender) |
Yes |
| 5 · Boot & run | A small assembly entry point starts the runtime and calls Main. No kernel is under it |
Yes |
The driver is in src/bflat/:
BuildCommand.cshas the options.- Its nested
Buildclass runs one build in stages:BuildCommand.Build.cs(the target and the packages),BuildCommand.Compile.cs(Roslyn and ILC) andBuildCommand.Link.cs(the linker, the post-link steps and the image checks). BflatManifest.csreads the platform and module manifests. The manifests hold every zkVM decision.IsaVerifier.csdecodes the linked image (ELF64 and ELF32) for the ISA checks.ILFloatScanner.csis the IL check of--error-on-float. The methods that it reports only as a warning are inchecks.il_float_allowofguest_subst.
Under the hood
The rest of this page describes each stage: the exact switches, the link command, the post-link passes and the boot sequence.
Stage 1 — Microsoft’s NativeAOT (ILC) emits an object file
ILC is the .NET NativeAOT compiler. It is the compiler that Microsoft ships with the standard .NET SDK, built by dotnet-riscv with the riscv fixups. bflat embeds Roslyn to parse the source, and then gives the IL to ILC. Microsoft’s compiler has the tests of the full .NET ecosystem. Thus bflat gets the safety, correctness and portability of the mainline toolchain, and we do not keep an AOT compiler of our own.
The output is one RISC-V ELF relocatable file. It contains:
- compiled managed code (
__managedcodesection); - type system metadata;
- module initialisation tables;
- references to runtime symbols (GC, exception handling, dispatch, …).
For a platform build, the manifests give ILC its inputs (refer to the
ilc section):
| Input | Source | Effect |
|---|---|---|
| Feature switches | ilc.features of guest_subst |
System.Globalization.Invariant and System.TimeZoneInfo.Invariant: no ICU data and no zoneinfo files. A musl target is invariant in any case. |
| Defines | ilc.defines of the platform |
ZKVM_ZISK, ZKVM_SP1, ZKVM_OPENVM, ZKVM_AIRBENDER |
| Direct P/Invoke | ilc.direct_pinvoke of ubootstrap |
__Internal, the same as -i |
| References | ilc.references of a package |
managed bindings that the guest compiles against |
| Substitutions | ilc.substitutions of guest_subst |
refer to Stage 1.5 |
--no-pie is also an input of this stage, but no platform sets it. You
must give the flag yourself. Position-independent code does not agree with
the fixed memory layout. The flag changes that layout, so a guest without
it can fail where a guest with it does not. Every guest in this repository
and in the zkVM builds of Nethermind gives the flag explicitly.
ILC resolves references against the lib/<os>/<arch>/<libc> directory of
the runtime. bflat downloads it from the
dotnet-riscv release
that bflat.variant.props names.
Stage 1.5 — ILC-stage substitutions
The target has no FPU, so the guests are soft-float. Without F and D, the
JIT lowers double arithmetic to calls to the compiler-rt builtins. bflat
links libgcc.a (on riscv32, libclang_rt.builtins-riscv32.a), which
defines them. The bundled musl is built for rv64im (rv32im on riscv32),
so its libm has no F or D instructions either. The exact operations
(sqrt, ceil, floor, modf, fma, fmod) run. Only the
transcendental functions trap (see nofp), and CoreLib
does not call them on its ordinary paths. Thus floating point is not a
reason to replace a method.
The reason to replace a method is the single hart. Some CoreLib work has
no meaning on one hart. Examples are a thread-pool tuner that samples
worker counts, and a processor count that comes from cgroups. bflat does not patch
the runtime for this. It applies an ILLink substitutions file while ILC
compiles: modules/guest_subst/substitutions.xml replaces the bodies of
those methods with constants. bflat applies the user --substitution files
after it. A user file cannot declare the same method again.
Design against drift. A substituted method can get a new name or a new signature. Then the entry stops matching, and the original body stays in the image. ILLink gives no error for this. Read the build output for IL2009 (see verification).
--wrap cannot redirect a constant stub. ILC inlines the stub into its
callers. Thus no call is left for the linker to divert to a native
replacement, and the constant of the stub runs. An optimized build fails
when a --wrap target is a managed method that the scanner reached but
codegen never emitted. A method that ILC inlined into all its callers looks
like that.
zkVM RyuJIT codegen knobs
The platform manifests give the ISA to ILC (codegen.isa) and give RyuJIT
options (codegen.options, which GetBackendOptions in
BuildCommand.Compile.cs reads). --codegenopt Name=Value adds or replaces
one option by hand. There are two groups. The ISA gate applies to every
build of a platform. Most tuning options apply only to optimized builds
("when": {"optimize": true}).
RyuJIT parses these integer values as hexadecimal with no 0x prefix
(JitConfigProvider.getIntConfigValue uses NumberStyles.AllowHexSpecifier).
Thus "2000" is 0x2000 = 8192. bflat rejects a value with 0x.
The ISA gate. Every zkVM here decodes only the base 32-bit encoding,
and the guest has one hart. Each platform disables C, A, F, D, Zicond, Zba,
Zbb and Zbs (codegen.isa.disable). bflat gives the result to ILC as it
does for -m. The ZisK transpiler implements Zba, Zbb and Zbs. Thus ZisK
enables them again (codegen.isa.enable) when the compiler package has the
zb-lowering-fixes capability. Two options make the JIT obey the gate:
| Knob | Value | Effect |
|---|---|---|
EnableRiscV64Compressed |
0 |
Never emit a compressed (C) encoding. RyuJIT otherwise uses c.add/c.mv in switch dispatch |
EnableRiscV64Atomic |
0 |
Lower Interlocked to plain load/modify/store instead of lr/sc and amo* |
Both options need a cross-JIT built with the matching runtime fixups. A JIT
without the fixups ignores options that it does not know. For this reason,
--error-on-compressed and --error-on-atomic check the result on the
linked binary. Only the platforms get these options, because a real
riscv64+musl target needs C and A.
Memory access. bflat writes JitNoUnalignedAccess and
JitRiscV64StrictAlign for every RISC-V target, with the value 0 by
default. A platform that sets them replaces the default.
--no-unaligned-access sets JitNoUnalignedAccess=1 for any target.
The options of each platform. “opt” means optimized builds only.
| Knob | zisk |
zisk_sim |
sp1 |
openvm |
airbender |
Effect |
|---|---|---|---|---|---|---|
JitZkBumpAddr |
bffbfff8 |
bffbfff8 |
bffefff8 |
bffefff8 |
— | The address of the bump-pointer cell: inline allocation at each site (fixup 26) |
JitNoUnalignedAccess |
— | — | 1 |
— | 1 |
Expand the accesses flagged unaligned byte-wise |
JitRiscV64StrictAlign |
— | — | 1 |
— | 0 |
Check at run time the accesses whose alignment the JIT cannot know |
JitRiscVUnsignedMulDiv |
— | — | — | — | 1 |
Build the signed RV32M instructions from unsigned ones |
JitObjectStackAllocation |
opt 1 |
opt 1 |
opt 1 |
opt 1 |
opt 1 |
Enable escape-analysis stack allocation |
JitObjectStackAllocationSize |
opt 2000 |
opt 2000 |
opt 2000 |
opt 2000 |
— | Raise the max stack-allocatable object size to 8192 (default 0x210 = 528). A matching runtime patch removes the in-loop heap restriction |
JitExtDefaultPolicyMaxIL, …MaxBB |
opt 200, 10 |
opt | opt | opt | — | Inlinees up to 512 bytes of IL and 16 basic blocks (default 128, 7). Stays on ExtendedDefaultPolicy, which weighs code growth; JitAggressiveInlining overflows the fixed ZisK ROM |
RiscV64ElideLeafRaSave |
opt 1 |
opt 1 |
opt 1 |
opt 1 |
opt 1 |
Elide RA spill/reload + frame in eligible leaf methods. A matching runtime patch refuses methods whose LIR uses REG_RA as scratch or uses FP. Sound only on a single-threaded deterministic guest with no GC suspension and no return-address hijacking |
JitRiscV64DmaCompare |
opt 1 |
— | — | — | — | Constant-size SequenceEqual as the ZisK dma_xmemcmp idiom |
JitZkCsrPInvokes, JitZkMemsetCsr, JitZkMemcpyCsr |
opt | — | — | — | — | libziskos P/Invokes, zero fills and block copies as the CSR idioms of the ZisK precompiles |
JitInlineAdditionalMultiplier |
opt 3 |
— | — | — | — | Inline more: code size is free on ZisK |
JitZkNoPinning, JitZkSynthesizeCounts, JitZkSpillKeepReg, JitZkColdCallSpill |
opt | — | — | — | opt | Register tuning for a guest with a non-moving GC |
JitForceInlineMaxIL and eight other JitZk* |
— | — | — | — | opt | Tuning measured on the Nethermind guest; refer to airbender |
The ugc-zero module adds JitZkNoGcMustInit=1 on every platform. uGC
never scans a stack, so the JIT does not have to zero a slot only because
the slot holds GC references.
JitRiscV64DmaCompare applies to the fewest targets. Only the ZisK ROM
transpiler folds that instruction pair. A real riscv64 CPU executes a CSR
access to 0x814 and then an addi to x0. zisk_sim runs on such a CPU,
under QEMU or on hardware. SP1 and OpenVM do not know the idiom. The option
is 0 by default in the JIT, so every other target keeps the ordinary
call.
The register that receives the result is the rd of the csrrs. The rd
of the addi must be x0. The mirrored form also transpiles, but through
the branch that ZisK marks as deprecated (riscv2zisk_context.rs prints
“DEPRECATED INSTRUCTION … please update toolchain/zisklibs/ziskos”). The
encoding above agrees with the reference,
ziskos/entrypoint/src/dma/memcmp.s.
These options make the ROM and .text larger, and in return give fewer heap
allocations and shorter hot paths. If a workload overflows the fixed ZisK
ROM, the notes in the ZisK platform manifest say which options to decrease
(JitExtDefaultPolicyMaxIL, JitObjectStackAllocationSize). Airbender has
a 4 MiB ROM, so it keeps the defaults of both.
bflat-modules link <layout>/lib/modules <platform manifest> prints the
effective ISA, ABI and options of a platform without a guest build (refer to
bflat-modules).
Stage 2 — The link command
ld.lld (the Clang linker, which bflat ships) makes the final ELF. bflat
writes the stock part of the command, and the manifests fill the groups
(refer to the objects section).
- The package (
<pkg>below) gives the platform part. The bindings manifest (libziskos) gives its library and wraps. The platform manifest (zisk) gives the linker script and the entry point. - The manifests in
lib/modules/(<m>below) give the module part. Each module that the platform requires gives its objects, their link group and their wraps.
For --libc zisk, abridged:
ld.lld -flavor ld --no-relax -L<lib> -z now -z relro -z noexecstack \
--hash-style=gnu --eh-frame-hdr -z nostart-stop-gc \
-static <lib>/crt1-platform.o <lib>/crti.o -o app \
app.o --as-needed --gc-sections <lib>/libbootstrapper.o --no-pie \
-lSystem.Native -latomic -leventpipe-disabled -laotminipal ... \
--as-needed -ldl -lm -lz -z relro -z now --discard-all --gc-sections \
<lib>/libc.a <lib>/libgcc.a <lib>/crtn.o \
<pkg>/libziskos.a --wrap=memcpy ... \
-T <pkg>/zisk.ld <pkg>/zisk_entry.o \
<m>/nofp/module.o --wrap=acos ... --wrap=asprintf \
--whole-archive \
<m>/ubootstrap/module.o \
<m>/stdcppshim/module.o \
<m>/rhp/module.o --wrap=RhpPInvoke ... --wrap=RhpNewFast ... \
<m>/gs_cookie/module.o --wrap=__security_cookie \
<m>/rhp_native/module.o --wrap=RhpAssignRefRiscV64 --wrap=RhpCheckedAssignRef ... \
<m>/pal/module.o --wrap=getenv ... --wrap=exit --wrap=_Exit --wrap=abort \
<m>/eh/module.o --wrap=dl_iterate_phdr \
<m>/tls/module.o --wrap=__tls_get_addr --wrap=__init_tls ... \
--no-whole-archive \
<m>/rng_stupid/module.o --wrap=minipal_get_cryptographically_secure_random_bytes ... \
<m>/rust_sys/module.o --wrap=sys_alloc_aligned \
<m>/ugc-zero/release/uGC.cpp.obj --wrap=GC_Initialize --wrap=GC_VersionInfo \
<m>/ugc-zero/release/uGCHandleManager.cpp.obj ... <m>/ugc-zero/release/ugc_zalloc.c.obj
`bflat-modules link