[llvm] LSV: forbid load-cycles when vectorizing; fix bug (PR #104815)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 13:01:25 PDT 2024


================
@@ -996,10 +996,32 @@ bool Vectorizer::isSafeToMove(
   LLVM_DEBUG(dbgs() << "LSV: isSafeToMove(" << *ChainElem << " -> "
                     << *ChainBegin << ")\n");
 
-  assert(isa<LoadInst>(ChainElem) == IsLoadChain);
+  assert(isa<LoadInst>(ChainElem) == IsLoadChain &&
+         isa<LoadInst>(ChainBegin) == IsLoadChain);
+
   if (ChainElem == ChainBegin)
     return true;
 
+  if constexpr (IsLoadChain) {
+    // If ChainElem depends on ChainBegin, they're not safe to reorder.
+    SmallVector<Instruction *, 8> Worklist;
+    Worklist.emplace_back(ChainElem);
+    while (!Worklist.empty()) {
+      Instruction *I = Worklist.pop_back_val();
+      for (Use &O : I->operands()) {
+        if (isa<PHINode>(O))
+          continue;
+        if (auto *J = dyn_cast<Instruction>(O)) {
+          if (J == ChainBegin) {
----------------
artagnon wrote:

Actually, the code is correct, because `isSafeToMove` gets called in a loop from `std::next(ChainBegin)` to `ChainEnd`, so we are indeed checking all elements of the chain. I was wondering why we didn't pass a first-class chain, and found the answer.

https://github.com/llvm/llvm-project/pull/104815


More information about the llvm-commits mailing list