Skip to content

Commit 68bb005

Browse files
authored
[Matrix] Add -debug-only prints when matrices get flattened (#142078)
This is a potential source of overhead, which we might be able to alleviate in some cases. For example, static element extracts, or shuffles that pluck out a specific row. Since these diagnostics are highly specific to the pass itself and not immediately actionable for compiler users, these prints don't make a whole lot of sense as Remarks.
1 parent 9c54512 commit 68bb005

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ADT/ScopeExit.h"
2323
#include "llvm/ADT/SmallSet.h"
2424
#include "llvm/ADT/SmallVector.h"
25+
#include "llvm/ADT/Statistic.h"
2526
#include "llvm/Analysis/AliasAnalysis.h"
2627
#include "llvm/Analysis/DomTreeUpdater.h"
2728
#include "llvm/Analysis/LoopInfo.h"
@@ -40,6 +41,7 @@
4041
#include "llvm/IR/PatternMatch.h"
4142
#include "llvm/Support/Alignment.h"
4243
#include "llvm/Support/CommandLine.h"
44+
#include "llvm/Support/Compiler.h"
4345
#include "llvm/Support/Debug.h"
4446
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
4547
#include "llvm/Transforms/Utils/LoopUtils.h"
@@ -52,6 +54,10 @@ using namespace PatternMatch;
5254

5355
#define DEBUG_TYPE "lower-matrix-intrinsics"
5456

57+
STATISTIC(FlattenedMatrices, "Number of matrix flattenings");
58+
STATISTIC(ReshapedMatrices, "Number of matrix reshapes");
59+
STATISTIC(SplitMatrices, "Number of matrix splits");
60+
5561
static cl::opt<bool>
5662
FuseMatrix("fuse-matrix", cl::init(true), cl::Hidden,
5763
cl::desc("Enable/disable fusing matrix instructions."));
@@ -221,7 +227,16 @@ struct ShapeInfo {
221227

222228
/// Returns the transposed shape.
223229
ShapeInfo t() const { return ShapeInfo(NumColumns, NumRows); }
230+
231+
friend raw_ostream &operator<<(raw_ostream &OS, ShapeInfo SI);
232+
233+
LLVM_DUMP_METHOD void dump() const { dbgs() << *this << '\n'; }
224234
};
235+
236+
raw_ostream &operator<<(raw_ostream &OS, ShapeInfo SI) {
237+
return OS << SI.NumRows << 'x' << SI.NumColumns;
238+
}
239+
225240
} // namespace
226241

227242
static bool isUniformShape(Value *V) {
@@ -466,6 +481,8 @@ class LowerMatrixIntrinsics {
466481
return getNumColumns();
467482
}
468483

484+
ShapeInfo shape() const { return {getNumRows(), getNumColumns()}; }
485+
469486
/// Extract a vector of \p NumElts starting at index (\p I, \p J). If the
470487
/// matrix is column-major, the result vector is extracted from a column
471488
/// vector, otherwise from a row vector.
@@ -578,6 +595,28 @@ class LowerMatrixIntrinsics {
578595
SplitVecs.push_back(V);
579596
}
580597

598+
LLVM_DEBUG(if (Instruction *Inst = dyn_cast<Instruction>(MatrixVal)) {
599+
if (Found != Inst2ColumnMatrix.end()) {
600+
// FIXME: re: "at least": SplitVecs.size() doesn't count the shuffles
601+
// that embedInVector created.
602+
dbgs() << "matrix reshape from " << Found->second.shape() << " to "
603+
<< SI << " using at least " << SplitVecs.size()
604+
<< " shuffles on behalf of:\n"
605+
<< *Inst << '\n';
606+
ReshapedMatrices++;
607+
} else if (!ShapeMap.contains(MatrixVal)) {
608+
dbgs() << "splitting a " << SI << " matrix with " << SplitVecs.size()
609+
<< " shuffles beacuse we do not have a shape-aware lowering for "
610+
"its def:\n"
611+
<< *Inst << '\n';
612+
SplitMatrices++;
613+
} else {
614+
// The ShapeMap has it, so it's a case where we're being lowered
615+
// before the def, and we expect that InstCombine will clean things up
616+
// afterward.
617+
}
618+
});
619+
581620
return {SplitVecs};
582621
}
583622

