BlazorWASMSIMDDetectExample
This .Net 8 Blazor WASM project demonstrates a way of detecting SIMD support and using it if available.
Install / Use
/learn @LostBeard/BlazorWASMSIMDDetectExampleREADME
Blazor WASM SIMD Detect Example
Blazor Web App SIMD Detect Example
BlazorWebAppSIMDDetectExample
Blazor WebAssembly Standalone SIMD Detect Example (this repo)
BlazorWASMSIMDDetectExample
If you have done a lot of testing with Blazor WASM you may eventually hit some compatibility issues you weren't expecting. This .Net 8 Blazor WASM project demonstrates a way of detecting SIMD support and using it if available.
SIMD
Single Instruction, Multiple Data support has been added to Blazor WASM and is now enabled by default in .Net 8, and for good reason. Enabling SIMD brings some large speed improvements in many areas of Blazor WASM. There are many articles that praise the benefits of SIMD. While the linked articles below do not specifically mention Blazor, they all talk about the benefits SIMD can bring to C#.
SIMD and C# articles:
- Use SIMD-accelerated numeric types
- .Net 9 AVX10v1 Support
- LINQ on steroids with SIMD
- SIMD, a parallel processing at hardware level in C#
- LINQ Internals: Speed Optimizations
- Optimizing string.Count all the way from LINQ to hardware accelerated vectorized instructions
- Faster Guid comparisons using Vectors (SIMD) in .NET
The problem - Inconsistent SIMD support
SIMD WASM support is far from universal and a lack of support kills any Blazor WASM app that requires it before it starts.
A lack of WASM SIMD support can be caused by:
- old browsers. Convincing end users to upgrade for your site is not a great option.
- old hardware. An AMD Phenom 2 X6 has 6 cores and runs at a base speed of 3.2 GHz but no browser running on it can support SIMD because the CPU does not support it. This, and a lot of hardware like it, is still in use and is quite capable.
I also have an updated test phone running Android 9 "Pie" with the latest Firefox 118 and SIMD is not supported. Chrome on the same device supports SIMD. That same version of Firefox on a tablet running Android 11 "Red Velvet Cake" supports SIMD.
So it is obvious that SIMD support is a bit fractured.
Here are 2 options to handle SIMD compatibility issues.
Option 1 - Disable SIMD support
This is the simplest option and only requires adding the flag <WasmEnableSIMD>false</WasmEnableSIMD> to your project's .csproj file inside a <PropertyGroup>. This is the easiest and most compatible way to get around a lack of SIMD support but you lose the ability to take advantage if it is supported. <WasmEnableExceptionHandling>false</WasmEnableExceptionHandling> is also recommended for compatibility builds.
I also recommend disabling BlazorWebAssemblyJiterpreter in your compatibility build. Testing on systems that did not support SIMD with SIMD disabled builds would get an exception MONO_WASM: get_Cache:0 code generation failed: CompileError: at offset 161: bad type U ... and also the message MONO_WASM: Disabling jiterpreter after 2 failures. Setting <BlazorWebAssemblyJiterpreter>false</BlazorWebAssemblyJiterpreter> fixes it.
Option 2 - Detect SIMD support and load a supported build (method this project demos)
- Modify your index.html to detect SIMD support and load a compatibility build if needed.
- Create a compatibility build with SIMD and BlazorWebAssemblyJiterpreter disabled.
HTML file and Blazor startup
Modify the Blazor startup in the main html file. Depending on the template you used it could be index.html if a Blazor WebAssembly Standalone app or App.razor in the server project if a Blazor Web App.
The code below will detect SIMD support and use the appropriate build folder.
Modify the html file like below.
<!-- autostart is set to false so we can detect SIMD support and load the appropriate build -->
<!-- the below script can also be "_framework/blazor.web.js" depending on the hosting model -->
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<!--
WASM Feature Detect - from GoogleChromeLabs
CDN UMD Version: https://unpkg.com/wasm-feature-detect/dist/umd/index.js
Repo: https://github.com/GoogleChromeLabs/wasm-feature-detect
-->
<script webworker-enabled src="wasm-feature-detect.1.5.1.js"></script>
<!--
The below script tag is used to detect SIMD support on the running device and load the appropriate build
If SIMD is not supported it loads _frameworkCompat/ instead of _framework/
-->
<script webworker-enabled>
// Blazor WASM will fail to load if BigInt64Array or BigUint64Array is not found, but it does not use them on startup
if (!globalThis.BigInt64Array) globalThis.BigInt64Array = function () { };
if (!globalThis.BigUint64Array) globalThis.BigUint64Array = function () { };
(async () => {
var url = new URL(location.href);
let verboseStart = url.searchParams.get('verboseStart') === '1';
var forceCompatMode = url.searchParams.get('forceCompatMode') === '1';
var supportsSimd = await wasmFeatureDetect.simd();
if (verboseStart) console.log('supportsSimd', supportsSimd);
// compat mode build could be built without wasm exception support if needed and detected here
var supportsExceptions = await wasmFeatureDetect.exceptions();
if (verboseStart) console.log('supportsExceptions', supportsExceptions);
var useCompatMode = !supportsSimd || !supportsExceptions;
if (forceCompatMode) {
if (verboseStart) console.log('forceCompatMode', forceCompatMode);
useCompatMode = true;
}
if (verboseStart) console.log('useCompatMode', useCompatMode);
// Blazor United (.Net 8 Blazor Web App) Blazor.start settings are slightly different than Blazor WebAssembly (Blazor WebAssembly Standalone App)
var getRuntimeType = function () {
for (var script of document.scripts) {
if (script.src.indexOf('_framework/blazor.web.js') !== -1) return 'united';
if (script.src.indexOf('_framework/blazor.webassembly.js') !== -1) return 'wasm';
}
return '';
}
var runtimeType = getRuntimeType();
// customize the resource loader for the runtime that is loaded
// https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0#load-boot-resources
var webAssemblyConfig = {
loadBootResource: function (type, name, defaultUri, integrity) {
if (verboseStart) console.log(`Loading: '${type}', '${name}', '${defaultUri}', '${integrity}'`);
if (useCompatMode) {
let newUrl = defaultUri.replace('_framework/', '_frameworkCompat/');
return newUrl;
}
},
};
if (runtimeType === 'wasm') {
// Blazor WebAssembly Standalone App
Blazor.start(webAssemblyConfig);
} else if (runtimeType === 'united') {
// Blazor Web App (formally Blazor United)
Blazor.start({ webAssembly: webAssemblyConfig });
} else {
// Fallback supports both known Blazor WASM runtimes
// Modified loader that will work with both United and WASM runtimes (doesn't require detection)
webAssemblyConfig.webAssembly = webAssemblyConfig;
Blazor.start(webAssemblyConfig);
}
})();
</script>
ASP.Net Core Hosted
If using ASP.Net Core hosted Blazor WASM, the server needs to be told to serve the .dat file type or some files will not be served from _frameworkCompat resulting in File not found errors in the browser.
In the server project Program.cs file modify the app.UseStaticFiles call to allow serving .dat files.
// Enable the .dat file extension (required to serve icudt.dat from _frameworkCompat/
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".dat"] = "application/octet-stream";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
Blazor WASM Project Configuration
Add ReleaseCompat configuration rule to the Blazor WASM .csproj file (used during publish)
<PropertyGroup Condition=" '$(Configuration)' == 'ReleaseCompat' ">
<WasmEnableSIMD>false</WasmEnableSIMD>
<BlazorWebAssemblyJiterpreter>false</BlazorWebAssemblyJiterpreter>
<WasmEnableExceptionHandling>false</WasmEnableExceptionHandling>
</PropertyGroup>
Publish
Example publish.bat to build first with SIMD support, and then without SIMD support for compatibility. This batch script is located in the project folder. (If using ASP.Net Core hosted Blazor WASM this file would be in the Server's project folder)
REM Normal build with SIMD and BlazorWebAssemblyJiterpreter enabled (.Net 8 RC 2 defaults)
dotnet publish --nologo --configuration Release --output "bin\Publish"
REM ReleaseCompat build with SIMD and BlazorWebAssemblyJiterpreter disabled
dotnet publish --nologo --no-restore --configuration ReleaseCompat --output "bin\PublishCompat"
REM Combine builds
REM Copy the 'wwwroot\_framework' folder contents from the 2nd build to 'wwwroot\_frameworkCompat' in the 1st build
xcopy /I /
Related Skills
openhue
350.8kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
350.8kElevenLabs text-to-speech with mac-style say UX.
weather
350.8kGet current weather and forecasts via wttr.in or Open-Meteo
casdoor
13.3kAn open-source AI-first Identity and Access Management (IAM) /AI MCP & agent gateway and auth server with web UI supporting OpenClaw, MCP, OAuth, OIDC, SAML, CAS, LDAP, SCIM, WebAuthn, TOTP, MFA, Face ID, Google Workspace, Azure AD
