[PATCH] D59312: AMDGPU: Fix a SIAnnotateControlFlow issue when there are multiple backedges.

Changpeng Fang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 13 10:50:13 PDT 2019


cfang created this revision.
cfang added reviewers: msearles, nhaehnle, arsenm.
Herald added subscribers: jdoerfert, t-tye, tpr, dstuttard, yaxunl, wdng, jvesely, kzhuravl.

At the exit of the loop, the compiler uses a register to remember and accumulate the number of threads that have already exited. When all active threads exit the loop, this register is used to restore the exec mask, and the execution continues for the post loop code.

When there is a "continue" in the loop, the compiler made a mistake to reset the register to 0 when the "continue" backedge is taken. This will result in some threads not executing the post loop code as they are supposed to.

This patch fixed the issue ( the test is yet to be developed).


https://reviews.llvm.org/D59312

Files:
  lib/Target/AMDGPU/SIAnnotateControlFlow.cpp


Index: lib/Target/AMDGPU/SIAnnotateControlFlow.cpp
===================================================================
--- lib/Target/AMDGPU/SIAnnotateControlFlow.cpp
+++ lib/Target/AMDGPU/SIAnnotateControlFlow.cpp
@@ -264,8 +264,17 @@
   Term->setCondition(BoolTrue);
   Value *Arg = handleLoopCondition(Cond, Broken, L, Term);
 
-  for (BasicBlock *Pred : predecessors(Target))
-    Broken->addIncoming(Pred == BB ? Arg : Int64Zero, Pred);
+  for (BasicBlock *Pred : predecessors(Target)) {
+    Value *PHIValue = Int64Zero;
+    if (Pred == BB) // Remember the value of the previous iteration.
+      PHIValue = Arg;
+    // If the backedge from Pred to Target could be executed before the exit
+    // of the loop at BB, it shoudl not reset or change "Broken", which keeps
+    // track of the number of threads exited the loop at BB.
+    else if (L->contains(Pred) && DT->dominates(Pred, BB))
+      PHIValue = Broken;
+    Broken->addIncoming(PHIValue, Pred);
+  }
 
   Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59312.190450.patch
Type: text/x-patch
Size: 1038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190313/30ecb287/attachment.bin>


More information about the llvm-commits mailing list