Repository navigation
JIT: Enable EH Write Thru by default #35923
Description
Activity
- addedarea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
on May 6, 2020 - addeduntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
on May 6, 2020 - removeduntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
on May 6, 2020 @CarolEidt is this 5.0? I can look at cleaning up some of the hot path workarounds if that would be helpful?
@benaadams - I think I should have put this in "Future". The issue is that it degrades performance in some cases (see the comments above, and the linked issue). What's needed is to first fully understand the causes for the scenarios where performance degrades and then determine the best approach to mitigate those.
I'm hoping it's caused by the hot-paths current higher register cost to workaround the EH issue; which then the writethrough adds to; so taking those out should make it lighter? 52d6d27
Oh, shoot. At the time I was working on EH WriteThru I had change those workarounds, but I'm pretty sure that wasn't done for the measurements described in #35534. The one in
AsyncMethodBuilderCore:Startcould just be removed with EH WriteThru, but the other one did slightly worse when removed, because it has multiple definitions, so I'd modified that one to use a temporary variable for the conditional definition (which got a register) and then there could be a single EH-live definition. In that case the JIT could possibly be taught to only do the write on entry to the try, but that would, of course, add more complexity.While starting to analyzing the scenarios where we would see regression, here is an example of:
In situations where there are multiple definitions in a Try clause, each of those will do a store, and may also define a register - requiring an additional move if the value could have been directly defined to memory.
During build interval, since EH Var is register candidate, we create an interval for it.
Here,
#157is a def and#177is a use as see below:During register allocation, we assign
raxto#157and then use the value from
raxat#177

We update the register information in our tree as seen here:

and then finally, during code generation, we store value of cnst
1inraxbefore moving it to a register:Ideally, if there is just a single def, single use, we should avoid allocating register to it. That is one of the criteria of not allocating register as Carol pointed.
That said, it could presumably decide that some of the candidates should never get a register, effectively making it the same as if it were not a candidate, though that would require some tweaking to avoid actually allocating a register when not needed (it does better with RegOptional uses than defs).
Here, although the
#157def was marked withregOptional, we ended up allocating a register to it.- addedUser StoryA single user-facing feature. Can be grouped under an epic.A single user-facing feature. Can be grouped under an epic.
on Jan 27, 2021 - addedBottom Up WorkNot part of a theme, epic, or user storyNot part of a theme, epic, or user story
on Feb 11, 2021 Moved future since .NET 6 scope items are complete now.
I spent some time to enable this for multi-vars and the ones that needs zero initialization and from the asmdiffs, we continue to see the regressions because of the reason Carol mentioned:
the increase in register pressure due to the EH vars can expose (known) weaknesses in the register allocator's handling of code with high register-pressure.
The first line of attack would be to track the def/use ratio and see if we can enable enregistration of lvlVar if
def/use <= 0.25(for every def there should be at least 4 uses).- During early phase, in
lvaMarkLocalVars(), continue marking the variables assingleDef. - Because of step 1, the liveness update that happens during SSABuild will skip marking
singleDefasDoNotRegister. - During liveness update that happens after lowering, calculate the def/use ratio (which should be possible in
lvaComputeRefCounts). Mark all the variables that arelvLiveInOutOfHndlr == trueand whosedef/use > 0.25asdoNotEnregister.
- During early phase, in
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone





Today, if all the variables that are live cross exception handlers (EH) are kept on stack. This proves expensive for hot methods containing EH code. It is also expensive for certain language constructs like presence
asyncwhich calls in .NET libraries code that has EH. In below example, even if methodCalculatedoesn't have any EH regions, the presence ofasyncintroduces EH region.The call to AsyncMethodBuilderCore:Start contains EH code and the performance for these methods can be degraded because of accessing the EH vars from stack. We added a work around specially inside
AsyncMethodBuilderCore:Start()in dotnet/coreclr#15629 to make sure that EH vars get registers.We have an implementation of enregistering EH Vars but from the analysis of #35534 showed mixed results for enabling EH Write Thru by default. This issue tracks of enabling the EH Write thru by default. The outcome that we are expecting is that in addition to storing the EH variable values on stack, also store them in a register and use register (as much as possible) during the uses in non-EH code. Additionally, also assign a register for the code that is totally contained inside a EH region.
There are at least a couple of issues, though further analysis should be done to validate that these are the major contributing factors to performance regressions:
One option would be to track the number/ratio of defs and uses. Currently we don't readily have that information until we build
Intervals, and at that point it is difficult for the register allocator to change its mind about making something a candidate. That said, it could presumably decide that some of the candidates should never get a register, effectively making it the same as if it were not a candidate, though that would require some tweaking to avoid actually allocating a register when not needed (it does better with RegOptional uses than defs).It's possible that this tracking could be done prior to register allocation at the time of ref counting.
For now, we will take an incremental approach:
Future work
Related issue:
Workaround added so far: dotnet/coreclr#15629
category:cq
theme:register-allocator
skill-level:expert
cost:medium