-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add ILLink/ILCompiler support for feature checks #99267
Conversation
Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas Issue DetailsThis will substitute static boolean properties with The "feature" of unreferenced code, represented by Additionally, a feature is considered disabled if the type has We don't want this kicking in when trimming in library mode, so this adds an ILLink option to disable the optimization. XML substitutions take precedence over this behavior. Includes a few tweaks and cleanup to the analyzer logic added in #94944. See #94625 for notes on the design.
|
I will update this to match the approved API shape once #99340 is merged. |
This is ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/FeatureGuardAttributeDataFlow.cs
Show resolved
Hide resolved
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/FeatureGuardAttributeDataFlow.cs
Outdated
Show resolved
Hide resolved
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/FeatureGuardAttributeDataFlow.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Native AOT side LGTM modulo the questions. Thanks!
|
||
foreach (var featureGuardAttribute in property.GetDecodedCustomAttributes("System.Diagnostics.CodeAnalysis", "FeatureGuardAttribute")) | ||
{ | ||
if (featureGuardAttribute.FixedArguments is not [CustomAttributeTypedArgument<TypeDesc> { Value: EcmaType featureType }]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just double checking that we don't allow feature types to be generic/arrays/pointers (non-definition type will never be EcmaType).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature types aren't restricted in any way, but everything will be a no-op except for RequiresUnreferencedCodeAttribute
, RequiresDynamicCodeAttribute
, and RequiresAssemblyFilesAttribute
. A third-party analyzer could theoretically add support for other types (including generics) so we don't block other types.
if (_hashtable._switchValues.TryGetValue( | ||
"System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported", | ||
out bool isDynamicCodeSupported) | ||
&& !isDynamicCodeSupported) | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this? IsDynamicCodeSupported/Compiled is hardcoded to false in native AOT corelib.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this to match the behavior of some substitutions I noticed for System.Linq, that only kick in when the feature is explicitly passed on the command-line. It's kind of a moot point unless somebody tries to set DynamicCodeSupport
to true in an AOT app.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
This will substitute static boolean properties with
false
when decorated with[FeatureGuard(typeof(SomeFeature))]
, whereSomeFeature
is known to be disabled.The "feature" of unreferenced code, represented by
RequiresUnreferencedCodeAttribute
, is always considered disabled. For ILC,RequiresDynamicCodeAttribute
andRequiresAssemblyFilesAttribute
are also disabled. ILLink also respects the feature switch forIsDynamicCodeSupported
to disable theRequiresDynamicCodeAttribute
feature.We don't want this kicking in when trimming in library mode, so this adds an ILLink option to disable the optimization.
Additionally, a property is substituted if it has
[FeatureSwitchDefinition("SomeFeatureName")]
and"SomeFeatureName"
is set in the command-line arguments.XML substitutions take precedence over this behavior.
Includes a few tweaks and cleanup to the analyzer logic added in #94944.
See #94625 for notes on the design.
See #96859 for the API proposal.