@@ -1386,11 +1425,21 @@ class LowerMatrixIntrinsics {
13861425
ToRemove.push_back(Inst);
13871426
Value *Flattened = nullptr;
13881427
for (Use &U : llvm::make_early_inc_range(Inst->uses())) {
1389-
if (!ShapeMap.contains(U.getUser())) {
1390-
if (!Flattened)
1391-
Flattened = Matrix.embedInVector(Builder);
1392-
U.set(Flattened);
1428+
if (ShapeMap.contains(U.getUser()))
1429+
continue;
1430+
1431+
if (!Flattened) {
1432+
Flattened = Matrix.embedInVector(Builder);
1433+
LLVM_DEBUG(
1434+
if (Instruction *User = dyn_cast<Instruction>(U.getUser())) dbgs()
1435+
<< "flattening a " << Matrix.shape() << " matrix:\n"
1436+
<< *Inst
1437+
<< "\nbecause we do not have a shape-aware lowering for its "
1438+
"user:\n"
1439+
<< *User << '\n';);
1440+
FlattenedMatrices++;
13931441
}
1442+
U.set(Flattened);
13941443
}
13951444
}
13961445

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
; RUN: opt -passes=lower-matrix-intrinsics -debug-only=lower-matrix-intrinsics -disable-output < %s 2>&1 | FileCheck %s --check-prefix=CHECK
2+
; REQUIRES: asserts
3+
4+
define void @diag_3x3(ptr %in, ptr %out) {
5+
%inv = call <9 x float> @llvm.matrix.column.major.load(ptr %in, i64 3, i1 false, i32 3, i32 3)
6+
%diag = shufflevector <9 x float> %inv, <9 x float> poison, <3 x i32> <i32 0, i32 4, i32 8>
7+
store <3 x float> %diag, ptr %out
8+
ret void
9+
}
10+
; CHECK-LABEL: flattening a 3x3 matrix:
11+
; CHECK-NEXT: %{{.*}} = call <9 x float> @llvm.matrix.column.major.load.v9f32.i64(ptr %{{.*}}, i64 3, i1 false, i32 3, i32 3)
12+
; CHECK-NEXT: because we do not have a shape-aware lowering for its user:
13+
; CHECK-NEXT: %{{.*}} = shufflevector <9 x float> %{{.*}}, <9 x float> poison, <3 x i32> <i32 0, i32 4, i32 8>
14+
15+
define void @reshape(ptr %in, ptr %out) {
16+
entry:
17+
%0 = load <4 x double>, ptr %in, align 8
18+
%1 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %0, i32 4, i32 1)
19+
%2 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %1, i32 1, i32 4)
20+
%3 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %2, i32 2, i32 2)
21+
%4 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %3, i32 2, i32 2)
22+
%5 = tail call <4 x double> @llvm.matrix.transpose.v4f64(<4 x double> %4, i32 2, i32 2)
23+
store <4 x double> %5, ptr %out, align 8
24+
ret void
25+
}
26+
; CHECK-LABEL: matrix reshape from 4x1 to 2x2 using at least 2 shuffles on behalf of:
27+
; CHECK-NEXT: %{{.*}} = load <4 x double>, ptr %{{.*}}, align 8
28+
29+
define void @multiply_ntt(ptr %A, ptr %B, ptr %C, ptr %R) {
30+
entry:
31+
%a = load <6 x double>, ptr %A, align 16
32+
%b = load <6 x double>, ptr %B, align 16
33+
%c = load <8 x double>, ptr %C, align 16
34+
%b_t = call <6 x double> @llvm.matrix.transpose.v6f64.v6f64(<6 x double> %b, i32 2, i32 3)
35+
%c_t = call <8 x double> @llvm.matrix.transpose.v8f64.v8f64(<8 x double> %c, i32 4, i32 2)
36+
%m1 = call <12 x double> @llvm.matrix.multiply.v12f64.v6f64.v8f64(<6 x double> %b_t, <8 x double> %c_t, i32 3, i32 2, i32 4)
37+
%m2 = call <8 x double> @llvm.matrix.multiply.v8f64.v6f64.v12f64(<6 x double> %a, <12 x double> %m1, i32 2, i32 3, i32 4)
38+
store <8 x double> %m2, ptr %R, align 16
39+
ret void
40+
}
41+
; CHECK-LABEL: flattening a 2x3 matrix:
42+
; CHECK-NEXT: %{{.*}} = load <6 x double>, ptr %{{.*}}, align 16
43+
; CHECK-NEXT: because we do not have a shape-aware lowering for its user:
44+
; CHECK-NEXT: %{{.*}} = shufflevector <6 x double> %{{.*}}, <6 x double> poison, <2 x i32> <i32 4, i32 5>
45+
46+
; CHECK-LABEL: flattening a 4x3 matrix:
47+
; CHECK-NEXT: %{{.*}} = call <12 x double> @llvm.matrix.multiply.v12f64.v8f64.v6f64(<8 x double> %{{.*}}, <6 x double> %{{.*}}, i32 4, i32 2, i32 3)
48+
; CHECK-NEXT: because we do not have a shape-aware lowering for its user:
49+
; CHECK-NEXT: %{{.*}} = shufflevector <12 x double> %{{.*}}, <12 x double> poison, <4 x i32> <i32 8, i32 9, i32 10, i32 11>
50+
51+
52+
define void @redundant_transpose_of_shuffle(<4 x float> %m, ptr %dst) {
53+
entry:
54+
%shuffle = shufflevector <4 x float> %m, <4 x float> zeroinitializer, <4 x i32> zeroinitializer
55+
%t = tail call <4 x float> @llvm.matrix.transpose.v3f32(<4 x float> %shuffle, i32 1, i32 4)
56+
store <4 x float> %t, ptr %dst, align 4
57+
ret void
58+
}
59+
60+
; CHECK-LABEL: splitting a 4x1 matrix with 1 shuffles beacuse we do not have a shape-aware lowering for its def:
61+
; CHECK-NEXT: %{{.*}} = shufflevector <4 x float> %{{.*}}, <4 x float> zeroinitializer, <4 x i32> zeroinitializer

0 commit comments

Comments
 (0)