[release/8.0-rc1] [wasm] Do not build mono libs with `-msimd128` (#90750)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fri, 18 Aug 2023 19:50:30 +0000 (12:50 -0700)
committerGitHub <noreply@github.com>
Fri, 18 Aug 2023 19:50:30 +0000 (12:50 -0700)
* [wasm] Do not build mono libs with `-msimd128`

Make it optional, build only minimal set of code witch required
`-msimd128` to separate library. Also provide "stub" nosimd
version of this library.

Choose the appropriate library during linking.

* Fix build

* Fix build of non-wasm platforms

* Add simd options for wasi

* Fix wasi build

---------

Co-authored-by: Radek Doulik <radek.doulik@gmail.com>
Co-authored-by: Larry Ewing <lewing@microsoft.com>
14 files changed:
src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props
src/mono/CMakeLists.txt
src/mono/mono.proj
src/mono/mono/mini/CMakeLists.txt
src/mono/mono/mini/interp/interp-nosimd.c [new file with mode: 0644]
src/mono/mono/mini/interp/interp-simd.c
src/mono/mono/mini/interp/interp-simd.h
src/mono/mono/mini/interp/transform-simd.c
src/mono/wasi/build/WasiApp.Native.targets
src/mono/wasi/runtime/CMakeLists.txt
src/mono/wasi/wasi.proj
src/mono/wasm/build/WasmApp.Native.targets
src/mono/wasm/runtime/CMakeLists.txt
src/mono/wasm/wasm.proj

index 13cf1fc..5a96f57 100644 (file)
     <PlatformManifestFileEntry Include="libmono-profiler-browser.a" IsNative="true" />
     <PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
     <PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
+    <PlatformManifestFileEntry Include="libmono-wasm-simd.a" IsNative="true" />
+    <PlatformManifestFileEntry Include="libmono-wasm-nosimd.a" IsNative="true" />
     <PlatformManifestFileEntry Include="wasm-bundled-timezones.a" IsNative="true" />
     <PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
     <PlatformManifestFileEntry Include="dotnet.js.map" IsNative="true" />
index 66e92d8..a57cc52 100644 (file)
@@ -242,14 +242,6 @@ elseif(CLR_CMAKE_HOST_OS STREQUAL "emscripten")
   add_compile_options(-Wno-strict-prototypes)
   add_compile_options(-Wno-unused-but-set-variable)
   add_compile_options(-Wno-single-bit-bitfield-constant-conversion)
-  # Allow using WASM simd intrinsics in the interpreter
-  add_compile_options(-msimd128)
-  # Disable autovectorization (it is automatically turned on by msimd128)
-  add_compile_options(-disable-loop-vectorization)
-  add_compile_options(-disable-vectorization)
-  add_compile_options(-fno-vectorize)
-  add_compile_options(-fno-tree-vectorize)
-  add_compile_options(-fno-slp-vectorize)
   set(DISABLE_EXECUTABLES 1)
   # FIXME: Is there a cmake option for this ?
   set(DISABLE_SHARED_LIBS 1)
index cf99f32..6be683f 100644 (file)
       <_MonoRuntimeArtifacts Condition="'$(TargetsBrowser)' == 'true' and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-eh-wasm.a">
         <Destination>$(RuntimeBinDir)libmono-wasm-eh-wasm.a</Destination>
       </_MonoRuntimeArtifacts>
+      <_MonoRuntimeArtifacts Condition="('$(TargetsBrowser)' == 'true'  or '$(TargetsWasi)' == 'true') and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-simd.a">
+        <Destination>$(RuntimeBinDir)libmono-wasm-simd.a</Destination>
+      </_MonoRuntimeArtifacts>
+      <_MonoRuntimeArtifacts Condition="('$(TargetsBrowser)' == 'true'  or '$(TargetsWasi)' == 'true') and '$(BuildMonoAOTCrossCompilerOnly)' != 'true'" Include="$(MonoObjDir)out\lib\libmono-wasm-nosimd.a">
+        <Destination>$(RuntimeBinDir)libmono-wasm-nosimd.a</Destination>
+      </_MonoRuntimeArtifacts>
       <_MonoICorDebugArtifacts Condition="'$(MonoMsCorDbi)' == 'true'" Include="$(MonoObjDir)out\lib\$(LibPrefix)mscordbi$(LibSuffix)">
         <Destination>$(RuntimeBinDir)$(LibPrefix)mscordbi$(LibSuffix)</Destination>
       </_MonoICorDebugArtifacts>
index 884b43c..5d6ef3d 100644 (file)
@@ -288,7 +288,6 @@ set(interp_sources
     interp/interp.h
     interp/interp-internals.h
     interp/interp.c
-    interp/interp-simd.c
     interp/interp-intrins.h
     interp/interp-intrins.c
     interp/mintops.h
@@ -297,11 +296,17 @@ set(interp_sources
     interp/tiering.h
     interp/tiering.c
     interp/jiterpreter.c)
+set(interp_simd_sources
+    interp/interp-simd.c)
 set(interp_stub_sources
     interp-stubs.c)
 
 if(NOT DISABLE_INTERPRETER)
-set(mini_interp_sources ${interp_sources})
+  if(HOST_WASM)
+    set(mini_interp_sources ${interp_sources})
+  else()
+    set(mini_interp_sources ${interp_sources} ${interp_simd_sources})
+  endif()
 else()
 set(mini_interp_sources ${interp_stub_sources})
 endif()
@@ -504,6 +509,19 @@ if(HOST_BROWSER)
   install(TARGETS mono-wasm-eh-wasm LIBRARY)
 endif()
 
+if(HOST_BROWSER OR HOST_WASI)
+  add_library(mono-wasm-simd STATIC interp/interp-simd.c)
+  target_link_libraries (mono-wasm-simd PRIVATE monoapi eglib_api)
+  set_target_properties(mono-wasm-simd PROPERTIES COMPILE_FLAGS "-msimd128")
+  install(TARGETS mono-wasm-simd LIBRARY)
+endif()
+
+if(HOST_BROWSER OR HOST_WASI OR TARGET_WASM)
+  add_library(mono-wasm-nosimd STATIC interp/interp-nosimd.c)
+  target_link_libraries (mono-wasm-nosimd PRIVATE monoapi eglib_api)
+  install(TARGETS mono-wasm-nosimd LIBRARY)
+endif()
+
 find_package(Python3 COMPONENTS Interpreter)
 
 add_custom_command(
@@ -576,6 +594,9 @@ if(NOT DISABLE_EXECUTABLES)
     endif()
   endif()
   target_link_libraries(mono-sgen PRIVATE monoapi eglib_api monosgen-static)
+  if (HOST_WASM)
+    target_link_libraries(mono-sgen PRIVATE mono-wasm-nosimd)
+  endif()
   if(HAVE_ICU_SHIM)
     target_link_libraries(mono-sgen PRIVATE icu_shim_objects)
   endif()
diff --git a/src/mono/mono/mini/interp/interp-nosimd.c b/src/mono/mono/mini/interp/interp-nosimd.c
new file mode 100644 (file)
index 0000000..63bcf27
--- /dev/null
@@ -0,0 +1,31 @@
+
+#include "interp-internals.h"
+#include "interp-simd.h"
+
+#ifdef INTERP_ENABLE_SIMD
+
+gboolean interp_simd_enabled = FALSE;
+
+#ifdef HOST_BROWSER
+
+int interp_simd_p_p_wasm_opcode_table [] = {
+};
+
+int interp_simd_p_pp_wasm_opcode_table [] = {
+};
+
+int interp_simd_p_ppp_wasm_opcode_table [] = {
+};
+
+#endif // HOST_BROWSER
+
+PP_SIMD_Method interp_simd_p_p_table [] = {
+};
+
+PPP_SIMD_Method interp_simd_p_pp_table [] = {
+};
+
+PPPP_SIMD_Method interp_simd_p_ppp_table [] = {
+};
+
+#endif // INTERP_ENABLE_SIMD
index 65e60b4..5031c87 100644 (file)
@@ -8,6 +8,8 @@
 
 #ifdef INTERP_ENABLE_SIMD
 
+gboolean interp_simd_enabled = TRUE;
+
 typedef gint64 v128_i8 __attribute__ ((vector_size (SIZEOF_V128)));
 typedef guint64 v128_u8 __attribute__ ((vector_size (SIZEOF_V128)));
 typedef gint32 v128_i4 __attribute__ ((vector_size (SIZEOF_V128)));
index e3306a2..8e02226 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <glib.h>
 
+extern gboolean interp_simd_enabled;
+
 typedef void (*PP_SIMD_Method) (gpointer, gpointer);
 typedef void (*PPP_SIMD_Method) (gpointer, gpointer, gpointer);
 typedef void (*PPPP_SIMD_Method) (gpointer, gpointer, gpointer, gpointer);
index 255a2ab..7df7f92 100644 (file)
@@ -3,6 +3,7 @@
  */
 
 #include "config.h"
+#include "interp-simd.h"
 #include <glib.h>
 #include <mono/utils/bsearch.h>
 #include <mono/metadata/class-internals.h>
@@ -900,6 +901,9 @@ interp_emit_simd_intrinsics (TransformData *td, MonoMethod *cmethod, MonoMethodS
        if (image != mono_get_corlib ())
                return FALSE;
 
+       if (!interp_simd_enabled)
+               return FALSE;
+
        class_ns = m_class_get_name_space (cmethod->klass);
        class_name = m_class_get_name (cmethod->klass);
 
index 36c6658..ba9c085 100644 (file)
       <!--<_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a</_WasmEHLib>-->
       <!--<_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-js.a</_WasmEHLibToExclude>-->
       <_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a</_WasmEHLibToExclude>
+      <_WasmSIMDLib Condition="'$(WasmEnableSIMD)' == 'true'">libmono-wasm-simd.a</_WasmSIMDLib>
+      <_WasmSIMDLib Condition="'$(WasmEnableSIMD)' != 'true'">libmono-wasm-nosimd.a</_WasmSIMDLib>
+      <_WasmSIMDLibToExclude Condition="'$(WasmEnableSIMD)' != 'true'">libmono-wasm-simd.a</_WasmSIMDLibToExclude>
+      <_WasmSIMDLibToExclude Condition="'$(WasmEnableSIMD)' == 'true'">libmono-wasm-nosimd.a</_WasmSIMDLibToExclude>
     </PropertyGroup>
 
     <ItemGroup>
           Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)*.a"
           Exclude="@(_MonoRuntimeComponentDontLink->'$(MicrosoftNetCoreAppRuntimePackRidNativeDir)%(Identity)')" />
       <_WasmNativeFileForLinking Condition="'$(_WasmEHLib)' != ''" Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLib)" />
+      <_WasmNativeFileForLinking Condition="'$(_WasmSIMDLib)' != ''" Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmSIMDLib)" />
       <_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLibToExclude)" />
