Skip to content

Do not shortcut CompilerComparer #115807

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

Merged
merged 1 commit into from
May 21, 2025
Merged

Conversation

MichalStrehovsky
Copy link
Member

In rt-sz (https://github.com/MichalStrehovsky/rt-sz/actions/runs/15128663241/job/42525897839) I ran into following exception:

  System.AggregateException: One or more errors occurred. (Specified cast is not valid.)
   ---> System.InvalidCastException: Specified cast is not valid.
     at ILCompiler.DependencyAnalysis.RuntimeImportMethodNode.CompareToImpl(ISortableNode, CompilerComparer) + 0x3c

I am not actually able to repro this locally, but this seems to be the only way how this could happen (we're trying to compare two nodes that are not of the same type - CompilerComparer will handle that case; CompareToImpl will not).

In rt-sz (https://github.com/MichalStrehovsky/rt-sz/actions/runs/15128663241/job/42525897839) I ran into following exception:

```
  System.AggregateException: One or more errors occurred. (Specified cast is not valid.)
   ---> System.InvalidCastException: Specified cast is not valid.
     at ILCompiler.DependencyAnalysis.RuntimeImportMethodNode.CompareToImpl(ISortableNode, CompilerComparer) + 0x3c
```

I am not actually able to repro this locally, but this seems to be the only way how this could happen (we're trying to compare two nodes that are not of the same type - `CompilerComparer` will handle that case; `CompareToImpl` will not).
@Copilot Copilot AI review requested due to automatic review settings May 20, 2025 21:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a runtime exception in the comparison logic between nodes by replacing the direct call to CompareToImpl with a call to CompilerComparer.Compare instead.

  • Replace direct node comparison calls with CompilerComparer.Compare to avoid invalid cast exceptions.
  • Applies the change consistently across TentativeMethodNode.cs and AddressTakenMethodNode.cs.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/TentativeMethodNode.cs Changed node comparison implementation to delegate to CompilerComparer.
src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/AddressTakenMethodNode.cs Updated node comparison to use CompilerComparer for consistent type handling.
Comments suppressed due to low confidence (2)

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/TentativeMethodNode.cs:68

  • Replacing the direct call to _methodNode.CompareToImpl with comparer.Compare prevents invalid cast exceptions when comparing nodes of different types. Please verify that this change maintains the expected ordering across all node comparisons.
return comparer.Compare(_methodNode, ((TentativeMethodNode)other)._methodNode);

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/AddressTakenMethodNode.cs:62

  • Using CompilerComparer.Compare here correctly resolves the invalid cast issue by handling type differences in node comparisons. Ensure that the new comparison logic meets all sorting requirements for AddressTakenMethodNode.
return comparer.Compare(_methodNode, ((AddressTakenMethodNode)other)._methodNode);

@MichalStrehovsky
Copy link
Member Author

/ba-g timeouts

@MichalStrehovsky MichalStrehovsky merged commit df5c6cb into dotnet:main May 21, 2025
87 of 94 checks passed
@MichalStrehovsky MichalStrehovsky deleted the compare branch May 21, 2025 04:47
SimaTian pushed a commit that referenced this pull request May 27, 2025
In rt-sz (https://github.com/MichalStrehovsky/rt-sz/actions/runs/15128663241/job/42525897839) I ran into following exception:

```
  System.AggregateException: One or more errors occurred. (Specified cast is not valid.)
   ---> System.InvalidCastException: Specified cast is not valid.
     at ILCompiler.DependencyAnalysis.RuntimeImportMethodNode.CompareToImpl(ISortableNode, CompilerComparer) + 0x3c
```

I am not actually able to repro this locally, but this seems to be the only way how this could happen (we're trying to compare two nodes that are not of the same type - `CompilerComparer` will handle that case; `CompareToImpl` will not).
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants