gpt4 book ai didi

C# 'is' 结构类型检查 - 奇怪的 .NET 4.0 x86 优化行为

转载 作者:IT王子 更新时间:2023-10-29 04:00:14 26 4
gpt4 key购买 nike

更新:我已经提交了 bug report Microsoft Connect,请为它投票!

更新 2:Microsoft 已将错误报告标记为已修复

Posted by Microsoft on 18/08/2010 at 17:25

This bug will be fixed in afuture version of the runtime. I'mafraid it's too early to tell if thatwill be in a service pack or the nextmajor release.

自升级到 VS2010 后,“is”关键字出现了一些非常奇怪的行为。

下面的程序 (test.cs) 在 Debug模式下编译时(对于 x86)输出 True,在优化编译时输出 False(对于 x86)。在 x64 或 AnyCPU 中编译所有组合会给出预期的结果,True。

在 .NET 3.5 下编译的所有组合都给出了预期的结果,True。

我正在使用下面的批处理文件 (runtest.bat) 来编译和测试使用编译器 .NET 框架的各种组合的代码。

  • 还有其他人在 .NET 4.0 下遇到过这类问题吗?
  • 当运行 runtests.bat 时,其他人是否在他们的计算机上看到与我相同的行为?
  • @$@#$??

  • 有解决办法吗?

测试.cs

using System;

public class Program
{
public static bool IsGuid(object item)
{
return item is Guid;
}

public static void Main()
{
Console.Write(IsGuid(Guid.NewGuid()));
}
}

运行测试.bat

@echo off

rem Usage:
rem runtest -- runs with csc.exe x86 .NET 4.0
rem runtest 64 -- runs with csc.exe x64 .NET 4.0
rem runtest v3.5 -- runs with csc.exe x86 .NET 3.5
rem runtest v3.5 64 -- runs with csc.exe x64 .NET 3.5

set version=v4.0.30319
set platform=Framework

for %%a in (%*) do (
if "%%a" == "64" (set platform=Framework64)
if "%%a" == "v3.5" (set version=v3.5)
)

echo Compiler: %platform%\%version%\csc.exe
set csc="C:\Windows\Microsoft.NET\%platform%\%version%\csc.exe"

set make=%csc% /nologo /nowarn:1607 test.cs
rem CS1607: Referenced assembly targets a different processor
rem This happens if you compile for x64 using csc32, or x86 using csc64

%make% /platform:x86
test.exe
echo =^> x86

%make% /platform:x86 /optimize
test.exe
echo =^> x86 (Optimized)

%make% /platform:x86 /debug
test.exe
echo =^> x86 (Debug)

%make% /platform:x86 /debug /optimize
test.exe
echo =^> x86 (Debug + Optimized)

%make% /platform:x64
test.exe
echo =^> x64

%make% /platform:x64 /optimize
test.exe
echo =^> x64 (Optimized)

%make% /platform:x64 /debug
test.exe
echo =^> x64 (Debug)

%make% /platform:x64 /debug /optimize
test.exe
echo =^> x64 (Debug + Optimized)

%make% /platform:AnyCPU
test.exe
echo =^> AnyCPU

%make% /platform:AnyCPU /optimize
test.exe
echo =^> AnyCPU (Optimized)

%make% /platform:AnyCPU /debug
test.exe
echo =^> AnyCPU (Debug)

%make% /platform:AnyCPU /debug /optimize
test.exe
echo =^> AnyCPU (Debug + Optimized)

测试结果

运行 runtest.bat 时,我在 Win7 x64 安装上得到以下结果。

> runtest 32 v4.0
Compiler: Framework\v4.0.30319\csc.exe
False => x86
False => x86 (Optimized)
True => x86 (Debug)
False => x86 (Debug + Optimized)
True => x64
True => x64 (Optimized)
True => x64 (Debug)
True => x64 (Debug + Optimized)
True => AnyCPU
True => AnyCPU (Optimized)
True => AnyCPU (Debug)
True => AnyCPU (Debug + Optimized)

> runtest 64 v4.0
Compiler: Framework64\v4.0.30319\csc.exe
False => x86
False => x86 (Optimized)
True => x86 (Debug)
False => x86 (Debug + Optimized)
True => x64
True => x64 (Optimized)
True => x64 (Debug)
True => x64 (Debug + Optimized)
True => AnyCPU
True => AnyCPU (Optimized)
True => AnyCPU (Debug)
True => AnyCPU (Debug + Optimized)

> runtest 32 v3.5
Compiler: Framework\v3.5\csc.exe
True => x86
True => x86 (Optimized)
True => x86 (Debug)
True => x86 (Debug + Optimized)
True => x64
True => x64 (Optimized)
True => x64 (Debug)
True => x64 (Debug + Optimized)
True => AnyCPU
True => AnyCPU (Optimized)
True => AnyCPU (Debug)
True => AnyCPU (Debug + Optimized)

> runtest 64 v3.5
Compiler: Framework64\v3.5\csc.exe
True => x86
True => x86 (Optimized)
True => x86 (Debug)
True => x86 (Debug + Optimized)
True => x64
True => x64 (Optimized)
True => x64 (Debug)
True => x64 (Debug + Optimized)
True => AnyCPU
True => AnyCPU (Optimized)
True => AnyCPU (Debug)
True => AnyCPU (Debug + Optimized)

tl;dr

最佳答案

我设计了一个类似的例子,但以同样的方式失败了:

using System;
using System.Runtime.CompilerServices;

public class Program {
static void Main() {
Console.Write(Verify(Test.Create()));
Console.ReadLine();
}
//[MethodImpl(MethodImplOptions.NoInlining)]
static bool Verify(IDisposable item) {
return item is Test;
}
struct Test : IDisposable {
public void Dispose() { }
public static Test Create() { return new Test(); }
}
}

这是一个 JIT 优化器错误。不能完全指责它,它大量优化了代码。但在我看来,它在优化装箱转换时遇到了麻烦。坦率地说,这是一个非常严重的错误。


此错误已修复,我无法再重现它。我当前的 clrjit.dll 版本是 4.0.30319.237,日期为 2011 年 5 月 17 日。我无法确切地说出是哪个更新修复了它。我在 2011 年 8 月 5 日收到了一个安全更新,将 clrjit.dll 更新为修订版 235,日期为 4 月 12 日,那将是最早的。

关于C# 'is' 结构类型检查 - 奇怪的 .NET 4.0 x86 优化行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2819102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com