Skip to content

Conversation

@ivanpovazan
Copy link
Member

@ivanpovazan ivanpovazan commented Jun 16, 2022

This change improves the AOTed code size by reducing the number of generated methods for generics.
It covers the following cases:

  • if a generic method or a method of a generic type has all type parameters constrained to value types - it will not generate REF shared methods
  • if a generic method or a method of a generic type has all type parameters constrained to reference types - it will not generate GSHAREDVT shared methods

After testing the impact of this change on the HelloWorld app built for iOSSimulator the code size is reduced by ~2%
App was build with:

AOT=true MONO_ARCH=arm64 MONO_CONFIG=Release make run-sim

and compared between branches main - Size A and ios-size-reduction - Size B giving the following results:

Files Size A Size B diff %
HelloiOS 19539824 19158672 -381152 -1,95%
System.Private.CoreLib.dll-llvm.o 6870572 6734416 -136156 -1,98%

Fixes #54850

@ivanpovazan
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ivanpovazan
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ivanpovazan
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ivanpovazan
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ivanpovazan
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ivanpovazan ivanpovazan changed the title WIP: Do not generate impossible instantiations for constrained generics in Mono AOT Do not generate impossible instantiations for constrained generics in Mono AOT Jun 19, 2022
@ivanpovazan
Copy link
Member Author

Failures are unrelated.

@ivanpovazan ivanpovazan marked this pull request as ready for review June 19, 2022 13:16

if (method->is_generic || mono_class_is_gtd (method->klass)) {
if (should_emit_extra_method_for_generics (method, TRUE)) {
/* Compile the ref shared version instead */
Copy link
Contributor

Choose a reason for hiding this comment

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

The aot runtime assumes that the index of 'normal' methods is the same as their token, which means that all normal methods need to be added. They can fail compilation later.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if I quite understand this:

  1. Are ref shared methods considered as normal?
  2. If they are, how could we make a workaround on this assumption?
  3. And finally, wouldn't this change already break some CI pipelines?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we add the method below using add_method_with_index () so it will work.

Copy link
Member Author

@ivanpovazan ivanpovazan Jun 20, 2022

Choose a reason for hiding this comment

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

Can this then be marked as resolved?

Copy link
Contributor

Choose a reason for hiding this comment

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

So what happens here is we add the original generic method, and the JIT fails to compile it, since you can't compile uninflated generic methods. So the ids/indexes will be correct, since we do add these methods, but this looks a bit ugly. You can see this in action by running with
--aot=print-skipped

@SamMonoRT
Copy link
Member

SamMonoRT commented Jun 22, 2022

Nice work - I see a 1.9% size reduction on the iOS Sample App with LLVM at : MonoScenarios - Power BI -- (please note - That might not be a publicly available report)

@ivanpovazan
Copy link
Member Author

cc @rolfbjarne

@ghost ghost locked as resolved and limited conversation to collaborators Jul 23, 2022
@ivanpovazan ivanpovazan deleted the ios-size-reduction branch June 9, 2023 12:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done & Blogged

Development

Successfully merging this pull request may close these issues.

[mono] iOS - Size reduction: don't emit impossible generic function instantiations

3 participants