summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Regression/JitBlue/GitHub_10780/GitHub_10780.cs
blob: 7b69838a3ac4a7ed5f6bd60b6de4885884b5b8de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Repro case for a bug involving hoisting of static field loads out of
// loops and (illegally) above the corresponding type initializer calls.

using System.Runtime.CompilerServices;

namespace N
{
    public struct Pair
    {
        public int Left;
        public int Right;

        public static Pair TenFour = new Pair() { Left = 10, Right = 4 };
    }

    static class C
    {
        static int Sum;
        static int Two;

        // Bug repro requires a use of a Pair value; this is a small fn that takes
        // a Pair by value to serve as that use.  Inline it aggressively so that
        // we won't think the call might kill the static field.
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static void Accumulate(Pair pair)
        {
            Sum += pair.Left + pair.Right;
        }


        [MethodImpl(MethodImplOptions.NoInlining)]
        static void SumNFourteens(int n)
        {
            for (int i = 0; i < n; ++i)
            {
                Two = 2; // Store to C.Two here is a global side-effect above which we won't hoist the static initializer (since it may throw).
                Accumulate(Pair.TenFour);  // Hoisting the load of Pair.TenFour above the static init call is incorrect.
            }
        }

        public static int Main(string[] args)
        {
            Sum = 0;
            SumNFourteens(7);  // Now Sum = 14 * 7 = 98 (and Two = 2)
            return Sum + Two;  // 98 + 2 = 100 on success
        }
    }
}