Skip to content

Commit

Permalink
[AOT] Audit dynamic code flags and add comments (#45946)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Jan 8, 2023
1 parent e321474 commit cefc6cc
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ static void InvokeContainer(GenericWebHostBuilder genericWebHostBuilder, Configu
{
var containerType = configureContainerBuilder.GetContainerType();

// Configure container uses MakeGenericType with the container type. MakeGenericType + struct container type requires IsDynamicCodeSupported.
if (containerType.IsValueType && !RuntimeFeature.IsDynamicCodeSupported)
{
throw new InvalidOperationException("A ValueType TContainerBuilder isn't supported with AOT.");
Expand Down
1 change: 1 addition & 0 deletions src/Hosting/Hosting/src/Internal/StartupLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static StartupMethods LoadMethods(IServiceProvider hostingServiceProvider
Justification = "There is a runtime check for ValueType startup container. It's unlikely anyone will use a ValueType here.")]
static Type CreateConfigureServicesDelegateBuilder(Type type)
{
// Configure container uses MakeGenericType with the container type. MakeGenericType + struct container type requires IsDynamicCodeSupported.
if (type.IsValueType && !RuntimeFeature.IsDynamicCodeSupported)
{
throw new InvalidOperationException("ValueType startup container isn't supported with AOT.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public static IApplicationBuilder UseMiddleware(
return (RequestDelegate)invokeMethod.CreateDelegate(typeof(RequestDelegate), instance);
}

var factory = RuntimeFeature.IsDynamicCodeSupported
// Performance optimization: Use compiled expressions to invoke middleware with services injected in Invoke.
// If IsDynamicCodeCompiled is false then use standard reflection to avoid overhead of interpreting expressions.
var factory = RuntimeFeature.IsDynamicCodeCompiled
? CompileExpression<object>(invokeMethod, parameters)
: ReflectionFallback<object>(invokeMethod, parameters);

Expand Down
3 changes: 2 additions & 1 deletion src/Http/Http.Extensions/src/RequestDelegateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ private static Expression[] CreateArgumentsAndInferMetadata(MethodInfo methodInf

for (var i = 0; i < argTypes.Length; i++)
{
// MakeGenericMethod + value type requires IsDynamicCodeSupported to be true.
if (RuntimeFeature.IsDynamicCodeSupported)
{
// Register expressions containing the boxed and unboxed variants
Expand Down Expand Up @@ -560,7 +561,7 @@ private static Expression CreateEndpointFilterInvocationContextBase(RequestDeleg
DefaultEndpointFilterInvocationContextConstructor,
new Expression[] { HttpContextExpr, paramArray });

if (!RuntimeFeature.IsDynamicCodeCompiled)
if (!RuntimeFeature.IsDynamicCodeSupported)
{
// For AOT platforms it's not possible to support the closed generic arguments that are based on the
// parameter arguments dynamically (for value types). In that case, fallback to boxing the argument list.
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Routing/src/Matching/JumpTableBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public static JumpTable Build(int defaultDestination, int exitDestination, (stri
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Guarded by IsDynamicCodeCompiled")]
static JumpTable MakeILEmitTrieJumpTableIfSupported(int defaultDestination, int exitDestination, (string text, int destination)[] pathEntries, JumpTable fallback)
{
// ILEmitTrieJumpTable use IL emit to generate a custom, high-performance jump table.
// EL emit requires IsDynamicCodeCompiled to be true. Fallback to another jump table implementation if not available.
return RuntimeFeature.IsDynamicCodeCompiled
? new ILEmitTrieJumpTable(defaultDestination, exitDestination, pathEntries, vectorize: null, fallback)
: fallback;
Expand Down
2 changes: 2 additions & 0 deletions src/Shared/PropertyHelper/PropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ public static PropertyHelper[] GetVisibleProperties(
Debug.Assert(!getMethod.IsStatic);
Debug.Assert(getMethod.GetParameters().Length == 0);

// MakeGenericMethod + value type requires IsDynamicCodeSupported to be true.
if (RuntimeFeature.IsDynamicCodeSupported)
{
// TODO: Remove disable when https://github.com/dotnet/linker/issues/2715 is complete.
Expand Down Expand Up @@ -285,6 +286,7 @@ public static PropertyHelper[] GetVisibleProperties(
var parameters = setMethod.GetParameters();
Debug.Assert(parameters.Length == 1);

// MakeGenericMethod + value type requires IsDynamicCodeSupported to be true.
if (RuntimeFeature.IsDynamicCodeSupported)
{
// TODO: Remove disable when https://github.com/dotnet/linker/issues/2715 is complete.
Expand Down

0 comments on commit cefc6cc

Please sign in to comment.