Skip to content

Commit fa44ed2

Browse files
lewingCopilot
andcommitted
[wasi] Enable PackedSimd intrinsics and ILLink substitutions
The mono interpreter only emitted wasm packed-simd intrinsic opcodes when HOST_BROWSER was defined. On WASI the interpreter returned FALSE for every PackedSimd method, so calls fell through to the C# method body which recursively calls itself, producing a stack overflow (observed with ConvertNarrowingSaturateUnsigned and other PackedSimd intrinsics). Additionally, WasiApp.targets did not inject the ILLink substitutions that fold PackedSimd.IsSupported to a constant, so even with WasmEnableSIMD=false the simd code paths survived trimming and the call sites still recursed. Changes: * src/mono/mono/mini/interp/interp-simd.h, interp-simd.c, interp-nosimd.c, transform-simd.c: extend the HOST_BROWSER gates around the wasm packed-simd intrinsic tables and emission to also cover HOST_WASI. The mono-wasm-simd static library is already built with -msimd128 for both hosts in cmake. * src/mono/wasi/build/WasiApp.targets: add _ExtraTrimmerArgs for the WasmIntrinsics.xml / NoWasmIntrinsics.xml substitutions, mirroring BrowserWasmApp.targets, and uncomment the -msimd128 clang flag when WasmEnableSIMD=true. * src/mono/nuget/Microsoft.NET.Runtime.WebAssembly.Wasi.Sdk/ Microsoft.NET.Runtime.WebAssembly.Wasi.Sdk.pkgproj: package the two ILLink.Substitutions.*.xml files next to WasiApp.targets in the WASI SDK so $(MSBuildThisFileDirectory) resolves at app build time. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 263d5b9 commit fa44ed2

6 files changed

Lines changed: 26 additions & 14 deletions

File tree

src/mono/mono/mini/interp/interp-nosimd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
gboolean interp_simd_enabled = FALSE;
88

9-
#ifdef HOST_BROWSER
9+
#if HOST_BROWSER || HOST_WASI
1010

