Skip to content

ILC: fold RuntimeHelpers.IsBitwiseEquatable<T> branches before scanning - #137569

Open
Sergio0694 wants to merge 5 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/ilc-fold-bitwise-equatable-main
Open

Sergio0694 wants to merge 5 commits into
dotnet:mainfrom
Sergio0694:user/sergiopedri/ilc-fold-bitwise-equatable-main

Conversation

@Sergio0694

Copy link
Copy Markdown
Contributor

RuntimeHelpers.IsBitwiseEquatable<T>() is expanded per instantiation by RuntimeHelpersIntrinsics.EmitIL, so RyuJIT only folds the branch after inlining the synthesized body. The dependency scanner has no equivalent knowledge and scans both sides of the branch. In shared code (e.g. Array.IndexOf<T>, where T is __Canon) the dead side adds generic lookups to the dictionary layouts, such as the Unsafe.BitCast<T, long> method dictionaries, even though the compiled code never uses them.

This folds the intrinsic in SubstitutedILProvider, next to the existing typeof, IsValueType and IsEnum folding. RuntimeHelpersIntrinsics.TryGetConstantValue is now the single evaluation, and EmitIL uses it too. Since the scanner and RyuJIT both read the rewritten IL, they cannot disagree on which side of the branch is live. The fold only applies to concrete types (not universal canon, runtime-determined types or signature variables), and the result for a canonical form must match every exact instantiation sharing it, which holds because canonical forms only replace reference types with __Canon.

Binary size on a large Native AOT app (the Microsoft Store), same inputs, ILC with and without this change:

WinStore.App.dll Deflate (level 6)
Before 84,881,408 27,617,883
After 84,504,576 (-376,832) 27,479,313 (-138,570)

No method body changes: 7,250 Unsafe.BitCast method dictionaries and their generic method hashtable entries are no longer kept. A full Store build with the change produced the same 84,504,576 byte image.

Validated locally with the NativeAOT UnitTests smoke test, which includes a new IsBitwiseEquatable search test covering shared and exact instantiations, floating point, padded, Equals-overriding and reference-containing types.

Part of #137567.

Note

This PR description and the change were prepared with GitHub Copilot.

Sergio0694 and others added 2 commits November 14, 2026 17:12
IsBitwiseEquatable<T> is expanded per instantiation by RuntimeHelpersIntrinsics.EmitIL, so
RyuJIT only folds the branch after inlining the synthesized body. The dependency scanner
had no way to know that and added the generic lookups of the dead side to the dictionary
layouts (for example the Unsafe.BitCast<T, long> method dictionaries pulled in by
Array.IndexOf<T> for every shared instantiation).

Evaluate the intrinsic in SubstitutedILProvider, next to the existing typeof and IsValueType
branch folding, using the same function EmitIL uses. Both the scanner and RyuJIT consume
the rewritten IL, so they cannot disagree about which side of the branch is live.

Co-authored-by: Copilot App <[email protected]>
Covers Array.IndexOf/LastIndexOf and List<T> for shared and exact instantiations, including
floating point, padded, Equals overriding and reference containing structs.

Co-authored-by: Copilot App <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Sergio0694 and others added 2 commits November 14, 2026 18:17
Asking an indefinite type whether it is a canonical or runtime-determined subtype
asserts in checked builds, and dataflow can pass such types through
SubstitutedILProvider.

Co-authored-by: Copilot App <[email protected]>
@Sergio0694

Copy link
Copy Markdown
Contributor Author

@MichalStrehovsky related question to this, this optimization is fine on its own but it also makes me wonder, is it expected that "7,250 Unsafe.BitCast method dictionaries and their generic method hashtable entries" were otherwise being needed and causing almost 400KB of size? That seems like quite a ton of size just for something so tiny like this, is it normal? 😅

@MichalStrehovsky

Copy link
Copy Markdown
Member

@MichalStrehovsky related question to this, this optimization is fine on its own but it also makes me wonder, is it expected that "7,250 Unsafe.BitCast method dictionaries and their generic method hashtable entries" were otherwise being needed and causing almost 400KB of size? That seems like quite a ton of size just for something so tiny like this, is it normal? 😅

I can have a look if rt-sz turns up something. Looks like 60 bytes per one? Two dozens of bytes would be trivial to explain, it's possible this enables optimizations elsewhere.

}
}

static class TestBitwiseEquatableSearch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand what this is testing. For a fix like this I would expect a test under src\tests\nativeaot\SmokeTests\TrimmingBehaviors spot checking the interesting pattern(s).

But in particular (for AI), we don't want an exhaustive check testing everything that TryGetConstantValue handles. That code is shared, there's no unique behaviors in it. It's covered elsewhere.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, I see IsBitwiseEquatable is not a public API so we can't write a simple targeted trimming test for it. That's annoying. Testability did shut down potential improvements in the past.

I'll look if we can introduce a shim for the test tree that would allow us to access internals like this.

@MichalStrehovsky

Copy link
Copy Markdown
Member

I can have a look if rt-sz turns up something. Looks like 60 bytes per one? Two dozens of bytes would be trivial to explain, it's possible this enables optimizations elsewhere.

I looked into (the TodosApi from rt-sz). It looks expected:

  • 50000 bytes in generic methods hashtable
  • 1000 bytes in generic methods templates
  • Each of the generic dictionaries is 16 bytes on its own
    • a tiny bit of butterfly effects because we stopped scanning a codepath

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants