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

Compiling application with LanguageExt as a dependency and ReadyToRun enabled using .NET 6 #66079

Closed
AndreSteenveld opened this issue Mar 2, 2022 · 8 comments · Fixed by #71426

Comments

@AndreSteenveld
Copy link

Description

When compiling an application with .NET 5 and ReadyToRun enabled a binary is produced, trying to do the same with .NET 6 just "hangs". If a publish profile is used and -p:PublishReadyToRunCrossgen2ExtraArgs='--verbose' is provided it looks like crossgen2 is really busy processing all possible combinations of all possible generic classes. (That is more speculation from my side than anything else though)

Reproduction Steps

I've built a small application which can be found here; AndreSteenveld.CrossgenLanguageExt. Summarizing the readme; Running dotnet publish ./AndreSteenveld.CrossgenLanguageExt5.csproj -p:PublishSingleFile=true -p:PublishReadyToRun=true --configuration Release --runtime win-x64 --self-contained true produces a binary as expected. But doing the same for a .NET 6 project doesn't.

Expected behavior

I'm not expecting any super fast compilation, but nevertheless I do expect it to finish some time.

Actual behavior

Compile doesn't finish, even after several hours.

Regression?

I've read in the release notes that crossgen2 was released with .NET 6, so I'm assuming there is another (older) version of crossgen for .NET 5. So that would be my first suspect.

Known Workarounds

No response

Configuration

Running dotnet --info outputs the following:

.NET SDK (reflecting any global.json):
 Version:   6.0.102
 Commit:    02d5242ed7

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.102\

Host (useful for support):
  Version: 6.0.2
  Commit:  839cdfb0ec

.NET SDKs installed:
  3.1.416 [C:\Program Files\dotnet\sdk]
  5.0.211 [C:\Program Files\dotnet\sdk]
  5.0.404 [C:\Program Files\dotnet\sdk]
  5.0.405 [C:\Program Files\dotnet\sdk]
  6.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

My compile target is also windows, x86-64. I do want to try to reproduce this on linux if possible, although I'm not expecting that it does compile (within a reasonable timeframe) on linux either.

Other information

As this is an issue which seems related to LanguageExt.Core I've also opened a ticket over there. louthy/language-ext#1000 If there is anything else I can do to help please feel free to reach out :)

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Mar 2, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@BrunoJuchli
Copy link
Contributor

BrunoJuchli commented Mar 14, 2022

I ran into the same or at least a similar issue today.
I have an azure-function app (functions v4) where I use LanguageExt.
I ran the following command from a win-x64 machine:

dotnet publish --configuration Release --runtime linux-x64 --no-self-contained -p:PublishReadyToRun=true

After waiting for 3hours for compilation of my (rather simple) functions project to end I was presented with the following error message. Note that while I waited the memory usage crew continuously. Crossgen2 always used at least 1 CPU core to a 100%. I didn't check earlier, but after about 2 hours the disk usage also was consistently at about 60-80% (at that time memory usage was at 22Gib and there basically wasn't any memory left, so I assume disk usage might have been caused by swapping).

Also I tried running this via github action on ubuntu-linux, but I stopped it after 1.5 hours.

....\Microsoft.NET.CrossGen.targets(463,5): error : Error: One or more errors occurred. (Code generation failed for method '[LanguageExt.Core]LanguageExt.TypeClass.biIterAsync<MOptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,OptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>,Unit>(OptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Action`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Action`1)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Either`2<LanguageExt.EitherUnsafe`2<System.__Canon,System.__Canon>,LanguageExt.Unit>>>>>>>>.Concat(IReadOnlyCollection`1<Seq`1<Seq`1<Seq`1<Seq`1<Seq`1<Seq`1<Either`2<EitherUnsafe`2<__Canon,__Canon>,Unit>>>>>>>>)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.SetInternal`2<LanguageExt.ClassInstances.OrdDefault`1<LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.EitherUnsafe`2<LanguageExt.Common.Error,float32>>>,LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.EitherUnsafe`2<LanguageExt.Common.Error,float32>>>.Skip(int32)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Either`2<LanguageExt.Either`2<LanguageExt.Seq`1<LanguageExt.Seq`1>,LanguageExt.Common.Error>,LanguageExt.Common.Error>.Right(Action`1)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Choice.rightAsEnumerable<MEither`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>,Either`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>,Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>(Either`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Pool`2<LanguageExt.ListEnumerator`1+NewStack<LanguageExt.Either`2<LanguageExt.Seq`1<LanguageExt.Common.Error>,LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.Unit>>>,System.__Canon>..cctor()') [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : System.AggregateException: One or more errors occurred. (Code generation failed for method '[LanguageExt.Core]LanguageExt.TypeClass.biIterAsync<MOptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,OptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>,Unit>(OptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Action`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Action`1)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Either`2<LanguageExt.EitherUnsafe`2<System.__Canon,System.__Canon>,LanguageExt.Unit>>>>>>>>.Concat(IReadOnlyCollection`1<Seq`1<Seq`1<Seq`1<Seq`1<Seq`1<Seq`1<Either`2<EitherUnsafe`2<__Canon,__Canon>,Unit>>>>>>>>)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.SetInternal`2<LanguageExt.ClassInstances.OrdDefault`1<LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.EitherUnsafe`2<LanguageExt.Common.Error,float32>>>,LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.EitherUnsafe`2<LanguageExt.Common.Error,float32>>>.Skip(int32)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Either`2<LanguageExt.Either`2<LanguageExt.Seq`1<LanguageExt.Seq`1>,LanguageExt.Common.Error>,LanguageExt.Common.Error>.Right(Action`1)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Choice.rightAsEnumerable<MEither`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>,Either`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>,Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>(Either`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>)') (Code generation failed for method '[LanguageExt.Core]LanguageExt.Pool`2<LanguageExt.ListEnumerator`1+NewStack<LanguageExt.Either`2<LanguageExt.Seq`1<LanguageExt.Common.Error>,LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.Unit>>>,System.__Canon>..cctor()') [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[LanguageExt.Core]LanguageExt.TypeClass.biIterAsync<MOptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,OptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>,Unit>(OptionAsync`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Action`1<Seq`1<Seq`1<Either`2<Aff`1<__Canon>,__Canon>>>>,Action`1)' [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> System.OverflowException: Arithmetic operation resulted in an overflow. [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.Expand(TValue[] oldHashtable) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.AddOrGetExistingInner(TValue value, Boolean& addedValue) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.MethodDesc.InstantiateSignature(Instantiation typeInstantiation, Instantiation methodInstantiation) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.IL.InstantiatedMethodIL.GetObject(Int32 token, NotFoundBehavior notFoundBehavior) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.ResolveTokenInScope(MethodILScope methodIL, Object typeOrMethodContext, mdToken token) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.resolveToken(CORINFO_RESOLVED_TOKEN& pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl._resolveToken(IntPtr thisHandle, IntPtr* ppException, CORINFO_RESOLVED_TOKEN* pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of inner exception stack trace --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, MethodIL methodIL) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger logger) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.b__36_0(DependencyNodeCore`1 dependency) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of stack trace from previous location --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.TaskReplicator.Replica.Execute() [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of inner exception stack trace --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1 action, ParallelOptions options, Boolean stopOnFirstFailure) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of stack trace from previous location --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection exceptions, CancellationToken cancelToken, Exception otherException) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IList`1 list, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.ComputeDependencyNodeDependencies(List`1 obj) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.Compile(String outputFile) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.Program.RunSingleCompilation(Dictionary`2 inFilePaths, InstructionSetSupport instructionSetSupport, String compositeRootPath, Dictionary`2 unrootedInputFilePaths, HashSet`1 versionBubbleModulesHash, CompilerTypeSystemContext typeSystemContext) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.Program.Run(String[] args) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.Program.Main(String[] args) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> (Inner Exception #1) ILCompiler.CodeGenerationFailedException: Code generation failed for method '[LanguageExt.Core]LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Seq`1<LanguageExt.Either`2<LanguageExt.EitherUnsafe`2<System.__Canon,System.__Canon>,LanguageExt.Unit>>>>>>>>.Concat(IReadOnlyCollection`1<Seq`1<Seq`1<Seq`1<Seq`1<Seq`1<Seq`1<Either`2<EitherUnsafe`2<__Canon,__Canon>,Unit>>>>>>>>)' [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> System.OverflowException: Arithmetic operation resulted in an overflow. [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.Expand(TValue[] oldHashtable) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.AddOrGetExistingInner(TValue value, Boolean& addedValue) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.MethodDesc.InstantiateSignature(Instantiation typeInstantiation, Instantiation methodInstantiation) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.IL.InstantiatedMethodIL.GetObject(Int32 token, NotFoundBehavior notFoundBehavior) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.ResolveTokenInScope(MethodILScope methodIL, Object typeOrMethodContext, mdToken token) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.resolveToken(CORINFO_RESOLVED_TOKEN& pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl._resolveToken(IntPtr thisHandle, IntPtr* ppException, CORINFO_RESOLVED_TOKEN* pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of inner exception stack trace --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, MethodIL methodIL) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger logger) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.b__36_0(DependencyNodeCore`1 dependency) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of stack trace from previous location --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.TaskReplicator.Replica.Execute()<--- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> (Inner Exception #2) ILCompiler.CodeGenerationFailedException: Code generation failed for method '[LanguageExt.Core]LanguageExt.SetInternal`2<LanguageExt.ClassInstances.OrdDefault`1<LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.EitherUnsafe`2<LanguageExt.Common.Error,float32>>>,LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.EitherUnsafe`2<LanguageExt.Common.Error,float32>>>.Skip(int32)' [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> System.OverflowException: Arithmetic operation resulted in an overflow. [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.Expand(TValue[] oldHashtable) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.AddOrGetExistingInner(TValue value, Boolean& addedValue) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.MethodDesc.InstantiateSignature(Instantiation typeInstantiation, Instantiation methodInstantiation) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.IL.InstantiatedMethodIL.GetObject(Int32 token, NotFoundBehavior notFoundBehavior) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.ResolveTokenInScope(MethodILScope methodIL, Object typeOrMethodContext, mdToken token) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.resolveToken(CORINFO_RESOLVED_TOKEN& pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl._resolveToken(IntPtr thisHandle, IntPtr* ppException, CORINFO_RESOLVED_TOKEN* pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of inner exception stack trace --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, MethodIL methodIL) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger logger) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.b__36_0(DependencyNodeCore`1 dependency) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of stack trace from previous location --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.TaskReplicator.Replica.Execute()<--- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> (Inner Exception #3) ILCompiler.CodeGenerationFailedException: Code generation failed for method '[LanguageExt.Core]LanguageExt.Either`2<LanguageExt.Either`2<LanguageExt.Seq`1<LanguageExt.Seq`1>,LanguageExt.Common.Error>,LanguageExt.Common.Error>.Right(Action`1)' [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> System.OverflowException: Arithmetic operation resulted in an overflow. [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.Expand(TValue[] oldHashtable) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.AddOrGetExistingInner(TValue value, Boolean& addedValue) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.MethodDesc.InstantiateSignature(Instantiation typeInstantiation, Instantiation methodInstantiation) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.IL.InstantiatedMethodIL.GetObject(Int32 token, NotFoundBehavior notFoundBehavior) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.ResolveTokenInScope(MethodILScope methodIL, Object typeOrMethodContext, mdToken token) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.resolveToken(CORINFO_RESOLVED_TOKEN& pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl._resolveToken(IntPtr thisHandle, IntPtr* ppException, CORINFO_RESOLVED_TOKEN* pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of inner exception stack trace --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, MethodIL methodIL) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger logger) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.b__36_0(DependencyNodeCore`1 dependency) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of stack trace from previous location --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.TaskReplicator.Replica.Execute()<--- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> (Inner Exception #4) ILCompiler.CodeGenerationFailedException: Code generation failed for method '[LanguageExt.Core]LanguageExt.Choice.rightAsEnumerable<MEither`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>,Either`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>,Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>(Either`2<Fin`1<Identity`1<__Canon>>,Seq`1<Seq`1<Seq`1>>>)' [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> System.OverflowException: Arithmetic operation resulted in an overflow. [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.Expand(TValue[] oldHashtable) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.AddOrGetExistingInner(TValue value, Boolean& addedValue) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.MethodDesc.InstantiateSignature(Instantiation typeInstantiation, Instantiation methodInstantiation) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.ResolveTokenWithSubstitution(MethodILScope methodIL, mdToken token, Instantiation typeInst, Instantiation methodInst) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.GetRuntimeDeterminedObjectForToken(CORINFO_RESOLVED_TOKEN& pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.getReadyToRunHelper(CORINFO_RESOLVED_TOKEN& pResolvedToken, CORINFO_LOOKUP_KIND& pGenericLookupKind, CorInfoHelpFunc id, CORINFO_CONST_LOOKUP& pLookup) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl._getReadyToRunHelper(IntPtr thisHandle, IntPtr* ppException, CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_LOOKUP_KIND* pGenericLookupKind, CorInfoHelpFunc id, CORINFO_CONST_LOOKUP* pLookup) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of inner exception stack trace --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, MethodIL methodIL) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger logger) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.b__36_0(DependencyNodeCore`1 dependency) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of stack trace from previous location --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.TaskReplicator.Replica.Execute()<--- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> (Inner Exception #5) ILCompiler.CodeGenerationFailedException: Code generation failed for method '[LanguageExt.Core]LanguageExt.Pool`2<LanguageExt.ListEnumerator`1+NewStack<LanguageExt.Either`2<LanguageExt.Seq`1<LanguageExt.Common.Error>,LanguageExt.Either`2<LanguageExt.Common.Error,LanguageExt.Unit>>>,System.__Canon>..cctor()' [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : ---> System.OverflowException: Arithmetic operation resulted in an overflow. [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.Expand(TValue[] oldHashtable) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.AddOrGetExistingInner(TValue value, Boolean& addedValue) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey key) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.TypeSystem.MethodDesc.GetSharedRuntimeFormMethodTarget() [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.GetRuntimeDeterminedObjectForToken(CORINFO_RESOLVED_TOKEN& pResolvedToken) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.getReadyToRunHelper(CORINFO_RESOLVED_TOKEN& pResolvedToken, CORINFO_LOOKUP_KIND& pGenericLookupKind, CorInfoHelpFunc id, CORINFO_CONST_LOOKUP& pLookup) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl._getReadyToRunHelper(IntPtr thisHandle, IntPtr* ppException, CORINFO_RESOLVED_TOKEN* pResolvedToken, CORINFO_LOOKUP_KIND* pGenericLookupKind, CorInfoHelpFunc id, CORINFO_CONST_LOOKUP* pLookup) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of inner exception stack trace --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode methodCodeNodeNeedingCode, MethodIL methodIL) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger logger) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at ILCompiler.ReadyToRunCodegenCompilation.b__36_0(DependencyNodeCore`1 dependency) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : --- End of stack trace from previous location --- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : at System.Threading.Tasks.TaskReplicator.Replica.Execute()<--- [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(463,5): error : [<..ProjectDir..>\Functions.csproj]
....\Microsoft.NET.CrossGen.targets(351,5): error NETSDK1096: Optimizing assemblies for performance failed. You can either exclude the failing assemblies from being optimized, or set the PublishReadyToRun property to false. [<..ProjectDir..>\Functions.csproj]

Note: '....\Microsoft.NET.CrossGen.targets' originally was 'C:\Program Files\dotnet\sdk\6.0.200\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.CrossGen.targets'

@BrunoJuchli
Copy link
Contributor

BrunoJuchli commented Mar 14, 2022

As recommended by the NETSDK1096 error I tried disabling optimization for LanguageExt.Core.dll by adding the following to the *.csproj:

<ItemGroup>
  <PublishReadyToRunExclude Include="LanguageExt.Core.dll" />
</ItemGroup>

As a result, the build now seems to hang indefinitely. Crossgen2 is not running, cpu is idle, memory usage remains low.
This is how the console looks:
image

Using Ctrl+C to cancel results in the following:

Microsoft.NET.Sdk.Functions.Publish.targets(148,5): warning MSB4220: Waiting for the currently executing task "GenerateFunctions" to cancel.

Which just gets repeated, for ever.
Also, I've tried rebooting, then performing dotnet clean followed by publish again. Same result.

@AndreSteenveld
Copy link
Author

Trimming LanguageExt allows it to build to native, which seems like a perfectly fine workaround. I've read in the .NET7 beta release notes that it is an essential step for compiling to native so I guess it's fine. I still don't know the root cause though.

I've updated my reproduction case, in summary:

<!-- I've added this section to the `AndreSteenveld.CrossgrenLangueageExt6.csproj` file, it was taken from: https://github.com/louthy/language-ext/issues/673#issuecomment-778358899 -->
<Target Name="ConfigureTrimming" BeforeTargets="PrepareForILLink">
  <ItemGroup>
    <ManagedAssemblyToLink Condition="'%(Filename)' == 'LanguageExt.Core'">
      <IsTrimmable>true</IsTrimmable>
      <TrimMode>link</TrimMode>
    </ManagedAssemblyToLink>
  </ItemGroup>
</Target>
$ time dotnet publish ./AndreSteenveld.CrossgenLanguageExt6.csproj  \
    --configuration Release                                         \
    --runtime win-x64                                               \
    --self-contained true                                           \
    -p:PublishSingleFile=true                                       \
    -p:PublishReadyToRun=true                                       \
    -p:PublishReadyToRunShowWarnings=true                           \
    -p:PublishTrimmed=true
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored C:\Users\asteenveld\source\repos\AndreSteenveld.CrossgenLanguageExt\AndreSteenveld.CrossgenLanguageExt6.csproj (in 15.47 sec).
  AndreSteenveld.CrossgenLanguageExt6 -> C:\Users\asteenveld\source\repos\AndreSteenveld.CrossgenLanguageExt\bin\Release\net6.0\win-x64\AndreSteenveld.CrossgenLanguageExt6.dll 
    C:\Users\asteenveld\.nuget\packages\languageext.core\4.0.3\lib\netstandard2.0\LanguageExt.Core.dll : warning IL2104: Assembly 'LanguageExt.Core' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [C:\Users\asteenveld\source\repos\AndreSteenveld.CrossgenLanguageExt\AndreSteenveld.CrossgenLanguageExt6.csproj]
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  AndreSteenveld.CrossgenLanguageExt6 -> C:\Users\asteenveld\source\repos\AndreSteenveld.CrossgenLanguageExt\bin\Release\net6.0\win-x64\publish\

real   	1m35.580s
user   	0m0.000s
sys    	0m0.093s

@normj
Copy link

normj commented May 5, 2022

While debugging this issue for somebody trying to run this package with ReadyToRun on AWS I noticed that when you can get the crossgen2 compiler to complete it turns the 11 Meg LanguageExt.Core.dll into a 270+ Meg dll.

This library uses a lot of generics and Reflection.Emit over generic types. I wonder if the crossgen2 is getting into some overaggressive mode trying to generate ReadyToRun code for all of the possible types.

@mangod9 mangod9 removed the untriaged New issue has not been triaged by the area owner label May 6, 2022
@mangod9 mangod9 added this to the 7.0.0 milestone May 6, 2022
@trylek
Copy link
Member

trylek commented May 6, 2022

I have managed to repro this locally. The problem is indeed caused by generic explosion, the proximate bug the debug compiler build hits is an integer overflow when resizing the generic unification hashtable. As the next experimental step I'm thinking about introducing a heuristic "generic complexity" and using it to trim too complex instantiations; I'm also looking into ways to somehow record the increasing generic complexity as part of the dependency analysis as a tool for understanding what triggers the infinite instantiation loop.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 29, 2022
@mangod9
Copy link
Member

mangod9 commented Aug 25, 2022

Moving to 8 due to the risk associated with the fix.

@mangod9 mangod9 modified the milestones: 7.0.0, 8.0.0 Aug 25, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 7, 2022
@trylek trylek mentioned this issue May 4, 2023
46 tasks
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 19, 2023
trylek added a commit to trylek/runtime that referenced this issue Jun 9, 2023
This change modifies Crossgen2 to use the generic cycle detector
originally implemented for NativeAOT to trim infinite generic
expansion as observed in the LanguageExt public nuget package
and tracked by the issue

dotnet#66079

At this point I have only implemented a back-compat option to
opt out of the generic cycle detection in case it turns out to
cause problems for certain payloads. I haven't added the option
to set the cutoff level - in practice, LanguageExt compilation
still fails when the cutoff is set to anything higher than 0;
moreover for Crossgen2 skipping precompilation of certain methods
merely incurs slightly reduced performance, not runtime failures
as is the case with NativeAOT.

Thanks

Tomas

Rebase against latest main

Temporary instrumentation to track dependency level

Improve dependency level instrumentation

Instrumentation for dumping dropped cycles

Add more places for cycle detection

More instrumentation

Log generic cycles for each module
trylek added a commit to trylek/runtime that referenced this issue Jun 9, 2023
So I have a trivial depth cutoff test working; without enabling
generic cycle detection Crossgen2 crashes after about an hour on my
box with the Arithmetic Overflow as described in the issue dotnet#66079.
As next step I'll work on the equivalent breadth test, that is
somewhat more tricky.

Thanks

Tomas
trylek added a commit that referenced this issue Jun 13, 2023
This change modifies Crossgen2 to use the generic cycle detector
originally implemented for NativeAOT to trim infinite generic
expansion as observed in the LanguageExt public nuget package
and tracked by the issue

#66079

For now the generic cycle detector is opt-in as it's relatively costly
in terms of compiler performance and it seems to be a relatively
niche case. We can follow up by optimizing the detector if it turns
out to be more prevalent or if we make the call to turn it on by
default, I have mentioned several options for that in the PR.

Thanks

Tomas
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 13, 2023
@trylek
Copy link
Member

trylek commented Jun 13, 2023

FYI, I have merged in Crossgen2 support for handling this situation cleanly. One caveat is that for now we decided to make the new switch an opt-in because this issue is the first one we discovered hitting this problem and the implementation makes the compilation slower. It should ship as part of .NET 8 Preview 6 and it would be naturally great if @AndreSteenveld and / or some of the other contributors to this thread could verify that it fixes their issues. The relevant option that needs to be put in the project file is as follows:

<PublishReadyToRunCrossgen2ExtraArgs>--enable-generic-cycle-detection</PublishReadyToRunCrossgen2ExtraArgs>

Thanks in advance!

P.S. The change implements two additional switches that control the internal heuristic, maxgenericcycle and maxgenericcyclebreadth but these shouldn't be needed for compiling LanguageExt.Core.

trylek added a commit to trylek/runtime that referenced this issue Jun 13, 2023
As part of investigation of the bug dotnet#66079 and implementation of
the fix dotnet#71426 I noticed that one aggravating factor is that
Crossgen2 starts analyzing the same assembly multiple times on
its various parallel threads; this busywork additionally makes the
app compete for access to the same metadata, making the initial
analysis even slower.

In accordance with Michal's suggestion from the PR thread dotnet#71426
I propose to modify the generic cycle detector to run the initial
analysis single-threaded if the module in question is expected
to be "generics-heavy" using the number of TypeSpec rows in its
ECMA metadata as an indicator of module generic complexity.

I have written a simple managed app scanning all runtime framework
assemblies, ASP.NET assemblies and assemblies used in internal
CoreCLR testing. I found out that the largest number of TypeSpec
rows is in FSharp.Core (3855), followed by Microsoft.CodeAnalysis
(3148). Based on these findings I have set the initial value of
the cutoff for single-threaded analysis to 5000.

Thanks

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

Successfully merging a pull request may close this issue.

6 participants