A patched build of .NET 11

dotnet-riscv takes the .NET VMR (dotnet/dotnet) at a release branch and builds it with its build system, with a patch series on top. The series is large. For .NET 11:

Patches Count Lines added What they change
riscv64 base (NN_*.patch) 22 about 2000 the soft-float lp64 ABI, the ISA sets, the gates on F, D, C and A emission, the -march/-mabi of the native build
zkVM code generation (perf-*.patch) 67 about 6000 RyuJIT, ILC and the NativeAOT runtime: inline allocation, CSR P/Invokes, strict alignment, DMA idioms and more
riscv32 (fixup/11/riscv32/) 7 about 4600 a new RV32 target in RyuJIT, ILC and the NativeAOT runtime

Most of the lines are in RyuJIT. The patches change the JIT/EE interface, so the ILCompiler of a release works only with the runtime of the same release.

bflat applies each zkVM change that it can around the runtime (see Architecture):

  • ILC-stage substitutions;
  • link-time modules;
  • post-link changes to the ELF.

A change goes into the earliest of these layers that can express it. A change that none of them can make goes into the patch series.

The patches we carry

Each patch is in a fixup profile for one .NET version. Thus you can see which patches apply to which .NET. bflat builds for .NET 11 only, so this section describes the .NET 11 profiles:

upstream
The numbered patches (NN_*.patch): the soft-float lp64 ABI, the ISA sets and the gates on F, D, C and A emission, and the configurable -march/-mabi of the native build. The profile stands alone: its patches apply and build without the patches of the other profiles.
upstream-perf
upstream plus the perf-*.patch files next to it: riscv64 code-quality patches for the zkVM guests. They work with the RyuJIT knobs of the platforms. They are for zkVMs only. bflat bundles this profile. The riscv32 target is available only with it.
minimal, perf
The older downstream series: correctness fixes only, and the same with code-quality work on top. They stay for comparison runs.

The runtime build selects the profile (patch_runtime.sh minimal|perf|upstream|upstream-perf). The patches have one directory for each .NET major, so a new runtime release cannot take, by accident, the fixups for the previous major. With the riscv32 target, the build applies fixup/<major>/riscv32/ on top of upstream-perf. It adds riscv32 to the JIT, ILC and the NativeAOT runtime.

The directory has the full list of patches: fixup/11/profile/upstream/ (its README.md gives the origin and the status of each patch) and fixup/11/riscv32/.

What the build produces

NethermindEth/dotnet-riscv makes the files that bflat links against. Its workflow “Build .NET SDK” (build.yml) does these steps:

  1. Pulls the upstream .NET VMR (dotnet/dotnet) at a release branch. The releases that bflat uses are built from release/11.0.1xx.
  2. Applies the fixup profile for that .NET major (upstream-perf for bflat), and the riscv32 fixups when the riscv32 target is built.
  3. Builds against an Alpine RISC-V64 cross rootfs from upstream’s own eng/common/cross/build-rootfs.sh. We do not rebuild a distribution. Two packages are the exception. Alpine’s feed has musl and libatomic for rv64gc, so the build compiles both again for rv64im (build_musl_rv64im.sh, build_libatomic_rv64im.sh). They replace the stock libc.a, crt objects and libatomic.a. A guest for the base ISA cannot link against compressed or atomic instructions. A change to the ABI marker of an object does not remove those instructions.
  4. Packs the results into archives, signs them and publishes them as a release. bflat downloads the release by URL (see Package signatures).

Keeping the native runtime on the base ISA

The fixups guard hand-written assembly with #ifdef __riscv_flen and #ifdef __riscv_atomic. The guards work only when these macros are not defined, that is, when the code compiles for the base ISA. Upstream’s CMake sets no -march for a riscv64 cross target. Thus the build takes the default of the toolchain, and the guards have no effect.

Patches of the upstream profile add two CMake variables. The build sets them for all of the native build:

build.sh --arch riscv64 --os linux-musl \
    -cmakeargs "-DCLR_CMAKE_RISCV64_MABI=lp64" \
    -cmakeargs "-DCLR_CMAKE_RISCV64_MARCH=rv64im"

All components use one ISA: the NativeAOT runtime, the GC, both libunwinds, System.Native and the SDK side. The CoreCLR PAL uses the savannah libunwind. A patch lets it build with __riscv_flen == 0, so no component needs a wider ISA. Earlier releases used a compiler wrapper (tools/clang) for this. The CMake variables replace it.

After the build, check_lp64.sh makes sure that each riscv64 object has the lp64 soft-float ABI. To find an object that has F/D or A code, build a guest with --error-on-float-binary --error-on-atomic. The check gives the name of the symbol.

Step Script Output
0 00_build_rootfs.sh GNU and musl RISC-V64 cross rootfs, both from upstream tooling
1 01_pack_compiler_linux.sh x64-Linux–hosted bflat driver binary
2 02_pack_crossrootfs.sh Compressed cross rootfs as a release artifact
3 03_pack_gnu_libs.sh GNU runtime libraries needed at link time
4 04_pack_libs.sh Built CoreCLR runtime libraries (libSystem.Native, …)
6 06_pack_refs.sh Reference assemblies bflat consumes
7 07_pack_bflat_libs_linux.sh The NativeAOT runtime, CoreLib and the framework libraries of the riscv64 target
8 08_pack_bflat_compiler_nupkg.sh ILCompiler (the AOT compiler bflat drives) packed as a NuGet .nupkg
9 09_pack_bflat_compiler_native_linux.sh Native-RISC-V64–hosted driver
10 10_build_runtime_x64.sh The x64 JIT and NativeAOT runtime, for the linux-x64 target
11 11_pack_x64.sh The three assets of the linux-x64 target (see below)
12 12_build_runtime_riscv32.sh The riscv32 JIT and NativeAOT runtime, for the riscv32 target
13 13_pack_riscv32.sh The four assets of the riscv32 target (see below)
⋯ xx_pack_whole_source.sh Source archive of the full tree

