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

[Mono] Add the capability of trimming IL code of individual methods #86722

Merged
merged 27 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0c4bf19
Add the capability of trimming individual methods
fanyang-mono May 24, 2023
d817f30
Fix build errors
fanyang-mono May 24, 2023
634ef1e
Remove printf's
fanyang-mono May 24, 2023
b766c33
Add the option to use compiled-methods-outfile
fanyang-mono May 25, 2023
10b3b01
Avoid trimming shared methods when they are still in use
fanyang-mono May 26, 2023
2bc884e
Add parameter description
fanyang-mono May 26, 2023
b23d833
Add the option to trim compiled methods
fanyang-mono May 26, 2023
f6d99d7
Address review feedback
fanyang-mono May 31, 2023
69bf960
Add metadata MethodTokenFile to CompiledAssemblies
fanyang-mono May 31, 2023
1f0fcbb
Add GUID checks and use metadata of assemblies
fanyang-mono Jun 2, 2023
eaa939c
Create smaller functions and use hex value
fanyang-mono Jun 2, 2023
f517e79
Update src/tasks/AotCompilerTask/MonoAOTCompiler.cs
fanyang-mono Jun 5, 2023
ae66d52
Move parameter validation code
fanyang-mono Jun 5, 2023
30306e4
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
56a781a
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
78981c8
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
b519b4c
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
3322022
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
1a83902
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
f1b2753
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
328e9be
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 5, 2023
10226b7
Add more error handling
fanyang-mono Jun 5, 2023
fc088bd
Provide a list of trimmed assemblies as output
fanyang-mono Jun 12, 2023
3aad84a
Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
fanyang-mono Jun 13, 2023
91803d9
Address coding style feedbacks
fanyang-mono Jun 13, 2023
8cb9621
Fix var anmes
fanyang-mono Jun 13, 2023
ee2b37f
Delete trimmed assemblies after copy
fanyang-mono Jun 15, 2023
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
7 changes: 6 additions & 1 deletion src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -9828,8 +9828,9 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
mono_atomic_inc_i32 (&acfg->stats.ccount);

if (acfg->aot_opts.compiled_methods_outfile && acfg->compiled_methods_outfile != NULL) {
if (!mono_method_is_generic_impl (method) && method->token != 0)
if (!mono_method_is_generic_impl (method) && method->token != 0) {
fprintf (acfg->compiled_methods_outfile, "%x\n", method->token);
}
}
}

Expand Down Expand Up @@ -14832,6 +14833,10 @@ aot_assembly (MonoAssembly *ass, guint32 jit_opts, MonoAotOptions *aot_options)
acfg->compiled_methods_outfile = fopen (acfg->aot_opts.compiled_methods_outfile, "w+");
if (!acfg->compiled_methods_outfile)
aot_printerrf (acfg, "Unable to open compiled-methods-outfile specified file %s\n", acfg->aot_opts.compiled_methods_outfile);
else {
fprintf(acfg->compiled_methods_outfile, "%s\n", ass->image->filename);
fprintf(acfg->compiled_methods_outfile, "%s\n", ass->image->guid);
}
}

