[PATCH] D147429: [GuardWidening] Fix the crash while replacing the users of poison.

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 3 03:21:12 PDT 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2b9509627caa: [GuardWidening] Fix the crash while replacing the users of poison. (authored by skatkov).

Changed prior to commit:
  https://reviews.llvm.org/D147429?vs=510436&id=510441#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147429/new/

https://reviews.llvm.org/D147429

Files:
  llvm/lib/Transforms/Scalar/GuardWidening.cpp
  llvm/test/Transforms/GuardWidening/hang.ll
  llvm/test/Transforms/GuardWidening/posion.ll


Index: llvm/test/Transforms/GuardWidening/posion.ll
===================================================================
--- llvm/test/Transforms/GuardWidening/posion.ll
+++ llvm/test/Transforms/GuardWidening/posion.ll
@@ -219,3 +219,34 @@
   %landing_pad = landingpad { ptr, i32 } cleanup
   ret void
 }
+
+declare void @dummy_vec(<4 x i1> %arg)
+
+define void @freeze_poison(i1 %c, i1 %g) {
+; CHECK-LABEL: @freeze_poison(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[DOTGW_FR:%.*]] = freeze i1 poison
+; CHECK-NEXT:    br i1 [[C:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
+; CHECK:       left:
+; CHECK-NEXT:    call void @dummy_vec(<4 x i1> <i1 false, i1 poison, i1 poison, i1 poison>)
+; CHECK-NEXT:    ret void
+; CHECK:       right:
+; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[G:%.*]], [[DOTGW_FR]]
+; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
+; CHECK-NEXT:    ret void
+;
+entry:
+  br i1 %c, label %left, label %right
+
+left:
+  call void @dummy_vec(<4 x i1> <i1 0, i1 poison, i1 poison, i1 poison>)
+  ret void
+
+
+right:
+  call void (i1, ...) @llvm.experimental.guard(i1 %g) [ "deopt"() ]
+  call void (i1, ...) @llvm.experimental.guard(i1 poison) [ "deopt"() ]
+  ret void
+
+}
+
Index: llvm/test/Transforms/GuardWidening/hang.ll
===================================================================
--- llvm/test/Transforms/GuardWidening/hang.ll
+++ llvm/test/Transforms/GuardWidening/hang.ll
@@ -10,7 +10,7 @@
 ; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"() ]
 ; CHECK-NEXT:    br label [[BB2:%.*]]
 ; CHECK:       bb2:
-; CHECK-NEXT:    br i1 [[DOTGW_FR]], label [[BB3:%.*]], label [[BB2]]
+; CHECK-NEXT:    br i1 poison, label [[BB3:%.*]], label [[BB2]]
 ; CHECK:       bb3:
 ; CHECK-NEXT:    [[CALL:%.*]] = call i64 (...) @llvm.experimental.deoptimize.i64() [ "deopt"() ]
 ; CHECK-NEXT:    ret i64 [[CALL]]
Index: llvm/lib/Transforms/Scalar/GuardWidening.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/GuardWidening.cpp
+++ llvm/lib/Transforms/Scalar/GuardWidening.cpp
@@ -627,13 +627,23 @@
     I->dropPoisonGeneratingFlagsAndMetadata();
 
   Value *Result = Orig;
+  SmallPtrSet<Value *, 16> NeedFreezeSet(NeedFreeze.begin(), NeedFreeze.end());
   for (Value *V : NeedFreeze) {
     auto *FreezeInsertPt = getFreezeInsertPt(V, DT);
     FreezeInst *FI = new FreezeInst(V, V->getName() + ".gw.fr", FreezeInsertPt);
     ++FreezeAdded;
     if (V == Orig)
       Result = FI;
-    V->replaceUsesWithIf(FI, [&](Use & U)->bool { return U.getUser() != FI; });
+    if (isa<Instruction>(V) || isa<Argument>(V))
+      V->replaceUsesWithIf(
+          FI, [&](const Use & U)->bool { return U.getUser() != FI; });
+    else
+      // if it is a constant or global, just make a change only in instructions
+      // we visited and which are not marked as NeedFreeze itself.
+      V->replaceUsesWithIf(FI, [&](const Use & U)->bool {
+        return U.getUser() != FI && Visited.contains(U) &&
+               !NeedFreezeSet.count(U);
+      });
   }
 
   return Result;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147429.510441.patch
Type: text/x-patch
Size: 3134 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230403/2e752135/attachment.bin>


More information about the llvm-commits mailing list