[PATCH] D98602: [SCCP] Avoid modifying AdditionalUsers while iterating over it

Dimitry Andric via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 14 09:46:09 PDT 2021


dim created this revision.
dim added reviewers: ahatanak, hans, kazu, fhahn, nikic, lebedev.ri.
Herald added a subscriber: hiraditya.
dim requested review of this revision.
Herald added a project: LLVM.

When run under valgrind, or with a malloc that poisons freed memory,
this can lead to segfaults or other problems.

To avoid modifying the AdditionalUsers DenseMap while still iterating,
save the instructions to be notified in a separate SmallPtrSet, and use
this to later call OperandChangedState on each instruction.

Fixes PR49582.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D98602

Files:
  llvm/lib/Transforms/Scalar/SCCP.cpp


Index: llvm/lib/Transforms/Scalar/SCCP.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/SCCP.cpp
+++ llvm/lib/Transforms/Scalar/SCCP.cpp
@@ -533,9 +533,17 @@
 
     auto Iter = AdditionalUsers.find(I);
     if (Iter != AdditionalUsers.end()) {
-      for (User *U : Iter->second)
-        if (auto *UI = dyn_cast<Instruction>(U))
-          OperandChangedState(UI);
+      // Cache Instructions to update and update later
+      // in order to avoid iterator invalidation
+      SmallPtrSet<Instruction *, 2> toNotify;
+      for (User *U : Iter->second) {
+        if (auto *UI = dyn_cast<Instruction>(U)) {
+          toNotify.insert(UI);
+        }
+      }
+      for (Instruction *UI : toNotify) {
+        OperandChangedState(UI);
+      }
     }
   }
   void handleCallOverdefined(CallBase &CB);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98602.330517.patch
Type: text/x-patch
Size: 857 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210314/d5163994/attachment.bin>


More information about the llvm-commits mailing list