Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Official] The collected il2cpp forum topic.

Discussion in 'General Discussion' started by RalphH, May 20, 2014.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Thanks for the clarification.
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    How often does this happen for you guys? I've had it once (pretty sure I fixed it by adding an explicit command to an XML document somewhere?).

    While it's less of a concern for me, it's still a bit of one. If nothing else it means we have to explicitly test for this stuff, which is time I'd rather spend elsewhere.

    The good news is that it sounds like you guys have a pretty solid repro case. ;)
     
  3. arturaz

    arturaz

    Joined:
    Mar 28, 2013
    Posts:
    30
    It happens a lot more than I'd like to. I'm coming from Scala background and using functional programming techniques a lot to write shorter, bug free code.

    While this works with jit platforms, with some minor annoyances, it totally flops on ios. Most of our structs are defined as classes on ios, because it's too much of a hassle to type hint every possible combination of types which aot does not calculate.

    Check out http://github.com/tinylabproductions/tlplib and grep the code for "bug". You'll see a lot of places where perfectly valid c# code crashes mono compiler, runtime or fails on aot platforms. And that definately isn't seamless porting experience Unity touts in their webpage. :-(

    I understand the legal and technical challenges behind it, but at the end of a day it's just wasted time spent searching for bugs in unity codebase, not my code.

    And then you start wondering whether you are really more productive with C# than with proper C++.
     
  4. arturaz

    arturaz

    Joined:
    Mar 28, 2013
    Posts:
    30
  5. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    Have you guys considered open sourcing il2cpp?

    I am sure it has applications well beyond game development, and some compiler freaks (like myself) might pitch in a patch or two.

    I have some math heavy scripts that will greatly benefit from this (procedural terrain generation), and will probably spend quite a bit of time making sure it runs as fast as it can
     
    Last edited: Aug 29, 2014
  6. made-on-jupiter

    made-on-jupiter

    Joined:
    May 19, 2013
    Posts:
    25
    and


    at 58:38


    What are the chances of this feature surviving the beta?
    And if it does survive, do you envisage any limitations?

    We've always been limited to Mono/C# as far as 3rd party libraries, to build to the WebPlayer. So this is *game changing* for us, and I imagine a host of other non-game applications that require a 3d web front-end. It means we can drop in mature C++ geometry libraries to target the browser. It means we can stick with our current C#/Mono codebase, and ultimately, Unity.
     
    twobob likes this.
  7. ASDAlexander77

    ASDAlexander77

    Joined:
    Aug 3, 2013
    Posts:
    2
    Hi Guys

    It is very interesting idea to convert IL to CPP. But may I ask some questions.

    Will it support generic virtual methods and generic methods in interfaces?
    example:
    interface IHello<T>
    {
    void Print (T t);
    }

    interface Foo
    {
    IHello<U> Test<U> ();
    }

    Will it support static members with default initialization?
    example:
    private static int[] array = {0, 1, 2, 3};

    Thank you.
     
  8. ASDAlexander77

    ASDAlexander77

    Joined:
    Aug 3, 2013
    Posts:
    2
    there is already plenty open-sources in this area.
     
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    Yes, all of this should be working. I've just tried this code, and it work as expected with il2cpp/WebGL:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewBehaviourScript : MonoBehaviour
    4. {
    5.     private interface IHello<T>
    6.     {
    7.         void Print(T t);
    8.     }
    9.  
    10.     private class Hey<T> : IHello<T>
    11.     {
    12.         private static int[] array = {0, 1, 2, 3};
    13.  
    14.         public void Print(T t)
    15.         {
    16.             Debug.Log(string.Format("Hey {0}", t));
    17.             Debug.Log(string.Format("Here is the array: {0} {1} {2} {3}", array[0], array[1], array[2], array[3]));
    18.         }
    19.     }
    20.  
    21.     private void Start()
    22.     {
    23.         IHello<string> greeter = new Hey<string>();
    24.         greeter.Print("Josh");
    25.     }
    26. }
     
  10. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    766
    The goal is not to learn how compilers work, the goal is to make my unity code run faster. I don't care if there are other open source compilers, that does not help me in any way.
     
  11. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
  12. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
  13. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    Does IL2CPP support code being run at run time? There's a name for it but I can't remember but the way IronPython works? IIRC it doesn't run on IOS ATM.
     
  14. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    No, IL2CPP does not support things like System.Reflection.Emit. It is an ahead-of-time compiler, with restrictions similar to the iOS build in Unity now.
     
    TylerPerry likes this.
  15. tiantian_

    tiantian_

    Joined:
    Mar 6, 2014
    Posts:
    9
    I still could not understand, why AOT preventing Unity from updating mono? Is the current mono in Unity for iOS not AOT?
     
  16. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Mono AOT is currently used on iOS and the consoles. The newer, open source, version of mono AOT for the various targets is licensed with LGPL and is thus not an upgrade option.

    IL2CPP replaces mono AOT where it was used before and then goes further, thus opening up for the option of upgrading mono as a whole.
     
  17. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    This is not quite correct. Mono is and has always been LGPL. However, some platforms, such as iOS and consoles don't allow complying to the LGPL, because it is not possible for the user to replace a library in a built app (at least, this is a common interpretation of the LGPL, and also Xamarin's interpretation. Some people have different opinions, and this has, afaik, never been tested in the courts, but we are not keen to do so, either).

    So, under that interpretation, for those platforms, we cannot use Mono under an LGPL license. We have a custom license agreement for Mono with Novell to allow us to deploy to those platform. However, we could not reach a similar agreement with Xamarin, which prevents us from updating mono to newer versions released by Xamarin, and we have to stick to the old Novell code.

    If we can target those platforms without requiring mono, we can use mono under the LGPL for other platforms, so then we can update.
     
    JaredThirsk likes this.
  18. tiantian_

    tiantian_

    Joined:
    Mar 6, 2014
    Posts:
    9
    Fully understood now. Best wishes for IL2CPP ^^
     
  19. AM_1

    AM_1

    Joined:
    Nov 18, 2013
    Posts:
    7
    Will loading new classes in C# still be supported after IL2CPP?

    I ask this because my game that I'm working on right now is supposed to teach users how to code in C# (in a fun way, hopefully :) ). The user writes some code, then the game compiles it using a modified version of gmcs and writes the executable to disk. Later, the game loads the code up in a secure sandbox and runs it. I know that its a really small corner case, but I wanted to know if it will be possible in IL2CPP.

    Thanks,
    Arka M.
     
  20. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    Even if it doesn't, you could just run without IL2CPP (sounds like this is a desktop game?)
     
    AM_1 likes this.
  21. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    I don't think that'll be supported by IL2CPP itself, no. I believe the plan is to keep the mono runtime available as an alternative on platforms where that's feasible, though, so if you're making this for the desktop then you should still be fine.

    Of course, if you're able to make your executable be entirely standalone - run it as a completely separate process and have your Unity app communicate with it over pipes or network sockets or something - then that's no problem.
     
    AM_1 likes this.
  22. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    No, IL2CPP will not support loading of code at runtime. It is an Ahead-Of-Time compiler, so we have to know about all of the code at build time. As others mentioned, there are other ways to accomplish your desired behavior though.
     
    AM_1 likes this.
  23. AM_1

    AM_1

    Joined:
    Nov 18, 2013
    Posts:
    7
    Thanks for the info and the ideas, I think I understand better now.
     
  24. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Hey Josh, how far along is IL2CPP in terms of working with Generics and Reflection? I ask because I've been working on getting my JSON .NET asset working with WebGL and so far it is a no-go. I can compile, however there are runtime exceptions. So far I've only been able to serialize and deserialize a basic type (Vector3). I'm using pre-compiler directives and using the exact same code that I use for iOS which works without issue.

    And on that note, since my asset makes heavy use of reflection, would anyone there at Unity be interested in using my asset as a base for some IL2CPP / WebGL testing?
     
  25. AM_1

    AM_1

    Joined:
    Nov 18, 2013
    Posts:
    7
    Also, I have another question just about IL2CPP: will we get to see the C++ code that IL2CPP generates?
     
  26. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Now that IL2CPP is going to be available to 4.x users for iOS builds is there any chance it will be available for PC builds in 4.x?
     
  27. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    No. We are backporting this for iOS due to necessity (Apple's 64-bit requirement), but all future developments will happen on 5.x only.
     
  28. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Is IL2CPP available for x86 on 5 Beta or is it a later feature and will it be available for all platforms?
     
  29. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I believe right now it's available for WebGL only... not sure if the iOS x64 support has made it in or not as I haven't tried it.
     
  30. Ostwind

    Ostwind

    Joined:
    Mar 22, 2011
    Posts:
    2,804
    From some careful reply (don't remember who's) I understood that it will be mainly WebGL, iOS, consoles in 2015 and then maybe? 2016 desktop and others. I think it was low priority for desktops cause of mono upgrade and etc.
     
  31. RyuK

    RyuK

    Joined:
    Feb 12, 2014
    Posts:
    55
    waspswarm likes this.
  32. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    So does this mean we can use the newest version of Mono now once the 64-bit iOS requirement is in place? Forgive me, having a hard time understanding all of this.
     
  33. PhobicGunner

    PhobicGunner

    Joined:
    Jun 28, 2011
    Posts:
    1,813
    I think the idea is that after IL2CPP has been ported to all necessary platforms (iOS, consoles, etc) then supposedly Unity will be able to update Mono without having to pay incredibly high fees to Xamarin (which, from what I can tell, is what's been preventing a Mono upgrade?)
     
    angrypenguin likes this.
  34. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Ah, ok. So then are there no real performance/optimization gains with IL2CPP if it's still the same ol' mono?
     
  35. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There are, since with IL2CPP you're no longer using Mono, but C++ code. That's what IL2CPP means: IL to C++.

    --Eric
     
  36. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    No, but it's part of paving the way for it.
     
  37. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    That's my understanding. In fact, I don't think it even has to be Mono - I expect it'd be anything that outputs valid .NET-compatible IL.
     
    Dustin-Horne likes this.
  38. helios

    helios

    Joined:
    Oct 5, 2009
    Posts:
    308
    Thanks for the responses. This all makes sense now.

    Sorry if this has already been asked, but are there any benchmarks available regarding iOS IL2CPP vs current Mono? Particularly interested in GC.
     
  39. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    I don't think such benchmarks exist yet; but if they do, they're likely to be out of date very quickly, as IL2CPP is under (extremely) active development right now - I know that bugs have been fixed relating to GC performance in the very latest beta, for example.
     
  40. Jarhead

    Jarhead

    Joined:
    Jul 4, 2009
    Posts:
    38
    So I'm assuming this means code like this won't work either:
    Code (CSharp):
    1. var lambda = Expression.Lambda<Func<object>>(body.Arguments[0]);
    2. var resolveArgument = lambda.Compile();
    3. var argumentValue = resolveArgument();
     
  41. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Most likely not. This is also why some linq is not aot friendly today. If it wont work for IOS today without aot errors it probably wont work in il2cpp
     
  42. Jarhead

    Jarhead

    Joined:
    Jul 4, 2009
    Posts:
    38
    Cool, thanks for the response. Hopefully this won't be too limiting with other 3rd part libraries.
     
  43. minicraft

    minicraft

    Joined:
    Jan 24, 2015
    Posts:
    1
    it seems that C# socket is not supported in IL2CPP in Unity 4.6.x? how to fix this problem?
     
  44. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I'm assuming you're talking about IOS and not webgl since you mentioned 4.6. Maybe file a bug report... Not sure why sockets wouldn't be supported, except for webgl where they won't.
     
  45. matmuze

    matmuze

    Joined:
    Jan 14, 2014
    Posts:
    27
    Sorry for the n00bish question guys but it seems that mono already supports LLVM compilation which is basically the intermediate step for compiling C++ to asm.js.

    If the purpose of IL2CPP is to provide HTML5 support, why not simply convert C# to LLVM and then to asm.js instead of going through C++...?

    I understand that mono licensing is a pain in the ass for Unity, however, hiring and group of developers (50 - 100 K per dev and per year) to develop the home made IL2CPP also seems to be a pricey solution in my opinion...

    This is also a very risky since there are few only chances the compilation results with IL2CPP will perform any better than Mono since those guys have been developing it for years already, and furthermore with the help of the .NET foundation it will keep growing stronger and more reliable...

    So would it no be more preferable if Xamarin and Unity3D would try to find a agreement instead quarrelling ?
     
  46. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    As stated in this thread, that is not the only purpose. But, even for that purpose, using Mono's LLVM support would not be an option (licensing aside), as mono's llvm support is not a complete solution for emitting independent standalone code. It sill heavily depends on the mono runtime, and it AFAIK, not all code is handled by the LLVM backend either.

    This is difficult to make estimates on without knowing the terms of mono licensing. Trust us that we can do basic math :)

    il2cpp already outperforms mono significantly in every benchmarks we have seen, both internally and by third parties. Some info can be found in this blog post: http://blogs.unity3d.com/2014/10/07/benchmarking-unity-performance-in-webgl/ , expect us to share more on this later with less of a WebGL focus - but in summary, performance is definitely not a reason to stick with mono.
     
    JaredThirsk likes this.
  47. JaredThirsk

    JaredThirsk

    Joined:
    Jan 17, 2010
    Posts:
    38
    UT employees and moderators, I appreciate your posts here. I just now read all of your posts in this thread, and I feel for you, having to deal with all of the "sky is falling" posts, and the people who say "I think you should abandon C#" (I wonder why such people don't use another engine, and I think C# is one of the strongest points of Unity, but anyway.) Keep up the good fight :)

    You say after IL2CPP, a mono upgrade is on the agenda. One thing I don't understand is how you plan to upgrade mono when Xamarin isn't providing a way to get around the LGPL license*? Can you use an up to date (LGPL?) mono runtime on desktops, and then IL2CPP on iOS/WebGL?

    If so, perhaps I do understand. I have been compiling C# using Visual Studio and .NET (not mono) and bringing in the DLLs (which are just IL) for Unity to use... always seemed to work for me (notwithstanding my iOS AOT nightmares of tracing crashes and assert fails through the pseudo-JIT/AOT/IMT bowels of mono -- which forced me to give up on Unity for a while but I want to start using it again someday).

    So now is it just a matter of making sure I can create DLLs that are AOT-ready for IL2CPP? Will I be able to provide IL2CPP with any .NET DLL (made for .NET 2.0 or some day 4.0/4.5 ), and it will tell me what non-AOT code needs to be stripped out? Or will I have to run it and wait for a crash at runtime when a non-AOT chunk of code is reached?

    * Those jerks! Maybe I should stop adoring them so much as a Xamarin customer (making non-game apps) and bashing you guys for having an old mono. Are they holding out for a big payday or what?? Hoping some competitor of yours hops on the C# bandwagon and cuts a huge deal? I have been crusading against the LGPL everywhere I go, because it is a disaster for multi-platform game development -- I did convince one library author to switch to a more permissive license. (Score!)
     
  48. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    One thing I have not seen asked or discussed is what does the roadmap looks like for having IL2CPP for Android?

    That would be a massive benefit for me personally (sorry everyone else - I'm being selfish here!!! :) )

    Currently I maintain native libraries for each platform we deploy to (for some maths heavy custom physics) and having the ability to have that built as part of the Unity tool chain would be a massive benefit.

    Anyway my question is really just - what is planned for IL2CPP with regards to Android? Can we expect anything soon?

    I'll add that this is excellent news Team Unity! Exactly what we need IMO and I've just tested one of our large projects with IL2CPP for iOS and it worked flawlessly on a large existing project - excellent work guys! Obviously this won't come as a surprise to most given that iOS was already AOT but still excellent progress!
     
    Last edited: Jan 29, 2015
  49. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    More or less, yep. (Way, waaaay easier said than done of course, but I believe that's the overall idea).

    I think, today, the idea is "if it works on Mono AOT then it should work on IL2CPP as well" (barring the occasional bug here and there, though the team are turning around fixes on reported bugs at an insane speed). Beyond that... what do you mean by 'AOT-ready' exactly?
     
  50. JaredThirsk

    JaredThirsk

    Joined:
    Jan 17, 2010
    Posts:
    38
    UT has finally set my mind at ease regarding mono! It has been a while since I watched what was going on in Unity land, but IL2CPP sounds like an ideal approach. I realize now how bogged down you guys must have been with the platform specific problems, and I am glad things will be easier with C++ as a common intermediate compilation target.

    (I will sure be happy when the day arrives with an up to date .NET 4.x+ support and bug free iOS AOT in Unity! Also looking forward to WebGL, and the new open source UI.)

    Well, I had to give up on Unity's AOT on iOS when I tried to port an existing .NET codebase, so I don't really know what is supposed to work and what has been patched since I was using it (3.5.7. I know at least one AOT bug with generic interfaces was finally fixed a few months ago I think). When you mention Mono AOT, are you talking about Unity's older/forked Mono AOT, or Xamarin's current AOT?

    There were 3 ways a dll could blow up on me at runtime with Unity when running in an AOT environment like iOS:

    1) assert fails in the bowels of the mono runtime. g_assert, IMTs, more than I ever wanted to know about how mono works.
    2) wacky behavior like setting IMyObject<MyType> obj = objInstanceThatIsNotAString, with obj.GetType() then telling me it is typeof(string). (Can't remember the exact repro that caused the glitch in the Matrix. "They've changed something.")
    3) Runtime exception: "ExecutionEngineException: Attempting to JIT compile method". This is so much nicer than #1 and #2 that I almost want to give it a hug.

    But nicer still is if the IL2CPP compiler can tell me at compile time whether it will even be possible for my code to throw an ExecutionEngineException, so that I don't have to worry about code coverage tests. (I'm assuming #1 and #2 are bugs that are specific to the mono implementation and I won't have to worry about them with Il2CPP.)

    I wrote a tool that did some analysis of IL (using Cecil) to try to detect some cases of AOT-incompatible IL. (https://github.com/jaredthirsk/AOT-Compatlyzer - I see that some have starred and forked this on github -- cool, but I worry about the fate of these poor souls.) My tool also rewrote some IL to fix some of the issues, such as C# events which were implemented with Interlocked.CompareExchange<T>().

    Anyway, if for some reason IL2CPP didn't detect non-AOT-able code at compile time (although I would think it should at least warn on most or all cases), it might be nice to have an official static analysis tool like this to tell us: "warning: your code might blow up at runtime because of generic virtual method X, or whatever."
     
    movra and thxfoo like this.