|
| 1 | +# Using NativeAOT-LLVM with SpacetimeDB C# Modules |
| 2 | + |
| 3 | +This guide provides instructions for enabling NativeAOT-LLVM compilation for C# SpacetimeDB modules, which can provide performance improvements. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +NativeAOT-LLVM compiles C# modules to native WebAssembly (WASM) instead of using the Mono runtime. |
| 8 | + |
| 9 | +> [!WARNING] |
| 10 | +> This is currently only supported for Windows server modules and is experimental. |
| 11 | +
|
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- **.NET SDK 8.x** (same version used by SpacetimeDB) |
| 15 | +- **Emscripten SDK (EMSDK)** installed (must contain `upstream/emscripten/emcc.bat`) |
| 16 | +- **(Optional) Binaryen (wasm-opt)** installed and on `PATH` (recommended: `version_116`) |
| 17 | +- **Windows** - NativeAOT-LLVM is currently only supported for Windows server modules |
| 18 | + |
| 19 | +## Prerequisites Installation |
| 20 | + |
| 21 | +### Install Emscripten SDK (EMSDK) |
| 22 | + |
| 23 | +The Emscripten SDK is required for NativeAOT-LLVM compilation: |
| 24 | + |
| 25 | +1. **Download and extract** the Emscripten SDK from `https://github.com/emscripten-core/emsdk` |
| 26 | + - Example path: `D:\Tools\emsdk` |
| 27 | + |
| 28 | +2. **Set environment variable** (optional - the CLI will detect it automatically): |
| 29 | + ``` |
| 30 | + $env:EMSDK="D:\Tools\emsdk" |
| 31 | + ``` |
| 32 | + |
| 33 | +### Install Binaryen (Optional) |
| 34 | + |
| 35 | +Binaryen provides `wasm-opt` for WASM optimization (recommended for performance): |
| 36 | + |
| 37 | +1. Download Binaryen https://github.com/WebAssembly/binaryen/releases/tag/version_116 for Windows |
| 38 | +2. Extract to e.g. `D:\Tools\binaryen` |
| 39 | +3. Add `D:\Tools\binaryen\bin` to `PATH` |
| 40 | + |
| 41 | + To temporarily add to your current PowerShell session: |
| 42 | + ``` |
| 43 | + $env:PATH += ";D:\Tools\binaryen\bin" |
| 44 | + ``` |
| 45 | +4. Verify: |
| 46 | + ``` |
| 47 | + wasm-opt --version |
| 48 | + ``` |
| 49 | + |
| 50 | +## Creating a New NativeAOT Project |
| 51 | + |
| 52 | +When creating a new C# project, use the `--native-aot` flag: |
| 53 | + |
| 54 | +``` |
| 55 | +spacetime init --lang csharp --native-aot my-native-aot-project |
| 56 | +``` |
| 57 | + |
| 58 | +This automatically: |
| 59 | +- Creates a C# project with the required package references |
| 60 | +- Generates a `spacetime.json` with `"native-aot": true` |
| 61 | +- Configures the project for NativeAOT-LLVM compilation |
| 62 | + |
| 63 | +## Converting an Existing Project |
| 64 | + |
| 65 | +1. **Update spacetime.json** |
| 66 | + Add `"native-aot": true` to your `spacetime.json`: |
| 67 | + ```json |
| 68 | + { |
| 69 | + "module": "your-module-name", |
| 70 | + "native-aot": true |
| 71 | + } |
| 72 | + ``` |
| 73 | + |
| 74 | + **Note:** Once `spacetime.json` has `"native-aot": true`, you can simply run `spacetime publish` without the `--native-aot` flag. The CLI will automatically detect the configuration and use NativeAOT compilation. |
| 75 | + |
| 76 | +2. **Ensure NuGet feed is configured** |
| 77 | + NativeAOT-LLVM packages come from **dotnet-experimental**. Add to `NuGet.Config`: |
| 78 | + ```xml |
| 79 | + <?xml version="1.0" encoding="utf-8"?> |
| 80 | + <configuration> |
| 81 | + <packageSources> |
| 82 | + <clear /> |
| 83 | + <add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" /> |
| 84 | + <add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> |
| 85 | + </packageSources> |
| 86 | + </configuration> |
| 87 | + ``` |
| 88 | + |
| 89 | +3. **Add NativeAOT package references** |
| 90 | + Add this `ItemGroup` to your `.csproj`: |
| 91 | + ```xml |
| 92 | + <ItemGroup Condition="'$(EXPERIMENTAL_WASM_AOT)' == '1'"> |
| 93 | + <PackageReference Include="Microsoft.NET.ILLink.Tasks" Version="8.0.0-*" Condition="'$(ILLinkTargetsPath)' == ''" /> |
| 94 | + <PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> |
| 95 | + <PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> |
| 96 | + </ItemGroup> |
| 97 | + ``` |
| 98 | + |
| 99 | + Your complete `.csproj` should look like: |
| 100 | + ```xml |
| 101 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 102 | + <PropertyGroup> |
| 103 | + <TargetFramework>net8.0</TargetFramework> |
| 104 | + <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier> |
| 105 | + <ImplicitUsings>enable</ImplicitUsings> |
| 106 | + <Nullable>enable</Nullable> |
| 107 | + </PropertyGroup> |
| 108 | + <ItemGroup> |
| 109 | + <PackageReference Include="SpacetimeDB.Runtime" Version="2.0.*" /> |
| 110 | + </ItemGroup> |
| 111 | + <ItemGroup Condition="'$(EXPERIMENTAL_WASM_AOT)' == '1'"> |
| 112 | + <PackageReference Include="Microsoft.NET.ILLink.Tasks" Version="8.0.0-*" Condition="'$(ILLinkTargetsPath)' == ''" /> |
| 113 | + <PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> |
| 114 | + <PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> |
| 115 | + </ItemGroup> |
| 116 | + </Project> |
| 117 | + ``` |
| 118 | + |
| 119 | +## Publishing Your NativeAOT Module |
| 120 | + |
| 121 | +After completing either the **Creating a New NativeAOT Project** or **Converting an Existing Project** steps above, you can publish your module normally: |
| 122 | + |
| 123 | +``` |
| 124 | +# From your project directory |
| 125 | +spacetime publish your-database-name |
| 126 | +``` |
| 127 | + |
| 128 | +If you have `"native-aot": true` in your `spacetime.json`, the CLI will automatically detect this and use NativeAOT compilation. Alternatively, you can use: |
| 129 | + |
| 130 | +``` |
| 131 | +spacetime publish --native-aot your-database-name |
| 132 | +``` |
| 133 | + |
| 134 | +The CLI will display "Using NativeAOT-LLVM compilation (experimental)" when NativeAOT is enabled. |
| 135 | + |
| 136 | +## Troubleshooting |
| 137 | + |
| 138 | +### Package source mapping enabled |
| 139 | +If you have **package source mapping** enabled in `NuGet.Config`, add mappings for the LLVM packages: |
| 140 | + |
| 141 | +```xml |
| 142 | +<packageSourceMapping> |
| 143 | + <packageSource key="bsatn-runtime"> |
| 144 | + <package pattern="SpacetimeDB.BSATN.Runtime" /> |
| 145 | + </packageSource> |
| 146 | + <packageSource key="SpacetimeDB.Runtime"> |
| 147 | + <package pattern="SpacetimeDB.Runtime" /> |
| 148 | + </packageSource> |
| 149 | + <packageSource key="dotnet-experimental"> |
| 150 | + <package pattern="Microsoft.DotNet.ILCompiler.LLVM" /> |
| 151 | + <package pattern="runtime.*" /> |
| 152 | + </packageSource> |
| 153 | + <packageSource key="nuget.org"> |
| 154 | + <package pattern="*" /> |
| 155 | + </packageSource> |
| 156 | +</packageSourceMapping> |
| 157 | +``` |
| 158 | + |
| 159 | +### wasi-experimental workload install fails |
| 160 | +If the CLI cannot install the `wasi-experimental` workload automatically, install it manually: |
| 161 | + |
| 162 | +``` |
| 163 | +dotnet workload install wasi-experimental |
| 164 | +``` |
| 165 | + |
| 166 | +### Duplicate PackageReference warning |
| 167 | +You may see a `NU1504` warning about duplicate `PackageReference` items. This is expected and non-blocking. |
| 168 | + |
| 169 | +### Code generation failed |
| 170 | +If you see errors like "Code generation failed for method", ensure: |
| 171 | +1. You're using `SpacetimeDB.Runtime` version 2.0.4 or newer |
| 172 | +2. All required package references are in your `.csproj` |
| 173 | +3. The `dotnet-experimental` feed is configured in `NuGet.Config` |
| 174 | + |
0 commit comments