Skip to content
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

Wasm dedup #49538

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/mono/mono/mini/aot-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -4337,9 +4337,12 @@ mono_aot_can_dedup (MonoMethod *method)
info->subtype == WRAPPER_SUBTYPE_INTERP_LMF ||
info->subtype == WRAPPER_SUBTYPE_AOT_INIT)
return FALSE;
#if 0
// See is_linkonce_method () in mini-llvm.c
if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG || info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)
/* Handled using linkonce */
return FALSE;
#endif
return TRUE;
}
default:
Expand Down
12 changes: 12 additions & 0 deletions src/mono/mono/mini/llvm-runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@

#include <glib.h>

#ifdef HOST_WASM
#include <emscripten.h>
#endif

extern "C" {

void
mono_llvm_cpp_throw_exception (void)
{
gint32 *ex = NULL;

#ifdef HOST_WASM
EM_ASM(
var err = new Error();
console.log ("Throw stacktrace: \n");
console.log (err.stack);
);
#endif

/* The generated code catches an int32* */
throw ex;
}
Expand Down
3 changes: 3 additions & 0 deletions src/mono/mono/mini/mini-llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9521,11 +9521,14 @@ is_linkonce_method (MonoMethod *method)
* FIXME: Fails System.Core tests
* -> amodule->sorted_methods contains duplicates, screwing up jit tables.
*/
// FIXME: This works, but the aot data for the methods is still kept, so size still increases
#if 0
if (method->wrapper_type == MONO_WRAPPER_OTHER) {
WrapperInfo *info = mono_marshal_get_wrapper_info (method);
if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG || info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)
return TRUE;
}
#endif
#endif
return FALSE;
}
Expand Down
26 changes: 25 additions & 1 deletion src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- $(WasmDebugLevel)
- $(WasmNativeDebugSymbols) - Build with native debug symbols, useful only with `$(RunAOTCompilation)`, or `$(WasmBuildNative)`
Defaults to true.
- $(WasmDedup) - Whenever to dedup generic instances when using AOT. Defaults to true.

- $(WasmProfilers) - Profilers to use
- $(AOTMode) - Defaults to `AotInterp`
Expand Down Expand Up @@ -64,6 +65,7 @@

<PropertyGroup>
<WasmStripAOTAssemblies>false</WasmStripAOTAssemblies>
<WasmDedup Condition="'$(WasmDedup)' == ''">$(RunAOTCompilation)</WasmDedup>

<!--<WasmStripAOTAssemblies Condition="'$(AOTMode)' == 'AotInterp'">false</WasmStripAOTAssemblies>-->
<!--<WasmStripAOTAssemblies Condition="'$(WasmStripAOTAssemblies)' == ''">$(RunAOTCompilation)</WasmStripAOTAssemblies>-->
Expand Down Expand Up @@ -106,6 +108,27 @@
Text="Builing in AOTMode=LLVMonly, but found some assemblies marked as _InternalForceInterpret: @(_AOT_InternalForceInterpretAssemblies)" />

<Message Text="AOT'ing @(_AotInputAssemblies->Count()) assemblies" Importance="High" />

<!-- Dedup -->
<PropertyGroup Condition="'$(WasmDedup)' == 'true'">
<_WasmDedupAssembly>$(_WasmIntermediateOutputPath)\aot-instances.dll</_WasmDedupAssembly>
</PropertyGroup>
<WriteLinesToFile Condition="'$(WasmDedup)' == 'true'" File="$(_WasmIntermediateOutputPath)/aot-instances.cs" Overwrite="true" Lines="" />
<Csc
Condition="'$(WasmDedup)' == 'true'"
Sources="$(_WasmIntermediateOutputPath)\aot-instances.cs"
OutputAssembly="$(_WasmDedupAssembly)"
TargetType="library"
References="@(ReferencePath)"
ToolExe="$(CscToolExe)"
ToolPath="$(CscToolPath)" />
<ItemGroup Condition="'$(WasmDedup)' == 'true'">
<_AotInputAssemblies Include="$(_WasmDedupAssembly)">
<AotArguments>@(MonoAOTCompilerDefaultAotArguments, ';')</AotArguments>
<ProcessArguments>@(MonoAOTCompilerDefaultProcessArguments, ';')</ProcessArguments>
</_AotInputAssemblies>
</ItemGroup>

