[llvm-branch-commits] [llvm] release/23.x: [Matrix] De-duplicate reshaped matrixes used as incoming values for phi. (#211210) (PR #211498)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Jul 23 01:49:35 PDT 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/211498
Backport 0e78045
Requested by: @fhahn
>From 876b122f929def649f8421ca469a48f779e61f9e Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Thu, 23 Jul 2026 09:21:09 +0100
Subject: [PATCH] [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)
---
.../Scalar/LowerMatrixIntrinsics.cpp | 8 ++++-
.../Transforms/LowerMatrixIntrinsics/phi.ll | 34 +++++++++++++++++++
2 files changed, 41 insertions(+), 1 deletion(-)
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