Repository navigation
Enable Enum.HasFlag box elision in Tier0 - #130590
Conversation
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
There was a problem hiding this comment.
Pull request overview
This PR adjusts Tier0/minopts boxing import behavior so that boxing integral values (including enums) uses per-box single-def, exact-typed temps, enabling existing box-elision optimizations (notably Enum.HasFlag) to trigger in Tier0.
Changes:
- Stops sharing the “Reusable Box Helper” temp for integral box operations in minopts/debug when the method doesn’t already have many locals.
- Updates the importer rationale/comments to reflect the Tier0 motivation and intended downstream optimization enablement.
Show a summary per file
| File | Description |
|---|---|
| src/coreclr/jit/importer.cpp | Refines Tier0/minopts box-temp sharing logic to preserve exact types for integral (incl. enum) boxes, enabling Enum.HasFlag box elision. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 1
It definitely did supposed to be optimized in tier0 as well via #89348 the isEnum check was probably put there to avoid size regressions when we don't reuse the shared box temp |
|
@EgorBo Maybe, but it’s doing both the opposite of what the comment said it was supposed to do (ie it’s skipping enum and allowing everything else through) and it’s a throughput hit on top to boot. We’re seeing essentially pure improvement here by actually allowing enums to use the opt in T0: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1505225&view=ms.vss-build-web.run-extensions-tab |
It's probably because something has been improved since then and it no longer leads to regressions |
impImportAndPushBox has a Tier0 carve-out that skips box-temp sharing for integral values so each box gets its own single-def exact temp. gtOptimizeEnumHasFlag needs that exact temp to recover the value and drop the box. The isEnum guard on the carve-out was inverted (!= Must), so it applied to everything except enums -- the one case it was added for -- and Enum.HasFlag kept allocating and boxing at Tier0. Fix the guard to == Must so the carve-out targets enum boxes, restoring the Enum.HasFlag box elision at Tier0. Co-authored-by: Copilot App <[email protected]>
f23d0eb to
3f008ad
Compare
|
Force-pushed a rebase onto latest Both designs deliver the identical enum Updated the description with the full breakdown. Note This comment was generated with the assistance of GitHub Copilot. |
|
-175.9k bytes on Windows x64, -88.7k bytes on Windows Arm64. Similar numbers for Linux, and similar TP improvement to the other at So altogether better and inline with the original intent, most look to be removing |
The JIT can already elide the boxes generated for `Enum.HasFlag` in optimized (Tier1) code, but not in Tier0/minopts. This is the motivating case here: `Enum.HasFlag` at Tier0 allocates and boxes both the `this` value and the flag argument, then makes a real call, which is a lot of work for something that should fold to a couple of `and`/`cmp` instructions. ## Root cause To keep the local count (and prolog zeroing) down, Tier0 shares a single "Reusable Box Helper" temp across boxes. Shared temps carry no exact type, so `gtOptimizeEnumHasFlag` cannot recover the underlying value and bails, leaving the box in place. `impImportAndPushBox` has a carve-out that instead hands integral box values their own single-def *exact* temp precisely so this elision can fire (added in #89348). However, its condition included `isEnum(hClass) != TypeCompareState::Must`, which *excludes* actual enums, the exact case it was meant to enable. So enum boxes kept sharing a temp and never got their exact type. ## Fix Correct the inverted guard from `!= TypeCompareState::Must` to `== TypeCompareState::Must` so the carve-out targets enum boxes, the case it was written for. This has two effects at Tier0: 1. **Enum boxes get a unique exact temp**, so `gtOptimizeEnumHasFlag` recovers the value and elides the box entirely. This is the allocation win and the motivating fix. 2. **Non-enum integral boxes go back to sharing a reusable temp** (their pre-#89348 behavior). This does not change which allocations happen; it only reuses a single ref-temp slot across boxes with non-overlapping lifetimes, which shrinks the Tier0 frame and prolog zeroing. ## Measured impact SuperPMI, x64, checked JIT, vs clean `main`. **Allocations.** The only heap allocations removed are the enum `HasFlag` boxes themselves (`CORINFO_HELP_NEWSFAST` 2 -> 0 on the `HasFlag` methods). Non-enum box allocation counts are invariant. A cross-collection `NEWSFAST`-delta scan confirmed this change never emits *more* allocations than base (it never reintroduces a box the base was eliding) — the receiver/flag temps are only shared across non-overlapping lifetimes, so a live boxed value is never clobbered. **Code size (asmdiffs).** Net **-175,983 bytes** across 6,641 contexts: | Collection | contexts | net bytes | |---|--:|--:| | aspnet2 | 53 | -2,174 | | benchmarks.run_pgo | 141 | -3,850 | | coreclr_tests | 1,363 | -37,656 | | libraries_tests | 5,062 | -132,292 | | libraries_tests_no_tiered | 22 | -11 | That is -218,115 bytes of improvements over 4,583 methods against +42,132 bytes of regressions over 1,789 methods. The regressions are small (worst single method +47 bytes, clustered +6..+47) and are Tier0 prolog/`mov` zero-init churn from the different temp layout when a non-enum box reverts to the shared slot — no added allocations. The bulk of the win is the frame/prolog shrink from re-sharing non-enum integral box temps; the enum box elision itself is the smaller, allocation-reducing slice. **JIT throughput (tpdiff).** MinOpts -0.06% (aspnet2) to ~-0.00/-0.01% overall; FullOpts unchanged (`shareBoxedTemps` is already false there). This is one import-time condition, so per-method JIT work is unchanged — effectively free on throughput. **Tier0 repro.** `HasFlagConst` 103 -> 49 bytes, `HasFlagVar` 105 -> 59 bytes, with the `NEWSFAST`/`HasFlag` calls gone and correct runtime results. Net: correcting the guard enables the enum box elision at Tier0 (the allocation win) and, as a side effect, restores shared non-enum integral box temps for a substantial additional code-size and PerfScore win, at effectively zero JIT throughput cost. > [!NOTE] > This PR description was generated with the assistance of GitHub Copilot. Co-authored-by: Copilot App <[email protected]>
The JIT can already elide the boxes generated for
Enum.HasFlagin optimized (Tier1) code, but not in Tier0/minopts. This is the motivating case here:Enum.HasFlagat Tier0 allocates and boxes both thethisvalue and the flag argument, then makes a real call, which is a lot of work for something that should fold to a couple ofand/cmpinstructions.Root cause
To keep the local count (and prolog zeroing) down, Tier0 shares a single "Reusable Box Helper" temp across boxes. Shared temps carry no exact type, so
gtOptimizeEnumHasFlagcannot recover the underlying value and bails, leaving the box in place.impImportAndPushBoxhas a carve-out that instead hands integral box values their own single-def exact temp precisely so this elision can fire (added in #89348). However, its condition includedisEnum(hClass) != TypeCompareState::Must, which excludes actual enums, the exact case it was meant to enable. So enum boxes kept sharing a temp and never got their exact type.Fix
Correct the inverted guard from
!= TypeCompareState::Mustto== TypeCompareState::Mustso the carve-out targets enum boxes, the case it was written for. This has two effects at Tier0:gtOptimizeEnumHasFlagrecovers the value and elides the box entirely. This is the allocation win and the motivating fix.Measured impact
SuperPMI, x64, checked JIT, vs clean
main.Allocations. The only heap allocations removed are the enum
HasFlagboxes themselves (CORINFO_HELP_NEWSFAST2 -> 0 on theHasFlagmethods). Non-enum box allocation counts are invariant. A cross-collectionNEWSFAST-delta scan confirmed this change never emits more allocations than base (it never reintroduces a box the base was eliding) — the receiver/flag temps are only shared across non-overlapping lifetimes, so a live boxed value is never clobbered.Code size (asmdiffs). Net -175,983 bytes across 6,641 contexts:
That is -218,115 bytes of improvements over 4,583 methods against +42,132 bytes of regressions over 1,789 methods. The regressions are small (worst single method +47 bytes, clustered +6..+47) and are Tier0 prolog/
movzero-init churn from the different temp layout when a non-enum box reverts to the shared slot — no added allocations. The bulk of the win is the frame/prolog shrink from re-sharing non-enum integral box temps; the enum box elision itself is the smaller, allocation-reducing slice.JIT throughput (tpdiff). MinOpts -0.06% (aspnet2) to ~-0.00/-0.01% overall; FullOpts unchanged (
shareBoxedTempsis already false there). This is one import-time condition, so per-method JIT work is unchanged — effectively free on throughput.Tier0 repro.
HasFlagConst103 -> 49 bytes,HasFlagVar105 -> 59 bytes, with theNEWSFAST/HasFlagcalls gone and correct runtime results.Net: correcting the guard enables the enum box elision at Tier0 (the allocation win) and, as a side effect, restores shared non-enum integral box temps for a substantial additional code-size and PerfScore win, at effectively zero JIT throughput cost.
Note
This PR description was generated with the assistance of GitHub Copilot.