Commit 30295f8
authored
[CoreCLR] Initial support of R2R builds (#10007)
Context: https://learn.microsoft.com/en-us/dotnet/core/deploying/ready-to-run
Context: https://github.com/dotnet/runtime/blob/c83f92ad7aa7afa152b833a8e8f5ffe36a3287d1/docs/design/coreclr/botr/readytorun-overview.md
Context: https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/readytorun-format.md
Context: xamarin/monodroid@f22cc20
ReadyToRun (R2R) is a form of ahead-of-time compilation supported by
CoreCLR.It works by adding setting the `ManagedNativeHeader`
CLR header entrypoint to refer to a `READYTORUN_HEADER` section.
R2R is enabled by using `dotnet publish` alongside
`$(PublishReadyToRun)`=true:
dotnet new console
dotnet publish -p:PublishReadyToRun=true
You can see some of the changes that R2R produces by using
[`dumpbin /CLRHEADER`][0]. Consider the original assembly:
> dumpbin /clrheader .\obj\Release\net9.0\win-x64\*.dll
…
clr Header:
…
6000001 entry point token
…
0 [ 0] RVA [size] of ManagedNativeHeader Directory
vs. the R2R assembly:
> dumpbin /clrheader .\obj\Release\net9.0\win-x64\R2R\*.dll
…
clr Header:
…
6000001 entry point token
…
1548 [ 94] RVA [size] of ManagedNativeHeader Directory
The `ManagedNativeHeader` entry point having a non-zero RVA indicates
that the assembly contains native machine code, possibly R2R data.
Enable R2R usage with .NET for Android and CoreCLR, when
`$(UseMonoRuntime)`=false. Additionally, `$(RuntimeIdentifier)` must
be set, via `-r RID`:
dotnet new android
dotnet publish -p:PublishReadyToRun=true -p:UseMonoRuntime=false -r android-arm64
The resulting packaged assemblies will contain R2R data.
## Problem
Android app build runs two assembly post-processing build tasks which
modify assemblies using Mono.Cecil:
`<RewriteMarshalMethods/>` (86260ed) and
`<RemoveRegisterAttribute/>` (xamarin/monodroid@f22cc208).
Unfortunately, if this happens *after* R2R image generation, Cecil
will throw a NotSupportedException, as Cecil does not support
modifying assemblies which contain native data:
System.NotSupportedException: Writing mixed-mode assemblies is not supported
at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
at Mono.Cecil.ModuleDefinition.Write(Stream stream, WriterParameters parameters)
at Mono.Cecil.ModuleDefinition.Write(WriterParameters parameters)
at Mono.Cecil.ModuleDefinition.Write()
at Mono.Cecil.AssemblyDefinition.Write()
at Xamarin.Android.Tasks.RemoveRegisterAttribute.RunTask() in /Users/runner/work/1/s/xamarin-android/src/Xamarin.Android.Build.Tasks/Tasks/RemoveRegisterAttribute.cs:line 37
at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 25
"Fix" this by *disabling* use of the `<RewriteMarshalMethods/>`
and `<RemoveRegisterAttribute/>` tasks when using R2R.
## TODO
Investigate running R2R image generation build task after
`<RewriteMarshalMethods/>` and `<RemoveRegisterAttribute/>` so that
R2R builds could also benefit from the performance improvements these
build tasks bring.
Investigate running R2R image generation as part of the
`$(RuntimeIdentifiers)` inner build, so that `-r RID` is not required.
Co-authored-by: Jonathan Peppers <jonathan.peppers@microsoft.com>
[0]: https://learn.microsoft.com/en-us/cpp/build/reference/clrheader?view=msvc-1701 parent 33763ef commit 30295f8
File tree
2 files changed
+62
-10
lines changed- src/Xamarin.Android.Build.Tasks
- Tests/Xamarin.Android.Build.Tests
2 files changed
+62
-10
lines changedLines changed: 59 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
111 | 111 | | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | 112 | | |
116 | 113 | | |
117 | 114 | | |
| |||
124 | 121 | | |
125 | 122 | | |
126 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
127 | 182 | | |
128 | 183 | | |
129 | 184 | | |
| |||
Lines changed: 3 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
345 | | - | |
| 345 | + | |
346 | 346 | | |
347 | | - | |
| 347 | + | |
348 | 348 | | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | 349 | | |
353 | 350 | | |
354 | 351 | | |
| |||
2074 | 2071 | | |
2075 | 2072 | | |
2076 | 2073 | | |
2077 | | - | |
| 2074 | + | |
2078 | 2075 | | |
2079 | 2076 | | |
2080 | 2077 | | |
| |||
0 commit comments