Skip to content

[NativeAOT] Improve the transformation of [Preserve(AllMembers = true)]#19516

Merged
simonrozsival merged 5 commits intodotnet:mainfrom
simonrozsival:fix-ilc-warning-il3050
Nov 30, 2023
Merged

[NativeAOT] Improve the transformation of [Preserve(AllMembers = true)]#19516
simonrozsival merged 5 commits intodotnet:mainfrom
simonrozsival:fix-ilc-warning-il3050

Conversation

@simonrozsival
Copy link
Copy Markdown
Member

Closes #19505

We transform [Preserve(AllMembers = true)] into [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(T))]. There is difference between which members are preserved in these two cases. The DynamicallyAccessedMemberTypes.All preserves many more, especially nested types and their members. This manifested with the IL3050 AOT analysis warning we got from ILC when building any app:

  1. NSObject_Disposer has [Preserve(AllMembers = true)]
  2. NSObject_Disposer is a subclass of NSObject
  3. NSObject has nested enums Flags and XamarinGCHandleFlags
  4. the base class Enum has a public static method GetValues(Type)
  5. the GetValues method is annotated with RequiresDynamicCode and ILC produces a warning because even though it isn't used anywhere in the codebase, it could be used via reflection

I changed two things when transforming Preserve:

  • For enums, we only need to preserve public fields. We especially want to avoid PublicMethods since that would preserve public methods in the base class which includes the GetValues(Type) method.
  • For other types, I list explicitly the member types that should be preserved instead of using All.
    • The code is verbose, but in the end, I chose the explicit list instead of All ^ PublicNestedTypes ^ NonPublicNestedTypes since that would automatically include any new flag added to the enum in the future.

@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Your code has been reformatted. ⚠️

If this is not desired, add the actions-disable-autoformat label, and revert the reformatting commit.

If files unrelated to your change were modified, try reverting the reformatting commit + merging with the target branch (and push those changes).

( { IsEnum: true }, _) => DynamicallyAccessedMemberTypes.PublicFields,
(_, false) => DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors,
(_, true) => allMemberTypes,
};
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.

nit: I find the pattern matching switch kinda hard to read compared to splitting it up a bit:

var members = allMembers
	? allMemberTypes
	: DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;

// only preserve fields for enums
if (type.IsEnum)
	members = DynamicallyAccessedMemberTypes.PublicFields

@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

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.

@simonrozsival
Copy link
Copy Markdown
Member Author

The CMBlockBufferCustomBlockSource test needed updating. This is most likely because due to the change, NativeAOT doesn't generate the same metadata for the struct as it did before, and we can't resolve the type just using its name. The struct is still there, we just need to access it slightly differently. I think it is an OK tradeoff.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: 5c57994acd076191f1cc0c5811aa33bba19644b7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

📚 [PR Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
  • iOS (no change detected)
  • tvOS (no change detected)
  • watchOS (no change detected)
  • macOS (no change detected)
NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

✅ API diff vs stable

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)
Legacy Xamarin (stable) vs .NET

ℹ️ Generator diff

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

Pipeline on Agent
Hash: 5c57994acd076191f1cc0c5811aa33bba19644b7 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

❌ [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) failed ❌

Failed tests are:

  • dontlink
  • introspection
  • linksdk
  • linkall
  • xammac_tests
  • monotouch-test

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
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

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: simulator tests.

🎉 All 235 tests passed 🎉

Tests counts

✅ bcl: All 69 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 7 tests passed. Html Report (VSDrops) Download
✅ framework: All 8 tests passed. Html Report (VSDrops) Download
✅ generator: All 2 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 7 tests passed. Html Report (VSDrops) Download
✅ install_source: All 1 tests passed. Html Report (VSDrops) Download
✅ introspection: All 8 tests passed. Html Report (VSDrops) Download
✅ linker: All 65 tests passed. Html Report (VSDrops) Download
✅ mac_binding_project: All 1 tests passed. Html Report (VSDrops) Download
✅ mmp: All 2 tests passed. Html Report (VSDrops) Download
✅ mononative: All 6 tests passed. Html Report (VSDrops) Download
✅ monotouch: All 41 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ mtouch: All 1 tests passed. Html Report (VSDrops) Download
✅ xammac: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 8 tests passed. Html Report (VSDrops) Download
✅ xtro: All 2 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 5c57994acd076191f1cc0c5811aa33bba19644b7 [PR build]

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.

[NativeAOT] Fix AOT analysis warning IL3050

4 participants