-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[Matrix] Propagate shape information through Select insts #141876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-llvm-transforms Author: Jon Roelofs (jroelofs) ChangesFull diff: https://github.com/llvm/llvm-project/pull/141876.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
index 756a72e6d97bc..6cfe077b602d3 100644
--- a/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
+++ b/llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
@@ -269,6 +269,15 @@ computeShapeInfoForInst(Instruction *I,
return OpShape->second;
}
+ if (isa<SelectInst>(I)) {
+ auto OpShape = ShapeMap.find(I->getOperand(1));
+ if (OpShape != ShapeMap.end())
+ return OpShape->second;
+ OpShape = ShapeMap.find(I->getOperand(2));
+ if (OpShape != ShapeMap.end())
+ return OpShape->second;
+ }
+
if (isUniformShape(I)) {
// Find the first operand that has a known shape and use that.
for (auto &Op : I->operands()) {
@@ -623,7 +632,7 @@ class LowerMatrixIntrinsics {
default:
return false;
}
- return isUniformShape(V) || isa<StoreInst>(V) || isa<LoadInst>(V);
+ return isUniformShape(V) || isa<StoreInst>(V) || isa<LoadInst>(V) || isa<SelectInst>(V);
}
/// Propagate the shape information of instructions to their users.
@@ -710,6 +719,12 @@ class LowerMatrixIntrinsics {
} else if (isa<StoreInst>(V)) {
// Nothing to do. We forward-propagated to this so we would just
// backward propagate to an instruction with an already known shape.
+ } else if (auto *Select = dyn_cast<SelectInst>(V)) {
+ ShapeInfo Shape = ShapeMap[V];
+ if (setShapeInfo(Select->getOperand(1), Shape))
+ pushInstruction(Select, WorkList);
+ if (setShapeInfo(Select->getOperand(2), Shape))
+ pushInstruction(Select, WorkList);
} else if (isUniformShape(V)) {
// Propagate to all operands.
ShapeInfo Shape = ShapeMap[V];
@@ -1068,6 +1083,8 @@ class LowerMatrixIntrinsics {
Changed |= VisitBinaryOperator(BinOp);
if (auto *UnOp = dyn_cast<UnaryOperator>(Inst))
Changed |= VisitUnaryOperator(UnOp);
+ if (auto *Select = dyn_cast<SelectInst>(Inst))
+ Changed |= VisitSelectInst(Select);
if (match(Inst, m_Load(m_Value(Op1))))
Changed |= VisitLoad(cast<LoadInst>(Inst), Op1, Builder);
else if (match(Inst, m_Store(m_Value(Op1), m_Value(Op2))))
@@ -2198,6 +2215,35 @@ class LowerMatrixIntrinsics {
return true;
}
+ /// Lower selects, if shape information is available.
+ bool VisitSelectInst(SelectInst *Inst) {
+ auto I = ShapeMap.find(Inst);
+ if (I == ShapeMap.end())
+ return false;
+
+ Value *Cond = Inst->getOperand(0);
+ Value *OpA = Inst->getOperand(1);
+ Value *OpB = Inst->getOperand(2);
+
+ IRBuilder<> Builder(Inst);
+ ShapeInfo &Shape = I->second;
+
+ MatrixTy Result;
+ MatrixTy A = getMatrix(OpA, Shape, Builder);
+ MatrixTy B = getMatrix(OpB, Shape, Builder);
+
+ for (unsigned I = 0; I < Shape.getNumVectors(); ++I) {
+ auto *Sel = Builder.CreateSelect(Cond, A.getVector(I), B.getVector(I));
+ Result.addVector(Sel);
+ }
+
+ finalizeLowering(Inst,
+ Result.addNumComputeOps(getNumOps(Result.getVectorTy()) *
+ Result.getNumVectors()),
+ Builder);
+ return true;
+ }
+
/// Helper to linearize a matrix expression tree into a string. Currently
/// matrix expressions are linarized by starting at an expression leaf and
/// linearizing bottom up.
diff --git a/llvm/test/Transforms/LowerMatrixIntrinsics/select.ll b/llvm/test/Transforms/LowerMatrixIntrinsics/select.ll
new file mode 100644
index 0000000000000..acfd9228ee17a
--- /dev/null
+++ b/llvm/test/Transforms/LowerMatrixIntrinsics/select.ll
@@ -0,0 +1,26 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes='lower-matrix-intrinsics' -S < %s | FileCheck %s
+
+define void @select_2x2(i1 %cond, ptr %lhs, ptr %rhs, ptr %out) {
+; CHECK-LABEL: @select_2x2(
+; CHECK-NEXT: [[COL_LOAD:%.*]] = load <2 x float>, ptr [[LHS:%.*]], align 16
+; CHECK-NEXT: [[VEC_GEP:%.*]] = getelementptr float, ptr [[LHS]], i64 2
+; CHECK-NEXT: [[COL_LOAD1:%.*]] = load <2 x float>, ptr [[VEC_GEP]], align 8
+; CHECK-NEXT: [[COL_LOAD2:%.*]] = load <2 x float>, ptr [[RHS:%.*]], align 16
+; CHECK-NEXT: [[VEC_GEP3:%.*]] = getelementptr float, ptr [[RHS]], i64 2
+; CHECK-NEXT: [[COL_LOAD4:%.*]] = load <2 x float>, ptr [[VEC_GEP3]], align 8
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[COND:%.*]], <2 x float> [[COL_LOAD]], <2 x float> [[COL_LOAD2]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND]], <2 x float> [[COL_LOAD1]], <2 x float> [[COL_LOAD4]]
+; CHECK-NEXT: store <2 x float> [[TMP1]], ptr [[OUT:%.*]], align 16
+; CHECK-NEXT: [[VEC_GEP5:%.*]] = getelementptr float, ptr [[OUT]], i64 2
+; CHECK-NEXT: store <2 x float> [[TMP2]], ptr [[VEC_GEP5]], align 8
+; CHECK-NEXT: ret void
+;
+ %lhsv = load <4 x float>, ptr %lhs
+ %rhsv = load <4 x float>, ptr %rhs
+ %op = select i1 %cond, <4 x float> %lhsv, <4 x float> %rhsv
+ %opt = call <4 x float> @llvm.matrix.transpose(<4 x float> %op, i32 2, i32 2)
+ %optt = call <4 x float> @llvm.matrix.transpose(<4 x float> %opt, i32 2, i32 2)
+ store <4 x float> %optt, ptr %out
+ ret void
+}
|
fdc3e42
to
85d3716
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
85d3716
to
3ad9143
Compare
3ad9143
to
4040d3f
Compare
; | ||
%lhsv = load <4 x float>, ptr %lhs | ||
%rhsv = load <4 x float>, ptr %rhs | ||
%op = select i1 %cond, <4 x float> %lhsv, <4 x float> %rhsv |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably also have a test where %cond is a vector.
And possibly one where we have shape info for %cond, to make sure we don't pick it for the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should pick that operand for the shape info if we have it and it's a vector, right?
auto OpShape = ShapeMap.find(I->getOperand(1)); | ||
if (OpShape != ShapeMap.end()) | ||
return OpShape->second; | ||
OpShape = ShapeMap.find(I->getOperand(2)); | ||
if (OpShape != ShapeMap.end()) | ||
return OpShape->second; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Picking the first operand with shape info should match what we do for uniform shapes, would probably be good to add a similar comment here (or unify the code below by only iterating over op1 & op2 for selects, via something like drop_front(I->operands())
not sure how much extra code that would be)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh, that's a little nicer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should almost match, but not quite: we need to ignore the condition operand for shape info iff it is not a vector.
No description provided.