7

The Mono documentation has a code example about full AOT not supporting generic interface instantiation:

interface IFoo<T> {
... 
    void SomeMethod();
}

It says:

Since Mono has no way of determining from the static analysis what method will implement the interface IFoo<int>.SomeMethod this particular pattern is not supported."

So I think the compiler can't work with this method under type inference. But I still can't understand the underlying reason about the full AOT limitation.

There is still a similar problem with the Unity AOT script restrictions. In the following code:

using UnityEngine;
using System;

public class AOTProblemExample : MonoBehaviour, IReceiver 
{
    public enum AnyEnum {
        Zero,
        One,
    }

    void Start() {
        // Subtle trigger: The type of manager *must* be
        // IManager, not Manager, to trigger the AOT problem.
        IManager manager = new Manager();
        manager.SendMessage(this, AnyEnum.Zero);
    }

    public void OnMessage<T>(T value) {
        Debug.LogFormat("Message value: {0}", value);
    }
}

public class Manager : IManager {
    public void SendMessage<T>(IReceiver target, T value) {
        target.OnMessage(value);
    }
}

public interface IReceiver {
    void OnMessage<T>(T value);
}

public interface IManager {
    void SendMessage<T>(IReceiver target, T value);
}

I am confused by this:

The AOT compiler does not realize that it should generate code for the generic method OnMessage with a T of AnyEnum, so it blissfully continues, skipping this method. When that method is called, and the runtime can’t find the proper code to execute, it gives up with this error message.

Why does the AOT not know the type when the JIT can infer the type? Can anyone offer a detailed answer?

3
  • Hi user, this just looks bizarre in a Unity context? (Are you trying to make a singleton or something? You can't have a singleton in an ECS system - it is meaningless. Unity has no connection whatsoever to OO or inheritance. Actual "things" can't "inherit".) What is it you're trying to do?
    – Fattie
    Commented May 22, 2016 at 16:11
  • Note that what you have here is a MonoBehavior, so it is indeed simply a behavior attached to some specific actual GameObject. Explain what you are trying to do in terms of "I have a dinosaur and then I have a weapon and I want to do this..."
    – Fattie
    Commented May 22, 2016 at 16:13
  • Possible duplicate of Coroutine wrong Behavior when scene is loaded
    – Fattie
    Commented May 22, 2016 at 16:22

2 Answers 2

11

Before describing the issues, consider this excerpt from another answer of mine that describes the generics situation on platforms that do support dynamic code generation:

In C# generics, the generic type definition is maintained in memory at runtime. Whenever a new concrete type is required, the runtime environment combines the generic type definition and the type arguments and creates the new type (reification). So we get a new type for each combination of the type arguments, at runtime.

The phrase at runtime is key to this, because it brings us to another point:

