Skip to content

Commit

Permalink
Merge pull request #774 from unoplatform/dev/jela/restore-simd-option
Browse files Browse the repository at this point in the history
Restore SIMD option
  • Loading branch information
jeromelaban authored Sep 18, 2023
2 parents ef10bb7 + 9ac2b82 commit 6fef00a
Show file tree
Hide file tree
Showing 16 changed files with 950 additions and 878 deletions.
10 changes: 6 additions & 4 deletions doc/features-module-linking.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ If the bitcode file to be added is named `libTest.bc` or `libTest.a`, the follow
| `libTest.bc/2.0.9/st/libTest.bc` | Emscripten 2.0.9 and later, single threaded |
| `libTest.bc/2.0.6/mt/libTest.bc` | Emscripten 2.0.6 and later, multi threaded |
| `libTest.bc/2.0.9/mt/libTest.bc` | Emscripten 2.0.9 and later, multi threaded |
| `libTest.bc/2.0.6/st,simd/libTest.bc` | Emscripten 2.0.6 and later, single threaded with SIMD |
| `libTest.bc/2.0.9/st,simd/libTest.bc` | Emscripten 2.0.9 and later, single threaded with SIMD |
| `libTest.bc/2.0.6/mt,simd/libTest.bc` | Emscripten 2.0.6 and later, multi threaded with SIMD |
| `libTest.bc/2.0.9/mt,simd/libTest.bc` | Emscripten 2.0.9 and later, multi threaded with SIMD |

Based on the emscripten version used by the .NET runtime and the enabled runtime features, the bootstrapper will choose the closest matching version.

As of bootstrapper 8.0, the following runtime features are supported:
As of bootstrapper 7.0, the following runtime features are supported:
- `st` for Single Threaded runtime
- `mt` for Multi Threaded runtime

## Deprecated features
`simd` is deprecated for .NET 8 and is always enabled
- `simd` for SIMD enabled runtime

#### Static Linking additional emcc flags
Static linking may also require some additional emscripten flags, for instance when using libpng. In such a case, add the following to your project:
Expand Down
25 changes: 25 additions & 0 deletions doc/features-simd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Support for SIMD

Starting from .NET 7, support for SIMD is available through the following property:

```xml
<PropertyGroup>
<WasmShellEnableSimd>true</WasmShellEnableSimd>
</PropertyGroup>
```

With .NET 8, SIMD support is enabled by default and can be disabled using:
```xml
<PropertyGroup>
<WasmShellEnableSimd>false</WasmShellEnableSimd>
</PropertyGroup>
```