+      <_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmSIMDLibToExclude)" />
 
       <_WasmNativeFileForLinking Include="$(WasiSysRoot)\lib\wasm32-wasi\libc++.a" />
       <_WasmNativeFileForLinking Include="$(WasiSysRoot)\lib\wasm32-wasi\libc++abi.a" />
index ede21d7..912132b 100644 (file)
@@ -26,6 +26,7 @@ target_link_libraries(dotnet
     ${MONO_ARTIFACTS_DIR}/libmono-ee-interp.a
     ${MONO_ARTIFACTS_DIR}/libmonosgen-2.0.a
     ${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
+    ${MONO_ARTIFACTS_DIR}/libmono-wasm-${CONFIGURATION_INTERPSIMDTABLES_LIB}.a
     ${NATIVE_BIN_DIR}/wasm-bundled-timezones.a
     ${NATIVE_BIN_DIR}/libSystem.Native.a
     ${NATIVE_BIN_DIR}/libSystem.Globalization.Native.a
index 611d796..b942b6f 100644 (file)
       <CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DICU_LIB_DIR=&quot;$(ICULibDir.TrimEnd('\/'))&quot;</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DMONO_ARTIFACTS_DIR=&quot;$(MonoArtifactsPath.TrimEnd('\/'))&quot;</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DNATIVE_BIN_DIR=&quot;$(NativeBinDir.TrimEnd('\/'))&quot;</CMakeBuildRuntimeConfigureCmd>
+      <CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_COMPILE_OPTIONS=&quot;-msimd128&quot; -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;simd&quot;</CMakeBuildRuntimeConfigureCmd>
+      <CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' != 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;nosimd&quot;</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd Condition="'$(MonoWasmThreads)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=0</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd Condition="'$(OS)' == 'Windows_NT'">call &quot;$(RepositoryEngineeringDir)native\init-vs-env.cmd&quot; wasm &amp;&amp; $(CMakeBuildRuntimeConfigureCmd)</CMakeBuildRuntimeConfigureCmd>
 
index 52125a8..7060397 100644 (file)
       <_WasmEHLib Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-js.a</_WasmEHLib>
       <_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' == 'true'">libmono-wasm-eh-js.a</_WasmEHLibToExclude>
       <_WasmEHLibToExclude Condition="'$(WasmEnableExceptionHandling)' != 'true'">libmono-wasm-eh-wasm.a</_WasmEHLibToExclude>
+      <_WasmSIMDLib Condition="'$(WasmEnableSIMD)' == 'true'">libmono-wasm-simd.a</_WasmSIMDLib>
+      <_WasmSIMDLib Condition="'$(WasmEnableSIMD)' != 'true'">libmono-wasm-nosimd.a</_WasmSIMDLib>
+      <_WasmSIMDLibToExclude Condition="'$(WasmEnableSIMD)' != 'true'">libmono-wasm-simd.a</_WasmSIMDLibToExclude>
+      <_WasmSIMDLibToExclude Condition="'$(WasmEnableSIMD)' == 'true'">libmono-wasm-nosimd.a</_WasmSIMDLibToExclude>
       <_EmccExportedLibraryFunction>&quot;[@(EmccExportedLibraryFunction -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedLibraryFunction>
       <_EmccExportedRuntimeMethods>&quot;[@(EmccExportedRuntimeMethod -> '%27%(Identity)%27', ',')]&quot;</_EmccExportedRuntimeMethods>
       <_EmccExportedFunctions>@(EmccExportedFunction -> '%(Identity)',',')</_EmccExportedFunctions>
           Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)*.a"
           Exclude="@(_MonoRuntimeComponentDontLink->'$(MicrosoftNetCoreAppRuntimePackRidNativeDir)%(Identity)')" />
       <_WasmNativeFileForLinking Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLib)" />
+      <_WasmNativeFileForLinking Condition="'$(_WasmSIMDLib)' != ''" Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmSIMDLib)" />
       <_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmEHLibToExclude)" />
+      <_WasmNativeFileForLinking Remove="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)$(_WasmSIMDLibToExclude)" />
 
       <_WasmExtraJSFile Include="@(Content)" Condition="'%(Content.Extension)' == '.js'" />
 
index 6b8ef87..6d93908 100644 (file)
@@ -25,6 +25,7 @@ target_link_libraries(dotnet.native
     ${MONO_ARTIFACTS_DIR}/libmonosgen-2.0.a
     ${MONO_ARTIFACTS_DIR}/libmono-icall-table.a
     ${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
+    ${MONO_ARTIFACTS_DIR}/libmono-wasm-${CONFIGURATION_INTERPSIMDTABLES_LIB}.a
     ${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
     ${MONO_ARTIFACTS_DIR}/libmono-profiler-browser.a
     ${NATIVE_BIN_DIR}/wasm-bundled-timezones.a
index c46c92a..66bc509 100644 (file)
       <CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DICU_LIB_DIR=&quot;$(ICULibDir.TrimEnd('\/').Replace('\','/'))&quot;</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DMONO_ARTIFACTS_DIR=&quot;$(MonoArtifactsPath.TrimEnd('\/').Replace('\','/'))&quot;</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) -DNATIVE_BIN_DIR=&quot;$(NativeBinDir.TrimEnd('\/').Replace('\','/'))&quot;</CMakeBuildRuntimeConfigureCmd>
-      <CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_COMPILE_OPTIONS=&quot;-msimd128&quot;</CMakeBuildRuntimeConfigureCmd>
+      <CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_COMPILE_OPTIONS=&quot;-msimd128&quot; -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;simd&quot;</CMakeBuildRuntimeConfigureCmd>
+      <CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableSIMD)' != 'true'">$(CMakeBuildRuntimeConfigureCmd) -DCONFIGURATION_INTERPSIMDTABLES_LIB=&quot;nosimd&quot;</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd Condition="'$(MonoWasmThreads)' == 'true'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_THREADS=0</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd Condition="'$(WasmEnableLegacyJsInterop)' == 'false'">$(CMakeBuildRuntimeConfigureCmd) -DDISABLE_LEGACY_JS_INTEROP=1</CMakeBuildRuntimeConfigureCmd>
       <CMakeBuildRuntimeConfigureCmd>$(CMakeBuildRuntimeConfigureCmd) $(CMakeConfigurationEmsdkPath)</CMakeBuildRuntimeConfigureCmd>