Several types in System.Runtime.Intrinsics namespace (for example: Scalar<T>) are defined with struct constraint, but additionally in many places (in different methods) there are runtime checks to check for a specific type support. If the type is not supported the method just throws.
AOTing such pattern with Mono results in unnecessary large and slow methods, especially GSHAREDVT variant (fallback method for any value-type).
In order to avoid generating GSHAREDVT methods introduce a custom attribute which can be attached to methods, for the Scalar<T>::Abs example:
[MonoAotMethodSupportedTypes(new Type[]{ typeof(double), typeof(short),... }, typeof(NotSupportedException))]
public static T Abs(T value)
MonoAotMethodSupportedTypesAttribute would be then utilized by the AOT compiler at the method's call site.
For example:
- If in the caller, T is known concretely, caller can call the specialized version,
- Otherwise, if the caller doesn't have a concrete T it does a switch on T for the supported types specified by the attribute, calling the specialized versions, or in the default case (as unsupported) would throw exception of the type specified by the attribute.
This way we would eliminate the need for generating GSHAREDVT fallbacks for methods which have the MonoAotMethodSupportedTypesAttribute attached
This is related to #56385
cc @lambdageek @vargaz