[WebAssembly Support for SIMD](https://github.com/webassembly/simd) enables faster execution for specialized pieces of code, and .NET increasingly uses those instructions to make applications run faster.

You can take a look at [this article](https://platform.uno/blog/safari-16-4-support-for-webassembly-fixed-width-simd-how-to-use-it-with-c/) for more information.

## Restriction for the use of SIMD
While SIMD is supported by all major browsers by default, some security modes can require disabling it.

For instance, Microsoft Edge's [Enhanced Security mode](https://learn.microsoft.com/en-us/deployedge/microsoft-edge-security-browse-safer) disables the use of SIMD, as well as iOS's [Lockdown mode](https://support.apple.com/en-us/HT212650).
2 changes: 2 additions & 0 deletions doc/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ items:
href: features-memory-corruption-troubleshooting.md
- name: Module Linking
href: features-module-linking.md
- name: SIMD Support
href: features-simd.md
- name: Profiling
href: features-profiling.md
- name: Node JS
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Wasm.Bootstrap/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Uno.Wasm.Bootstrap
internal class Constants
{
public const string DefaultDotnetRuntimeSdkUrl = "https://unowasmbootstrap.azureedge.net/runtime/"
+ "dotnet-runtime-wasm-linux-d3ee003-49b266d7eb2-6123022990-Release.zip";
+ "dotnet-runtime-wasm-linux-e7bb704-49b266d7eb2-6216507467-Release.zip";

/// <summary>
/// Min version of the emscripten SDK. Must be aligned with dotnet/runtime SDK build in <see cref="NetCoreWasmSDKUri"/>.
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ private FileNameObfuscationMode _assembliesFileNameObfuscationMode

public int PThreadsPoolSize { get; set; } = 4;

[Microsoft.Build.Framework.Required]
public bool EnableSimd { get; set; }

[Microsoft.Build.Framework.Required]
public bool RuntimeDebuggerEnabled { get; set; }

Expand Down Expand Up @@ -795,6 +798,7 @@ private void RunPackager()

var runtimeConfigurationParam = $"--runtime-config={RuntimeConfiguration.ToLowerInvariant()}"
+ (EnableThreads ? "-threads" : "") + " "
+ (EnableSimd ? "-simd" : "") + " "
+ (EnableJiterpreter ? "-jiterpreter" : "") + " "
+ (string.IsNullOrWhiteSpace(RuntimeOptions) ? "" : $"--runtime-options \"{RuntimeOptions}\" ");

Expand Down Expand Up @@ -1408,6 +1412,11 @@ private IEnumerable<string> GetBitcodeFilesParams()
EnableThreads ? "mt" : "st"
};

if (EnableSimd)
{
features.Add("simd");
}

Log.LogMessage(MessageImportance.Low, $"Bitcode files features lookup filter: {string.Join(",", features)}");

_bitcodeFilesCache = BitcodeFilesSelector.Filter(CurrentEmscriptenVersion, features.ToArray(), _bitcodeFilesCache);
Expand Down
8 changes: 8 additions & 0 deletions src/Uno.Wasm.Bootstrap/UnoInstallSDKTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class UnoInstallSDKTask_v0 : Microsoft.Build.Utilities.Task, ICancelableT
[Microsoft.Build.Framework.Required]
public bool EnableThreads { get; set; }

[Microsoft.Build.Framework.Required]
public bool EnableSimd { get; set; }

public bool EnableEmscriptenWindows { get; set; } = true;

[Microsoft.Build.Framework.Required]
Expand Down Expand Up @@ -110,6 +113,11 @@ private async Task InstallNetCoreWasmSdk(CancellationToken ct)
sdkUri = sdkUri.Replace(".zip", "-threads.zip");
}

if (EnableSimd)
{
sdkUri = sdkUri.Replace(".zip", "-simd.zip");
}

var sdkName = Path.GetFileNameWithoutExtension(new Uri(sdkUri).AbsolutePath.Replace('/', Path.DirectorySeparatorChar));
Log.LogMessage("NetCore-Wasm SDK: " + sdkName);
SdkPath = Path.Combine(GetMonoTempPath(), sdkName);
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<WasmShellBrotliCompressionQuality Condition="'$(WasmShellBrotliCompressionQuality)'==''">7</WasmShellBrotliCompressionQuality>
<WasmShellEmccLinkOptimization Condition="'$(WasmShellEmccLinkOptimization)'=='' and '$(Configuration)'=='Release'">true</WasmShellEmccLinkOptimization>
<WasmShellEnableThreads Condition="'$(WasmShellEnableThreads)'==''">false</WasmShellEnableThreads>
<WasmShellEnableSimd Condition="'$(WasmShellEnableSimd)'==''">true</WasmShellEnableSimd>

<!--
Force all applicable references to be present in the ReferenceCopyLocalPaths property.
Expand Down Expand Up @@ -184,6 +185,7 @@
CilStripOverrideFolderPath="$(MSBuildThisFileDirectory)/cilstrip"
DisableSDKCheckSumValidation="$(WasmShellDisableSDKCheckSumValidation)"
EnableEmscriptenWindows="$(WasmShellEnableEmscriptenWindows)"
EnableSimd="$(WasmShellEnableSimd)"
EnableThreads="$(WasmShellEnableThreads)"
GenerateAOTProfile="$(WasmShellGenerateAOTProfile)"
IsOSUnixLike="$([MSBuild]::IsOsUnixLike())"
Expand Down Expand Up @@ -229,6 +231,7 @@
EnableLogProfiler="$(WasmShellEnableLogProfiler)"
EnableLongPathSupport="$(WasmShellEnableLongPathSupport)"
EnableNetCoreICU="$(WasmShellEnableNetCoreICU)"
EnableSimd="$(WasmShellEnableSimd)"
EnableThreads="$(WasmShellEnableThreads)"
ExtraEmccFlags="@(WasmShellExtraEmccFlags)"
ForceDisableWSL="$(WasmShellForceDisableWSL)"
Expand Down
30 changes: 25 additions & 5 deletions src/Uno.Wasm.Packager/packager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ class WasmOptions {
public bool EnableFS;
public bool EnableThreads;
public bool EnableJiterpreter;
public bool Simd;
public bool EnableDynamicRuntime;
public bool LinkerExcludeDeserialization;
public bool EnableCollation;
Expand Down Expand Up @@ -481,6 +482,7 @@ int Run (string[] args) {
bool enable_dynamic_runtime = false;
bool is_netcore = false;
bool is_windows = Environment.OSVersion.Platform == PlatformID.Win32NT;
bool enable_simd = false;
var il_strip = false;
var linker_verbose = false;
var runtimeTemplate = "runtime.js";
Expand Down Expand Up @@ -524,6 +526,7 @@ int Run (string[] args) {
LinkerVerbose = false,
EnableZLib = false,
EnableFS = false,
Simd = false,
EnableDynamicRuntime = false,
LinkerExcludeDeserialization = true,
EnableCollation = false,
Expand Down Expand Up @@ -588,6 +591,7 @@ int Run (string[] args) {
AddFlag (p, new BoolFlag ("jiterpreter", "enable jiterpreter", opts.EnableJiterpreter, b => opts.EnableJiterpreter = b));
AddFlag (p, new BoolFlag ("dedup", "enable dedup pass", opts.EnableDedup, b => opts.EnableDedup = b));
AddFlag (p, new BoolFlag ("dynamic-runtime", "enable dynamic runtime (support for Emscripten's dlopen)", opts.EnableDynamicRuntime, b => opts.EnableDynamicRuntime = b));
AddFlag (p, new BoolFlag ("simd", "enable SIMD support", opts.Simd, b => opts.Simd = b));
AddFlag (p, new BoolFlag ("wasm-exceptions", "enable exceptions", opts.EnableWasmExceptions, b => opts.EnableWasmExceptions = b));
AddFlag (p, new BoolFlag ("linker-exclude-deserialization", "Link out .NET deserialization support", opts.LinkerExcludeDeserialization, b => opts.LinkerExcludeDeserialization = b));
AddFlag (p, new BoolFlag ("collation", "enable unicode collation support", opts.EnableCollation, b => opts.EnableCollation = b));
Expand Down Expand Up @@ -633,6 +637,7 @@ int Run (string[] args) {
enable_fs = opts.EnableFS;
enable_threads = opts.EnableThreads;
enable_dynamic_runtime = opts.EnableDynamicRuntime;
enable_simd = opts.Simd;
invariant_globalization = opts.InvariantGlobalization;

// Dedup is disabled by default https://github.com/dotnet/runtime/issues/48814
Expand Down Expand Up @@ -739,6 +744,11 @@ int Run (string[] args) {
return 1;
}

if (enable_simd && !is_netcore) {
Console.Error.WriteLine ("--simd is only supported with netcore.");
return 1;
}

//are we working from the tree?
if (sdkdir != null) {
framework_prefix = Path.Combine (tool_prefix, "framework"); //all framework assemblies are currently side built to packager.exe
Expand Down Expand Up @@ -1087,7 +1097,16 @@ int Run (string[] args) {
if (is_netcore)
{
runtime_libs += $"$runtime_libdir/wasm-bundled-timezones.a ";
runtime_libs += $"$runtime_libdir/libmono-wasm-simd.a ";

if (enable_simd)
{
runtime_libs += $"$runtime_libdir/libmono-wasm-simd.a ";
}
else
{
runtime_libs += $"$runtime_libdir/libmono-wasm-nosimd.a ";
}

runtime_libs += $"$runtime_libdir/libSystem.Native.a ";
runtime_libs += $"$runtime_libdir/libSystem.IO.Compression.Native.a ";
runtime_libs += $"$runtime_libdir/libSystem.Globalization.Native.a ";
Expand Down Expand Up @@ -1346,10 +1365,11 @@ int Run (string[] args) {
? "; if ($$LastExitCode -ne 0) { exit 1; }"
: "";

aot_args += "mattr=simd,";
emcc_flags += "-msimd128 ";
emcc_flags += "-DCONFIGURATION_COMPILE_OPTIONS=\"-msimd128\" -DCONFIGURATION_INTERPSIMDTABLES_LIB=\"simd\" ";

if (enable_simd) {
aot_args += "mattr=simd,";
emcc_flags += "-msimd128 ";
emcc_flags += "-DCONFIGURATION_COMPILE_OPTIONS=\"-msimd128\" -DCONFIGURATION_INTERPSIMDTABLES_LIB=\"simd\" ";
}
if (is_netcore) {
emcc_flags += $"-DGEN_PINVOKE -I{src_prefix} ";

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Wasm.StaticLinking.Aot.UITests/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Uno.Wasm.StaticLinking.Aot.UITests/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const path = require("path");
console.log(`Results: ${value}`);
}

const expected = "InterpreterAndAOT;42;42.30;42.7;e42;True;true;True;1.3;1.4;3.1;0;42;requireJs:true;jsInterop:Invoked;gl:true;functionsExportsAvailable:true;sat:True;";
const expected = "InterpreterAndAOT;42;42.30;42.7;e42;True;true;True;1.2;1.4;3.1;0;42;requireJs:true;jsInterop:Invoked;gl:true;functionsExportsAvailable:true;sat:True;";

if (value !== expected) {
console.log(`Invalid results got ${value}, expected ${expected}`);
Expand Down
Binary file not shown.
6 changes: 5 additions & 1 deletion src/Uno.Wasm.StaticLinking.Shared/side_module/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ emcc main.cpp -s MAIN_MODULE=1 -o hello.html -s WASM=1 -s EXPORT_ALL=1 --pre-js

buildSideModule(){
mkdir -p `dirname $2`
emcc $1 -std=c++17 -s LEGALIZE_JS_FFI=0 -r -o $2 -s WASM=1 -fwasm-exceptions -msimd128 -DCUSTOM_VERSION="\"$3\"" $5 -DCUSTOM_FUNCTION_NAME="$4_getCustomVersion"
emcc $1 -std=c++17 -s LEGALIZE_JS_FFI=0 -r -o $2 -s WASM=1 -fwasm-exceptions -msimd128 -s USE_LIBPNG=1 -DCUSTOM_VERSION="\"$3\"" $5 -DCUSTOM_FUNCTION_NAME="$4_getCustomVersion"
}

buildSideModule "mysideModule.cpp" "../native/side.bc/1.2/side.bc" "1.2" "side" ""
buildSideModule "mysideModule.cpp" "../native/side.bc/1.2/st,simd/side.bc" "1.2" "side" "-msimd128"

buildSideModule "version_test.cpp" "../native/side2.bc/1.3/side2.bc" "1.3" "side2" ""
buildSideModule "version_test.cpp" "../native/side2.bc/1.2/st,simd/side2.bc" "1.2" "side2" "-msimd128"

buildSideModule "version_test.cpp" "../native/side3.bc/1.3/side3.bc" "1.3" "side3" ""
buildSideModule "version_test.cpp" "../native/side3.bc/1.4/side3.bc" "1.4" "side3" ""
buildSideModule "version_test.cpp" "../native/side3.bc/1.4/st,simd/side3.bc" "1.4" "side3" "-msimd128"

buildSideModule "version_test.cpp" "../native/side4.bc/1.3/side4.bc" "1.3" "side4" ""
buildSideModule "version_test.cpp" "../native/side4.bc/2.0/side4.bc" "2.0" "side4" ""
buildSideModule "version_test.cpp" "../native/side4.bc/3.1/side4.bc" "3.1" "side4" ""
buildSideModule "version_test.cpp" "../native/side4.bc/3.1/st,simd/side4.bc" "3.1" "side4" "-msimd128"
buildSideModule "version_test.cpp" "../native/side4.bc/5.0/side4.bc" "5.0" "side4" ""
Loading

0 comments on commit 6fef00a

Please sign in to comment.