[llvm] [LoopVectorize] Add support for vectorisation of more early exit loops (PR #88385)

Graham Hunter via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 02:31:18 PDT 2024


================
@@ -1489,4 +1489,82 @@ void InterleaveGroup<Instruction>::addMetadata(Instruction *NewInst) const {
                  [](std::pair<int, Instruction *> p) { return p.second; });
   propagateMetadata(NewInst, VL);
 }
+
+bool loopMayFault(const Loop *L, ScalarEvolution *SE,
+                  const DenseMap<Value *, SmallVector<const Value *, 16>>
+                      &UnderlyingObjects) {
+  auto &DL = L->getHeader()->getModule()->getDataLayout();
+  for (auto &UO : UnderlyingObjects) {
+    // TODO: For now if we encounter more than one underlying object we just
+    // assume it could fault. However, with more analysis it's possible to look
+    // at all of them and calculate a common range of permitted GEP indices.
+    if (UO.second.size() != 1)
+      return true;
+
+    // For now only the simplest cases are permitted, but this could be
+    // extended further.
+    auto *GEP = dyn_cast<GetElementPtrInst>(UO.first);
+    if (!GEP || GEP->getPointerOperand() != UO.second[0])
+      return true;
+
+    // The only current supported case for 2 GEP indices is when accessing an
+    // array, i.e.
+    //   getelementptr [32 x i32], ptr %arr, i64 0, i64 %ind
+    Value *GEPInd;
+    Type *GEPElemType;
+    if (GEP->getNumIndices() == 2) {
----------------
huntergr-arm wrote:

Is this actually needed, instead of just working with the SCEVAddRecExpr terms? e.g. getSCEVAtScope to figure out the exit value.

Maybe it would be better to rethink this in terms of accesses being safe; even if a GEP uses one type for its offset calculations, a load or store could access a larger type with that address.

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


More information about the llvm-commits mailing list