-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir][scf]: Avoid inserting affine.min when tiling dynamic operation sizes if possible #113819
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,11 +21,13 @@ | |
| #include "mlir/Dialect/Utils/IndexingUtils.h" | ||
| #include "mlir/IR/Dominance.h" | ||
| #include "mlir/IR/Matchers.h" | ||
| #include "mlir/IR/OpDefinition.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Interfaces/DestinationStyleOpInterface.h" | ||
| #include "mlir/Interfaces/TilingInterface.h" | ||
| #include "mlir/Rewrite/FrozenRewritePatternSet.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
| #include "llvm/ADT/STLExtras.h" | ||
| #include "llvm/ADT/TypeSwitch.h" | ||
| #include "llvm/Support/Debug.h" | ||
| #include <optional> | ||
|
|
@@ -186,18 +188,49 @@ static void checkSafeToTileToForall(TilingInterface op, | |
| } | ||
| } | ||
|
|
||
| /// Collect divider of the `ofr`. | ||
| static void collectDividers(OpFoldResult ofr, | ||
| SmallVector<OpFoldResult> ÷rs) { | ||
| dividers.push_back(ofr); | ||
| if (ofr.is<Attribute>()) | ||
| return; | ||
| auto mulOp = cast<Value>(ofr).getDefiningOp<arith::MulIOp>(); | ||
| if (!mulOp) | ||
| return; | ||
|
|
||
| // Given `ofr` = `x` * `y`, all dividers of `x` and `y` are dividers of `ofr`. | ||
| collectDividers(mulOp.getLhs(), dividers); | ||
| collectDividers(mulOp.getRhs(), dividers); | ||
| } | ||
|
|
||
| /// Check if `stride` evenly divides the trip count `size - offset`. | ||
| static bool tileDividesIterationDomain(Range loopRange) { | ||
| std::optional<int64_t> strideAsInt = getConstantIntValue(loopRange.stride); | ||
| std::optional<int64_t> offsetAsInt = getConstantIntValue(loopRange.offset); | ||
| if (!offsetAsInt) | ||
| return false; | ||
| std::optional<int64_t> sizeAsInt = getConstantIntValue(loopRange.size); | ||
| if (!sizeAsInt) | ||
| return false; | ||
| std::optional<int64_t> strideAsInt = getConstantIntValue(loopRange.stride); | ||
| if (!strideAsInt) | ||
| return false; | ||
| return ((sizeAsInt.value() - offsetAsInt.value()) % strideAsInt.value() == 0); | ||
| if (strideAsInt && offsetAsInt && sizeAsInt) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Please add |
||
| // `stride`/`size`/`offset` are static, checking (size - offset) % stride = | ||
| // 0. | ||
| return ((sizeAsInt.value() - offsetAsInt.value()) % strideAsInt.value() == | ||
| 0); | ||
|
|
||
| // At least `stride`/`size`/`offset` is dynamic. | ||
| SmallVector<OpFoldResult> dividersOfSize, dividersOfOffset; | ||
| collectDividers(loopRange.size, dividersOfSize); | ||
| collectDividers(loopRange.offset, dividersOfOffset); | ||
|
|
||
| // Return true if `stride` divides one of the dividers of both `size` and | ||
| // `offset`. | ||
| auto isStrideDividesDivider = [&](OpFoldResult divider) { | ||
| if (!strideAsInt) | ||
| // `stride` is dynamic. | ||
| return divider == loopRange.stride; | ||
|
|
||
| std::optional<int64_t> dividerAsInt = getConstantIntValue(divider); | ||
| return dividerAsInt && *dividerAsInt % *strideAsInt == 0; | ||
| }; | ||
| return llvm::any_of(dividersOfSize, isStrideDividesDivider) && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dont you think this logic is more complicated that you want it to be. I'd like to scope this a bit more narrowly. Just look for the immediate operations being a The more general solution should be using something the |
||
| llvm::any_of(dividersOfOffset, isStrideDividesDivider); | ||
| } | ||
|
|
||
| /// Returns the bounded tile size given the current `offset`, `loopRange` and | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recursions are generally discouraged.