This implementation technique depends heavily on runtime support and JIT-compilation (which is why you often hear that C# generics have some limitations on platforms like iOS, where dynamic code generation is restricted).

So is it possible for a full AOT compiler to do that as well? It most certainly is possible. But is it easy?

There is a paper from Microsoft Research on pre-compiling .NET generics that describes the interaction of generics with AOT compilation, highlights some potential problems and proposes solutions. In this answer, I will use that paper to try to demonstrate why .NET generics aren't widely pre-compiled (yet).

Everything must be instantiated

Consider your example :

IManager manager = new Manager();
manager.SendMessage(this, AnyEnum.Zero);

Clearly we're calling the method IManager.SendMessage<AnyEnum> here, so the fully AOT compiler needs to compile that method.

But this is an interface call, which is effectively a virtual call, which means the we can't know ahead of time which implementation of the interface method will be called.

The JIT compiler doesn't care about this problem. When someone attempts to run a method that hasn't been compiled yet, the JIT will be notified and it will compile the method lazily.

On the contrary, a fully AOT compiler doesn't have access to all this runtime type information. So it has to pessimistically compile all possible instantiations of the generic method on all implementations of the interface. (Or just give up and not offer that feature.)

Generics can be infinitely recursive

object M<T>(long n)
{
    if (n == 1)
    {
        return new T[]();
    }
    else 
    {
        return M<T[]>(n - 1);
    }
}

To instantiate M<int>(), the compiler needs to instantiate int[] and M<int[]>(). To instantiate M<int[]>(), the compiler needs to instantiate int[][] and M<int[][]>(). To instantiate M<int[][]>(), the compiler needs to instantiate int[][][] and M<int[][][]>().

This can be solved by using representative instantiations (just like the JIT compiler uses). This means that all generic arguments that are reference types can share their code. So:

  • int[][], int[][][], int[][][][] (and so on) can all share the same code, because they are arrays of references.
  • M<int[]>, M<int[][]>, M<int[][][]> (and so on) can all share the same code, because they operate on references.

Assemblies need to own their generics...

Since C# programs are compiled in assemblies, it's hard to tell exactly who should "own" which instantiation of each type.

  • Assembly1 declares the type G<T>.
  • Assembly2 (references Assembly1) instantiates the type G<int>.
  • Assembly3 (references Assembly1) instantiates the type G<int> as well.
  • AssemblyX (references all the above) wants to use G<int>.

Which assembly gets to compile the actual G<int>? If they happen to be standalone libraries, neither Assembly2 nor Assembly3 can be compiled without each owning a copy of G<int>. So we're already looking at duplicated native code.

...and those generics must still be compatible with each other

But then, when AssemblyX is compiled, which copy of G<int> should it use? Clearly, it has to be able to handle both, because it may need to receive a G<int> from or send a G<int> to either assembly.

But more importantly, in C# you can't have two types with identical fully qualified names that turn out to be incompatible. In other words:

G<int> obj = new G<int>();

The above can never fail on the grounds that G<int> (the variable's type) is the G<int> from Assembly2 while G<int> (the constructor's type) is the G<int> from Assembly3. If it fails for a reason like that, we're not in C# anymore!

So both types need to exist and they need to be made transparently compatible, even though they are compiled separately. For this to happen, the type handles need to be manipulated at link time in such a way that the semantics of the language are retained, including the fact that they should be assignable to each other, their type handles should compare as equal (e.g. when using typeof), and so on.

2
  • Could i understand the List<stirng> and List<Object> share the same List<T> code . but List<int> and List<float> have their each generic code. Image link follow.(imgur.com/JIIk1xq). Commented May 23, 2016 at 5:28
  • @user4568159 Yes, that's correct. Do you need more details about it? Commented May 23, 2016 at 9:24
4

Unity is using an old version of Mono's Full-AOT the does not support generic interface method.

This is due to how generics are represented in the JIT vs in native code. (I would like to elaborate, but frankly, I do not trust myself to be accurate)

Newer versions of Mono's AOT compiler address this issue (of course, with other limitations), but Unity keeps an old version of Mono. (I think I remember hearing that they changed their approach from AOT to something a bit else, but I'm not sure how it works anymore).


I don't fully understand the topic WARNING

The way "generics" are handled in C++ (for example), which compiles to assembly, binary, is using a language mechanism called templates. These templates are more like glorified macros, and different code is actually generated for each type used. (Edit: Actually there are more differences between C# generics and C++ templates, but for the purpose of this answer I'll treat them as equivalent).

For example; for the following code:

template<typename T>
class Foo
{
public:
    T GetValue() { return value; }
    void SetValue(T a) {value = a;}
private:
    T value;
};

int main()
{
    Foo<int> a;
    Foo<char *> b;

    a.SetValue(0);
    b.SetValue((char*)0);

    a.GetValue();
    b.GetValue();

    return 0;
}

The following functions will be generated (got this by running nm --demangle)

00000000004005e4 W Foo<int>::GetValue()
00000000004005b2 W Foo<int>::SetValue(int)
00000000004005f4 W Foo<char*>::GetValue()
00000000004005ca W Foo<char*>::SetValue(char*)

This means that for every type you use this class with, another of instance of practically the same code will be generated (although I'm sure that GCC is smart enough to optimize some of the obvious cases, like getters and setters, and maybe more).

C#'s generics are a bit more complex. Here's a very interesting article by Eric Lippert. The summary is that compiled C# generic code has only one instance that is, well, generic, and what that depends on the type is calculated at runtime.

When translating C# code to native/machine code (which is essentially what AOT does), there's a problem translating generics.

This is where the subject gets a bit fuzzy to me. I can only assume that AOT'd code does not retain runtime type information, so it needs code-per-type for generic cases.

When receiving an object of type IFooable, it is possible that the native virtual table format is not verbose enough to enable finding the correct implementation; although I admit I have no idea why would that be, or the exact details of the virtual table of AOT'd code (is it identical to that of C++'s?)

1
  • ie, AOT can't infer some type parameter during compile time, When application call the generic method, application can't get correct type info from virtual table, so it will crash. Commented May 23, 2016 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.