-
Notifications
You must be signed in to change notification settings - Fork 1k
Rekhoff/reapply update nativeaot llvm infrastructure #4897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rekhoff
merged 25 commits into
master
from
rekhoff/reapply-update-nativeaot-llvm-infrastructure
Apr 28, 2026
Merged
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8526de8
Update NativeAOT-LLVM infrastructure to current ABI and add CI smoketest
cloutiertyler 1b01ebb
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 3e64dbd
Update C# Runtime dependencies to support NativeAOT-LLVM prerequisite…
rekhoff ae56bb7
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 9316256
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 6b8fa90
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 006b564
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 06a2161
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 793684b
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 5893f67
Enable `--native-aot` flag during `spacetime init` and `spacetime pub…
rekhoff 56f0b96
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff 6ea7178
Corrected test to use valid publish format
rekhoff 84726cf
Move NativeAOT-LLVM for .NET 8 job to smoketest
rekhoff 586ddf9
Change `skip` to `fail` if `emscripten` is not installed for test
rekhoff 396a59c
Add `emscripten` to GITHUB_PATH and clear NuGet packages in NativeAOT…
rekhoff 28023d0
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
rekhoff e4c2516
Reapply "Update NativeAOT-LLVM infrastructure to current ABI (#4515)"
jdetter e453e4b
Merge branch 'master' into tyler/update-nativeaot-llvm-infrastructure
jdetter 9f44637
Update `init` with default_server
rekhoff 486dcc3
Fix call sites and lints
rekhoff b69ed9a
Merge branch 'master' into rekhoff/reapply-update-nativeaot-llvm-infr…
rekhoff 4f4e41b
Merge branch 'master' into rekhoff/reapply-update-nativeaot-llvm-infr…
rekhoff 4c4b5eb
Merge branch 'master' into rekhoff/reapply-update-nativeaot-llvm-infr…
rekhoff 5c665c9
Removing emscripten from `GITHUB_PATH` environment variable
rekhoff f8a1860
Merge branch 'master' into rekhoff/reapply-update-nativeaot-llvm-infr…
rekhoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| # Using NativeAOT-LLVM with SpacetimeDB C# Modules | ||
|
|
||
| This guide provides instructions for enabling NativeAOT-LLVM compilation for C# SpacetimeDB modules, which can provide performance improvements. | ||
|
|
||
| ## Overview | ||
|
|
||
| NativeAOT-LLVM compiles C# modules to native WebAssembly (WASM) instead of using the Mono runtime. | ||
|
|
||
| > [!WARNING] | ||
| > This is currently only supported for Windows server modules and is experimental. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - **.NET SDK 8.x** (same version used by SpacetimeDB) | ||
| - **Emscripten SDK (EMSDK)** installed (must contain `upstream/emscripten/emcc.bat`) | ||
| - **(Optional) Binaryen (wasm-opt)** installed and on `PATH` (recommended: `version_116`) | ||
| - **Windows** - NativeAOT-LLVM is currently only supported for Windows server modules | ||
|
|
||
| ## Prerequisites Installation | ||
|
|
||
| ### Install Emscripten SDK (EMSDK) | ||
|
|
||
| The Emscripten SDK is required for NativeAOT-LLVM compilation: | ||
|
|
||
| 1. **Download and extract** the Emscripten SDK from `https://github.com/emscripten-core/emsdk` | ||
| - Example path: `D:\Tools\emsdk` | ||
|
|
||
| 2. **Set environment variable** (optional - the CLI will detect it automatically): | ||
| ``` | ||
| $env:EMSDK="D:\Tools\emsdk" | ||
| ``` | ||
|
|
||
| ### Install Binaryen (Optional) | ||
|
|
||
| Binaryen provides `wasm-opt` for WASM optimization (recommended for performance): | ||
|
|
||
| 1. Download Binaryen https://github.com/WebAssembly/binaryen/releases/tag/version_116 for Windows | ||
| 2. Extract to e.g. `D:\Tools\binaryen` | ||
| 3. Add `D:\Tools\binaryen\bin` to `PATH` | ||
|
|
||
| To temporarily add to your current PowerShell session: | ||
| ``` | ||
| $env:PATH += ";D:\Tools\binaryen\bin" | ||
| ``` | ||
| 4. Verify: | ||
| ``` | ||
| wasm-opt --version | ||
| ``` | ||
|
|
||
| ## Creating a New NativeAOT Project | ||
|
|
||
| When creating a new C# project, use the `--native-aot` flag: | ||
|
|
||
| ``` | ||
| spacetime init --lang csharp --native-aot my-native-aot-project | ||
| ``` | ||
|
|
||
| This automatically: | ||
| - Creates a C# project with the required package references | ||
| - Generates a `spacetime.json` with `"native-aot": true` | ||
| - Configures the project for NativeAOT-LLVM compilation | ||
|
|
||
| ## Converting an Existing Project | ||
|
|
||
| 1. **Update spacetime.json** | ||
| Add `"native-aot": true` to your `spacetime.json`: | ||
| ```json | ||
| { | ||
| "module": "your-module-name", | ||
| "native-aot": true | ||
| } | ||
| ``` | ||
|
|
||
| **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. | ||
|
|
||
| 2. **Ensure NuGet feed is configured** | ||
| NativeAOT-LLVM packages come from **dotnet-experimental**. Add to `NuGet.Config`: | ||
| ```xml | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <configuration> | ||
| <packageSources> | ||
| <clear /> | ||
| <add key="dotnet-experimental" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-experimental/nuget/v3/index.json" /> | ||
| <add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> | ||
| </packageSources> | ||
| </configuration> | ||
| ``` | ||
|
|
||
| 3. **Add NativeAOT package references** | ||
| Add this `ItemGroup` to your `.csproj`: | ||
| ```xml | ||
| <ItemGroup Condition="'$(EXPERIMENTAL_WASM_AOT)' == '1'"> | ||
| <PackageReference Include="Microsoft.NET.ILLink.Tasks" Version="8.0.0-*" Condition="'$(ILLinkTargetsPath)' == ''" /> | ||
| <PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> | ||
| <PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| Your complete `.csproj` should look like: | ||
| ```xml | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <RuntimeIdentifier>wasi-wasm</RuntimeIdentifier> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="SpacetimeDB.Runtime" Version="2.0.*" /> | ||
| </ItemGroup> | ||
| <ItemGroup Condition="'$(EXPERIMENTAL_WASM_AOT)' == '1'"> | ||
| <PackageReference Include="Microsoft.NET.ILLink.Tasks" Version="8.0.0-*" Condition="'$(ILLinkTargetsPath)' == ''" /> | ||
| <PackageReference Include="Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> | ||
| <PackageReference Include="runtime.$(NETCoreSdkPortableRuntimeIdentifier).Microsoft.DotNet.ILCompiler.LLVM" Version="8.0.0-*" /> | ||
| </ItemGroup> | ||
| </Project> | ||
| ``` | ||
|
|
||
| ## Publishing Your NativeAOT Module | ||
|
|
||
| After completing either the **Creating a New NativeAOT Project** or **Converting an Existing Project** steps above, you can publish your module normally: | ||
|
|
||
| ``` | ||
| # From your project directory | ||
| spacetime publish your-database-name | ||
| ``` | ||
|
|
||
| If you have `"native-aot": true` in your `spacetime.json`, the CLI will automatically detect this and use NativeAOT compilation. Alternatively, you can use: | ||
|
|
||
| ``` | ||
| spacetime publish --native-aot your-database-name | ||
| ``` | ||
|
|
||
| The CLI will display "Using NativeAOT-LLVM compilation (experimental)" when NativeAOT is enabled. | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Package source mapping enabled | ||
| If you have **package source mapping** enabled in `NuGet.Config`, add mappings for the LLVM packages: | ||
|
|
||
| ```xml | ||
| <packageSourceMapping> | ||
| <packageSource key="bsatn-runtime"> | ||
| <package pattern="SpacetimeDB.BSATN.Runtime" /> | ||
| </packageSource> | ||
| <packageSource key="SpacetimeDB.Runtime"> | ||
| <package pattern="SpacetimeDB.Runtime" /> | ||
| </packageSource> | ||
| <packageSource key="dotnet-experimental"> | ||
| <package pattern="Microsoft.DotNet.ILCompiler.LLVM" /> | ||
| <package pattern="runtime.*" /> | ||
| </packageSource> | ||
| <packageSource key="nuget.org"> | ||
| <package pattern="*" /> | ||
| </packageSource> | ||
| </packageSourceMapping> | ||
| ``` | ||
|
|
||
| ### wasi-experimental workload install fails | ||
| If the CLI cannot install the `wasi-experimental` workload automatically, install it manually: | ||
|
|
||
| ``` | ||
| dotnet workload install wasi-experimental | ||
| ``` | ||
|
|
||
| ### Duplicate PackageReference warning | ||
| You may see a `NU1504` warning about duplicate `PackageReference` items. This is expected and non-blocking. | ||
|
|
||
| ### Code generation failed | ||
| If you see errors like "Code generation failed for method", ensure: | ||
| 1. You're using `SpacetimeDB.Runtime` version 2.0.4 or newer | ||
| 2. All required package references are in your `.csproj` | ||
| 3. The `dotnet-experimental` feed is configured in `NuGet.Config` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 41 additions & 23 deletions
64
crates/bindings-csharp/Runtime/build/SpacetimeDB.Runtime.targets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.