[llvm] 9400270 - [SLP]Fix comparator for vector operands of extractelements in PHICompare
Alexey Bataev via llvm-commits
llvm-commits at lists.llvm.org
Thu May 1 14:28:28 PDT 2025
Author: Alexey Bataev
Date: 2025-05-01T14:28:20-07:00
New Revision: 9400270449bc848fa8dd2a7575fc4fc102e34d0d
URL: https://github.com/llvm/llvm-project/commit/9400270449bc848fa8dd2a7575fc4fc102e34d0d
DIFF: https://github.com/llvm/llvm-project/commit/9400270449bc848fa8dd2a7575fc4fc102e34d0d.diff
LOG: [SLP]Fix comparator for vector operands of extractelements in PHICompare
Need to make comparator to follow strict-weak ordering to fix compiler
crashes.
Fixes #138178
Added:
llvm/test/Transforms/SLPVectorizer/X86/phi-comparator-fix-vec-ops-compare.ll
Modified:
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index a20bd990ded75..5df38bc96fc9c 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -24069,10 +24069,23 @@ bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
const auto *V1 = dyn_cast<Instruction>(E1->getVectorOperand());
const auto *V2 = dyn_cast<Instruction>(E2->getVectorOperand());
if (V1 != V2) {
- if (!V1 || !V2)
- continue;
- if (V1->getParent() != V2->getParent())
- continue;
+ if (V1 && !V2)
+ return true;
+ if (!V1 && V2)
+ return false;
+ DomTreeNodeBase<BasicBlock> *NodeI1 =
+ DT->getNode(V1->getParent());
+ DomTreeNodeBase<BasicBlock> *NodeI2 =
+ DT->getNode(V2->getParent());
+ if (!NodeI1)
+ return NodeI2 != nullptr;
+ if (!NodeI2)
+ return false;
+ assert((NodeI1 == NodeI2) ==
+ (NodeI1->getDFSNumIn() == NodeI2->getDFSNumIn()) &&
+ "Different nodes should have
diff erent DFS numbers");
+ if (NodeI1 != NodeI2)
+ return NodeI1->getDFSNumIn() < NodeI2->getDFSNumIn();
return V1->comesBefore(V2);
}
// If we have the same vector operand, try to sort by constant
diff --git a/llvm/test/Transforms/SLPVectorizer/X86/phi-comparator-fix-vec-ops-compare.ll b/llvm/test/Transforms/SLPVectorizer/X86/phi-comparator-fix-vec-ops-compare.ll
new file mode 100644
index 0000000000000..42d3d8049a897
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/phi-comparator-fix-vec-ops-compare.ll
@@ -0,0 +1,46 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S --passes=slp-vectorizer -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
+
+define void @test({ <2 x float>, float } %0, <2 x float> %1, i1 %2) {
+; CHECK-LABEL: define void @test(
+; CHECK-SAME: { <2 x float>, float } [[TMP0:%.*]], <2 x float> [[TMP1:%.*]], i1 [[TMP2:%.*]]) {
+; CHECK-NEXT: [[TMP4:%.*]] = extractvalue { <2 x float>, float } [[TMP0]], 0
+; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x float> [[TMP4]], i64 0
+; CHECK-NEXT: [[TMP6:%.*]] = extractelement <2 x float> [[TMP1]], i64 1
+; CHECK-NEXT: [[TMP7:%.*]] = extractelement <2 x float> [[TMP1]], i64 0
+; CHECK-NEXT: br i1 [[TMP2]], label %[[BB9:.*]], label %[[BB8:.*]]
+; CHECK: [[BB8]]:
+; CHECK-NEXT: br label %[[BB9]]
+; CHECK: [[BB9]]:
+; CHECK-NEXT: [[TMP10:%.*]] = phi float [ 0.000000e+00, %[[BB8]] ], [ [[TMP7]], [[TMP3:%.*]] ]
+; CHECK-NEXT: [[TMP11:%.*]] = phi float [ 0.000000e+00, %[[BB8]] ], [ [[TMP6]], [[TMP3]] ]
+; CHECK-NEXT: [[TMP12:%.*]] = phi float [ 0.000000e+00, %[[BB8]] ], [ [[TMP5]], [[TMP3]] ]
+; CHECK-NEXT: [[TMP13:%.*]] = fpext float [[TMP12]] to double
+; CHECK-NEXT: [[TMP14:%.*]] = fpext float [[TMP11]] to double
+; CHECK-NEXT: [[TMP15:%.*]] = tail call i32 (ptr, ptr, ...) @fprintf(ptr null, ptr null, double [[TMP13]], double [[TMP14]], double 0.000000e+00)
+; CHECK-NEXT: [[TMP16:%.*]] = fpext float [[TMP10]] to double
+; CHECK-NEXT: [[TMP17:%.*]] = tail call i32 (ptr, ptr, ...) @fprintf(ptr null, ptr null, double 0.000000e+00, double [[TMP16]], double 0.000000e+00)
+; CHECK-NEXT: ret void
+;
+ %4 = extractvalue { <2 x float>, float } %0, 0
+ %5 = extractelement <2 x float> %4, i64 0
+ %6 = extractelement <2 x float> %1, i64 1
+ %7 = extractelement <2 x float> %1, i64 0
+ br i1 %2, label %9, label %8
+
+8:
+ br label %9
+
+9:
+ %10 = phi float [ 0.000000e+00, %8 ], [ %7, %3 ]
+ %11 = phi float [ 0.000000e+00, %8 ], [ %6, %3 ]
+ %12 = phi float [ 0.000000e+00, %8 ], [ %5, %3 ]
+ %13 = fpext float %12 to double
+ %14 = fpext float %11 to double
+ %15 = tail call i32 (ptr, ptr, ...) @fprintf(ptr null, ptr null, double %13, double %14, double 0.000000e+00)
+ %16 = fpext float %10 to double
+ %17 = tail call i32 (ptr, ptr, ...) @fprintf(ptr null, ptr null, double 0.000000e+00, double %16, double 0.000000e+00)
+ ret void
+}
+
+declare i32 @fprintf(ptr, ptr, ...)
More information about the llvm-commits
mailing list