运行应用程序时.NET 3.5 JIT 不工作
以下代码在 Visual Studio 内部运行该版本和在 Visual Studio 外部运行该版本时提供不同的输出。我正在使用 Visual Studio 2008 并面向 .NET 3.5。我也尝试过.NET 3.5 SP1。
在 Visual Studio 外部运行时,JIT 应该启动。要么 (a) C# 发生了一些我忽略的微妙事情,要么 (b) JIT 实际上有错误。我怀疑 JIT 可能会出错,但我已经没有其他可能性了...
在 Visual Studio 内部运行时的输出:
0 0,
0 1,
1 0,
1 1,
在 Visual Studio 外部运行发布时的输出:
0 2,
0 2,
1 2,
1 2,
原因是什么?
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();
}
}
}
The following code gives different output when running the release inside Visual Studio, and running the release outside Visual Studio. I'm using Visual Studio 2008 and targeting .NET 3.5. I've also tried .NET 3.5 SP1.
When running outside Visual Studio, the JIT should kick in. Either (a) there's something subtle going on with C# that I'm missing or (b) the JIT is actually in error. I'm doubtful that the JIT can go wrong, but I'm running out of other possiblities...
Output when running inside Visual Studio:
0 0,
0 1,
1 0,
1 1,
Output when running release outside of Visual Studio:
0 2,
0 2,
1 2,
1 2,
What is the reason?
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个 JIT 优化器错误。它正在展开内部循环,但没有正确更新 oVec.y 值:
当您让 oVec.y 递增到 4 时,该错误就会消失,因为需要展开的调用太多。
一种解决方法是这样的:
更新:2012年8月重新检查,这个bug在4.0.30319抖动版本中修复。但在v2.0.50727中仍然存在抖动。经过这么长时间,他们似乎不太可能在旧版本中解决这个问题。
It is a JIT optimizer bug. It is unrolling the inner loop but not updating the oVec.y value properly:
The bug disappears when you let oVec.y increment to 4, that's too many calls to unroll.
One workaround is this:
UPDATE: re-checked in August 2012, this bug was fixed in the version 4.0.30319 jitter. But is still present in the v2.0.50727 jitter. It seems unlikely they'll fix this in the old version after this long.
我相信这是一个真正的 JIT 编译错误。我会将其报告给微软,看看他们怎么说。有趣的是,我发现x64 JIT没有同样的问题。
这是我对 x86 JIT 的阅读。
这对我来说似乎是一个糟糕的优化......
I believe this is in a genuine JIT compilation bug. I would report it to Microsoft and see what they say. Interestingly, I found that the x64 JIT does not have the same problem.
Here is my reading of the x86 JIT.
This looks like an optimization gone bad to me...
我将您的代码复制到新的控制台应用程序中。
所以这是 x86 JIT 错误地生成了代码。已删除我关于循环重新排序等的原始文本。此处的一些其他答案已确认 JIT 在 x86 上错误地展开循环。
要解决这个问题,您可以将 IntVec 的声明更改为一个类,它适用于所有风格。
认为这需要在 MS Connect 上继续...-
1 给 Microsoft!
I copied your code into a new Console App.
So it is the x86 JIT incorrectly generating the code. Have deleted my original text about reordering of loops etc. A few other answers on here have confirmed that the JIT is unwinding the loop incorrectly when on x86.
To fix the problem you can change the declaration of IntVec to a class and it works in all flavours.
Think this needs to go on MS Connect....
-1 to Microsoft!