Skip to content

JIT compiler bug: Incorrect caching of loop variable #13328

Closed
dotnet/coreclr
#26952

Description

@Grimfane

Environment:

•Visual Studio 2019 version 16.2.3
•.Net 4.8
•Console application
•x64 build
•Release build

Code to reproduce:

using System;

namespace Demo
    {
        class Program
        {
            static int _a;

            static void Main()
            {
                _a = 1;

                while (_a == 1)
                {
                    Console.WriteLine(_a);
                    _a = 0;
                }
            }
        }
    }

Expected Behavior:
"1" is printed once and the program terminates.

Actual Behavior:
"0" is repeatedly printed; the program does not terminate.

Notes
If you declare _a as volatile, it works OK.
If you initialise _a as static int _a = 0; it works.

This was reported on StackOverflow here: https://stackoverflow.com/questions/57693386/vs-2019-optimize-code-in-release-mode-broken

category:correctness
theme:optimization
skill-level:expert
cost:large

Activity

  1. svick commented on Aug 28, 2019

    @svick
    Member

    I can reproduce this on .Net Core 3.0, so I think this issue should be moved to dotnet/coreclr. Roslyn is the C# compiler, and so JIT bugs don't belong here.

  2. transferred this issue fromdotnet/roslynon Aug 28, 2019
  3. stephentoub commented on Aug 28, 2019

    @stephentoub
    Member
  4. AndyAyersMS commented on Aug 28, 2019

    @AndyAyersMS
    Member

    @BruceForstall can you follow up?

  5. mikedn commented on Aug 28, 2019

    @mikedn
    Contributor

    I'm taking a look, this seems to be a bug in loop hoisting and I just fixed a different bug in it.

  6. mikedn commented on Aug 28, 2019

    @mikedn
    Contributor

    That's interesting, VN figured out correctly that _a in while (_a == 1) is always 0.

    Loop hoisting considers trees having constant VN to be invariant and decides to hoist _a (the entire comparison is of course constant but it doesn't make sense to hoist it). By now things have already gone south because illegal code motion has already occurred.

    Now one may think that since the VN is constant the involved trees would end being substituted with actual constant nodes and the illegal code motion would be hidden. But only the liberal VN is constant, the conservative one is not.

    Not clear what a good fix for this would be. It may be that loop hoisting needs to consider the conservative VN as well if it wants to hoist a constant, otherwise what it tries to hoist isn't really a constant.

    Dump showing _a being hoisted into the pre-header for CSE to pick up:

    ***** BB01, stmt 1
    N003 (  5,  6)              [000002] -A--G-------              *  ASG       int    $41
    N001 (  3,  4)              [000001] ----G--N----              +--*  CLS_VAR   int    Hnd=0x925ab4d0 Fseq[_a] $100
    N002 (  1,  1)              [000000] ------------              \--*  CNS_INT   int    1 $41
    
    ***** BB01, stmt 2
    N004 (  7,  8)              [000019] ----G-------              *  JTRUE     void  
    N003 (  5,  6)              [000016] J---G--N----              \--*  NE        int    <l:$42, c:$180>
    N001 (  3,  4)              [000017] ----G-------                 +--*  CLS_VAR   int    Hnd=0x925ab4d0 Fseq[_a] <l:$41, c:$140>
    N002 (  1,  1)              [000018] ------------                 \--*  CNS_INT   int    1 $41
    
    ------------ BB04 [008..???), preds={BB01} succs={BB02}
    
    ***** BB04, stmt 3
    N003 (  3,  4)              [000022] ----G-------              *  COMMA     void  
    N001 (  3,  4) CSE dotnet/coreclr#1 (def)[000020] ----G--H----              +--*  CLS_VAR   int    Hnd=0x925ab4d0 Fseq[_a] <l:$42, c:$202>
    N002 (  0,  0)              [000021] ------------              \--*  NOP       void  
    
    ------------ BB02 [008..020) -> BB02 (cond), preds={BB02,BB04} succs={BB03,BB02}
    
    ***** BB02, stmt 4
    N005 ( 17, 10)              [000008] --CXG-------              *  CALL      void   System.Console.WriteLine $VN.Void
    N003 (  3,  4)              [000007] ----G------- arg0 in rcx  \--*  CLS_VAR   int    Hnd=0x925ab4d0 Fseq[_a] <l:$181, c:$201>
    
    ***** BB02, stmt 5
    N003 (  5,  6)              [000012] -A--G-------              *  ASG       int    $42
    N001 (  3,  4)              [000011] ----G--N----              +--*  CLS_VAR   int    Hnd=0x925ab4d0 Fseq[_a] $101
    N002 (  1,  1)              [000010] ------------              \--*  CNS_INT   int    0 $42
    
    ***** BB02, stmt 6
    N004 (  7,  8)              [000006] ----G-------              *  JTRUE     void  
    N003 (  5,  6)              [000005] J---G--N----              \--*  EQ        int    <l:$42, c:$182>
    N001 (  3,  4) CSE dotnet/coreclr#1 (use)[000003] ----G-------                 +--*  CLS_VAR   int    Hnd=0x925ab4d0 Fseq[_a] <l:$42, c:$202>
    N002 (  1,  1)              [000004] ------------                 \--*  CNS_INT   int    1 $41
    
  7. BruceForstall commented on Aug 28, 2019

    @BruceForstall
    Contributor
  8. briansull commented on Aug 28, 2019

    @briansull
    Contributor

    Taking a look

  9. BruceForstall commented on Sep 4, 2019

    @BruceForstall
    Contributor

    I'm moving this to 3.1 for now.

  10. maythamfahmi commented on Sep 6, 2019

    @maythamfahmi

    I have this issue on vs 2019 community 16.2.4, target framework .net core 2.2

  11. briansull commented on Sep 6, 2019

    @briansull
    Contributor

    @maythamfahmi
    Did you hit this issue with you own real world code?
    We are aware that this specific test case fails on older version of .net CoreCLR such as 2.2

  12. maythamfahmi commented on Sep 12, 2019

    @maythamfahmi

    @briansull no that was not real world so far, it is just me that was not a ware of it. Thank you for beeing active on it. :)

  13. transferred this issue fromdotnet/coreclron Jan 31, 2020
  14. added this to the 3.1 milestone on Jan 31, 2020
  15. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrbug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions