Skip to content

Add "points to stack" tree flag for GT_ADDR. - #48675

Closed
sandreenko wants to merge 9 commits into
dotnet:mainfrom
sandreenko:AddrOfStackExp
Closed

sandreenko wants to merge 9 commits into
dotnet:mainfrom
sandreenko:AddrOfStackExp

Conversation

@sandreenko

Copy link
Copy Markdown
Contributor

In #48308 (comment) we noticed that we are not consistent about our ADDR optimizations, long story short we don't need to emit a full write barrier when we change a byref that points to the stack, the current Jit does it by retyping TYP_BYREF to TYP_I_IMPL in some cases, however, other places are not aware of it and can generate worse code, asserts or bad code because of that.

I was trying to make it consistent and convert all such addresses to I_IMPL and then assert during check and rationalize, however, it did not work well, because:

  1. asserts in addrMode that can't use op2 with LCL_VAR_ADDR but it detects them by byref type, if it is IMPL we don't correctly update the liveness for it;
  2. asserts in codegen because we lose GC pointers, morph is trying to solve it by lvaGrabTemp(true DEBUGARG("Cast away GC")) but does not catch all necessary cases.
  3. CQ regressions because assertion and copy prop does not work when they see LclVar V01 byref = LclVar V02 long;
  4. CQ regressions with inlining when it expected a byref but got LONG and rejected it.

These changes are in the first 5 commits.

so mostly because of the asserts I've decided to try to keep them as BYREF and use a tree flag on such addresses:
0771c2e: use a flag instead of IMPL type.
it passed stress tests (revealed one with incorrect IL, was fixed), gave some improvements but overall regression.

cbc2de7: Fix regressions1.
Fix regressions when we had something like IND short(cast short(ADDR byref ()).

c8b8a4d: Fix regressions2.
Teach DefinesLocal, impIsAddressInLocal and IsLocalExpr to skip cast nodes.

Even with these fixes, the overall score is still a regression, mostly because of the new DEBUGARG("Cast away GC")) temps.

However, at least this change c723b6e#diff-d593fa9f647288fed0e0a54655a5d55586fab17368fe5212df2604a48d5a064dL644 looks like a correctness fix. This code is working because in most cases I_IMPL is used for stack addresses but it could be used for anything via a cast, it this case we will get a hole.

I want to gather @dotnet/jit-contrib optinions:

  1. What is the best representation for such addresses? I tend to Byref with a flag because of the existing logic that is checking Type == BYREF and assertion/copy prop;
  2. Is anybody aware of any existing opened issues that could be associated with this inconsistent logic/wrong memory barrier?
  3. Does anybody know how expensive would it be to get rid of lvaGrabTemp(true DEBUGARG("Cast away GC")) temps and support such implicit casts in emitter? How do we want to represent CAST I_IMPL<-byref in different phases?
  4. Do we remember why we have 3 similar functions with different logic: DefinesLocal, impIsAddressInLocal and IsLocalExpr? Is something preventing us from merging them?
  5. do we want to take this change with the regressions as a correctness fix or leave the existing state?

Diffs (spmi x64):

benchmarks.run.windows.x64.checked.mch: 
  Total bytes of delta: 259 (0.05% of base)
  25 total methods with Code Size differences (3 improved, 22 regressed), 355 unchanged.
libraries.crossgen.windows.x64.checked.mch: 
  96 total methods with Code Size differences (14 improved, 82 regressed), 2639 unchanged.
  Total bytes of delta: 187 (0.01% of base)
libraries.pmi.windows.x64.checked.mch: 
  96 total methods with Code Size differences (20 improved, 76 regressed), 2478 unchanged.
  Total bytes of delta: 36 (0.00% of base)
tests.pmi.windows.x64.checked.mch: 
  Total bytes of delta: 3895 (0.09% of base)
  924 total methods with Code Size differences (126 improved, 798 regressed), 695 unchanged.

@sandreenko sandreenko added design-discussion Ongoing discussion about design without consensus area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labels Feb 24, 2021
@sandreenko sandreenko self-assigned this Feb 24, 2021
@AndyAyersMS

Copy link
Copy Markdown
Member

See also #12122 and related issues, in particular the notes for #10821. The object stack allocation phase does some related analysis.

@sandreenko

Copy link
Copy Markdown
Contributor Author

thanks Andy!

Base automatically changed from master to main March 1, 2021 09:08
@sandreenko

Copy link
Copy Markdown
Contributor Author

closing as not 6.0 item.

@sandreenko sandreenko closed this Mar 3, 2021
@JulieLeeMSFT JulieLeeMSFT added this to the Future milestone Mar 24, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Apr 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI design-discussion Ongoing discussion about design without consensus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants