Repository navigation
ILC: fold RuntimeHelpers.IsBitwiseEquatable<T> branches before scanning - #137569
Sergio0694 wants to merge 5 commits into
Conversation
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: 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. |
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
Co-authored-by: Copilot App <[email protected]>
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]>
|
@MichalStrehovsky related question to this, this optimization is fine on its own but it also makes me wonder, is it expected that "7,250 |
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I looked into (the TodosApi from rt-sz). It looks expected:
|
Co-authored-by: Copilot App <[email protected]>
RuntimeHelpers.IsBitwiseEquatable<T>()is expanded per instantiation byRuntimeHelpersIntrinsics.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>, whereTis__Canon) the dead side adds generic lookups to the dictionary layouts, such as theUnsafe.BitCast<T, long>method dictionaries, even though the compiled code never uses them.This folds the intrinsic in
SubstitutedILProvider, next to the existingtypeof,IsValueTypeandIsEnumfolding.RuntimeHelpersIntrinsics.TryGetConstantValueis now the single evaluation, andEmitILuses 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.dllNo method body changes: 7,250
Unsafe.BitCastmethod 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
UnitTestssmoke test, which includes a newIsBitwiseEquatablesearch 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.