Coder Perfect

When running the application, the.NET 3.5 JIT does not operate.

Problem

When running the release inside Visual Studio and outside Visual Studio, the following code produces various results. I’m working with Visual Studio 2008 and.NET 3.5. I’ve also tried.NET 3.5 Service Pack 1 (SP1).

The JIT should kick in when operating outside of Visual Studio. Either (a) there’s something subtle going on with C# that I’m overlooking, or (b) the JIT is broken. I’m not optimistic that the JIT will fail, but I’m running out of options…

When using Visual Studio, the following is the output:

    0 0,
    0 1,
    1 0,
    1 1,

When you run release outside of Visual Studio, you’ll get the following results:

    0 2,
    0 2,
    1 2,
    1 2,

What is the explanation for this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
    struct IntVec
    {
        public int x;
        public int y;
    }

    interface IDoSomething
    {
        void Do(IntVec o);
    }

    class DoSomething : IDoSomething
    {
        public void Do(IntVec o)
        {
            Console.WriteLine(o.x.ToString() + " " + o.y.ToString()+",");
        }
    }

    class Program
    {
        static void Test(IDoSomething oDoesSomething)
        {
            IntVec oVec = new IntVec();
            for (oVec.x = 0; oVec.x < 2; oVec.x++)
            {
                for (oVec.y = 0; oVec.y < 2; oVec.y++)
                {
                    oDoesSomething.Do(oVec);
                }
            }
        }

        static void Main(string[] args)
        {
            Test(new DoSomething());
            Console.ReadLine();
        }
    }
}

Asked by Philip Welch

Solution #1

It’s a bug in the JIT optimizer. It unrolls the inner loop but fails to update the oVec.y value:

      for (oVec.x = 0; oVec.x < 2; oVec.x++) {
0000000a  xor         esi,esi                         ; oVec.x = 0
        for (oVec.y = 0; oVec.y < 2; oVec.y++) {
0000000c  mov         edi,2                           ; oVec.y = 2, WRONG!
          oDoesSomething.Do(oVec);
00000011  push        edi  
00000012  push        esi  
00000013  mov         ecx,ebx 
00000015  call        dword ptr ds:[00170210h]        ; first unrolled call
0000001b  push        edi                             ; WRONG! does not increment oVec.y
0000001c  push        esi  
0000001d  mov         ecx,ebx 
0000001f  call        dword ptr ds:[00170210h]        ; second unrolled call
      for (oVec.x = 0; oVec.x < 2; oVec.x++) {
00000025  inc         esi  
00000026  cmp         esi,2 
00000029  jl          0000000C 

When you set oVec.y to 4, the problem goes away because there are too many calls to unroll.

Here’s an example of a workaround:

  for (int x = 0; x < 2; x++) {
    for (int y = 0; y < 2; y++) {
      oDoesSomething.Do(new IntVec(x, y));
    }
  }

UPDATE: This error was resolved in the version 4.0.30319 jitter, which was re-checked in August 2012. However, the jitter in v2.0.50727 is still visible. After all this time, it appears improbable that they will repair this in the old version.

Answered by Hans Passant

Solution #2

This appears to be a true JIT compilation problem. I’d file a report with Microsoft and see what they have to say. Surprisingly, I discovered that the x64 JIT does not suffer from the same issue.

This is my interpretation of the x86 JIT.

// save context
00000000  push        ebp  
00000001  mov         ebp,esp 
00000003  push        edi  
00000004  push        esi  
00000005  push        ebx  

// put oDoesSomething pointer in ebx
00000006  mov         ebx,ecx 

// zero out edi, this will store oVec.y
00000008  xor         edi,edi 

// zero out esi, this will store oVec.x
0000000a  xor         esi,esi 

// NOTE: the inner loop is unrolled here.
// set oVec.y to 2
0000000c  mov         edi,2 

// call oDoesSomething.Do(oVec) -- y is always 2!?!
00000011  push        edi  
00000012  push        esi  
00000013  mov         ecx,ebx 
00000015  call        dword ptr ds:[002F0010h] 

// call oDoesSomething.Do(oVec) -- y is always 2?!?!
0000001b  push        edi  
0000001c  push        esi  
0000001d  mov         ecx,ebx 
0000001f  call        dword ptr ds:[002F0010h] 

// increment oVec.x
00000025  inc         esi  

// loop back to 0000000C if oVec.x < 2
00000026  cmp         esi,2 
00000029  jl          0000000C 

// restore context and return
0000002b  pop         ebx  
0000002c  pop         esi  
0000002d  pop         edi  
0000002e  pop         ebp  
0000002f  ret     

To me, this appears to be a failed optimization…

Answered by Nick Guerrera

Solution #3

I took your code and pasted it into a new Console App.

So it’s the x86 JIT that’s creating the code wrong. I’ve removed my earlier text concerning loop reordering and the like. A few other answers on here have confirmed that the JIT is unwinding the loop incorrectly when on x86.

To fix the problem, modify IntVec’s declaration to a class, which works in all flavors.

I believe this should be posted on MS Connect….

-1 to Microsoft!

Answered by Andras Zoltan

Post is based on https://stackoverflow.com/questions/2056948/net-3-5-jit-not-working-when-running-the-application