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

JIT: fix gc hole in peephole optimizations #78074

Merged
merged 2 commits into from
Nov 9, 2022

Conversation

AndyAyersMS
Copy link
Member

@AndyAyersMS AndyAyersMS commented Nov 8, 2022

We cannot safely peephole instructions that straddle a gc enable boundary. Detecting when this might happen is a bit subtle; currently we rely on emitForceNewIG to be set.

Add a new utility emitCannotPeepholeLastIns to centralize the logic that decides whether basing a peephole opt on emitLastIns is safe.

Closes #77661.

We cannot safely peephole instructions that straddle a gc enable boundary.
Detecting when this might happen is a bit subtle; currently we rely on
`emitForceNewIG` to be set.

Add a new utility 'emitCanPeepholeLastIns` to centralize the logic that
decides whether basing current emission on `emitLastIns` is safe.

Closed dotnet#77661.
@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 Nov 8, 2022
@AndyAyersMS
Copy link
Member Author

@BruceForstall PTAL
cc @dotnet/jit-contrib

One diff on arm64, still running x64 locally but don't expect much there.

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@BruceForstall BruceForstall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion, one question. Let me know if you want to leave it as-is.

src/coreclr/jit/emit.h Outdated Show resolved Hide resolved
src/coreclr/jit/emitarm64.cpp Show resolved Hide resolved
Copy link
Member

@BruceForstall BruceForstall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

I do think we should back-port.

@AndyAyersMS
Copy link
Member Author

One other thought -- if we had emitted the GC liveness update even though we didn't emit the instruction then the peephole in #77661 would have been ok.

Kind of makes me think that instead of not emitting instructions we should just shrink them to zero size or something similar, But that would have some annoying aspects too (perhaps we could discard them after the gc updates are done).

@AndyAyersMS
Copy link
Member Author

Per SPMI, just one arm64 method with diffs -- see #77661 (comment).

@AndyAyersMS AndyAyersMS merged commit af494f4 into dotnet:main Nov 9, 2022
@AndyAyersMS
Copy link
Member Author

/backport to release/7.0

@github-actions
Copy link
Contributor

github-actions bot commented Nov 9, 2022

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/3429844601

@BruceForstall
Copy link
Member

One other thought -- if we had emitted the GC liveness update even though we didn't emit the instruction then the peephole in #77661 would have been ok.

Are you suggesting that an instruction that gets removed by a peephole optimization would still have its GC effect applied? That seems dangerous. It seems like the peephole optimization needs to always do the right thing w.r.t. GC no matter what that might be.

@AndyAyersMS
Copy link
Member Author

One other thought -- if we had emitted the GC liveness update even though we didn't emit the instruction then the peephole in #77661 would have been ok.

Are you suggesting that an instruction that gets removed by a peephole optimization would still have its GC effect applied? That seems dangerous. It seems like the peephole optimization needs to always do the right thing w.r.t. GC no matter what that might be.

The net effect on GC needs to be the same whether we do the peephole or not. Maybe we can at least sanity check this is true somehow? Say save gc state before, then compare after states with/without peephole, they should match. Probably tricky to pull off.

@AndyAyersMS
Copy link
Member Author

FYI I think #77153 is the same bug -- so this may be more widespread than I first thought.

I tracked this down by doing an SPMI collect of the (intermittently failing) libraries run and then running SPMI diffs with and without the fix here.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2022
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.

Test failure JIT/Directed/nullabletypes/Desktop/boxunboxvaluetype_ro/boxunboxvaluetype_ro.sh
3 participants