Skip to content
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

.NET 7 AOT x86/x64 Unable to build a method of a generic class that returns Type #79710

Closed
jonathanpeppers opened this issue Dec 15, 2022 · 3 comments
Assignees
Milestone

Comments

@jonathanpeppers
Copy link
Member

From @akravch on Mon, 12 Dec 2022 15:50:00 GMT

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

.NET 7.0.100

Description

Can't build an app for x86 or x64 if there is a method that satisfies the following conditions:

  • It is a method of a generic class
  • It returns Type
  • It has a conditional structure inside (e.g. if, switch or a ternary operator)
  • One of the branches returns typeof(T)

Same code compiles just fine for ARM/ARM64 with .NET 7, or any runtime including x86/x64 with .NET 6.

Repro steps below or just see this repo: https://github.com/akravch/net_7_aot_issue

Steps to Reproduce

  1. Create a new android applicaiton for .NET 7
  2. Configure it to build for x86 and/or x64 runtime identifiers: <RuntimeIdentifiers>android-x64</RuntimeIdentifiers>
  3. Make sure AOT is enabled: <RunAOTCompilation>true</RunAOTCompilation>
  4. Add the following code anywhere in the project:
    public class ProblematicClass<T>
    {
        public Type ProblematicMethod(int i)
        {
            return i == 0 ? typeof(T) : null;
        }
    }
  5. Run the build in Release configuration

Expected:
The build has succeeded
Actual:
AOT compilation failed

Did you find any workaround?

Any of the following methods would work:

public class ProblematicClass<T>
{
    private readonly Type _type = typeof(T);
    private readonly Type _nullType = null;

    public Type WorkingMethod1(int i)
    {
        return i == 0 ? _type : null;
    }

    public Type WorkingMethod2(int i)
    {
        return i == 0 ? typeof(T) : _nullType;
    }
}

Although, in my case the issue actually comes from a NuGet package, so applying the workaround is somewhat complicated.

Relevant log output

Microsoft.Android.Sdk.Aot.targets(91,5): error : Precompiling failed for net_7_aot_issue\Net7AotIssue.Android\obj\Release\net7.0-android\android-x64\linked\Net7AotIssue.Android.dll with exit code -1073740791.
Microsoft.Android.Sdk.Aot.targets(91,5): error : Mono Ahead of Time compiler - compiling assembly net_7_aot_issue\Net7AotIssue.Android\obj\Release\net7.0-android\android-x64\linked\Net7AotIssue.Android.dll
Microsoft.Android.Sdk.Aot.targets(91,5): error : AOTID 6426D756-4265-334D-C2CE-551A78CD5437
Microsoft.Android.Sdk.Aot.targets(91,5): error : Opcode 'rttype' missing from machine description file.

Also attaching a binlog: binlog.zip

Copied from original issue dotnet/android#7631

@vargaz
Copy link
Contributor

vargaz commented Dec 18, 2022

Happens on all platforms.

@vargaz
Copy link
Contributor

vargaz commented Dec 18, 2022

This happens because the the bb->needs_decompose flag is not propagated by some optimizations in branch-opts.c.

@jandupej
Copy link
Member

The issue has been fixed in 79849 and backported to .NET 7.0 in 79855. Closing.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants