[llvm] 6c84884 - [LoopSimplify] don't separate nested loops with convergent calls

Sameer Sahasrabuddhe via llvm-commits llvm-commits at lists.llvm.org
Mon May 18 21:43:28 PDT 2020


Author: Sameer Sahasrabuddhe
Date: 2020-05-19T09:22:39+05:30
New Revision: 6c8488436644af41b3e29564ff85dc5eaa240347

URL: https://github.com/llvm/llvm-project/commit/6c8488436644af41b3e29564ff85dc5eaa240347
DIFF: https://github.com/llvm/llvm-project/commit/6c8488436644af41b3e29564ff85dc5eaa240347.diff

LOG: [LoopSimplify] don't separate nested loops with convergent calls

Summary:
When a loop has multiple backedges, loop simplification attempts to
separate them out into nested loops. This results in incorrect control
flow in the presence of some functions like a GPU barrier. This change
skips the transformation when such "convergent" function calls are
present in the loop body.

Reviewed By: nhaehnle

Differential Revision: https://reviews.llvm.org/D80078

Added: 
    llvm/test/Transforms/LoopSimplify/convergent.ll

Modified: 
    llvm/lib/Transforms/Utils/LoopSimplify.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/LoopSimplify.cpp b/llvm/lib/Transforms/Utils/LoopSimplify.cpp
index 28f88f39a712..14c6950508f0 100644
--- a/llvm/lib/Transforms/Utils/LoopSimplify.cpp
+++ b/llvm/lib/Transforms/Utils/LoopSimplify.cpp
@@ -230,6 +230,27 @@ static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,
   if (!Preheader)
     return nullptr;
 
+  // Treat the presence of convergent functions conservatively. The
+  // transformation is invalid if calls to certain convergent
+  // functions (like an AMDGPU barrier) get included in the resulting
+  // inner loop. But blocks meant for the inner loop will be
+  // identified later at a point where it's too late to abort the
+  // transformation. Also, the convergent attribute is not really
+  // sufficient to express the semantics of functions that are
+  // affected by this transformation. So we choose to back off if such
+  // a function call is present until a better alternative becomes
+  // available. This is similar to the conservative treatment of
+  // convergent function calls in GVNHoist and JumpThreading.
+  for (auto BB : L->blocks()) {
+    for (auto &II : *BB) {
+      if (auto CI = dyn_cast<CallBase>(&II)) {
+        if (CI->isConvergent()) {
+          return nullptr;
+        }
+      }
+    }
+  }
+
   // The header is not a landing pad; preheader insertion should ensure this.
   BasicBlock *Header = L->getHeader();
   assert(!Header->isEHPad() && "Can't insert backedge to EH pad");

diff  --git a/llvm/test/Transforms/LoopSimplify/convergent.ll b/llvm/test/Transforms/LoopSimplify/convergent.ll
new file mode 100644
index 000000000000..f957e3970688
--- /dev/null
+++ b/llvm/test/Transforms/LoopSimplify/convergent.ll
@@ -0,0 +1,25 @@
+; RUN: opt < %s -S -loop-simplify | FileCheck %s
+
+; Don't separate out nested loops if a convergent call is present
+
+; CHECK-NOT: BB1.outer
+; CHECK: BB1.backedge
+
+define i32 @test(i1 %loop_cond, i1 %exit_cond, i32 %init) {
+entry:
+  br label %BB1
+
+BB1:
+  %indvar = phi i32 [%indvar, %BB1], [%inc, %BB2], [%init, %entry]
+  call void @f() convergent
+  br i1 %loop_cond, label %BB1, label %BB2
+
+BB2:
+  %inc = add i32 %indvar, 1
+  br i1 %exit_cond, label %exit, label %BB1
+
+exit:
+  ret i32 %inc
+}
+
+declare void @f() convergent


        


More information about the llvm-commits mailing list