From cbf7b52a65890ea72cb6319bfdf98d92dd9ddc81 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 2 Dec 2023 15:22:08 +0000 Subject: [PATCH] [VPlan] Properly update reduction live-out after placing select. After inserting a select for the final value, update the VPlan def-use chains. At the moment, the incorrect live-out doesn't cause a mis-compile, as computing the final reduction value is not yet modeled in VPlan. --- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp | 3 +++ .../first-order-recurrence-sink-replicate-region.ll | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp index 09a6e01226ab68..496cd0dc963968 100644 --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -9271,6 +9271,9 @@ void LoopVectorizationPlanner::adjustRecipesForReductions( ? new VPInstruction(Instruction::Select, {Cond, Red, PhiR}, FMFs) : new VPInstruction(Instruction::Select, {Cond, Red, PhiR}); Result->insertBefore(&*Builder.getInsertPoint()); + Red->replaceUsesWithIf( + Result->getVPSingleValue(), + [](VPUser &U, unsigned) { return isa(&U); }); if (PreferPredicatedReductionSelect || TTI.preferPredicatedReductionSelect( PhiR->getRecurrenceDescriptor().getOpcode(), PhiTy, diff --git a/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll b/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll index f0d669ad225f8b..3198f19a8f9a9a 100644 --- a/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll +++ b/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll @@ -214,7 +214,7 @@ define i32 @sink_replicate_region_3_reduction(i32 %x, i8 %y, ptr %ptr) optsize { ; CHECK-NEXT: middle.block: ; CHECK-NEXT: No successors ; CHECK-EMPTY: -; CHECK-NEXT: Live-out i32 %res = ir<%and.red.next> +; CHECK-NEXT: Live-out i32 %res = vp<[[SEL]]> ; CHECK-NEXT: } ; entry: