[PATCH] D139152: [SLPVectorizer] Don't deref std::end() of object

Youngsuk Kim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 1 15:45:43 PST 2022


JOE1994 created this revision.
Herald added subscribers: vporpo, hiraditya.
Herald added a project: All.
JOE1994 requested review of this revision.
Herald added a subscriber: pcwang-thead.
Herald added a project: LLVM.

Dereferencing std::end() of an object can lead to undefined behavior
(e.g. segmentation fault, invalid memory access).

This commit updates SLPVectorizer to only dereference the iterator from
find_if_not() if it is not a std::end().

This prevents SLPVectorizer from crashing with certain workloads.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139152

Files:
  llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp


Index: llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -7955,8 +7955,11 @@
   // bundle. The end of the bundle is marked by null ScheduleData.
   if (BlocksSchedules.count(BB)) {
     Value *V = E->isOneOf(E->Scalars.back());
-    if (doesNotNeedToBeScheduled(V))
-      V = *find_if_not(E->Scalars, doesNotNeedToBeScheduled);
+    if (doesNotNeedToBeScheduled(V)) {
+      auto AltValIter = find_if_not(E->Scalars, doesNotNeedToBeScheduled);
+      if (AltValIter != std::end(E->Scalars))
+        V = *AltValIter;
+    }
     auto *Bundle = BlocksSchedules[BB]->getScheduleData(V);
     if (Bundle && Bundle->isPartOfBundle())
       for (; Bundle; Bundle = Bundle->NextInBundle)
@@ -9785,8 +9788,11 @@
       doesNotNeedToSchedule(VL))
     return;
 
-  if (doesNotNeedToBeScheduled(OpValue))
-    OpValue = *find_if_not(VL, doesNotNeedToBeScheduled);
+  if (doesNotNeedToBeScheduled(OpValue)) {
+    auto AltValIter = find_if_not(VL, doesNotNeedToBeScheduled);
+    if (AltValIter != std::end(VL))
+      OpValue = *AltValIter;
+  }
   ScheduleData *Bundle = getScheduleData(OpValue);
   LLVM_DEBUG(dbgs() << "SLP:  cancel scheduling of " << *Bundle << "\n");
   assert(!Bundle->IsScheduled &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139152.479458.patch
Type: text/x-patch
Size: 1373 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221201/0a914e56/attachment-0001.bin>


More information about the llvm-commits mailing list