Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fold nullchecks in Interlocked.* #101956

Merged
merged 4 commits into from
May 8, 2024
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented May 7, 2024

This PR removes redundant nullchecks emitted with Interlocked.* apis since Interlocked load/store itself is an implicit nullcheck (like any other indir). Also, Interlocked implies a potentially contended memory so we should avoid probing it when it's not needed.
Example:

int fld;

void Test() => Interlocked.Increment(ref fld);

diff:

; Method Program:Test():this (FullOpts)
-      cmp      byte  ptr [rcx], cl
       add      rcx, 8
       lock     
       inc      dword ptr [rcx]
       ret      
-; Total bytes of code: 10
+; Total bytes of code: 8

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 7, 2024
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@EgorBo EgorBo marked this pull request as ready for review May 7, 2024 09:47
@EgorBo
Copy link
Member Author

EgorBo commented May 7, 2024

PTAL @AndyAyersMS @jakobbotsch @dotnet/jit-contrib
We basically remove redundant bound checks like these:

N008 (  7,  7) [000014] -A-XGO-----                         +--*  XADD      int   
N006 (  5,  5) [000023] ---X-O-N---                         |  +--*  COMMA     byref 
N002 (  2,  2) [000019] ---X-O-----                         |  |  +--*  NULLCHECK byte  
N001 (  1,  1) [000018] -----------                         |  |  |  \--*  LCL_VAR   ref    V00 this         u:1
N005 (  3,  3) [000022] -----O-----                         |  |  \--*  ADD       byref 
N003 (  1,  1) [000020] -----------                         |  |     +--*  LCL_VAR   ref    V00 this         u:1 (last use)
N004 (  1,  1) [000021] -----------                         |  |     \--*  CNS_INT   long   8 Fseq[fld]
N007 (  1,  1) [000013] -----------                         |  \--*  CNS_INT   int    1

It's interesting that optFindNullCheckToFold already has in its description a promise to handle COMMA(nullcheck(addr), addr) or COMMA(nullcheck(addr), addr + smallCns)

@jakobbotsch
Copy link
Member

Doesn't this transformation effectively undo what fgMorphExpandInstanceField is creating? I'm not sure it's legal because of the possibility of "almost null" byrefs.

@jakobbotsch
Copy link
Member

Perhaps it's sufficient to verify that the base is TYP_REF and then allow it if the offset is <= MAX_UNCHECKED_OFFSET_FOR_NULL_OBJECT. That might still get most of diffs if I'm reading them right.

@AndyAyersMS
Copy link
Member

Perhaps it's sufficient to verify that the base is TYP_REF and then allow it if the offset is <= MAX_UNCHECKED_OFFSET_FOR_NULL_OBJECT. That might still get most of diffs if I'm reading them right.

There's a call to fgIsBigOffset down at the end of the method, is that not sufficient?

@jakobbotsch
Copy link
Member

Ah yes, it should be. I wonder then if it wouldn't be sufficient to just add a gtEffectiveVal() on top of tree->GetIndirOrArrMetaDataAddr()? Isn't the existing code essentially a more general version of the logic added here?

@EgorBo
Copy link
Member Author

EgorBo commented May 8, 2024

Thanks for the suggestion!

@EgorBo EgorBo merged commit 35ad5e1 into dotnet:main May 8, 2024
105 of 107 checks passed
@EgorBo EgorBo deleted the fold-nullcheck-atomic branch May 8, 2024 11:28
michaelgsharp pushed a commit to michaelgsharp/runtime that referenced this pull request May 9, 2024
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 8, 2024
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants