Skip to content

Commit ce446d0

Browse files
committed
[Matrix] Add -debug-only prints 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. 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 274f5a8 commit ce446d0

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/IR/PatternMatch.h"
4141
#include "llvm/Support/Alignment.h"
4242
#include "llvm/Support/CommandLine.h"
43+
#include "llvm/Support/Compiler.h"
4344
#include "llvm/Support/Debug.h"
4445
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
4546
#include "llvm/Transforms/Utils/LoopUtils.h"
@@ -221,7 +222,16 @@ struct ShapeInfo {
221222

222223
/// Returns the transposed shape.
223224
ShapeInfo t() const { return ShapeInfo(NumColumns, NumRows); }
225+
226+
friend raw_ostream &operator<<(raw_ostream &OS, ShapeInfo SI);
227+
228+
LLVM_DUMP_METHOD void dump() const { dbgs() << *this << '\n'; }
224229
};
230+
231+
raw_ostream &operator<<(raw_ostream &OS, ShapeInfo SI) {
232+
return OS << SI.NumRows << 'x' << SI.NumColumns;
233+
}
234+
225235
} // namespace
226236

227237
static bool isUniformShape(Value *V) {
@@ -466,6 +476,8 @@ class LowerMatrixIntrinsics {
466476
return getNumColumns();
467477
}
468478

479+
ShapeInfo shape() const { return {getNumRows(), getNumColumns()}; }
480+
469481
/// Extract a vector of \p NumElts starting at index (\p I, \p J). If the
470482
/// matrix is column-major, the result vector is extracted from a column
471483
/// vector, otherwise from a row vector.
@@ -578,6 +590,25 @@ class LowerMatrixIntrinsics {
578590
SplitVecs.push_back(V);
579591
}
580592

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

@@ -1386,11 +1417,19 @@ class LowerMatrixIntrinsics {
13861417
ToRemove.push_back(Inst);
13871418
Value *Flattened = nullptr;
13881419
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);
1420+
if (ShapeMap.contains(U.getUser()))
1421+
continue;
1422+
1423+
if (!Flattened) {
1424+
Flattened = Matrix.embedInVector(Builder);
1425+
LLVM_DEBUG(
1426+
if (Instruction *User = dyn_cast<Instruction>(U.getUser())) dbgs()
1427+
<< "flattening a " << Matrix.shape() << " matrix " << *Inst
1428+
<< " because we do not have a shape-aware lowering for its "
1429+
"user: "
1430+
<< *User << '\n';);
13931431
}
1432+
U.set(Flattened);
13941433
}
13951434
}
13961435

0 commit comments

Comments
 (0)