[llvm-branch-commits] [llvm] de7b2d2 - [Matrix] De-duplicate reshaped matrixes used as incoming values for phi. (#211210)
Douglas Yung via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Aug 15 08:36:48 PDT 2026
Author: Florian Hahn
Date: 2026-08-15T15:36:30Z
New Revision: de7b2d25a5c7a8fd7b22489903511b1834c121cb
URL: https://github.com/llvm/llvm-project/commit/de7b2d25a5c7a8fd7b22489903511b1834c121cb
DIFF: https://github.com/llvm/llvm-project/commit/de7b2d25a5c7a8fd7b22489903511b1834c121cb.diff
LOG: [Matrix] De-duplicate reshaped matrixes used as incoming values for phi. (#211210)
Phis can have multiple incoming entries for the same block. In that
case, all incoming values for the block must be the same.
Update visitPHI to avoid expanding the incoming matrix multiple times
for the some incoming block.
Fixes a verifier error for the newly added test case.
PR: https://github.com/llvm/llvm-project/pull/211210
(cherry picked from commit 0e780450034e4bf0c6aa6dd4b312e1e0b9a20689)
Added:
Modified:
llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
llvm/test/Transforms/LowerMatrixIntrinsics/phi.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
index fc5313b2d868f..1e363437348f9 100644
--- a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
@@ -2397,6 +2397,9 @@ class LowerMatrixIntrinsics {
Builder.SetInsertPoint(BlockIP);
MatrixTy PhiM = getMatrix(Inst, SI, Builder);
+ // Cache the reshaped columns per incoming block, so that a block listed
+ // more than once contributes identical incoming values to the new PHIs.
+ SmallDenseMap<BasicBlock *, MatrixTy> ReshapedIncoming;
for (auto [IncomingV, IncomingB] :
llvm::zip_equal(Inst->incoming_values(), Inst->blocks())) {
// getMatrix() may insert some instructions to help with reshaping. The
@@ -2407,7 +2410,10 @@ class LowerMatrixIntrinsics {
if (auto MaybeIP = IncomingInst->getInsertionPointAfterDef())
Builder.SetInsertPoint(*MaybeIP);
- MatrixTy OpM = getMatrix(IncomingV, SI, Builder);
+ auto [It, Inserted] = ReshapedIncoming.try_emplace(IncomingB);
+ if (Inserted)
+ It->second = getMatrix(IncomingV, SI, Builder);
+ const MatrixTy &OpM = It->second;
for (unsigned VI = 0, VE = PhiM.getNumVectors(); VI != VE; ++VI) {
PHINode *NewPHI = cast<PHINode>(PhiM.getVector(VI));
diff --git a/llvm/test/Transforms/LowerMatrixIntrinsics/phi.ll b/llvm/test/Transforms/LowerMatrixIntrinsics/phi.ll
index a8e71666421b4..2110d72951f10 100644
--- a/llvm/test/Transforms/LowerMatrixIntrinsics/phi.ll
+++ b/llvm/test/Transforms/LowerMatrixIntrinsics/phi.ll
@@ -861,3 +861,37 @@ exit:
%phi = phi <4 x float> [ %inv, %cont ], [ %t, %bb ]
ret <4 x float> %phi
}
+
+define <4 x float> @matrix_phi_duplicate_predecessor(ptr %arg, i32 %sw) {
+; CHECK-LABEL: @matrix_phi_duplicate_predecessor(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[SPLIT:%.*]] = load <2 x float>, ptr [[ARG:%.*]], align 16
+; CHECK-NEXT: [[VEC_GEP:%.*]] = getelementptr inbounds float, ptr [[ARG]], i64 2
+; CHECK-NEXT: [[SPLIT3:%.*]] = load <2 x float>, ptr [[VEC_GEP]], align 8
+; CHECK-NEXT: switch i32 [[SW:%.*]], label [[BB:%.*]] [
+; CHECK-NEXT: i32 0, label [[EXIT:%.*]]
+; CHECK-NEXT: i32 1, label [[EXIT]]
+; CHECK-NEXT: ]
+; CHECK: bb:
+; CHECK-NEXT: br label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: [[PHI1:%.*]] = phi <2 x float> [ [[SPLIT]], [[ENTRY:%.*]] ], [ [[SPLIT]], [[ENTRY]] ], [ zeroinitializer, [[BB]] ]
+; CHECK-NEXT: [[PHI2:%.*]] = phi <2 x float> [ [[SPLIT3]], [[ENTRY]] ], [ [[SPLIT3]], [[ENTRY]] ], [ zeroinitializer, [[BB]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = shufflevector <2 x float> [[PHI1]], <2 x float> [[PHI2]], <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+; CHECK-NEXT: ret <4 x float> [[TMP0]]
+;
+entry:
+ %m = load <4 x float>, ptr %arg
+ switch i32 %sw, label %bb [
+ i32 0, label %exit
+ i32 1, label %exit
+ ]
+
+bb:
+ %t = call <4 x float> @llvm.matrix.transpose.v4f32(<4 x float> zeroinitializer, i32 2, i32 2)
+ br label %exit
+
+exit:
+ %phi = phi <4 x float> [ %m, %entry ], [ %m, %entry ], [ %t, %bb ]
+ ret <4 x float> %phi
+}
More information about the llvm-branch-commits
mailing list