Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NativeAOT] When attempting to AOT compile a benchmark System.InvalidCastException: Specified cast is not valid is thrown #106439

Closed
neon-sunset opened this issue Aug 15, 2024 · 4 comments · Fixed by #106445
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged

Comments

@neon-sunset
Copy link
Contributor

neon-sunset commented Aug 15, 2024

Description

It appears that with the current RC.1 build it is impossible to build AOT benchmarks with BenchmarkDotNet due to an exception that occurs within ILC.

Reproduction Steps

  1. dotnet new console
  2. dotnet add package BenchmarkDotNet (0.14.0)
  3. Define simple benchmark:
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<Benchmark>();

[SimpleJob(RuntimeMoniker.NativeAot90)]
public class Benchmark {
    [Benchmark]
    public void DoNothing() { }
}
  1. dotnet run -c Release

Expected behavior

Benchmark successfully runs

Actual behavior

An exception is thrown:

// Build Error: Standard output: 
  
 Standard error: 
 /usr/local/share/dotnet/sdk/9.0.100-rc.1.24414.7/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.RuntimeIdentifierInference.targets(326,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [/Users/arseniy/Code/Local/ILScannerRepro/bin/Release/net9.0/7612d247-f6bf-43c3-b660-89158fea3a74/BenchmarkDotNet.Autogenerated.csproj]
  ILScannerRepro -> /Users/arseniy/Code/Local/ILScannerRepro/bin/Release/net9.0/7612d247-f6bf-43c3-b660-89158fea3a74/bin/Release/net9.0/osx-arm64/publish/ILScannerRepro.dll
  BenchmarkDotNet.Autogenerated -> /Users/arseniy/Code/Local/ILScannerRepro/bin/Release/net9.0/7612d247-f6bf-43c3-b660-89158fea3a74/bin/Release/net9.0/osx-arm64/publish/7612d247-f6bf-43c3-b660-89158fea3a74.dll
/Users/arseniy/.nuget/packages/microsoft.net.illink.tasks/9.0.0-rc.1.24410.5/build/Microsoft.NET.ILLink.targets(220,5): warning : Property 'TrimmerDefaultAction' is deprecated in .NET 7 and higher and will be ignored. Use TrimMode instead. [/Users/arseniy/Code/Local/ILScannerRepro/bin/Release/net9.0/7612d247-f6bf-43c3-b660-89158fea3a74/BenchmarkDotNet.Autogenerated.csproj]
  Generating native code
EXEC : error : Specified cast is not valid. [/Users/arseniy/Code/Local/ILScannerRepro/bin/Release/net9.0/7612d247-f6bf-43c3-b660-89158fea3a74/BenchmarkDotNet.Autogenerated.csproj]
  System.InvalidCastException: Specified cast is not valid.
     at ILCompiler.CompilerTypeSystemContext.EnsureLoadableTypeUncached(TypeDesc) + 0x428
     at ILCompiler.CompilerTypeSystemContext.EnsureLoadableType(TypeDesc) + 0x54
     at ILCompiler.LibraryRootProvider.CheckCanGenerateMethod(MethodDesc) + 0x64
     at ILCompiler.DependencyAnalysis.TypeMetadataNode.GetStaticDependencies(NodeFactory) + 0x430
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependenciesImpl(DependencyNodeCore`1) + 0x4c
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.GetStaticDependencies(DependencyNodeCore`1) + 0xf4
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ProcessMarkStack() + 0xe8
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() + 0x40
     at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan() + 0x20
     at ILCompiler.Program.<Run>g__RunScanner|4_0(Program.<>c__DisplayClass4_0&) + 0x1e0
     at ILCompiler.Program.Run() + 0x2368
     at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass240_0.<.ctor>b__0(ParseResult) + 0x308
/Users/arseniy/.nuget/packages/microsoft.dotnet.ilcompiler/9.0.0-rc.1.24410.5/build/Microsoft.NETCore.Native.targets(317,5): error MSB3073: The command ""/Users/arseniy/.nuget/packages/runtime.osx-arm64.microsoft.dotnet.ilcompiler/9.0.0-rc.1.24410.5/tools/ilc" @"/Users/arseniy/Code/Local/ILScannerRepro/bin/Release/net9.0/7612d247-f6bf-43c3-b660-89158fea3a74/obj/Release/net9.0/native/7612d247-f6bf-43c3-b660-89158fea3a74.ilc.rsp"" exited with code 1. [/Users/arseniy/Code/Local/ILScannerRepro/bin/Release/net9.0/7612d247-f6bf-43c3-b660-89158fea3a74/BenchmarkDotNet.Autogenerated.csproj]

Regression?

Yes

Known Workarounds

None that I'm aware of

Configuration

.NET SDK:
 Version:           9.0.100-rc.1.24414.7
 Commit:            71a23de93f
 Workload version:  9.0.100-manifests.14db7d11
 MSBuild version:   17.12.0-preview-24407-03+6bc91d5e2

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  15.0
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/9.0.100-rc.1.24414.7/

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Aug 15, 2024
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

MichalStrehovsky added a commit to MichalStrehovsky/BenchmarkDotNet that referenced this issue Aug 15, 2024
* `llcOptimizationPreference` was renamed to `OptimizationPreference` and became supported in .NET 8 (or maybe .NET 7, it doesn't matter)
* `IlcGenerateStackTraceData` is now `StackTraceSupport` and supported. (Also got renamed in .NET 8 or earlier.)

I'm leaving the old names of properties for backcompat. We could also delete them. I don't know how much BDN cares about .NET versions that are out of support.

IlcGenerateCompleteTypeMetadata is unsupported, we don't test it, it's a mode for troubleshooting trimming issues for people who ignore trimming warnings. I don't think BDN should be setting this. You'll only find bugs (dotnet/runtime#106439). So deleting that. Not deleting the API because it looks to be a public API.
@MichalStrehovsky
Copy link
Member

BDN is running into this because it sets an unsupported flag that we don't test. This will require a fix in BDN. I'm going to fix this in the compiler too, but since we already snapped for .NET 9 and this is in an unsupported switch, the fix will probably not meet the bar for 9.0 at this point. BDN fix: dotnet/BenchmarkDotNet#2616

MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this issue Aug 15, 2024
…teTypesOnly` is specified

Fixes dotnet#106439.

This is an unsupported flag that allows generating unusable metadata for type members that would have been trimmed. This is useful when troubleshooting trimming issues for people who ignore trimming warnings because it converts random `NullReferenceException` (e.g. `GetMethod` returned null and the program didn't check for it) into "Foo.Bar wasn't generated" exceptions.

Because this isn't tested, we regressed this. The code that checks whether it's possible to generate the metadata for the member at all stopped working for uninstantiated generics. This check is needed to deal with invalid input IL.
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Aug 15, 2024
@MichalStrehovsky
Copy link
Member

@neon-sunset it might be possible to work around this with existing version of BDN: it seems like IlcGenerateCompleteTypeMetadata (the problematic switch that BDN sets to true and triggering the bug) seems to be somehow configurable on the BDN side. Don't know enough about BDN to give you instructions though.

@neon-sunset
Copy link
Contributor Author

Thanks a lot!

adamsitnik pushed a commit to dotnet/BenchmarkDotNet that referenced this issue Aug 28, 2024
* Clean up unsupported native AOT flags

* `llcOptimizationPreference` was renamed to `OptimizationPreference` and became supported in .NET 8 (or maybe .NET 7, it doesn't matter)
* `IlcGenerateStackTraceData` is now `StackTraceSupport` and supported. (Also got renamed in .NET 8 or earlier.)

I'm leaving the old names of properties for backcompat. We could also delete them. I don't know how much BDN cares about .NET versions that are out of support.

IlcGenerateCompleteTypeMetadata is unsupported, we don't test it, it's a mode for troubleshooting trimming issues for people who ignore trimming warnings. I don't think BDN should be setting this. You'll only find bugs (dotnet/runtime#106439). So deleting that. Not deleting the API because it looks to be a public API.

Co-authored-by: Adeel Mujahid <3840695+am11@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Sep 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Projects
Archived in project
2 participants