[PATCH] D118607: [InstCombine] Remove weaker fence adjacent to a stronger fence

Anna Thomas via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 31 07:28:58 PST 2022


anna created this revision.
anna added reviewers: reames, jfb, davide.
Herald added a subscriber: hiraditya.
anna requested review of this revision.
Herald added a project: LLVM.

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."


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118607

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/consecutive-fences.ll


Index: llvm/test/Transforms/InstCombine/consecutive-fences.ll
===================================================================
--- llvm/test/Transforms/InstCombine/consecutive-fences.ll
+++ llvm/test/Transforms/InstCombine/consecutive-fences.ll
@@ -4,7 +4,6 @@
 
 ; CHECK-LABEL: define void @tinkywinky
 ; CHECK-NEXT:   fence seq_cst
-; CHECK-NEXT:   fence syncscope("singlethread") acquire
 ; CHECK-NEXT:   ret void
 ; CHECK-NEXT: }
 
@@ -31,9 +30,6 @@
 }
 
 ; CHECK-LABEL: define void @patatino
-; CHECK-NEXT:   fence acquire
-; CHECK-NEXT:   fence seq_cst
-; CHECK-NEXT:   fence acquire
 ; CHECK-NEXT:   fence seq_cst
 ; CHECK-NEXT:   ret void
 ; CHECK-NEXT: }
@@ -46,6 +42,27 @@
   ret void
 }
 
+; CHECK-LABEL: define void @weaker_fence_1
+; CHECK-NEXT: fence seq_cst
+; CHECK-NEXT: ret void
+define void @weaker_fence_1() {
+  fence seq_cst
+  fence release
+  fence seq_cst
+  ret void
+}
+
+; CHECK-LABEL: define void @weaker_fence_2
+; CHECK-NEXT: fence seq_cst
+; CHECK-NEXT: ret void
+define void @weaker_fence_2() {
+  fence seq_cst
+  fence release
+  fence seq_cst
+  fence acquire 
+  ret void
+}
+
 ; CHECK-LABEL: define void @debug
 ; CHECK-NOT: fence
 ; CHECK: call void @llvm.dbg.value
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2468,10 +2468,19 @@
 
 // Fence instruction simplification
 Instruction *InstCombinerImpl::visitFenceInst(FenceInst &FI) {
-  // Remove identical consecutive fences.
   Instruction *Next = FI.getNextNonDebugInstruction();
-  if (auto *NFI = dyn_cast<FenceInst>(Next))
-    if (FI.isIdenticalTo(NFI))
+  if (auto *NFI = dyn_cast<FenceInst>(Next)) {
+    // Remove identical consecutive fences.
+    // When we have a weaker-ordering fence consecutively followed by a
+    // stronger-ordering fence, we can remove the weaker one.
+    if (FI.isIdenticalTo(NFI) || isStrongerThan(NFI->getOrdering(), FI.getOrdering()))
+      return eraseInstFromFunction(FI);
+  }
+
+  // Remove weaker fence if it is preceeded immediately by a stronger ordering
+  // fence.
+  if (auto *PFI = dyn_cast_or_null<FenceInst>(FI.getPrevNonDebugInstruction()))
+    if (isStrongerThan(PFI->getOrdering(), FI.getOrdering()))
       return eraseInstFromFunction(FI);
   return nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118607.404517.patch
Type: text/x-patch
Size: 2419 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220131/7570e2fe/attachment.bin>


More information about the llvm-commits mailing list