[llvm] 2b95096 - [GuardWidening] Fix the crash while replacing the users of poison.

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


Author: Serguei Katkov
Date: 2023-04-03T17:20:38+07:00
New Revision: 2b9509627caa2b0ca27eb1b05099eb016491f027

URL: https://github.com/llvm/llvm-project/commit/2b9509627caa2b0ca27eb1b05099eb016491f027
DIFF: https://github.com/llvm/llvm-project/commit/2b9509627caa2b0ca27eb1b05099eb016491f027.diff

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

When we replace poison with freeze poison it might appear
that user of poison is a constant (for example vector constant).

In this case we will get that constant will get non-constant operand.

Moreover replacing poison and GlobalValue everywhere in module seems
to be overkill. So the solution will be just make a replacement
only in instructions we visited (contributing to hoisted condition).
Moreover if user of posion is constant, this constant also should need
a freeze and it does not make sense to replace poison with frozen version,
just freeze another constant.

Reviewed By: mkazantsev
Differential Revision: https://reviews.llvm.org/D147429

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/GuardWidening.cpp b/llvm/lib/Transforms/Scalar/GuardWidening.cpp
index 032ce9ba9b82d..fef3347a18877 100644
--- a/llvm/lib/Transforms/Scalar/GuardWidening.cpp
+++ b/llvm/lib/Transforms/Scalar/GuardWidening.cpp
@@ -627,13 +627,23 @@ Value *GuardWideningImpl::freezeAndPush(Value *Orig, Instruction *InsertPt) {
     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;

diff  --git a/llvm/test/Transforms/GuardWidening/hang.ll b/llvm/test/Transforms/GuardWidening/hang.ll
index 6123d5d69bab7..bb9f564a864ab 100644
--- a/llvm/test/Transforms/GuardWidening/hang.ll
+++ b/llvm/test/Transforms/GuardWidening/hang.ll
@@ -10,7 +10,7 @@ define i64 @test() {
 ; 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]]

diff  --git a/llvm/test/Transforms/GuardWidening/posion.ll b/llvm/test/Transforms/GuardWidening/posion.ll
index d55a36774be20..3623f014413e8 100644
--- a/llvm/test/Transforms/GuardWidening/posion.ll
+++ b/llvm/test/Transforms/GuardWidening/posion.ll
@@ -219,3 +219,34 @@ exception:
   %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
+
+}
+


        


More information about the llvm-commits mailing list