You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
UnreachableBlocksOptimizer hits NotSupportedException when compiling crossgen2 in runtime build #2872
Fatal error in IL Linker
Unhandled exception. System.NotImplementedException: switch
at Mono.Linker.Steps.UnreachableBlocksOptimizer.BodyReducer.IsConstantBranch(OpCode opCode, Int32 operand)
at Mono.Linker.Steps.UnreachableBlocksOptimizer.BodyReducer.RemoveConditions()
at Mono.Linker.Steps.UnreachableBlocksOptimizer.BodyReducer.RewriteBody()
at Mono.Linker.Steps.UnreachableBlocksOptimizer.ProcessMethod(MethodDefinition method)
at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method, DependencyInfo& reason, MessageOrigin& origin)
at Mono.Linker.Steps.MarkStep.ProcessQueue()
at Mono.Linker.Steps.MarkStep.ProcessPrimaryQueue()
at Mono.Linker.Steps.MarkStep.Process()
at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
at Mono.Linker.Pipeline.Process(LinkContext context)
at Mono.Linker.Driver.Run(ILogger customLogger)
at Mono.Linker.Driver.Main(String[] args)
It looks like the problematic method might be ComputeDefaultOptions which looks like
To replicate, commit is agocke/runtime@e564c27, then ./build.sh clr+libs+packs.product, on Mac (which is where trimming happens by default, this could be enabled on other platforms with some modifications to the crossgen2 project file)
Note about the repro:
There's a bug in linker where if the first thing method does is a switch the const. prop code will not recognize it as optimizable branch instruction. So the test adds a dummy call before it. We might as well fix it:
The bug is essentially the fact that the const. prop doesn't have code to optimize switch instructions. It recognizes them as branch instructions, so it tries to optimize them, but when it actually works on it, it doesn't know what to do.
Note that this is only hittable in Release built input, in Debug builds Roslyn produces a pattern around switch which linker doesn't recognize currently.
Workaround would be to simply skip over switch instruction - don't support optimizing them for now.
Actually optimizing switch instructions is obviously doable, but requires much more work. Some of the patterns which hit this would benefit from such support though (the one above doesn't really, but there are others hit in runtime repo).
Activity
agocke commentedon Jun 30, 2022
To replicate, commit is agocke/runtime@e564c27, then
./build.sh clr+libs+packs.product, on Mac (which is where trimming happens by default, this could be enabled on other platforms with some modifications to the crossgen2 project file)agocke commentedon Jul 1, 2022
@marek-safar Any ideas here?
sbomer commentedon Jul 8, 2022
This is being hit in dotnet/runtime#71378
vitek-karas commentedon Jul 11, 2022
Added a test which reproes this in #2887
Note about the repro:
There's a bug in linker where if the first thing method does is a switch the const. prop code will not recognize it as optimizable branch instruction. So the test adds a dummy call before it. We might as well fix it:
linker/src/linker/Linker.Steps/UnreachableBlocksOptimizer.cs
Line 895 in 4c95f11
Should be
i >= 3.vitek-karas commentedon Jul 11, 2022
The bug is essentially the fact that the const. prop doesn't have code to optimize
switchinstructions. It recognizes them as branch instructions, so it tries to optimize them, but when it actually works on it, it doesn't know what to do.Note that this is only hittable in Release built input, in Debug builds Roslyn produces a pattern around switch which linker doesn't recognize currently.
vitek-karas commentedon Jul 11, 2022
Workaround would be to simply skip over
switchinstruction - don't support optimizing them for now.Actually optimizing
switchinstructions is obviously doable, but requires much more work. Some of the patterns which hit this would benefit from such support though (the one above doesn't really, but there are others hit in runtime repo).vitek-karas commentedon Jul 12, 2022
#2889 has a "fix" - basically intentionally disable any support for switch instruction block removal to avoid the exception.
vitek-karas commentedon Jul 12, 2022
#2889 should fix this issue - that is should make the linker NOT crash.