Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set DynamicCodeSupport=false to enable trimming in full AOT mode #18555

Merged

Conversation

ivanpovazan
Copy link
Contributor

@ivanpovazan ivanpovazan commented Jul 13, 2023

This PR sets the feature switch DynamicCodeSupport to false for platforms:

  • where it's not possible to publish without the AOT compiler
  • when interpreter is not enabled

This enables ILLink to remove code paths depending on RuntimeFeature.IsDynamicCodeSupported for example:
https://github.com/dotnet/runtime/blob/dedaf46154a8938848fc9a7e0bd6e5ccebe7809c/src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/CallInstruction.cs#L19

To further clarify which platforms are included for this size optimizations according to comment:

  • Include iOS and tvOS
  • Include Mac Catalyst when building for arm64 (x64 has the JIT). This is slightly problematic though, since by default we build for arm64 only for release builds (i.e. the debug build doesn't necessarily know that the release build uses a different runtime identifier). As such, I think it would make sense to handle Mac Catalyst as if arm64 is required and disable dynamic code support even when only building for x64. If someone wants to publish a Mac Catalyst app on x64 only, then can force DynamicCodeSupport=true themselves.

Estimated size savings are shown bellow:

dotnet new maui (ios) old SLE.dll new SLE.dll + DynamicCodeSupported=false diff (%)
Size on disk (Mb) 40,53 38,78 -4,31%
.pkg (Mb) 14,83 14,20 -4,21%

Fixes #18340

@ivanpovazan ivanpovazan requested review from rolfbjarne and mauroa July 13, 2023 14:54
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@ivanpovazan
Copy link
Contributor Author

ivanpovazan commented Jul 14, 2023

The failures seem to be related.
Seems like as we treat maccatalyst-x64 and maccatalyst-arm64 differently now, there is an issue with that in multiple places.
Will look into it.

ivanpovazan added a commit to dotnet/runtime that referenced this pull request Jul 19, 2023
#88723)

- Ship a single assembly for all platforms, and have better compatibility between our AOT engines
- Size of the shipped assembly for iOS-like platforms is increased by ~113Kb
- The fullAOT + UseInterpreter=true configurations with Mono are now interpreting Ref.Emit generated code instead of using Reflection
- To prevent size regressions with Mono xamarin/xamarin-macios#18555 needs to be merged
@ivanpovazan
Copy link
Contributor Author

ivanpovazan commented Jul 20, 2023

The failures seem to be related. Seems like as we treat maccatalyst-x64 and maccatalyst-arm64 differently now, there is an issue with that in multiple places. Will look into it.

After an offline discussion with @mandel-macaque it seems this is related to building universal apps.

During the build, the app bundles created for maccatalyst-x64 and maccatalyst-arm64 need to be merged via MergeAppBundlesTaskBase. However, as this PR introduced a change where maccatalyst-arm64 is configured differently -> now has a feature switch enabled for DynamicCodeSupport, while maccatalyst-x64 does not, the build throws at:

Log.LogError (MSBStrings.E7077 /* Unable to merge the file '{0}', it's different between the input app bundles. */, entries [0].RelativePath);
as it is not possible to merge runtimeconfig.bin as they are different (I am posting the difference between runtimeconfig.jsons which is essentially the same thing):

diff /Users/ivan/repos/xamarin/xamarin-nativeaot/tests/dotnet/AppWithXCAssets/MacCatalyst/bin/Debug/net8.0-maccatalyst/maccatalyst-x64/AppWithXCAssets.runtimeconfig.json /Users/ivan/repos/xamarin/xamarin-nativeaot/tests/dotnet/AppWithXCAssets/MacCatalyst/bin/Debug/net8.0-maccatalyst/maccatalyst-arm64/AppWithXCAssets.runtimeconfig.json
26a27
>       "System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported": false,

Question

How much are we sensitive to maccatalyst app size?

Proposal

As this is purely a size-saving (trimming) optimization, a potential fix to this problem is to exclude maccatalyst-arm64 as a platform that could benefit from it. It is also important to note that if we choose this approach the app size would actually be worse than before due to changes introduced by: dotnet/runtime#88723 because the library now carries Ref.Emit backend (which will not be trimmed if DynamicCodeSupported is not set to false). Before, this was not an issue, because we had library-build-time trimming enabled which was removing Ref.Emit backend from it.

@SamMonoRT
Copy link

Your proposed solution seems fine to me.

@ivanpovazan
Copy link
Contributor Author

Your proposed solution seems fine to me.

Thanks, I will try to get some app size numbers for the proposed solution regarding maccatalyst-arm64.
But until, then still waiting on some feedback here.

@ivanpovazan
Copy link
Contributor Author

I have excluded maccatalyst-arm64 for now, to see if we can get a green CI with the proposed changes on other platforms.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@ivanpovazan
Copy link
Contributor Author

The failed tests seem unrelated as they are timing out.
I will do another run when we bump the net8.0 branch

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@dalexsoto
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

… watchOS as it is not supported on dotnet

Co-authored-by: Rolf Bjarne Kvinge <[email protected]>
@ivanpovazan ivanpovazan requested a review from rolfbjarne August 3, 2023 08:56
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: 29187818122138e1a265cd51662a360272a75d78 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [PR Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) passed 💻

All tests on macOS M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Ventura (13.0) passed 💻

All tests on macOS M1 - Mac Ventura (13.0) passed.

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

❗ API diff vs stable (Breaking changes)

.NET (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • iOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • tvOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • MacCatalyst: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • macOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • Microsoft.iOS vs Microsoft.MacCatalyst: vsdrops gist

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 29187818122138e1a265cd51662a360272a75d78 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@ivanpovazan
Copy link
Contributor Author

The previous build failures are now gone, but the tests are still failing on CI.
All failures seem to be caused by tests timing out, example:

MonoTouchFixtures.CoreServices.FSEventStreamTest
[05:36:27.7072430] 	[FAIL] TestExtendedFileEvents : System.AggregateException : One or more errors occurred. (test has timed out at 10s; increase the timeout or reduce the number of files created. Created directories: 0 Created files: 0 Removed files: 169 Created then removed files: 256)
[05:36:27.7073610]   ----> System.TimeoutException : test has timed out at 10s; increase the timeout or reduce the number of files created. Created directories: 0 Created files: 0 Removed files: 169 Created then removed files: 256
[05:36:27.7074290] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.TestFSMonitor.Run() in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 150
[05:36:27.7074560] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.RunTest(FSEventStreamCreateFlags createFlags) in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 77
[05:36:27.7074770] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.TestExtendedFileEvents() in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 74
[05:36:27.7074970] 		   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
[05:36:27.7075190] 		   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
[05:36:27.7075390] 		--TimeoutException
[05:36:27.7075930] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.TestFSMonitor.CreateFilesAndWaitForFSEventsThread() in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 203
[05:36:27.7076180] 		   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
[05:36:27.7076420] 		--- End of stack trace from previous location ---
[05:36:27.7076610] 		   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
[05:36:27.7076800] 		   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

@rolfbjarne should we just try to rerun them?

@rolfbjarne
Copy link
Member

The previous build failures are now gone, but the tests are still failing on CI. All failures seem to be caused by tests timing out, example:

MonoTouchFixtures.CoreServices.FSEventStreamTest
[05:36:27.7072430] 	[FAIL] TestExtendedFileEvents : System.AggregateException : One or more errors occurred. (test has timed out at 10s; increase the timeout or reduce the number of files created. Created directories: 0 Created files: 0 Removed files: 169 Created then removed files: 256)
[05:36:27.7073610]   ----> System.TimeoutException : test has timed out at 10s; increase the timeout or reduce the number of files created. Created directories: 0 Created files: 0 Removed files: 169 Created then removed files: 256
[05:36:27.7074290] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.TestFSMonitor.Run() in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 150
[05:36:27.7074560] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.RunTest(FSEventStreamCreateFlags createFlags) in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 77
[05:36:27.7074770] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.TestExtendedFileEvents() in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 74
[05:36:27.7074970] 		   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
[05:36:27.7075190] 		   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
[05:36:27.7075390] 		--TimeoutException
[05:36:27.7075930] 		   at MonoTouchFixtures.CoreServices.FSEventStreamTest.TestFSMonitor.CreateFilesAndWaitForFSEventsThread() in /Users/builder/azdo/_work/4/s/xamarin-macios/tests/monotouch-test/CoreServices/FSEventStreamTest.cs:line 203
[05:36:27.7076180] 		   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
[05:36:27.7076420] 		--- End of stack trace from previous location ---
[05:36:27.7076610] 		   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
[05:36:27.7076800] 		   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)

@rolfbjarne should we just try to rerun them?

Yup, done

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: simulator tests.

🎉 All 92 tests passed 🎉

Tests counts

⚠️ bcl: No tests selected. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ framework: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 1 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 4 tests passed. Html Report (VSDrops) Download
⚠️ install_source: No tests selected. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 40 tests passed. Html Report (VSDrops) Download
⚠️ mac_binding_project: No tests selected. Html Report (VSDrops) Download
⚠️ mmp: No tests selected. Html Report (VSDrops) Download
⚠️ mononative: No tests selected. Html Report (VSDrops) Download
✅ monotouch: All 26 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
⚠️ mtouch: No tests selected. Html Report (VSDrops) Download
⚠️ xammac: No tests selected. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 29187818122138e1a265cd51662a360272a75d78 [PR build]

@ivanpovazan ivanpovazan merged commit 0c0e5d7 into xamarin:net8.0 Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants