运行 C# 代码仅在发布版本中失败(可能是因为 JIT 编译器优化)
Running C# code fails only in release build (probably because of JIT compiler optimization)
下面的代码在调试版本中运行成功,但在发布版本中显示 "start" 后停止。
只要我研究过,在JIT编译器中优化编译该方法似乎需要花费很多时间。
它为什么停滞不前,解决方法是什么?
我查看了 visual studio 2017 社区版。
已编辑:
它仅在 CPU=AnyCPU(删除首选 32 位)或 CPU=x64.
时失败
我刚刚安装了 .Net Framework 4.7.1。 (目标框架是 .Net Framework 4。)
即使安装了.Net Framework 4.7.1,问题依然存在。
注册表设置useLegacyJit后,问题解决。 (去掉useLegacyJit,问题又来了。)好像是RyuJIT的bug。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args) {
Console.WriteLine("start");
Calculation calc = new Calculation();
calc.Run();
Console.WriteLine("end");
}
}
class Calculation
{
double[] V0 = new double[1];
double[] V1 = new double[1];
double[] V2 = new double[1];
double[] V3 = new double[1];
double[] V4 = new double[1];
double[] V5 = new double[1];
double[] V6 = new double[1];
double[] V7 = new double[1];
double[] V8 = new double[1];
double[] V9 = new double[1];
double[] V10 = new double[1];
double[] V11 = new double[1];
double[] V12 = new double[1];
double[] V13 = new double[1];
double[] V14 = new double[1];
double[] V15 = new double[1];
double[] V16 = new double[1];
double[] V17 = new double[1];
double[] V18 = new double[1];
double[] V19 = new double[1];
double[] V20 = new double[1];
double[] V21 = new double[1];
double[] V22 = new double[1];
double[] V23 = new double[1];
double[] V24 = new double[1];
double[] V25 = new double[1];
double[] V26 = new double[1];
double[] V27 = new double[1];
double[] V28 = new double[1];
double[] V29 = new double[1];
double[] V30 = new double[1];
double[] V31 = new double[1];
double[] V32 = new double[1];
double[] V33 = new double[1];
double[] V34 = new double[1];
double[] V35 = new double[1];
double[] V36 = new double[1];
double[] V37 = new double[1];
double[] V38 = new double[1];
double[] V39 = new double[1];
double[] V40 = new double[1];
double[] V41 = new double[1];
double[] V42 = new double[1];
double[] V43 = new double[1];
double[] V44 = new double[1];
double[] V45 = new double[1];
double[] V46 = new double[1];
double[] V47 = new double[1];
double[] V48 = new double[1];
double[] V49 = new double[1];
public void Run()
{
for (int x = 0; x < 1; x++){V0[x] = 0.0;}
for (int x = 0; x < 1; x++){V1[x] = 0.0;}
for (int x = 0; x < 1; x++){V2[x] = 0.0;}
for (int x = 0; x < 1; x++){V3[x] = 0.0;}
for (int x = 0; x < 1; x++){V4[x] = 0.0;}
for (int x = 0; x < 1; x++){V5[x] = 0.0;}
for (int x = 0; x < 1; x++){V6[x] = 0.0;}
for (int x = 0; x < 1; x++){V7[x] = 0.0;}
for (int x = 0; x < 1; x++){V8[x] = 0.0;}
for (int x = 0; x < 1; x++){V9[x] = 0.0;}
for (int x = 0; x < 1; x++){V10[x] = 0.0;}
for (int x = 0; x < 1; x++){V11[x] = 0.0;}
for (int x = 0; x < 1; x++){V12[x] = 0.0;}
for (int x = 0; x < 1; x++){V13[x] = 0.0;}
for (int x = 0; x < 1; x++){V14[x] = 0.0;}
for (int x = 0; x < 1; x++){V15[x] = 0.0;}
for (int x = 0; x < 1; x++){V16[x] = 0.0;}
for (int x = 0; x < 1; x++){V17[x] = 0.0;}
for (int x = 0; x < 1; x++){V18[x] = 0.0;}
for (int x = 0; x < 1; x++){V19[x] = 0.0;}
for (int x = 0; x < 1; x++){V20[x] = 0.0;}
for (int x = 0; x < 1; x++){V21[x] = 0.0;}
for (int x = 0; x < 1; x++){V22[x] = 0.0;}
for (int x = 0; x < 1; x++){V23[x] = 0.0;}
for (int x = 0; x < 1; x++){V24[x] = 0.0;}
for (int x = 0; x < 1; x++){V25[x] = 0.0;}
for (int x = 0; x < 1; x++){V26[x] = 0.0;}
for (int x = 0; x < 1; x++){V27[x] = 0.0;}
for (int x = 0; x < 1; x++){V28[x] = 0.0;}
for (int x = 0; x < 1; x++){V29[x] = 0.0;}
for (int x = 0; x < 1; x++){V30[x] = 0.0;}
for (int x = 0; x < 1; x++){V31[x] = 0.0;}
for (int x = 0; x < 1; x++){V32[x] = 0.0;}
for (int x = 0; x < 1; x++){V33[x] = 0.0;}
for (int x = 0; x < 1; x++){V34[x] = 0.0;}
for (int x = 0; x < 1; x++){V35[x] = 0.0;}
for (int x = 0; x < 1; x++){V36[x] = 0.0;}
for (int x = 0; x < 1; x++){V37[x] = 0.0;}
for (int x = 0; x < 1; x++){V38[x] = 0.0;}
for (int x = 0; x < 1; x++){V39[x] = 0.0;}
for (int x = 0; x < 1; x++){V40[x] = 0.0;}
for (int x = 0; x < 1; x++){V41[x] = 0.0;}
for (int x = 0; x < 1; x++){V42[x] = 0.0;}
for (int x = 0; x < 1; x++){V43[x] = 0.0;}
for (int x = 0; x < 1; x++){V44[x] = 0.0;}
for (int x = 0; x < 1; x++){V45[x] = 0.0;}
for (int x = 0; x < 1; x++){V46[x] = 0.0;}
for (int x = 0; x < 1; x++){V47[x] = 0.0;}
for (int x = 0; x < 1; x++){V48[x] = 0.0;}
for (int x = 0; x < 1; x++){V49[x] = 0.0;}
}
}
}
这似乎确实是 RyuJIT 的问题,可以通过暂时禁用它来验证:
Set-ItemProperty -Path HKLM:\Software\Microsoft\.NETFramework -Name useLegacyJit -Type DWord -Value 1
跟踪实际问题会更复杂一些,但是,如果我们构建 coreclr v2.0.5(最新的稳定版),并且 运行 使用 /v
详细标志和 COMPlus_JitDump=*
环境变量设置,一段时间后它开始打印出很多这样的日志:
AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b).
AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b).
AX2: $f5b != c0 ==> select([9]store($dbd, c0, 8), $f5b) ==> select($dbd, $f5b).
AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b).
AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b).
AX2: $f5b != c0 ==> select([9]store($dbd, c0, 8), $f5b) ==> select($dbd, $f5b).
AX2: $f5b != c0 ==> select([b]store($e0f, c0, a), $f5b) ==> select($e0f, $f5b).
AX2: $f5b != c0 ==> select([d]store($e21, c0, c), $f5b) ==> select($e21, $f5b).
AX2: $f5b != c0 ==> select([f]store($e32, c0, e), $f5b) ==> select($e32, $f5b).
打印日志的方法见https://github.com/dotnet/coreclr/blob/v2.0.5/src/jit/valuenum.cpp#L1377
这个问题很可能是由于 budget
变量溢出或由于某种原因没有减少引起的。不幸的是,没有太多时间进一步研究它,但对该方法进行了一些更改 here, and when I tried running the sample with master Core CLR 的构建问题似乎已经解决。
根据 Roadmap,它将于 2018 年第一季度发布。不过我不确定它何时会包含在实际的 .NET Framework 版本中。
作为解决方法,也许您可以尝试将循环重构为它们自己的方法:
public void RunLoop(double[] arr)
{
for (int x = 0; x < 1; x++)
{
arr[x] = 0.0;
// etc...
}
}
这似乎也解决了这个问题(如果在您的实际代码中这样做有意义的话)。
下面的代码在调试版本中运行成功,但在发布版本中显示 "start" 后停止。 只要我研究过,在JIT编译器中优化编译该方法似乎需要花费很多时间。
它为什么停滞不前,解决方法是什么?
我查看了 visual studio 2017 社区版。
已编辑:
它仅在 CPU=AnyCPU(删除首选 32 位)或 CPU=x64.
我刚刚安装了 .Net Framework 4.7.1。 (目标框架是 .Net Framework 4。)
即使安装了.Net Framework 4.7.1,问题依然存在。
注册表设置useLegacyJit后,问题解决。 (去掉useLegacyJit,问题又来了。)好像是RyuJIT的bug。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args) {
Console.WriteLine("start");
Calculation calc = new Calculation();
calc.Run();
Console.WriteLine("end");
}
}
class Calculation
{
double[] V0 = new double[1];
double[] V1 = new double[1];
double[] V2 = new double[1];
double[] V3 = new double[1];
double[] V4 = new double[1];
double[] V5 = new double[1];
double[] V6 = new double[1];
double[] V7 = new double[1];
double[] V8 = new double[1];
double[] V9 = new double[1];
double[] V10 = new double[1];
double[] V11 = new double[1];
double[] V12 = new double[1];
double[] V13 = new double[1];
double[] V14 = new double[1];
double[] V15 = new double[1];
double[] V16 = new double[1];
double[] V17 = new double[1];
double[] V18 = new double[1];
double[] V19 = new double[1];
double[] V20 = new double[1];
double[] V21 = new double[1];
double[] V22 = new double[1];
double[] V23 = new double[1];
double[] V24 = new double[1];
double[] V25 = new double[1];
double[] V26 = new double[1];
double[] V27 = new double[1];
double[] V28 = new double[1];
double[] V29 = new double[1];
double[] V30 = new double[1];
double[] V31 = new double[1];
double[] V32 = new double[1];
double[] V33 = new double[1];
double[] V34 = new double[1];
double[] V35 = new double[1];
double[] V36 = new double[1];
double[] V37 = new double[1];
double[] V38 = new double[1];
double[] V39 = new double[1];
double[] V40 = new double[1];
double[] V41 = new double[1];
double[] V42 = new double[1];
double[] V43 = new double[1];
double[] V44 = new double[1];
double[] V45 = new double[1];
double[] V46 = new double[1];
double[] V47 = new double[1];
double[] V48 = new double[1];
double[] V49 = new double[1];
public void Run()
{
for (int x = 0; x < 1; x++){V0[x] = 0.0;}
for (int x = 0; x < 1; x++){V1[x] = 0.0;}
for (int x = 0; x < 1; x++){V2[x] = 0.0;}
for (int x = 0; x < 1; x++){V3[x] = 0.0;}
for (int x = 0; x < 1; x++){V4[x] = 0.0;}
for (int x = 0; x < 1; x++){V5[x] = 0.0;}
for (int x = 0; x < 1; x++){V6[x] = 0.0;}
for (int x = 0; x < 1; x++){V7[x] = 0.0;}
for (int x = 0; x < 1; x++){V8[x] = 0.0;}
for (int x = 0; x < 1; x++){V9[x] = 0.0;}
for (int x = 0; x < 1; x++){V10[x] = 0.0;}
for (int x = 0; x < 1; x++){V11[x] = 0.0;}
for (int x = 0; x < 1; x++){V12[x] = 0.0;}
for (int x = 0; x < 1; x++){V13[x] = 0.0;}
for (int x = 0; x < 1; x++){V14[x] = 0.0;}
for (int x = 0; x < 1; x++){V15[x] = 0.0;}
for (int x = 0; x < 1; x++){V16[x] = 0.0;}
for (int x = 0; x < 1; x++){V17[x] = 0.0;}
for (int x = 0; x < 1; x++){V18[x] = 0.0;}
for (int x = 0; x < 1; x++){V19[x] = 0.0;}
for (int x = 0; x < 1; x++){V20[x] = 0.0;}
for (int x = 0; x < 1; x++){V21[x] = 0.0;}
for (int x = 0; x < 1; x++){V22[x] = 0.0;}
for (int x = 0; x < 1; x++){V23[x] = 0.0;}
for (int x = 0; x < 1; x++){V24[x] = 0.0;}
for (int x = 0; x < 1; x++){V25[x] = 0.0;}
for (int x = 0; x < 1; x++){V26[x] = 0.0;}
for (int x = 0; x < 1; x++){V27[x] = 0.0;}
for (int x = 0; x < 1; x++){V28[x] = 0.0;}
for (int x = 0; x < 1; x++){V29[x] = 0.0;}
for (int x = 0; x < 1; x++){V30[x] = 0.0;}
for (int x = 0; x < 1; x++){V31[x] = 0.0;}
for (int x = 0; x < 1; x++){V32[x] = 0.0;}
for (int x = 0; x < 1; x++){V33[x] = 0.0;}
for (int x = 0; x < 1; x++){V34[x] = 0.0;}
for (int x = 0; x < 1; x++){V35[x] = 0.0;}
for (int x = 0; x < 1; x++){V36[x] = 0.0;}
for (int x = 0; x < 1; x++){V37[x] = 0.0;}
for (int x = 0; x < 1; x++){V38[x] = 0.0;}
for (int x = 0; x < 1; x++){V39[x] = 0.0;}
for (int x = 0; x < 1; x++){V40[x] = 0.0;}
for (int x = 0; x < 1; x++){V41[x] = 0.0;}
for (int x = 0; x < 1; x++){V42[x] = 0.0;}
for (int x = 0; x < 1; x++){V43[x] = 0.0;}
for (int x = 0; x < 1; x++){V44[x] = 0.0;}
for (int x = 0; x < 1; x++){V45[x] = 0.0;}
for (int x = 0; x < 1; x++){V46[x] = 0.0;}
for (int x = 0; x < 1; x++){V47[x] = 0.0;}
for (int x = 0; x < 1; x++){V48[x] = 0.0;}
for (int x = 0; x < 1; x++){V49[x] = 0.0;}
}
}
}
这似乎确实是 RyuJIT 的问题,可以通过暂时禁用它来验证:
Set-ItemProperty -Path HKLM:\Software\Microsoft\.NETFramework -Name useLegacyJit -Type DWord -Value 1
跟踪实际问题会更复杂一些,但是,如果我们构建 coreclr v2.0.5(最新的稳定版),并且 运行 使用 /v
详细标志和 COMPlus_JitDump=*
环境变量设置,一段时间后它开始打印出很多这样的日志:
AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b). AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b). AX2: $f5b != c0 ==> select([9]store($dbd, c0, 8), $f5b) ==> select($dbd, $f5b). AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b). AX2: $f5b != c0 ==> select([7]store($dae, c0, 6), $f5b) ==> select($dae, $f5b). AX2: $f5b != c0 ==> select([9]store($dbd, c0, 8), $f5b) ==> select($dbd, $f5b). AX2: $f5b != c0 ==> select([b]store($e0f, c0, a), $f5b) ==> select($e0f, $f5b). AX2: $f5b != c0 ==> select([d]store($e21, c0, c), $f5b) ==> select($e21, $f5b). AX2: $f5b != c0 ==> select([f]store($e32, c0, e), $f5b) ==> select($e32, $f5b).
打印日志的方法见https://github.com/dotnet/coreclr/blob/v2.0.5/src/jit/valuenum.cpp#L1377
这个问题很可能是由于 budget
变量溢出或由于某种原因没有减少引起的。不幸的是,没有太多时间进一步研究它,但对该方法进行了一些更改 here, and when I tried running the sample with master Core CLR 的构建问题似乎已经解决。
根据 Roadmap,它将于 2018 年第一季度发布。不过我不确定它何时会包含在实际的 .NET Framework 版本中。
作为解决方法,也许您可以尝试将循环重构为它们自己的方法:
public void RunLoop(double[] arr)
{
for (int x = 0; x < 1; x++)
{
arr[x] = 0.0;
// etc...
}
}
这似乎也解决了这个问题(如果在您的实际代码中这样做有意义的话)。