[PATCH] D153414: [LLVM] Do not speculate convergent calls

Sameer Sahasrabuddhe via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 21 06:03:45 PDT 2023


sameerds created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
sameerds requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This is a rebase of D85604 <https://reviews.llvm.org/D85604> by Nicolai Haehnle <nicolai.haehnle at amd.com>. The
other changes in the original review have already been committed at different
points in time.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153414

Files:
  llvm/lib/Analysis/ValueTracking.cpp
  llvm/test/Transforms/SimplifyCFG/attr-convergent.ll


Index: llvm/test/Transforms/SimplifyCFG/attr-convergent.ll
===================================================================
--- llvm/test/Transforms/SimplifyCFG/attr-convergent.ll
+++ llvm/test/Transforms/SimplifyCFG/attr-convergent.ll
@@ -3,6 +3,7 @@
 ; Checks that the SimplifyCFG pass won't duplicate a call to a function marked
 ; convergent.
 ;
+; CHECK-LABEL: @check
 ; CHECK: call void @barrier
 ; CHECK-NOT: call void @barrier
 define void @check(i1 %cond, ptr %out) {
@@ -25,4 +26,23 @@
   ret void
 }
 
+; CHECK-LABEL: @dont_speculate_convergent
+; CHECK: entry:
+; CHECK: then:
+; CHECK:   call i32 @speculatable_convergent
+; CHECK: end:
+define i32 @dont_speculate_convergent(i1 %cond1, i32 %in) {
+entry:
+  br i1 %cond1, label %then, label %end
+
+then:
+  %v = call i32 @speculatable_convergent(i32 %in)
+  br label %end
+
+end:
+  %r = phi i32 [ 0, %entry ], [ %v, %then ]
+  ret i32 %r
+}
+
 declare void @barrier() convergent
+declare i32 @speculatable_convergent(i32) convergent readnone speculatable
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -5999,10 +5999,16 @@
     auto *CI = dyn_cast<const CallInst>(Inst);
     if (!CI)
       return false;
-    const Function *Callee = CI->getCalledFunction();
+
+    // The called function depends on the set of threads executing it, which
+    // could change if the call is moved to a different location in control
+    // flow.
+    if (CI->isConvergent())
+      return false;
 
     // The called function could have undefined behavior or side-effects, even
     // if marked readnone nounwind.
+    const Function *Callee = CI->getCalledFunction();
     return Callee && Callee->isSpeculatable();
   }
   case Instruction::VAArg:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153414.533231.patch
Type: text/x-patch
Size: 1853 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230621/24a07db9/attachment.bin>


More information about the llvm-commits mailing list