Skip to content

Commit

Permalink
[Tensor] Simplify tenor.pad tiling length calculations. (#119039)
Browse files Browse the repository at this point in the history
The current calculations calculate ending location of the new length and
then subtract the new offset from that location. It is possible to
directly calculate new length. Along with requiring less operations
(which can matter in dynamic case) this also has the advantage that the
values are upper bounded by length rather than source size which is more
friendly for range analysis. I believe the change is already being
tested by
`test/Dialect/Linalg/subtensor-of-padtensor.mlir` and
`test/Dialect/Linalg/tile-and-fuse-tensors.mlir`

---------

Signed-off-by: Nirvedh <nirvedh@gmail.com>
  • Loading branch information
nirvedhmeshram authored Dec 12, 2024
1 parent 61510b5 commit 3f136f7
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions mlir/lib/Dialect/Tensor/IR/TensorTilingInterfaceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,11 +746,6 @@ FailureOr<TilingResult> tensor::bubbleUpPadSlice(OpBuilder &b,
Location loc = padOp->getLoc();
AffineExpr dim0, dim1;
bindDims(b.getContext(), dim0, dim1);
// Add two integers.
auto addMap = AffineMap::get(2, 0, {dim0 + dim1});
auto add = [&](OpFoldResult v1, OpFoldResult v2) {
return affine::makeComposedFoldedAffineApply(b, loc, addMap, {v1, v2});
};
// Subtract two integers.
auto subMap = AffineMap::get(2, 0, {dim0 - dim1});
auto sub = [&](OpFoldResult v1, OpFoldResult v2) {
Expand Down Expand Up @@ -825,16 +820,14 @@ FailureOr<TilingResult> tensor::bubbleUpPadSlice(OpBuilder &b,
// The original read could also have stopped in the high padding zone.
// In that case, set the end positition of the read should be the end of
// the source tensor. (Similar to newOffset.)
//
// endLoc = min(max(offset - low + length, 0), srcSize)
//
// The new ExtractSliceOp length is `endLoc - newOffset`.
//
// Optimization: If low = 0, then the formula can be simplified.
OpFoldResult endLoc =
hasLowPad ? min(max(add(sub(offset, low), length), zero), srcSize)
: min(add(offset, length), srcSize);
OpFoldResult newLength = sub(endLoc, newOffset);
// srcSize - newOffset represents how much length we have available
// and length - newLow represents how much length we want at most.
// Note that there are many ways to order this indexing math to compute newLength, but we want to make sure that the final affine.min ops in the sequence are bounding the index to as small a value as possible. If ValueBoundsOpInterface is used, this calcuation will get upper bounds from the affine.min ops, so we want to use the smallest known value to set the bound at the end of the computation sequence. In this case, the index will be upper bounded by length - newLow.
OpFoldResult newLength = min(sub(srcSize, newOffset), sub(length, newLow));
// Optimization: If low = 0, then newLow = 0. then newLength >= 0 assuming
// length >= 0.
if (hasLowPad)
newLength = max(newLength, zero);
newLengths.push_back(newLength);

// Check if newLength is zero. In that case, no SubTensorOp should be
Expand Down

0 comments on commit 3f136f7

Please sign in to comment.