This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Remove weaker fence adjacent to a stronger fence
ClosedPublic

Authored by anna on Jan 31 2022, 7:28 AM.

Details

Summary

We have an instCombine rule to remove identical consecutive fences.
We can extend this to remove weaker fences when we have consecutive stronger
fence.

As stated in the LangRef, a fence with a stronger ordering also implies
ordering weaker than itself: "A fence which has seq_cst ordering, in addition to
having both acquire and release semantics specified above, participates in the
global program order of other seq_cst operations and/or fences."

Diff Detail

Event Timeline

anna created this revision.Jan 31 2022, 7:28 AM
anna requested review of this revision.Jan 31 2022, 7:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 31 2022, 7:28 AM
lebedev.ri added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2470–2492

Would it go past 80col if Next is inlined?

General idea seems reasonable to me, a few code comments.

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2472

Stale comment.

2475

I believe the second check should subsume the identicalto one here.

llvm/test/Transforms/InstCombine/consecutive-fences.ll
15

Ah! A case you need to be careful of and I think the current code is wrong on.

The stronger fence must also have a stronger scope. For the moment, you could restrict the transform to the case where both fences are global scoped if desired.

We can't use a singlethread fence seq_cst to remove a global release fence.

reames requested changes to this revision.Jan 31 2022, 10:04 AM
This revision now requires changes to proceed.Jan 31 2022, 10:04 AM
anna planned changes to this revision.Jan 31 2022, 10:31 AM

fix based on Philip's comments about scopes.

llvm/test/Transforms/InstCombine/consecutive-fences.ll
15

Thanks, I hadn't realized the part about scopes! Will fix the code.

anna updated this revision to Diff 404638.Jan 31 2022, 11:38 AM

addressed review comments. Added extra tests.

reames requested changes to this revision.Jan 31 2022, 11:42 AM
reames added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2471–2492

As I said before, I don't believe this check serves any purpose after the addition of the new transform.

2484

Style suggestion: pull out a isStrongerFence(FI1,FI2) helper which does both scope and ordering check, then call it twice. I believe the resulting code should be easier to read. Can be a lambda or static function as you prefer.

This revision now requires changes to proceed.Jan 31 2022, 11:42 AM
anna added inline comments.Jan 31 2022, 12:08 PM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2471–2492

Actually I intentionally left it in the updated code. It will consider fences with the same syncscopes as well (SyncScope::singleThread, "target-dependent" scope) for removal. I bail out for non-global syncscopes in the new transform.

I'm not sure what the "target-depdenent scope" is, but in the LangRef: "If an atomic operation is marked syncscope("<target-scope>"), where <target-scope> is a target specific synchronization scope, then it is target dependent if it synchronizes with and participates in the seq_cst total orderings of other operations." So, I am a bit iffy, if we can drop weaker fences in such scopes :)

2484

Yup, agreed.

anna added inline comments.Jan 31 2022, 12:12 PM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2471–2492

If we ignore "target dependent scopes" same ordered fence, then this check can be ignored, since I can update the new transform to handle both global and single-thread scopes.

reames added inline comments.Jan 31 2022, 12:23 PM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2471–2492

Ah, that concerns make sense. I don't really know the answer to the target scope question, so let's be conservative as well.

Please add a test with an arbitrary target scope, and a comment explicitly asking whether it's a missing transform. (i.e. give the next person who tries to remove this code a clue to hang their hat on)

anna updated this revision to Diff 404692.Jan 31 2022, 1:04 PM

support system and singlethread syncscopes. Added test for target-dependent syncscope to showcase need for isIdenticalTo check.

reames accepted this revision.Jan 31 2022, 7:27 PM

LGTM

This revision is now accepted and ready to land.Jan 31 2022, 7:27 PM
anna added a comment.Feb 1 2022, 8:12 AM

I spend sometime trying to see if the failing premerge tests seem related or maybe false positives, but I have no idea where these are from. Also, other completed unrelated review show the same failures: https://reviews.llvm.org/D94928

reames added a comment.Feb 1 2022, 9:24 AM

I spend sometime trying to see if the failing premerge tests seem related or maybe false positives, but I have no idea where these are from. Also, other completed unrelated review show the same failures: https://reviews.llvm.org/D94928

They look unrelated. The precommit test applies the patch to a randomly chosen base revision. If that revision happens to be bad, well...

(I generally ignore them tbh. Bitten me once, but only once so far.)

This revision was landed with ongoing or failed builds.Feb 1 2022, 11:06 AM
This revision was automatically updated to reflect the committed changes.