diff --git a/src/coreclr/jit/optimizer.cpp b/src/coreclr/jit/optimizer.cpp index 57a62af9c742f3..0e4173d4d42400 100644 --- a/src/coreclr/jit/optimizer.cpp +++ b/src/coreclr/jit/optimizer.cpp @@ -4812,6 +4812,10 @@ void Compiler::optHoistLoopBlocks(FlowGraphNaturalLoop* loop, { m_canHoistSideEffects = false; } + else if ((call->gtCallMoreFlags & GTF_CALL_M_ALLOC_SIDE_EFFECTS) != 0) + { + m_canHoistSideEffects = false; + } else if (s_helperCallProperties.MayRunCctor(helpFunc) && (call->gtFlags & GTF_CALL_HOISTABLE) == 0) { diff --git a/src/tests/JIT/Regression_ro_2/Runtime_134936.cs b/src/tests/JIT/Regression_ro_2/Runtime_134936.cs new file mode 100644 index 00000000000000..6a9e44b7ffb07c --- /dev/null +++ b/src/tests/JIT/Regression_ro_2/Runtime_134936.cs @@ -0,0 +1,49 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.CompilerServices; +using Xunit; + +public class Runtime_134936 +{ + private class Finalizable + { + public static int Count; + + ~Finalizable() + { + Count++; + } + } + + [Fact] + [SkipOnMono("Requires precise GC")] + public static void TestEntryPoint() + { + try + { + Test(1, 0, 10); + } + catch (DivideByZeroException) + { + } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + Assert.Equal(1, Finalizable.Count); + } + + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] + private static int Test(int a, int b, int n) + { + int sum = 0; + for (int i = 0; i < n; i++) + { + new Finalizable(); + sum += a / b; + } + + return sum; + } +}