Skip to content

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

Merged
fanyang-mono merged 27 commits into
dotnet:mainfrom
fanyang-mono:il_trim_tool
Jun 16, 2023
Merged

[Mono] Add the capability of trimming IL code of individual methods#86722
fanyang-mono merged 27 commits into
dotnet:mainfrom
fanyang-mono:il_trim_tool

Conversation

@fanyang-mono

@fanyang-monofanyang-mono commented May 24, 2023

Copy link
Copy Markdown
Member

Contributes to #44855

@jonathanpeppers After this is merged. It should be adoptable by Android. I have updated the HelloWorld sample as an example to showcase how to use this feature.

Usage of this feature:

  1. Enable AOT to log compiled methods. This could be achieved by setting CollectCompiledMethods and CompiledMethodsOutputDirectory for MonoAOTCompiler
  2. Run ILStrip after MonoAOTCompiler
  3. Overwrite the assemblies with the trimmed ones.

For more details, please refer to the HelloWorld sample app change included in this PR.

Comment on lines +47 to +49
int allowedParallelism = DisableParallelStripping ? 1 : Math.Min(Assemblies.Length, Environment.ProcessorCount);
if (BuildEngine is IBuildEngine9 be9)
allowedParallelism = be9.RequestCores(allowedParallelism);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
intallowedParallelism=DisableParallelStripping?1:Math.Min(Assemblies.Length,Environment.ProcessorCount);
if(BuildEngineisIBuildEngine9be9)
allowedParallelism=be9.RequestCores(allowedParallelism);
intallowedParallelism=DisableParallelStripping?1:BuildEngine9.RequestCores(Math.Min(Assemblies.Length,Environment.ProcessorCount));

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit an error when applying your change

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Comment on lines +77 to +79
int allowedParallelism = DisableParallelStripping ? 1 : Math.Min(MethodTokenFiles.Length, Environment.ProcessorCount);
if (BuildEngine is IBuildEngine9 be9)
allowedParallelism = be9.RequestCores(allowedParallelism);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
intallowedParallelism=DisableParallelStripping?1:Math.Min(MethodTokenFiles.Length,Environment.ProcessorCount);
if(BuildEngineisIBuildEngine9be9)
allowedParallelism=be9.RequestCores(allowedParallelism);
intallowedParallelism=DisableParallelStripping?1:BuildEngine9.RequestCores(Math.Min(Assemblies.Length,Environment.ProcessorCount));

@fanyang-monofanyang-mono changed the title [WIP][Mono] Add the capability of trimming IL code of individual methods[Mono] Add the capability of trimming IL code of individual methodsMay 26, 2023
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/mono/mono/mini/aot-compiler.c Outdated
Comment threadsrc/mono/sample/HelloWorld/HelloWorld.csproj Outdated
Comment threadsrc/mono/sample/HelloWorld/HelloWorld.csproj
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
@ivanpovazan

Copy link
Copy Markdown
Member

It would be nice to include a specification/documentation of how this feature is used/enabled (possibly with an example of MethodTokenFile). Might be enough to just include it in the PR description.

Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/mono/mono/mini/aot-compiler.c Outdated
fanyang-monoand others added 7 commits June 5, 2023 17:55
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Co-authored-by: Ankit Jain <radical@gmail.com>
Comment threadsrc/tasks/AotCompilerTask/MonoAOTCompiler.cs Outdated
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs Outdated
Comment threadsrc/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs
@ghostghost added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jun 12, 2023
Co-authored-by: Ankit Jain <radical@gmail.com>
@ghostghost removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Jun 13, 2023

@lambdageeklambdageek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@radicalradical left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 👍

@fanyang-mono

Copy link
Copy Markdown
MemberAuthor

CI failures are not related to this PR.

@fanyang-mono
fanyang-mono merged commit 9fc19a1 into dotnet:mainJun 16, 2023
jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this pull request Jul 6, 2023
Context: dotnet/runtime#86722
This adds an `<ILStrip/>` step after the `<MonoAOTCompiler/>` task.
This trims away IL of AOT-compiled methods. This is WIP.
Builds work currently, but the app crashes at runtime with:
07-06 16:57:07.413 8865 8865 E companyname.foo: * Assertion at /__w/1/s/src/mono/mono/mini/mini-trampolines.c:1416, condition `invoke' not met
@ghostghost locked as resolved and limited conversation to collaborators Aug 6, 2023
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants

@fanyang-mono@ivanpovazan@radical@vargaz@lambdageek@jonathanpeppers@akoeplinger@teo-tsirpanis