1111
int interp_simd_p_p_wasm_opcode_table [] = {
1212
};
@@ -17,7 +17,7 @@ int interp_simd_p_pp_wasm_opcode_table [] = {
1717
int interp_simd_p_ppp_wasm_opcode_table [] = {
1818
};
1919

20-
#endif // HOST_BROWSER
20+
#endif // HOST_BROWSER || HOST_WASI
2121

2222
PP_SIMD_Method interp_simd_p_p_table [] = {
2323
};

src/mono/mono/mini/interp/interp-simd.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "interp-internals.h"
33
#include "interp-simd.h"
44

5-
#if HOST_BROWSER
5+
#if HOST_BROWSER || HOST_WASI
66
#include <wasm_simd128.h>
77
#endif
88

@@ -611,6 +611,12 @@ interp_v128_i8_shuffle (gpointer res, gpointer v1, gpointer v2)
611611
// https://github.com/llvm/llvm-project/blob/main/clang/lib/Headers/wasm_simd128.h
612612
// In this context V means Vector128 and P means void* pointer.
613613
#ifdef HOST_BROWSER
614+
#define HOST_WASM_SIMD 1
615+
#elif defined(HOST_WASI)
616+
#define HOST_WASM_SIMD 1
617+
#endif
618+
619+
#if HOST_WASM_SIMD
614620

615621
static v128_t
616622
_interp_wasm_simd_assert_not_reached (v128_t lhs, v128_t rhs) {
@@ -931,7 +937,7 @@ int interp_simd_p_ppp_wasm_opcode_table [] = {
931937
#undef INTERP_SIMD_INTRINSIC_P_PPP
932938
#define INTERP_SIMD_INTRINSIC_P_PPP(a,b,c)
933939

934-
#endif // HOST_BROWSER
940+
#endif // HOST_WASM_SIMD
935941

936942
#undef INTERP_SIMD_INTRINSIC_P_P
937943
#define INTERP_SIMD_INTRINSIC_P_P(a,b,c) b,

src/mono/mono/mini/interp/interp-simd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern PP_SIMD_Method interp_simd_p_p_table [];
1313
extern PPP_SIMD_Method interp_simd_p_pp_table [];
1414
extern PPPP_SIMD_Method interp_simd_p_ppp_table [];
1515

16-
#if HOST_BROWSER
16+
#if HOST_BROWSER || HOST_WASI
1717
extern int interp_simd_p_p_wasm_opcode_table [];
1818
extern int interp_simd_p_pp_wasm_opcode_table [];
1919
extern int interp_simd_p_ppp_wasm_opcode_table [];

src/mono/mono/mini/interp/transform-simd.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ emit_sri_vector128 (TransformData *td, MonoMethod *cmethod, MonoMethodSignature
517517
if (csignature->hasthis)
518518
return FALSE;
519519

520-
#ifdef HOST_BROWSER
520+
#if defined(HOST_BROWSER) || defined(HOST_WASI)
521521
if (emit_sri_packedsimd (td, cmethod, csignature))
522522
return TRUE;
523523
#endif
@@ -731,7 +731,7 @@ emit_sri_vector128 (TransformData *td, MonoMethod *cmethod, MonoMethodSignature
731731
static gboolean
732732
emit_sri_vector128_t (TransformData *td, MonoMethod *cmethod, MonoMethodSignature *csignature)
733733
{
734-
#ifdef HOST_BROWSER
734+
#if defined(HOST_BROWSER) || defined(HOST_WASI)
735735
if (emit_sri_packedsimd (td, cmethod, csignature))
736736
return TRUE;
737737
#endif
@@ -775,7 +775,7 @@ emit_sri_vector128_t (TransformData *td, MonoMethod *cmethod, MonoMethodSignatur
775775
static gboolean
776776
emit_sn_vector_t (TransformData *td, MonoMethod *cmethod, MonoMethodSignature *csignature, gboolean newobj)
777777
{
778-
#ifdef HOST_BROWSER
778+
#if defined(HOST_BROWSER) || defined(HOST_WASI)
779779
if (emit_sri_packedsimd (td, cmethod, csignature))
780780
return TRUE;
781781
#endif
@@ -876,7 +876,7 @@ emit_sn_vector4 (TransformData *td, MonoMethod *cmethod, MonoMethodSignature *cs
876876
return TRUE;
877877
}
878878

879-
#if HOST_BROWSER
879+
#if defined(HOST_BROWSER) || defined(HOST_WASI)
880880

881881
#define PSIMD_ARGTYPE_I1 MONO_TYPE_I1
882882
#define PSIMD_ARGTYPE_I2 MONO_TYPE_I2
@@ -1122,7 +1122,7 @@ emit_sri_packedsimd (TransformData *td, MonoMethod *cmethod, MonoMethodSignature
11221122
// We don't want to emit the IsSupported or IsHardwareAccelerated methods for Vector* here
11231123
return FALSE;
11241124
}
1125-
#if HOST_BROWSER
1125+
#if defined(HOST_BROWSER) || defined(HOST_WASI)
11261126
interp_add_ins (td, MINT_LDC_I4_1);
11271127
#else
11281128
interp_add_ins (td, MINT_LDC_I4_0);
@@ -1133,7 +1133,7 @@ emit_sri_packedsimd (TransformData *td, MonoMethod *cmethod, MonoMethodSignature
11331133
if (!get_common_simd_info (vector_klass, csignature, &atype, &vector_size, &arg_size, &scalar_arg))
11341134
return FALSE;
11351135

1136-
#if HOST_BROWSER
1136+
#if defined(HOST_BROWSER) || defined(HOST_WASI)
11371137
if (!is_packedsimd) {
11381138
// transform the method name from the Vector(128|) name to the packed simd name
11391139
// FIXME: This is a hack, but it works for now.
@@ -1311,9 +1311,9 @@ emit_sri_packedsimd (TransformData *td, MonoMethod *cmethod, MonoMethodSignature
13111311

13121312
interp_add_ins (td, simd_opcode);
13131313
td->last_ins->data [0] = simd_intrins;
1314-
#else // HOST_BROWSER
1314+
#else // defined(HOST_BROWSER) || defined(HOST_WASI)
13151315
return FALSE;
1316-
#endif // HOST_BROWSER
1316+
#endif // defined(HOST_BROWSER) || defined(HOST_WASI)
13171317

13181318
opcode_added:
13191319
emit_common_simd_epilogue (td, vector_klass, csignature, vector_size, TRUE);

src/mono/nuget/Microsoft.NET.Runtime.WebAssembly.Wasi.Sdk/Microsoft.NET.Runtime.WebAssembly.Wasi.Sdk.pkgproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
<PackageFile Include="$(WasiProjectRoot)build\WasiApp.props" TargetPath="Sdk" />
1616
<PackageFile Include="$(WasiProjectRoot)build\WasiApp.targets" TargetPath="Sdk" />
1717

18+
<PackageFile Include="$(BrowserProjectRoot)build\ILLink.Substitutions.WasmIntrinsics.xml" TargetPath="Sdk" />
19+
<PackageFile Include="$(BrowserProjectRoot)build\ILLink.Substitutions.NoWasmIntrinsics.xml" TargetPath="Sdk" />
20+
1821
<PackageFile Include="$(WasmProjectRoot)build\WasmApp.Common.props" TargetPath="Sdk" />
1922
<PackageFile Include="$(WasmProjectRoot)build\WasmApp.Common.targets" TargetPath="Sdk" />
2023

src/mono/wasi/build/WasiApp.targets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
<WasmEmitSymbolMap Condition="'$(WasmEmitSymbolMap)' == '' and '$(RunAOTCompilation)' != 'true'">false</WasmEmitSymbolMap>
4747
<TrimMode Condition="'$(TrimMode)' == ''">full</TrimMode>
4848

49+
<_ExtraTrimmerArgs Condition="'$(WasmEnableSIMD)' != 'false'">$(_ExtraTrimmerArgs) --substitutions "$(MSBuildThisFileDirectory)ILLink.Substitutions.WasmIntrinsics.xml"</_ExtraTrimmerArgs>
50+
<_ExtraTrimmerArgs Condition="'$(WasmEnableSIMD)' == 'false'">$(_ExtraTrimmerArgs) --substitutions "$(MSBuildThisFileDirectory)ILLink.Substitutions.NoWasmIntrinsics.xml"</_ExtraTrimmerArgs>
51+
4952
<WasmRunWasmOpt Condition="'$(WasmRunWasmOpt)' == ''">false</WasmRunWasmOpt>
5053

5154
<!--<WasiBundleAssemblies Condition="'$(WasiBundleAssemblies)' == ''">true</WasiBundleAssemblies>-->
@@ -245,7 +248,7 @@
245248
<_WasiClangCommonFlags Include="-v" Condition="'$(WasiClangVerbose)' != 'false'" />
246249
<!--<_WasiClangCommonFlags Include="-s DISABLE_EXCEPTION_CATCHING=0" Condition="'$(WasmEnableExceptionHandling)' == 'false'" />-->
247250
<!--<_WasiClangCommonFlags Include="-fwasm-exceptions" Condition="'$(WasmEnableExceptionHandling)' == 'true'" />-->
248-
<!--<_WasiClangCommonFlags Include="-msimd128" Condition="'$(WasmEnableSIMD)' == 'true'" />-->
251+
<_WasiClangCommonFlags Include="-msimd128" Condition="'$(WasmEnableSIMD)' == 'true'" />
249252

250253
<_WasmCommonCFlags Include="-DGEN_PINVOKE=1" />
251254
<_WasmCommonCFlags Condition="'$(_WasmShouldAOT)' == 'true'" Include="-DENABLE_AOT=1" />

0 commit comments

Comments
 (0)