if (acfg->aot_opts.data_outfile) {
Expand Down
23 changes: 23 additions & 0 deletions src/mono/sample/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
LibraryFormat="$(_AotLibraryFormat)"
Assemblies="@(AotInputAssemblies)"
OutputDir="$(PublishDir)"
CollectCompiledMethods="$(StripILCode)"
CompiledMethodsOutputDirectory="$(CompiledMethodsOutputDirectory)"
IntermediateOutputPath="$(IntermediateOutputPath)"
UseAotDataFile="$(UseAotDataFile)"
CacheFilePath="$(IntermediateOutputPath)aot_compiler_cache.json"
Expand All @@ -35,4 +37,25 @@
<Output TaskParameter="CompiledAssemblies" ItemName="BundleAssemblies" />
</MonoAOTCompiler>
</Target>

<UsingTask TaskName="ILStrip"
AssemblyFile="$(MonoTargetsTasksAssemblyPath)" />

<Target Name="StripILCode" Condition="'$(StripILCode)' == 'true'" AfterTargets="AOTCompileApp">
fanyang-mono marked this conversation as resolved.
Show resolved Hide resolved
<PropertyGroup>
<TrimIndividualMethods>true</TrimIndividualMethods>
ivanpovazan marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>

<ILStrip
TrimIndividualMethods="$(TrimIndividualMethods)"
Assemblies="@(BundleAssemblies)">
<Output TaskParameter="TrimmedAssemblies" ItemName="TrimmedAssemblies" />
</ILStrip>

<Copy
SourceFiles="@(TrimmedAssemblies->Metadata('TrimmedAssemblyFileName'))"
DestinationFiles="@(TrimmedAssemblies)"
OverwriteReadOnlyFiles="true"
/>
</Target>
</Project>
4 changes: 4 additions & 0 deletions src/mono/sample/HelloWorld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ MONO_CONFIG?=Debug
MONO_ARCH?=$(shell . $(TOP)eng/native/init-os-and-arch.sh && echo $${arch})
TARGET_OS?=$(shell . $(TOP)eng/native/init-os-and-arch.sh && echo $${os})
AOT?=false
StripILCode?=false
MethodTokenFilePath?= #<path-to-a-writable-directory>

#NET_TRACE_PATH=<path-to-trace-of-sample>
#PGO_BINARY_PATH=<path-to-dotnet-pgo-executable>
Expand All @@ -18,6 +20,8 @@ publish:
-c $(MONO_CONFIG) \
-r $(TARGET_OS)-$(MONO_ARCH) \
/p:RunAOTCompilation=$(AOT) \
/p:StripILCode=$(StripILCode) \
/p:CompiledMethodsOutputDirectory=$(CompiledMethodsOutputDirectory) \
'/p:NetTracePath="$(NET_TRACE_PATH)"' \
'/p:PgoBinaryPath="$(PGO_BINARY_PATH)"' \
'/p:MibcProfilePath="$(MIBC_PROFILE_PATH)"'
Expand Down
39 changes: 39 additions & 0 deletions src/tasks/AotCompilerTask/MonoAOTCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class MonoAOTCompiler : Microsoft.Build.Utilities.Task
/// - LlvmObjectFile (if using LLVM)
/// - LlvmBitcodeFile (if using LLVM-only)
/// - ExportsFile (used in LibraryMode only)
/// - MethodTokenFile (when using CollectCompiledMethods=true)
/// </summary>
[Output]
public ITaskItem[]? CompiledAssemblies { get; set; }
Expand Down Expand Up @@ -152,6 +153,16 @@ public class MonoAOTCompiler : Microsoft.Build.Utilities.Task
/// </summary>
public bool UseDwarfDebug { get; set; }

/// <summary>
/// Instructs the AOT compiler to print the list of aot compiled methods
/// </summary>
public bool CollectCompiledMethods { get; set; }

/// <summary>
/// Directory to store the aot output when using switch compiled-methods-outfile
/// </summary>
public string? CompiledMethodsOutputDirectory { get; set; }

/// <summary>
/// File to use for profile-guided optimization, *only* the methods described in the file will be AOT compiled.
/// </summary>
Expand Down Expand Up @@ -436,6 +447,17 @@ private bool ProcessAndValidateArguments()
throw new LogAsErrorException($"Could not find {fullPath} to AOT");
}

if (CollectCompiledMethods)
{
if (string.IsNullOrEmpty(CompiledMethodsOutputDirectory))
throw new LogAsErrorException($"{nameof(CompiledMethodsOutputDirectory)} is empty. When {nameof(CollectCompiledMethods)} is set to true, the user needs to provide a directory for {nameof(CompiledMethodsOutputDirectory)}.");

if (!Directory.Exists(CompiledMethodsOutputDirectory))
{
Directory.CreateDirectory(CompiledMethodsOutputDirectory);
}
}

return !Log.HasLoggedErrors;
}

Expand Down Expand Up @@ -711,6 +733,23 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
aotArgs.Add("dedup-skip");
}

if (CollectCompiledMethods)
{
string assemblyName = assemblyFilename.Replace(".", "_");
string outputFileName = assemblyName + "_compiled_methods.txt";
string outputFilePath;
if (string.IsNullOrEmpty(CompiledMethodsOutputDirectory))
{
outputFilePath = outputFileName;
}
else
{
outputFilePath = Path.Combine(CompiledMethodsOutputDirectory, outputFileName);
}
aotArgs.Add($"compiled-methods-outfile={outputFilePath}");
aotAssembly.SetMetadata("MethodTokenFile", outputFilePath);
}

// compute output mode and file names
if (parsedAotMode == MonoAotMode.LLVMOnly || parsedAotMode == MonoAotMode.LLVMOnlyInterp)
{
Expand Down
Loading