<MonoAOTCompiler
CompilerBinaryPath="$(MonoAotCrossCompilerPath)"
OutputDir="$(_WasmIntermediateOutputPath)"
Expand All @@ -119,6 +142,7 @@
AotModulesTablePath="$(_WasmIntermediateOutputPath)driver-gen.c"
UseLLVM="true"
DisableParallelAot="true"
DedupAssembly="$(_WasmDedupAssembly)"
LLVMPath="$(EMSDK_PATH)\upstream\bin">
<Output TaskParameter="CompiledAssemblies" ItemName="_WasmAssembliesInternal" />
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
Expand Down Expand Up @@ -407,7 +431,7 @@ EMSCRIPTEN_KEEPALIVE void mono_wasm_load_profiler_aot (const char *desc) { mono_
</ItemGroup>

<!-- $(WasmResolveAssembliesBeforeBuild) can add more assemblies, so no point checking the count in that case -->
<Error Condition="'$(WasmResolveAssembliesBeforeBuild)' != 'true' and @(WasmAssembliesFinal->Distinct()->Count()) != @(WasmAssembliesToBundle->Distinct()->Count())"
<Error Condition="'$(WasmResolveAssembliesBeforeBuild)' != 'true' and @(WasmAssembliesFinal->Distinct()->Count()) != @(WasmAssembliesToBundle->Distinct()->Count()) and '$(WasmDedup)' != 'true'"
Text="Bug: The wasm build started with @(WasmAssembliesToBundle->Count()) assemblies, but completed with @(WasmAssembliesFinal->Count()).
WasmAssembliesToBundle: @(WasmAssembliesToBundle)
WasmAssembliesFinal: @(WasmAssembliesFinal)" />
Expand Down
48 changes: 46 additions & 2 deletions src/tasks/AotCompilerTask/MonoAOTCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public class MonoAOTCompiler : Microsoft.Build.Utilities.Task
/// </summary>
public string? MsymPath { get; set; }

/// <summary>
/// The assembly whose AOT image will contained dedup-ed generic instances
/// </summary>
public string? DedupAssembly { get; set; }

[Output]
public string[]? FileWrites { get; private set; }

Expand Down Expand Up @@ -242,6 +247,7 @@ private bool PrecompileLibrary(ITaskItem assemblyItem, string? monoPaths)
var aotAssembly = new TaskItem(assembly);
var aotArgs = new List<string>();
var processArgs = new List<string>();
bool isDedup = assembly == DedupAssembly;

var a = assemblyItem.GetMetadata("AotArguments");
if (a != null)
Expand Down Expand Up @@ -274,6 +280,15 @@ private bool PrecompileLibrary(ITaskItem assemblyItem, string? monoPaths)

string assemblyFilename = Path.GetFileName(assembly);

if (isDedup)
{
aotArgs.Add($"dedup-include={assemblyFilename}");
}
else if (!string.IsNullOrEmpty (DedupAssembly))
{
aotArgs.Add("dedup-skip");
}

// compute output mode and file names
if (parsedAotMode == MonoAotMode.LLVMOnly || parsedAotMode == MonoAotMode.AotInterp)
{
Expand Down Expand Up @@ -350,14 +365,43 @@ private bool PrecompileLibrary(ITaskItem assemblyItem, string? monoPaths)
// values, which wont work.
processArgs.Add($"\"--aot={string.Join(",", aotArgs)}\"");

processArgs.Add($"\"{assemblyFilename}\"");
string paths = "";
if (isDedup)
{
StringBuilder sb = new StringBuilder();
HashSet<string> allPaths = new HashSet<string>();
foreach (var aItem in Assemblies)
{
string filename = aItem.ItemSpec;
processArgs.Add(filename);
string dir = Path.GetDirectoryName(filename)!;
if (!allPaths.Contains(dir))
{
allPaths.Add(dir);
if (sb.Length > 0)
sb.Append(Path.PathSeparator);
sb.Append(dir);
}
}
if (sb.Length > 0)
sb.Append(Path.PathSeparator);
sb.Append(monoPaths);
paths = sb.ToString();
}
else
{
paths = $"{assemblyDir}{Path.PathSeparator}{monoPaths}";
processArgs.Add(assemblyFilename);
}

var envVariables = new Dictionary<string, string>
{
{"MONO_PATH", $"{assemblyDir}{Path.PathSeparator}{monoPaths}"},
{"MONO_PATH", paths},
{"MONO_ENV_OPTIONS", string.Empty} // we do not want options to be provided out of band to the cross compilers
};

Utils.LogInfo ($"MONO_PATH={paths}");

try
{
// run the AOT compiler
Expand Down