summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans Wennborg <hans@hanshq.net>2019-09-05 09:57:35 +0000
committerHans Wennborg <hans@hanshq.net>2019-09-05 09:57:35 +0000
commitc5d3eed5bcf785279ddceb1feb15ce53ee3c5b2a (patch)
tree943bbafaefd182d4ddb87c5d7ace4836f13c18fa
parent20e8d72470e72eabc1368e341f04c7723718f6bc (diff)
downloadllvm-c5d3eed5bcf785279ddceb1feb15ce53ee3c5b2a.tar.gz
Merging r370720 and r370721:
------------------------------------------------------------------------ r370720 | bjope | 2019-09-03 11:33:40 +0200 (Tue, 03 Sep 2019) | 13 lines [LV] Precommit test case showing miscompile from PR43166. NFC Summary: Precommit test case showing miscompile from PR43166. Reviewers: fhahn, Ayal Reviewed By: fhahn Subscribers: rkruppe, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D67072 ------------------------------------------------------------------------ ------------------------------------------------------------------------ r370721 | bjope | 2019-09-03 11:33:55 +0200 (Tue, 03 Sep 2019) | 20 lines [LV] Fix miscompiles by adding non-header PHI nodes to AllowedExit Summary: Fold-tail currently supports reduction last-vector-value live-out's, but has yet to support last-scalar-value live-outs, including non-header phi's. As it relies on AllowedExit in order to detect them and bail out we need to add the non-header PHI nodes to AllowedExit, otherwise we end up with miscompiles. Solves https://bugs.llvm.org/show_bug.cgi?id=43166 Reviewers: fhahn, Ayal Reviewed By: fhahn, Ayal Subscribers: anna, hiraditya, rkruppe, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D67074 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_90@371044 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h4
-rw-r--r--lib/Transforms/Vectorize/LoopVectorizationLegality.cpp1
-rw-r--r--test/Transforms/LoopVectorize/pr43166-fold-tail-by-masking.ll165
3 files changed, 168 insertions, 2 deletions
diff --git a/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h b/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
index b144006e2628..d7c1c2738ffe 100644
--- a/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
+++ b/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h
@@ -452,8 +452,8 @@ private:
/// Holds the widest induction type encountered.
Type *WidestIndTy = nullptr;
- /// Allowed outside users. This holds the induction and reduction
- /// vars which can be accessed from outside the loop.
+ /// Allowed outside users. This holds the variables that can be accessed from
+ /// outside the loop.
SmallPtrSet<Value *, 4> AllowedExit;
/// Can we assume the absence of NaNs.
diff --git a/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp b/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
index 6ef8dc2d3cd7..138f18e49c92 100644
--- a/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
+++ b/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
@@ -631,6 +631,7 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
// Unsafe cyclic dependencies with header phis are identified during
// legalization for reduction, induction and first order
// recurrences.
+ AllowedExit.insert(&I);
continue;
}
diff --git a/test/Transforms/LoopVectorize/pr43166-fold-tail-by-masking.ll b/test/Transforms/LoopVectorize/pr43166-fold-tail-by-masking.ll
new file mode 100644
index 000000000000..d3a8b91ec8e7
--- /dev/null
+++ b/test/Transforms/LoopVectorize/pr43166-fold-tail-by-masking.ll
@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -loop-vectorize -force-vector-width=4 -S | FileCheck %s
+
+
+; Test cases below are reduced (and slightly modified) reproducers based on a
+; problem seen when compiling a C program like this:
+;
+; #include <stdint.h>
+; #include <stdio.h>
+;
+; int y = 0;
+; int b = 1;
+; int d = 1;
+;
+; int main() {
+; #pragma clang loop vectorize_width(4)
+; for (int i = 0; i < 3; ++i) {
+; b = (y == 0) ? d : (d / y);
+; }
+;
+; if (b == 1)
+; printf("GOOD!\n");
+; else
+; printf("BAD!\n");
+; }
+;
+; When compiled+executed using
+; build-all/bin/clang -O1 lv-bug.c && ./a.out
+; the result is "GOOD!"
+;
+; When compiled+executed using
+; build-all/bin/clang -O1 lv-bug.c -fvectorize && ./a.out
+; the result is "BAD!"
+
+
+; This test case miscompiled with clang 8.0.0 (see PR43166), now we get
+; loop not vectorized: Cannot fold tail by masking in the presence of live outs.
+; instead.
+define i64 @test1(i64 %y) {
+; CHECK-LABEL: @test1(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_BODY:%.*]]
+; CHECK: for.body:
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[COND_END:%.*]] ]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], 0
+; CHECK-NEXT: br i1 [[CMP]], label [[COND_END]], label [[COND_FALSE:%.*]]
+; CHECK: cond.false:
+; CHECK-NEXT: [[DIV:%.*]] = xor i64 3, [[Y]]
+; CHECK-NEXT: br label [[COND_END]]
+; CHECK: cond.end:
+; CHECK-NEXT: [[COND:%.*]] = phi i64 [ [[DIV]], [[COND_FALSE]] ], [ 77, [[FOR_BODY]] ]
+; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I]], 1
+; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], 3
+; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]]
+; CHECK: for.cond.cleanup:
+; CHECK-NEXT: [[COND_LCSSA:%.*]] = phi i64 [ [[COND]], [[COND_END]] ]
+; CHECK-NEXT: ret i64 [[COND_LCSSA]]
+;
+entry:
+ br label %for.body
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %cond.end ]
+ %cmp = icmp eq i64 %y, 0
+ br i1 %cmp, label %cond.end, label %cond.false
+
+cond.false:
+ %div = xor i64 3, %y
+ br label %cond.end
+
+cond.end:
+ %cond = phi i64 [ %div, %cond.false ], [ 77, %for.body ]
+ %inc = add nuw nsw i32 %i, 1
+ %exitcond = icmp eq i32 %inc, 3
+ br i1 %exitcond, label %for.cond.cleanup, label %for.body
+
+for.cond.cleanup:
+ ret i64 %cond
+}
+
+; This test case miscompiled with clang 8.0.0 (see PR43166), now we get
+; loop not vectorized: Cannot fold tail by masking in the presence of live outs.
+; instead.
+define i64 @test2(i64 %y) {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_BODY:%.*]]
+; CHECK: for.body:
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[COND_END:%.*]] ]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], 0
+; CHECK-NEXT: br i1 [[CMP]], label [[COND_END]], label [[COND_FALSE:%.*]]
+; CHECK: cond.false:
+; CHECK-NEXT: br label [[COND_END]]
+; CHECK: cond.end:
+; CHECK-NEXT: [[COND:%.*]] = phi i64 [ 55, [[COND_FALSE]] ], [ 77, [[FOR_BODY]] ]
+; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I]], 1
+; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], 3
+; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]]
+; CHECK: for.cond.cleanup:
+; CHECK-NEXT: [[COND_LCSSA:%.*]] = phi i64 [ [[COND]], [[COND_END]] ]
+; CHECK-NEXT: ret i64 [[COND_LCSSA]]
+;
+entry:
+ br label %for.body
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %cond.end ]
+ %cmp = icmp eq i64 %y, 0
+ br i1 %cmp, label %cond.end, label %cond.false
+
+cond.false:
+ br label %cond.end
+
+cond.end:
+ %cond = phi i64 [ 55, %cond.false ], [ 77, %for.body ]
+ %inc = add nuw nsw i32 %i, 1
+ %exitcond = icmp eq i32 %inc, 3
+ br i1 %exitcond, label %for.cond.cleanup, label %for.body
+
+for.cond.cleanup:
+ ret i64 %cond
+}
+
+; This test case miscompiled with clang 8.0.0 (see PR43166), now we get
+; loop not vectorized: Cannot fold tail by masking in the presence of live outs.
+; instead.
+define i32 @test3(i64 %y) {
+; CHECK-LABEL: @test3(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_BODY:%.*]]
+; CHECK: for.body:
+; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[COND_END:%.*]] ]
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[Y:%.*]], 0
+; CHECK-NEXT: br i1 [[CMP]], label [[COND_END]], label [[COND_FALSE:%.*]]
+; CHECK: cond.false:
+; CHECK-NEXT: br label [[COND_END]]
+; CHECK: cond.end:
+; CHECK-NEXT: [[COND:%.*]] = phi i32 [ 55, [[COND_FALSE]] ], [ [[I]], [[FOR_BODY]] ]
+; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I]], 1
+; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i32 [[INC]], 3
+; CHECK-NEXT: br i1 [[EXITCOND]], label [[FOR_COND_CLEANUP:%.*]], label [[FOR_BODY]]
+; CHECK: for.cond.cleanup:
+; CHECK-NEXT: [[COND_LCSSA:%.*]] = phi i32 [ [[COND]], [[COND_END]] ]
+; CHECK-NEXT: ret i32 [[COND_LCSSA]]
+;
+entry:
+ br label %for.body
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %cond.end ]
+ %cmp = icmp eq i64 %y, 0
+ br i1 %cmp, label %cond.end, label %cond.false
+
+cond.false:
+ br label %cond.end
+
+cond.end:
+ %cond = phi i32 [ 55, %cond.false ], [ %i, %for.body ]
+ %inc = add nuw nsw i32 %i, 1
+ %exitcond = icmp eq i32 %inc, 3
+ br i1 %exitcond, label %for.cond.cleanup, label %for.body
+
+for.cond.cleanup:
+ ret i32 %cond
+}