Skip to content

[mlir][affine] Generalize canonicalization for one-element delinearize #114871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 9 additions & 61 deletions mlir/lib/Dialect/Affine/IR/AffineOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4605,74 +4605,22 @@ struct DropUnitExtentBasis
}
};

/// Drop delinearization pattern related to loops in the following way
/// Drop delinearization with a single basis element
///
/// ```
/// <loop>(%iv) = (%c0) to (%ub) step (%c1) {
/// %0 = affine.delinearize_index %iv into (%ub) : index
/// <some_use>(%0)
/// }
/// ```
///
/// can be canonicalized to
///
/// ```
/// <loop>(%iv) = (%c0) to (%ub) step (%c1) {
/// <some_use>(%iv)
/// }
/// ```
struct DropDelinearizeOfSingleLoop
/// By definition, `delinearize_index %linear into (%basis)` is
/// `%linear floorDiv 1` (since `1` is the product of the basis elememts,
/// ignoring the 0th one, and since there is no previous division we need
/// to use the remainder of). Therefore, a single-element `delinearize`
/// can be replaced by the underlying linear index.
struct DropDelinearizeOneBasisElement
: public OpRewritePattern<affine::AffineDelinearizeIndexOp> {
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(affine::AffineDelinearizeIndexOp delinearizeOp,
PatternRewriter &rewriter) const override {
if (delinearizeOp.getStaticBasis().size() != 1)
return failure();
auto basis = delinearizeOp.getMixedBasis();

// Check that the `linear_index` is an induction variable.
auto inductionVar = dyn_cast<BlockArgument>(delinearizeOp.getLinearIndex());
if (!inductionVar)
return failure();

// Check that the parent is a `LoopLikeOpInterface`.
auto loopLikeOp = dyn_cast<LoopLikeOpInterface>(
inductionVar.getParentRegion()->getParentOp());
if (!loopLikeOp)
return failure();

// Check that loop is unit-rank and that the `linear_index` is the induction
// variable.
auto inductionVars = loopLikeOp.getLoopInductionVars();
if (!inductionVars || inductionVars->size() != 1 ||
inductionVars->front() != inductionVar) {
return rewriter.notifyMatchFailure(
delinearizeOp, "`linear_index` is not loop induction variable");
}

// Check that the upper-bound is the basis.
auto upperBounds = loopLikeOp.getLoopUpperBounds();
if (!upperBounds || upperBounds->size() != 1 ||
upperBounds->front() != basis.front()) {
return rewriter.notifyMatchFailure(delinearizeOp,
"`basis` is not upper bound");
}

// Check that the lower bound is zero.
auto lowerBounds = loopLikeOp.getLoopLowerBounds();
if (!lowerBounds || lowerBounds->size() != 1 ||
!isZeroIndex(lowerBounds->front())) {
return rewriter.notifyMatchFailure(delinearizeOp,
"loop lower bound is not zero");
}

// Check that the step is one.
auto steps = loopLikeOp.getLoopSteps();
if (!steps || steps->size() != 1 || !isConstantIntValue(steps->front(), 1))
return rewriter.notifyMatchFailure(delinearizeOp, "loop step is not one");

rewriter.replaceOp(delinearizeOp, inductionVar);
rewriter.replaceOp(delinearizeOp, delinearizeOp.getLinearIndex());
return success();
}
};
Expand All @@ -4681,7 +4629,7 @@ struct DropDelinearizeOfSingleLoop

void affine::AffineDelinearizeIndexOp::getCanonicalizationPatterns(
RewritePatternSet &patterns, MLIRContext *context) {
patterns.insert<DropDelinearizeOfSingleLoop, DropUnitExtentBasis>(context);
patterns.insert<DropDelinearizeOneBasisElement, DropUnitExtentBasis>(context);
}

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions mlir/test/Dialect/Affine/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,7 @@ func.func @drop_single_loop_delinearize(%arg0 : index, %arg1 : index) -> index {
// -----

// CHECK-LABEL: func @delinearize_non_induction_variable
// CHECK-NOT: affine.delinearize
func.func @delinearize_non_induction_variable(%arg0: memref<?xi32>, %i : index, %t0 : index, %t1 : index, %t2 : index) -> index {
%1 = affine.apply affine_map<(d0)[s0, s1, s2] -> (d0 + s0 + s1 * 64 + s2 * 128)>(%i)[%t0, %t1, %t2]
%2 = affine.delinearize_index %1 into (1024) : index
Expand All @@ -1526,6 +1527,7 @@ func.func @delinearize_non_induction_variable(%arg0: memref<?xi32>, %i : index,
// -----

// CHECK-LABEL: func @delinearize_non_loop_like
// CHECK-NOT: affine.delinearize
func.func @delinearize_non_loop_like(%arg0: memref<?xi32>, %i : index) -> index {
%2 = affine.delinearize_index %i into (1024) : index
return %2 : index
Expand Down
Loading