dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.2k stars 4.72k forks source link

Mac Catalyst (arm64): Attempting to JIT compile method '...' while running in aot-only mode. #102728

Open rolfbjarne opened 5 months ago

rolfbjarne commented 5 months ago

Description

The AOT compiler seems unable to compile certain generic methods.

Reproduction Steps

Test project: maccatalyst-aa3902a.zip

The code goes something like this:

public unsafe void TickOnce ()
{
    delegate*<IntPtr, sbyte> convert = &BindAs.xamarin_nsnumber_to_sbyte;
    BindAs.Fails<sbyte> (convert);
}

public unsafe static class BindAs {
    public unsafe static T[]? Fails<T> (delegate*<IntPtr, T> convert1) where T: struct
    {
        return Fails<T, T> (convert1);
    }

    public unsafe static T[]? Fails<T, U> (delegate*<IntPtr, T> convert1) where T: struct
    {
        return ArrayFromHandleFunc<T> ((ptr) => convert1 (ptr));
    }

    static public T [] ArrayFromHandleFunc<T> (Func<NativeHandle, T> createObject)
    {
        createObject (NativeHandle.Zero);
        return new T [0];
    }

    public static System.SByte xamarin_nsnumber_to_sbyte (IntPtr value) { return 0; }
}

Expected behavior

No exception.

Actual behavior

System.ExecutionEngineException: Attempting to JIT compile method 'sbyte maccatalyst.BindAs/<>c__DisplayClass2_0`2<sbyte, sbyte>:<Fails>b__0 (ObjCRuntime.NativeHandle)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.

   at maccatalyst.BindAs.ArrayFromHandleFunc[SByte](Func`2 createObject) in /Users/rolf/test/dotnet/maccatalyst/AppDelegate.cs:line 73
2024-05-27 16:46:22.112 maccatalyst[8245:1311369]    at maccatalyst.BindAs.Fails[SByte,SByte]( convert1) in /Users/rolf/test/dotnet/maccatalyst/AppDelegate.cs:line 68
   at maccatalyst.BindAs.Fails[SByte]( convert1) in /Users/rolf/test/dotnet/maccatalyst/AppDelegate.cs:line 63
   at maccatalyst.AppDelegate.TickOnce() in /Users/rolf/test/dotnet/maccatalyst/AppDelegate.cs:line 45

Regression?

Not as far as I know.

Known Workarounds

Use less generics? NativeAOT also works.

Configuration

Mac Catalyst .NET 8.0.200, on arm64. Same thing happens on iOS and tvOS as well (on arm64).

$ dotnet --info
.NET SDK:
 Version:           8.0.201
 Commit:            4c2d78f037
 Workload version:  8.0.200-manifests.a7f084b6

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  14.5
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/8.0.201/

.NET workloads installed:
 [macos]
   Installation Source: SDK 8.0.200
   Manifest Version:    14.2.8043/8.0.100
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/8.0.100/microsoft.net.sdk.macos/14.2.8043/WorkloadManifest.json
   Install Type:        FileBased

 [maui-tizen]
   Installation Source: SDK 8.0.200
   Manifest Version:    8.0.7/8.0.100
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/8.0.100/microsoft.net.sdk.maui/8.0.7/WorkloadManifest.json
   Install Type:        FileBased

 [ios]
   Installation Source: SDK 8.0.200
   Manifest Version:    17.2.8043/8.0.100
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/8.0.100/microsoft.net.sdk.ios/17.2.8043/WorkloadManifest.json
   Install Type:        FileBased

 [maccatalyst]
   Installation Source: SDK 8.0.200
   Manifest Version:    17.2.8043/8.0.100
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/8.0.100/microsoft.net.sdk.maccatalyst/17.2.8043/WorkloadManifest.json
   Install Type:        FileBased

Host:
  Version:      9.0.0-preview.3.24129.2
  Architecture: arm64
  Commit:       5e603d595e

.NET SDKs installed:
  7.0.311 [/usr/local/share/dotnet/sdk]
  8.0.100 [/usr/local/share/dotnet/sdk]
  8.0.104 [/usr/local/share/dotnet/sdk]
  8.0.201 [/usr/local/share/dotnet/sdk]
  9.0.100-preview.3.24153.2 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0-preview.3.24151.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0-preview.3.24129.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  /Users/rolf/test/dotnet/maccatalyst/global.json

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

ivanpovazan commented 3 months ago

@rolfbjarne is this a customer facing issue or a failing test case?

We are triaging all the AOT issues and it is known that Mono AOT compiler struggles with generics, but it is a non-trivial problem and any fix is a high-risk one. With that said, we are trying to understand when this should be fixed.

vitek-karas commented 3 months ago

Moving to 9 for now - depending on the impact we should move it to 10 probably.

rolfbjarne commented 2 months ago

@rolfbjarne is this a customer facing issue or a failing test case?

It's a failing test case (see https://github.com/xamarin/xamarin-macios/issues/19781).

vitek-karas commented 2 months ago

@rolfbjarne Is there some timeframe when this started to happen? Or is it a new test which failed since forever?

rolfbjarne commented 2 months ago

@rolfbjarne Is there some timeframe when this started to happen? Or is it a new test which failed since forever?

I don't think this ever worked.