Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
Expand Down Expand Up @@ -221,7 +222,16 @@ struct ShapeInfo {

/// Returns the transposed shape.
ShapeInfo t() const { return ShapeInfo(NumColumns, NumRows); }

friend raw_ostream &operator<<(raw_ostream &OS, ShapeInfo SI);

LLVM_DUMP_METHOD void dump() const { dbgs() << *this << '\n'; }
};

raw_ostream &operator<<(raw_ostream &OS, ShapeInfo SI) {
return OS << SI.NumRows << 'x' << SI.NumColumns;
}

} // namespace

static bool isUniformShape(Value *V) {
Expand Down Expand Up @@ -466,6 +476,8 @@ class LowerMatrixIntrinsics {
return getNumColumns();
}

ShapeInfo shape() const { return {getNumRows(), getNumColumns()}; }

/// Extract a vector of \p NumElts starting at index (\p I, \p J). If the
/// matrix is column-major, the result vector is extracted from a column
/// vector, otherwise from a row vector.
Expand Down Expand Up @@ -578,6 +590,25 @@ class LowerMatrixIntrinsics {
SplitVecs.push_back(V);
}

LLVM_DEBUG(if (Instruction *Inst = dyn_cast<Instruction>(MatrixVal)) {
if (Found != Inst2ColumnMatrix.end()) {
// FIXME: re: "at least": SplitVecs.size() doesn't count the shuffles
// that embedInVector created.
dbgs() << "matrix reshape from " << Found->second.shape() << " to "
<< SI << " using at least " << SplitVecs.size()
<< " shuffles on behalf of " << *Inst << '\n';
} else if (!ShapeMap.contains(MatrixVal)) {
dbgs() << "splitting a " << SI << " matrix with " << SplitVecs.size()
<< " shuffles beacuse we do not have a shape-aware lowering for "
"its def: "
<< *Inst << '\n';
} else {
// The ShapeMap has it, so it's a case where we're being lowered
// before the def, and we expect that InstCombine will clean things up
// afterward.
}
});

return {SplitVecs};
}

Expand Down Expand Up @@ -1386,11 +1417,19 @@ class LowerMatrixIntrinsics {
ToRemove.push_back(Inst);
Value *Flattened = nullptr;
for (Use &U : llvm::make_early_inc_range(Inst->uses())) {
if (!ShapeMap.contains(U.getUser())) {
if (!Flattened)
Flattened = Matrix.embedInVector(Builder);
U.set(Flattened);
if (ShapeMap.contains(U.getUser()))
continue;

if (!Flattened) {
Flattened = Matrix.embedInVector(Builder);
LLVM_DEBUG(
if (Instruction *User = dyn_cast<Instruction>(U.getUser())) dbgs()
<< "flattening a " << Matrix.shape() << " matrix " << *Inst
<< " because we do not have a shape-aware lowering for its "
"user: "
<< *User << '\n';);
}
U.set(Flattened);
}
}

Expand Down
Loading