Skip to content

Commit

Permalink
[Mono] Add the capability of trimming IL code of individual methods (#…
Browse files Browse the repository at this point in the history
…86722)

* Add the capability of trimming individual methods

* Fix build errors

* Remove printf's

* Add the option to use compiled-methods-outfile

* Avoid trimming shared methods when they are still in use

* Add parameter description

* Add the option to trim compiled methods

* Address review feedback

* Add metadata MethodTokenFile to CompiledAssemblies

* Add GUID checks and use metadata of assemblies

* Create smaller functions and use hex value

* Update src/tasks/AotCompilerTask/MonoAOTCompiler.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Move parameter validation code

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Add more error handling

* Provide a list of trimmed assemblies as output

* Update src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs

Co-authored-by: Ankit Jain <radical@gmail.com>

* Address coding style feedbacks

* Fix var anmes

* Delete trimmed assemblies after copy

---------

Co-authored-by: Ankit Jain <radical@gmail.com>
  • Loading branch information
fanyang-mono and radical authored Jun 16, 2023
1 parent 3afe6bd commit 9fc19a1
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 8 deletions.
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
24 changes: 24 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,26 @@
<Output TaskParameter="CompiledAssemblies" ItemName="BundleAssemblies" />
</MonoAOTCompiler>
</Target>

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

<Target Name="StripILCode" Condition="'$(StripILCode)' == 'true'" AfterTargets="AOTCompileApp">
<PropertyGroup>
<TrimIndividualMethods>true</TrimIndividualMethods>
</PropertyGroup>

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

<Copy
SourceFiles="@(TrimmedAssemblies->Metadata('TrimmedAssemblyFileName'))"
DestinationFiles="@(TrimmedAssemblies)"
OverwriteReadOnlyFiles="true"
/>
<Delete Files="@(TrimmedAssemblies->Metadata('TrimmedAssemblyFileName'))" />
</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
CompiledMethodsOutputDirectory?= #<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 @@ -437,6 +448,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 @@ -712,6 +734,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

0 comments on commit 9fc19a1

Please sign in to comment.