From 8dc7c350a2636bc46249322f0a69a2ab350974bf Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Tue, 7 Jul 2020 16:53:19 +0300 Subject: [PATCH] [Scalarizer] When gathering scattered scalar, don't replace it with itself The (previously-crashing) test-case would cause us to seemingly-harmlessly replace some use with something else, but we can't replace it with itself, so we would crash. --- lib/Transforms/Scalar/Scalarizer.cpp | 2 ++ test/Transforms/Scalarizer/crash-bug.ll | 29 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/Transforms/Scalar/Scalarizer.cpp b/lib/Transforms/Scalar/Scalarizer.cpp index e8fea501b1d..3d650c66a86 100644 --- a/lib/Transforms/Scalar/Scalarizer.cpp +++ b/lib/Transforms/Scalar/Scalarizer.cpp @@ -944,6 +944,8 @@ bool ScalarizerVisitor::finish() { } else { assert(CV.size() == 1 && Op->getType() == CV[0]->getType()); Res = CV[0]; + if (Op == Res) + continue; } Res->takeName(Op); Op->replaceAllUsesWith(Res); diff --git a/test/Transforms/Scalarizer/crash-bug.ll b/test/Transforms/Scalarizer/crash-bug.ll index d581707971e..97756362d42 100644 --- a/test/Transforms/Scalarizer/crash-bug.ll +++ b/test/Transforms/Scalarizer/crash-bug.ll @@ -22,3 +22,32 @@ bb3: ret void } +; See https://reviews.llvm.org/D83101#2135945 +define void @f1_crash(<2 x i16> %base, i1 %c, <2 x i16>* %ptr) { +; CHECK-LABEL: @f1_crash( +; CHECK: vector.ph: +; CHECK: %base.i0 = extractelement <2 x i16> %base, i32 0 +; CHECK: %base.i1 = extractelement <2 x i16> %base, i32 1 +; CHECK: br label %vector.body115 +; CHECK: vector.body115: ; preds = %vector.body115, %vector.ph +; CHECK: %vector.recur.i0 = phi i16 [ %base.i0, %vector.ph ], [ %wide.load125.i0, %vector.body115 ] +; CHECK: %vector.recur.i1 = phi i16 [ %base.i1, %vector.ph ], [ %wide.load125.i1, %vector.body115 ] +; CHECK: %wide.load125 = load <2 x i16>, <2 x i16>* %ptr, align 1 +; CHECK: %wide.load125.i0 = extractelement <2 x i16> %wide.load125, i32 0 +; CHECK: %wide.load125.i1 = extractelement <2 x i16> %wide.load125, i32 1 +; CHECK: br i1 %c, label %middle.block113, label %vector.body115 +; CHECK: middle.block113: ; preds = %vector.body115 +; CHECK: ret void +; CHECK: } + +vector.ph: + br label %vector.body115 + +vector.body115: + %vector.recur = phi <2 x i16> [ %base, %vector.ph ], [ %wide.load125, %vector.body115 ] + %wide.load125 = load <2 x i16>, <2 x i16>* %ptr, align 1 + br i1 %c, label %middle.block113, label %vector.body115 + +middle.block113: + ret void +}