Skip to content

Commit d287e55

Browse files
d-smirnovRoboTux
andauthored
[MLIR][VectorToGPU] Cache slice analysis in getOpToConvert (#221795)
ConvertVectorToGPU spent a lot of time rebuilding the same backward and forward slices for overlapping vector/MMA use-def components, which was especially visible on int8 cases. This change caches backward slices, forward slices, and supportsMMaMatrixType() results per operation, and marks fully expanded components as already analyzed. That avoids repeatedly rediscovering and revalidating the same component from multiple seeds without changing the lowering itself. The resulting conversion is unchanged, but compile time for the affected int8 case drops significantly. --------- Co-authored-by: Thomas Preud'homme <[email protected]>
1 parent 5b46a5d commit d287e55

1 file changed

Lines changed: 57 additions & 42 deletions

File tree

‎mlir/lib/Conversion/VectorToGPU/VectorToGPU.cpp‎

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -324,30 +324,48 @@ static bool supportsMMaMatrixType(Operation *op, bool useNvGpu) {
324324
return elementwiseSupportsMMAMatrixType(op);
325325
}
326326

327-
/// Return an unsorted slice handling scf.for region differently than
328-
/// `getSlice`. In scf.for we only want to include as part of the slice elements
329-
/// that are part of the use/def chain.
330-
static SetVector<Operation *>
331-
getSliceContract(Operation *op,
332-
const BackwardSliceOptions &backwardSliceOptions,
333-
const ForwardSliceOptions &forwardSliceOptions) {
334-
SetVector<Operation *> slice;
335-
slice.insert(op);
336-
unsigned currentIndex = 0;
337-
SetVector<Operation *> backwardSlice;
338-
SetVector<Operation *> forwardSlice;
339-
while (currentIndex != slice.size()) {
340-
auto *currentOp = (slice)[currentIndex];
341-
// Compute and insert the backwardSlice starting from currentOp.
342-
backwardSlice.clear();
327+
// Analyze slice of operations based on convert op to figure out if the whole
328+
// slice can be converted to MMA operations.
329+
static SetVector<Operation *> getOpToConvert(mlir::Operation *op,
330+
bool useNvGpu) {
331+
auto hasVectorDest = [](Operation *op) {
332+
return llvm::any_of(op->getResultTypes(), llvm::IsaPred<VectorType>);
333+
};
334+
BackwardSliceOptions backwardSliceOptions;
335+
backwardSliceOptions.filter = hasVectorDest;
336+
337+
auto hasVectorSrc = [](Operation *op) {
338+
return llvm::any_of(op->getOperandTypes(), llvm::IsaPred<VectorType>);
339+
};
340+
ForwardSliceOptions forwardSliceOptions;
341+
forwardSliceOptions.filter = hasVectorSrc;
342+
343+
DenseMap<Operation *, SmallVector<Operation *>> backwardSliceCache;
344+
DenseMap<Operation *, SmallVector<Operation *>> forwardSliceCache;
345+
DenseMap<Operation *, bool> supportsMMAMatrixTypeCache;
346+
347+
auto getCachedBackwardSlice =
348+
[&](Operation *currentOp) -> ArrayRef<Operation *> {
349+
auto [it, inserted] = backwardSliceCache.try_emplace(currentOp);
350+
if (!inserted)
351+
return it->second;
352+
353+
SetVector<Operation *> backwardSlice;
343354
LogicalResult result =
344355
getBackwardSlice(currentOp, &backwardSlice, backwardSliceOptions);
345356
assert(result.succeeded() && "expected a backward slice");
346357
(void)result;
347-
slice.insert_range(backwardSlice);
358+
it->second = backwardSlice.takeVector();
359+
return it->second;
360+
};
348361

349-
// Compute and insert the forwardSlice starting from currentOp.
350-
forwardSlice.clear();
362+
auto getCachedForwardSlice =
363+
[&](Operation *currentOp) -> ArrayRef<Operation *> {
364+
auto [it, inserted] = forwardSliceCache.try_emplace(currentOp);
365+
if (!inserted)
366+
return it->second;
367+
368+
SetVector<Operation *> forwardSlice;
351369
// Special case for ForOp, we don't want to include the whole region but
352370
// only the value using the region arguments.
353371
// TODO: We should refine this to only care about the region arguments being
@@ -360,42 +378,39 @@ getSliceContract(Operation *op,
360378
} else {
361379
getForwardSlice(currentOp, &forwardSlice, forwardSliceOptions);
362380
}
363-
slice.insert_range(forwardSlice);
364-
++currentIndex;
365-
}
366-
return slice;
367-
}
368-
369-
// Analyze slice of operations based on convert op to figure out if the whole
370-
// slice can be converted to MMA operations.
371-
static SetVector<Operation *> getOpToConvert(mlir::Operation *op,
372-
bool useNvGpu) {
373-
auto hasVectorDest = [](Operation *op) {
374-
return llvm::any_of(op->getResultTypes(), llvm::IsaPred<VectorType>);
381+
it->second = forwardSlice.takeVector();
382+
return it->second;
375383
};
376-
BackwardSliceOptions backwardSliceOptions;
377-
backwardSliceOptions.filter = hasVectorDest;
378384

379-
auto hasVectorSrc = [](Operation *op) {
380-
return llvm::any_of(op->getOperandTypes(), llvm::IsaPred<VectorType>);
385+
auto cachedSupportsMMAMatrixType = [&](Operation *currentOp) {
386+
auto [it, inserted] =
387+
supportsMMAMatrixTypeCache.try_emplace(currentOp, false);
388+
if (inserted)
389+
it->second = supportsMMaMatrixType(currentOp, useNvGpu);
390+
return it->second;
381391
};
382-
ForwardSliceOptions forwardSliceOptions;
383-
forwardSliceOptions.filter = hasVectorSrc;
384392

385393
SetVector<Operation *> opToConvert;
386394
op->walk([&](Operation *nestedOp) {
387395
if (!isa<vector::ContractionOp>(nestedOp) &&
388396
!elementwiseSupportsMMAMatrixType(nestedOp))
389397
return;
390-
if (opToConvert.contains(nestedOp))
398+
if (backwardSliceCache.contains(nestedOp))
391399
return;
392-
SetVector<Operation *> dependentOps =
393-
getSliceContract(nestedOp, backwardSliceOptions, forwardSliceOptions);
400+
401+
SetVector<Operation *> dependentOps;
402+
dependentOps.insert(nestedOp);
403+
unsigned currentIndex = 0;
404+
while (currentIndex != dependentOps.size()) {
405+
Operation *currentOp = dependentOps[currentIndex++];
406+
dependentOps.insert_range(getCachedBackwardSlice(currentOp));
407+
dependentOps.insert_range(getCachedForwardSlice(currentOp));
408+
}
394409
// If any instruction cannot use MMA matrix type drop the whole
395410
// chain. MMA matrix are stored in an opaque type so they cannot be used
396411
// by all operations.
397-
if (llvm::any_of(dependentOps, [useNvGpu](Operation *op) {
398-
if (!supportsMMaMatrixType(op, useNvGpu)) {
412+
if (llvm::any_of(dependentOps, [&](Operation *op) {
413+
if (!cachedSupportsMMAMatrixType(op)) {
399414
LDBG() << "cannot convert op: " << *op;
400415
return true;
401416
}

0 commit comments

Comments
 (0)