No ahead of time (AOT) code was generated

Hi,
I’m currently trying to update from Unity 2019.4 to 2020.2. Editor is running fine, but I face a problem when trying to run the game after the IL2CPP build.

The error I get is as follows:

ExecutionEngineException: Attempting to call method 'System.Linq.Parallel.ForAllOperator`1<ImagineEarth.Logic.FactionSimulation>::WrapPartitionedStream<System.Int32>' for which no ahead of time (AOT) code was generated.
  at System.Linq.Parallel.UnaryQueryOperator`2+UnaryQueryOperatorResults+ChildResultsRecipient[TInput,TOutput].Receive[TKey] (System.Linq.Parallel.PartitionedStream`2[TElement,TKey] inputStream) [0x00000] in <00000000000000000000000000000000>:0
  at System.Linq.Parallel.ScanQueryOperator`1+ScanEnumerableQueryOperatorResults[TElement].GivePartitionedStream (System.Linq.Parallel.IPartitionedStreamRecipient`1[TElement] recipient) [0x00000] in <00000000000000000000000000000000>:0
  at System.Linq.Parallel.UnaryQueryOperator`2+UnaryQueryOperatorResults[TInput,TOutput].GivePartitionedStream (System.Linq.Parallel.IPartitionedStreamRecipient`1[TElement] recipient) [0x00000] in <00000000000000000000000000000000>:0
  at System.Linq.Parallel.QueryOperator`1[TOutput].GetOpenedEnumerator (System.Nullable`1[T] mergeOptions, System.Boolean suppressOrder, System.Boolean forEffect, System.Linq.Parallel.QuerySettings querySettings) [0x00000] in <00000000000000000000000000000000>:0
  at System.Linq.Parallel.ForAllOperator`1[TInput].RunSynchronously () [0x00000] in <00000000000000000000000000000000>:0

It seems not to be possible to deactivate code stripping, as documented here: Unity - Manual: Managed code stripping

So I tried to user a link.xml file, but it doesn’t make a difference and I’m not sure what might be wrong (for now I’ve only tried the brute force approach):

<linker>
  <assembly fullname="mscorlib" preserve="all"/>
  <assembly fullname="netstandard" preserve="all"/>
  <assembly fullname="System" preserve="all"/>
  <assembly fullname="System.Linq" preserve="all"/>
  <assembly fullname="System.Linq.Parallel" preserve="all"/>
  <assembly fullname="System.Linq.Queryable" preserve="all"/>
</linker>

I have tried to put the file in the main Assets folder and also into the Scripts sub-folder. Both doesn’t make a difference.

Any ideas?

The error about “No ahead of time (AOT) code was generated” is unrelated to to managed code stripping. Instead, it means that the code uses some generics with value types that IL2CPP cannot find at compile time, and therefore does not generate code for.

However, did this same project work with 2019.4? If so, this is likely a bug in IL2CPP that we would like to fix immediately.

Yes, it does work with 2019.4. When moving to 2020.2 the only thing I had to change was updating some shaders, so the related code has not changed.

The call that fails is:

dictionary.Values.AsParallel().ForAll(element => element.MethodCall());

Huh, it seems very odd that would fail in a newer version of Unity. Can you submit a bug report? https://unity3d.com/unity/qa/bug-reporting

Done. ID is 1320295

1 Like

Hi! Have the same problem, any ways how to fix this?

We have nothing to report yet, our QA team is still looking at this.

Hi, I have a similar error (only in IL2CPP build) that we can replicate in 2020.3.6 LTS.

The error is not related to System.Linq.Parallel.ForAllOperator but it is related to the use of System.Reactive

The same code worked fine in 2019.4 LTS.

The error I get is:

ExecutionEngineException: Attempting to call method
System.Reactive.Concurrency.CurrentThreadScheduler::Schedule<System.ValueTuple`2[[System.Action`1[[System.Reactive.AutoDetachObserver`1[[System.Collections.Generic.IEnumerable`1[[RebelAlliance.BusinessLogic.IEntity, com.rebelalliance.ss4.businesslogic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Reactive, Version=4.4.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]
for which no ahead of time (AOT) code was generated.

It seems that the issue Unity Issue Tracker - [IL2CPP] ExecutionEngineException error is being spammed in Build when calling Linq.ParallelEnumerable.ForAll has been solved in 2020.3.X but maybe it is a different bug.

This will be corrected in 2020.3.7, which should be released soon.

2 Likes

Hi, maybe you can help me with the next issue

JoshPeterson I’m currently getting: https://forum.unity.com/threads/is-there-any-limitations-to-deserializing-json-on-webgl.1250356/#post-7951618

This was a bug specific to this situation. In general it is still possible to have errors where the AOT code generator does not generate code used at runtime though. I’ll respond on the other thread.

I’m also seeing this issue on a LINQ .AsParallel call on version 2021.3.14f1. Is this expected? I see this IL2CPP was updated in 2022 here: https://blog.unity.com/technology/feature-preview-il2cpp-full-generic-sharing-in-unity-20221-beta

Yes, I would expect that Unity 2021.3 cannot handle LINQ .AsParallel very well with IL2CPP. I would recommend moving to 2022.2 for this feature if you can.

I’m having the same problem I think. I’m doing a UnityWebRequest and it works just fine with the mono backend but when I change it to il2cpp it crashes at the UnityWebRequest. I’m in unity 2021.3.16.1. I tried 2022.1.20 and 2022.2.8 with the same issue.

*FIXED In my case it’s SimpleJSON that doesn’t work with il2cpp. I switched to Newtonsoft and it is working fine.

1 Like

Hello. I have a related issue when I use FluentValidation. Unity 2021.3.25f

ExecutionEngineException: Attempting to call method ‘System.Linq.Expressions.Interpreter.LightLambda::MakeRun1<MyType,System.Int32>’ for which no ahead of time (AOT) code was generated.
at System.Linq.Expressions.Interpreter.LightLambda.MakeDelegate (System.Type delegateType) [0x00000] in <00000000000000000000000000000000>:0
at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate (System.Runtime.CompilerServices.IStrongBox[ ] closure) [0x00000] in <00000000000000000000000000000000>:0
at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate () [0x00000] in <00000000000000000000000000000000>:0
at System.Linq.Expressions.Expression1[TDelegate].Compile (System.Boolean preferInterpretation) [0x00000] in <00000000000000000000000000000000>:0 at System.Linq.Expressions.Expression1[TDelegate].Compile () [0x00000] in <00000000000000000000000000000000>:0

The Linq expression interpreter won’t work too well with Unity 2021.3 and IL2CPP. If you need to use it, I would recommend Unity 2022.2 and later, where IL2CPP can better handle generics that it cannot discover at compile time.

2 Likes

Thanks for the heads up.

Bumped into this issue today (2021.3.27f1) and the issue arises frm LINQing on a valuetuple type. Replace LINQ with foreach loop and issue resolved.

Unfortunately for us it is not feasible to do a major version upgrade. It would be of great help if you can provide more info on this bug. Under what condition would LINQ +Generics cause problem for IL2CPP?

LINQ in general is fine, but the LINQ expression interpreter will lead to problems. Those problems can occur when the expression interpreter uses value types as generic arguments that IL2CPP did not see at compile time. Unfortunately it is often difficult to determine when a problem will occur, because it is not clear from the source code what generic types the LINQ expression interpreter will use.

I think the best option it to avoid using the LINQ expression interpreter.

Hello there! we’re getting this error on our Android build (not happenin’ on editor)!

ScriptingBackend: IL2CPP
UnityVersion: 2021.3.19f1 LTS

07-28 15:21:03.070: E/Unity(12583): ExecutionEngineException: Attempting to call method 'System.Reactive.Concurrency.CurrentThreadScheduler::Schedule<System.ValueTuple2[[System.Action1[[System.ValueTuple2[[System.Reactive.Producer2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Reactive.Linq.ObservableImpl.AsObservable1+_[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Reactive, Version=5.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263]], System.Reactive, Version=5.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263],[System.Reactive.Linq.ObservableImpl.AsObservable1+_[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Reactive, Version=5.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.ValueTuple`2