Skip to content

Commit 232fe4e

Browse files
committed
[Matrix] Add a Remark when matrices get flattened
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.
1 parent 1f1c725 commit 232fe4e

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,11 +1351,28 @@ class LowerMatrixIntrinsics {
13511351
ToRemove.push_back(Inst);
13521352
Value *Flattened = nullptr;
13531353
for (Use &U : llvm::make_early_inc_range(Inst->uses())) {
1354-
if (!ShapeMap.contains(U.getUser())) {
1355-
if (!Flattened)
1356-
Flattened = Matrix.embedInVector(Builder);
1357-
U.set(Flattened);
1354+
if (ShapeMap.contains(U.getUser()))
1355+
continue;
1356+
1357+
if (!Flattened) {
1358+
Flattened = Matrix.embedInVector(Builder);
1359+
if (ORE) {
1360+
if (Instruction *User = dyn_cast<Instruction>(U.getUser())) {
1361+
std::string Str;
1362+
llvm::raw_string_ostream OS(Str);
1363+
OS << *User;
1364+
ORE->emit(OptimizationRemarkMissed(DEBUG_TYPE, "unknown-shape-lowering", User)
1365+
<< "flattening a "
1366+
<< ore::NV("Rows", Matrix.getNumRows()) << "x"
1367+
<< ore::NV("Cols", Matrix.getNumColumns())
1368+
<< " matrix because we do not have a shape-aware lowering for its user:"
1369+
<< ore::NV("Instr", OS.str())
1370+
<< ore::setExtraArgs()
1371+
<< ore::NV("Opcode", User->getOpcodeName()));
1372+
}
1373+
}
13581374
}
1375+
U.set(Flattened);
13591376
}
13601377
}
13611378

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes='lower-matrix-intrinsics' -S < %s | FileCheck %s
3+
; RUN: opt -passes=lower-matrix-intrinsics -pass-remarks-missed=lower-matrix-intrinsics < %s -pass-remarks-output=%t -disable-output && FileCheck --input-file %t %s --check-prefix=REMARK
4+
5+
define void @diag_3x3(ptr %in, ptr %out) {
6+
; REMARK-LABEL: Name: unknown-shape-lowering
7+
; REMARK-NEXT: Function: diag_3x3
8+
; REMARK-NEXT: Args:
9+
; REMARK-NEXT: - String: 'flattening a '
10+
; REMARK-NEXT: - Rows: '3'
11+
; REMARK-NEXT: - String: x
12+
; REMARK-NEXT: - Cols: '3'
13+
; REMARK-NEXT: - String: ' matrix because we do not have a shape-aware lowering for its user:'
14+
; REMARK-NEXT: - Instr: ' %diag = shufflevector <9 x float> %inv, <9 x float> poison, <3 x i32> <i32 0, i32 4, i32 8>'
15+
; REMARK-NEXT: - Opcode: shufflevector
16+
; REMARK-NEXT: ...
17+
; CHECK-LABEL: @diag_3x3(
18+
; CHECK-NEXT: [[COL_LOAD:%.*]] = load <3 x float>, ptr [[IN:%.*]], align 4
19+
; CHECK-NEXT: [[VEC_GEP:%.*]] = getelementptr float, ptr [[IN]], i64 3
20+
; CHECK-NEXT: [[COL_LOAD1:%.*]] = load <3 x float>, ptr [[VEC_GEP]], align 4
21+
; CHECK-NEXT: [[VEC_GEP2:%.*]] = getelementptr float, ptr [[IN]], i64 6
22+
; CHECK-NEXT: [[COL_LOAD3:%.*]] = load <3 x float>, ptr [[VEC_GEP2]], align 4
23+
; CHECK-NEXT: [[TMP1:%.*]] = shufflevector <3 x float> [[COL_LOAD]], <3 x float> [[COL_LOAD1]], <6 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5>
24+
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <3 x float> [[COL_LOAD3]], <3 x float> poison, <6 x i32> <i32 0, i32 1, i32 2, i32 poison, i32 poison, i32 poison>
25+
; CHECK-NEXT: [[TMP3:%.*]] = shufflevector <6 x float> [[TMP1]], <6 x float> [[TMP2]], <9 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8>
26+
; CHECK-NEXT: [[DIAG:%.*]] = shufflevector <9 x float> [[TMP3]], <9 x float> poison, <3 x i32> <i32 0, i32 4, i32 8>
27+
; CHECK-NEXT: store <3 x float> [[DIAG]], ptr [[OUT:%.*]], align 16
28+
; CHECK-NEXT: ret void
29+
;
30+
%inv = call <9 x float> @llvm.matrix.column.major.load(ptr %in, i64 3, i1 false, i32 3, i32 3)
31+
%diag = shufflevector <9 x float> %inv, <9 x float> poison, <3 x i32> <i32 0, i32 4, i32 8>
32+
store <3 x float> %diag, ptr %out
33+
ret void
34+
}

0 commit comments

Comments
 (0)