Repository navigation
Description
Description
On Apple mobile platforms, non-user assemblies are compiled into an R2R composite image, while their IL bodies are not stripped. In general, R2R is used for optimization, and when tiered compilation is enabled, the original IL method bodies must be preserved.
On Apple mobile, R2R is the default compilation engine for non-user assemblies. This means the IL bodies are not required for execution. If they were stripped, this would yield an estimated ~40% size reduction per dll in a MAUI template app bundle.
To validate this, I implemented a simple IL stripping mechanism in crossgen2 by emitting a minimal method body (tiny header + ret):
public override ObjectData GetData(NodeFactory factory, bool relocsOnly = false)
{
if (relocsOnly)
{
return new ObjectData(
data: Array.Empty<byte>(),
relocs: Array.Empty<Relocation>(),
alignment: 1,
definedSymbols: new ISymbolDefinitionNode[] { this });
}
// Emit a minimal stub method body instead of the original IL.
if (factory.OptimizationFlags.StripILBodies && !ShouldPreserveILBody())
{
return new ObjectData(new byte[] { 0x06, 0x2A }, Array.Empty<Relocation>(), 4, new ISymbolDefinitionNode[] { this });
}
var rva = _method.MetadataReader.GetMethodDefinition(_method.Handle).RelativeVirtualAddress;
var reader = _method.Module.PEReader.GetSectionData(rva).GetReader();
int size = MethodBodyBlock.Create(reader).Size;
return new ObjectData(reader.ReadBytes(size), Array.Empty<Relocation>(), 4, new ISymbolDefinitionNode[] { this });
}However, it appears that MAUI initialization code relies on IL bodies. This leads to several failures, including:
- Failure in xamarin_bridge_call_runtime_initialize
- Failure in xamarin_find_assembly (the managed function find_assembly could not be loaded)
- Failures in xamarin_print_all_exceptions_wrapper
A workaround was to strip IL only for void methods from third-party NuGet packages (excluding SDK assemblies). With this approach, the size reduction reached only ~20% of the maximum.
To fully enable IL stripping safely, we need a reliable set of rules to identify all methods that are used during MAUI startup.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
[-]Strip IL bodies for methods that are R2R, estimated ~40% size reduction per dll in the app bundle[/-][+][clr-ios] Strip IL method body in R2R composite for Apple mobile[/+]jkotas commentedon Feb 25, 2026
Can this dependency be deleted? Why is MAUI initialization parsing IL? If nothing else, it is bad for perf.
dotnet-policy-service commentedon Feb 25, 2026
Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.
akoeplinger commentedon Feb 25, 2026
this sounds strange since we did IL stripping using the Mono runtime before (https://github.com/dotnet/runtime/blob/main/src/tasks/MonoTargetsTasks/ILStrip/ILStrip.cs)
akoeplinger commentedon Feb 25, 2026
but maybe the mono runtime handled reflection specially in that case since the assembly would have been AOTd.
kotlarmilos commentedon Feb 25, 2026
Corrected myself - not necessarily via reflection, but it requires the IL body. Note that this was a simple PoC; ILStrip is likely more advanced.
rolfbjarne commentedon Feb 25, 2026
From the second ips:
something goes wrong here:
https://github.com/dotnet/macios/blob/6eaeeee9122570ea4f81a08bce7a30a2f5d1dc92/runtime/coreclr-bridge.m#L549-L555
I'm guessing an exception is thrown when we try to call the managed method "Runtime.SafeInitialize", because from another crash report it's trying to print exception details:
which goes sideways because we can't call into managed code to get information about the exception (because of the first problem).
It would be interesting to get the frame names for libcoreclr frames in the second ips.
MichalStrehovsky commentedon Feb 25, 2026
This could be tied into PublishTrimmed being set. We'd already generate a build time warning if an app accesses MethodBase.GetMethodBody, so eliminating the body is a fair game (doesn't matter it's a different tool that does the removing).
If we were to do this without trimming being enabled, it would silently break things like Ben.Demystifier.
jkotas commentedon Feb 26, 2026
What did your implementation of this method looked like? My guess is that it has some subtle bug.
rolfbjarne commentedon Feb 26, 2026
This is strange, could it be some kind of IL validation kicking in?
kotlarmilos commentedon Feb 27, 2026
Here is the stack trace:
The null TypeHandle suggests the type couldn't be loaded. Execution goes through the interpreter, which might be a fallback from R2R. I will investigate further.
jkotas commentedon Feb 27, 2026
You may also want to look at whether Marshal.SizeOf needs to be used in iOS startup. Marshal.SizeOf is relatively expensive since it returns size of the marshalled view. If you are dealing with blittable types. there are much cheaper equivalents -
sizeof(T)if the type is known statically andRuntimeHelpers.SizeOfif the type is not known statically.kotlarmilos commentedon Mar 17, 2026
It seems the crash is caused by stripping IL bodies of generic methods that require instantiation at runtime. I've opened a draft PR #125647 with a --strip-il-bodies implementation that guards against this by only stripping methods that were successfully compiled by crossgen2 and preserving generic methods.
I will evaluate size savings once the startup path is optimized and we reduce the number of interpreted methods further. As more methods get compiled by crossgen2, we may be able to strip more aggressively.
[crossgen2] Add --strip-il-bodies option for composite R2R (dotnet#12…
[crossgen2] Add --strip-il-bodies option for composite R2R (#125647)