The riscv32 sysroot has its own scripts: build_musl_rv32im.sh, build_builtins_rv32im.sh (the compiler-rt builtins, in place of libgcc.a), build_libatomic_rv32im.sh, build_libcxx_headers_rv32.sh and install_linux_headers_rv32.sh. check_ilp32.sh is the ABI gate of that target.

How the artifact reaches bflat

bflat downloads one release that a property names. The release tag is RuntimeReleaseTag in bflat.variant.props. The tag is the SDK version of the VMR plus the profile, and the run number when a release of that SDK version exists already. The default build and -p:BflatTargetRiscV32=true use the same release, 11.0.102-upstream-perf.r442, a release of dotnet-riscv main that has the riscv32 target too.

The tag changes with each new release. The download cache uses the tag as its key, so a new tag downloads again. The build checks the signature of the release before it uses it.

A build can also take the runtime of one run of the dotnet-riscv workflow, before there is a release. A run uploads the signed set of a release as artifacts: release-assets-* (the release assets) and bflat-signatures (bflat-signature.sha256 and bflat-signature.sha256.sig). -p:RuntimeLocalDir=<dir> names a directory with these files, unpacked from the artifacts into one directory, with the names of the release assets.

The build copies the files into the download cache on each build and names the missing ones. It checks them with verify-release as for a release. Without bflat-signature.sha256 and its signature, or without BFLAT_SIGNING_PUBLIC_KEY, the build fails; -p:AllowUnsignedRuntime=true does not apply to it. -p:RuntimeLocalId=<id> names the run (default dir); the cache key and bflat --info show local-<id>. When a file in the directory changes, the next build extracts the files again: the build compares the hashes of the files, not their times. Without RuntimeLocalDir, the build takes the release as before.

In CI, the input dotnet_riscv_run_id of build-image.yml (and of a manual run of build-riscv64.yml) does this. It accepts only a successful run of build.yml whose head repository is NethermindEth/dotnet-riscv, and it takes the release-assets-* and bflat-signatures artifacts of the run. A run without bflat-signatures, or whose files do not agree with the signed list, fails, and the input signing_public_key is required. The images get the GHCR tag <sha>-rt<run id> and are never published to Docker Hub.

The build unpacks the archives into lib/<os>/<arch>/<libc> next to the bflat binary. bflat build gives these files to the linker.

The layout build checks the float ABI of each RISC-V object (bflat-modules check-abi): lp64 in lib/linux/riscv64/musl and ilp32 in lib/linux/riscv32/musl. bflat links these files as they are.

The linux-x64 target

A release can also have the ordinary linux-x64 target of bflat. The runtime-x64 job of dotnet-riscv builds it from dotnet/runtime at the commit that the VMR pins. It uses the same fixups and the same JIT/EE GUID. Thus the x64 JIT and CoreLib agree with the ILCompiler of the release:

Asset Unpacked into Contents
bflat-compiler-native-x64-linux-glibc-x64.zip the bflat directory libclrjit_unix_x64_x64.so
bflat-libs-linux-glibc-x64.zip lib/linux/x64/glibc the NativeAOT runtime, CoreLib, the framework libraries
libs-linux-glibc-x64.zip lib/linux/x64/glibc crt objects, libc, libm, libgcc and the other files of the link, from the glibc rootfs of the dotnet/runtime cross-build container

Microsoft’s x64 packages do not work with this ILCompiler, because the fixups change the JIT/EE interface. bflat uses the target when BflatTargetLinuxX64 in bflat.variant.props is true (the default); -p:BflatTargetLinuxX64=false builds without it.

The riscv32 target

The runtime-riscv32 job of dotnet-riscv builds the RV32IM ilp32 target of the Airbender platform. It runs only with the upstream-perf profile and the riscv32 option of the workflow. Its assets are:

Asset Unpacked into Contents
bflat-compiler-native-riscv32-linux-glibc-x64.zip the bflat directory libclrjit_unix_riscv32_x64.so, the JIT that ILCompiler loads for riscv32
bflat-libs-linux-musl-riscv32.zip lib/linux/riscv32/musl the NativeAOT runtime, CoreLib, libSystem.Native, the framework libraries
libs-linux-musl-riscv32.zip lib/linux/riscv32/musl crt objects, libc, libatomic and the compiler-rt builtins of the link
crossrootfs-musl-riscv32.tar.xz RV32_SYSROOT the whole rv32im sysroot with headers, for the C modules (build.sh modules riscv32)

The pack step fails in these conditions:

  • a file that bflat links is missing;
  • an object is not ilp32, or claims the C extension;
  • an object has a compressed, atomic or fence instruction (tools/isa_scan.py).

bflat adds the target to its layout with -p:BflatTargetRiscV32=true. The layout keeps the riscv64 target too.

License

dotnet-riscv uses the MIT license. The fixups against upstream keep the licenses of their original authors (most of .NET is MIT). See the project’s LICENSE.md.