-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][Linalg] Refine how broadcast dims are treated #99015
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,10 +224,10 @@ struct VectorizationState { | |
/// Masks an operation with the canonical vector mask if the operation needs | ||
/// masking. Returns the masked operation or the original operation if masking | ||
/// is not needed. If provided, the canonical mask for this operation is | ||
/// permuted using `maybeMaskingMap`. | ||
/// permuted using `maybeIndexingMap`. | ||
Operation * | ||
maskOperation(RewriterBase &rewriter, Operation *opToMask, LinalgOp linalgOp, | ||
std::optional<AffineMap> maybeMaskingMap = std::nullopt); | ||
std::optional<AffineMap> maybeIndexingMap = std::nullopt); | ||
|
||
private: | ||
/// Initializes the iteration space static sizes using the Linalg op | ||
|
@@ -422,16 +422,28 @@ Value VectorizationState::getOrCreateMaskFor( | |
return mask; | ||
} | ||
|
||
/// Masks an operation with the canonical vector mask if the operation needs | ||
/// masking. Returns the masked operation or the original operation if masking | ||
/// is not needed. If provided, the canonical mask for this operation is | ||
/// permuted using `maybeMaskingMap`. | ||
Operation * | ||
VectorizationState::maskOperation(RewriterBase &rewriter, Operation *opToMask, | ||
LinalgOp linalgOp, | ||
std::optional<AffineMap> maybeMaskingMap) { | ||
std::optional<AffineMap> maybeIndexingMap) { | ||
LDBG("Trying to mask: " << *opToMask << "\n"); | ||
|
||
std::optional<AffineMap> maybeMaskingMap = std::nullopt; | ||
// The Operand indexing map may contain "zero" results, e.g.: | ||
// (d0, d1, d2, d3) -> (d0, d1, d2, 0) | ||
// When applied to canonical vector shapes like these: | ||
// (1, 16, 16, 4) | ||
// we would get: | ||
// (1, 16, 16, 0) | ||
// Instead, we should extract the following map permutation map for masking: | ||
// (d0, d1, d2, d3) -> (d0, d1, d2) | ||
// This way, the corresponding vector/mask type will be: | ||
// vector<1x16x16xty> | ||
// rather than: | ||
// vector<1x16x16x0xty> | ||
if (maybeIndexingMap) | ||
maybeMaskingMap = maybeIndexingMap->dropZeroResults(); | ||
|
||
// Create or retrieve mask for this operation. | ||
Value mask = | ||
getOrCreateMaskFor(rewriter, opToMask, linalgOp, maybeMaskingMap); | ||
|
@@ -476,7 +488,8 @@ static AffineMap reindexIndexingMap(AffineMap map) { | |
assert(map.isProjectedPermutation(/*allowZeroInResults=*/true) && | ||
"expected projected permutation"); | ||
auto res = compressUnusedDims(map); | ||
assert(res.getNumDims() == res.getNumResults() && | ||
assert(res.getNumDims() == | ||
(res.getNumResults() - res.getNumOfZeroResults()) && | ||
"expected reindexed map with same number of dims and results"); | ||
return res; | ||
} | ||
|
@@ -1317,16 +1330,6 @@ vectorizeAsLinalgGeneric(RewriterBase &rewriter, VectorizationState &state, | |
// permutation map and masking map. | ||
AffineMap indexingMap = linalgOp.getMatchingIndexingMap(opOperand); | ||
|
||
// Remove zeros from indexing map to use it as masking map. | ||
SmallVector<int64_t> zeroPos; | ||
auto results = indexingMap.getResults(); | ||
for (const auto &result : llvm::enumerate(results)) { | ||
if (isa<AffineConstantExpr>(result.value())) { | ||
zeroPos.push_back(result.index()); | ||
} | ||
} | ||
AffineMap maskingMap = indexingMap.dropResults(zeroPos); | ||
|
||
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. Oh, ok, we are just moving this into |
||
AffineMap readMap; | ||
VectorType readType; | ||
Type elemType = getElementTypeOrSelf(opOperand->get()); | ||
|
@@ -1356,7 +1359,7 @@ vectorizeAsLinalgGeneric(RewriterBase &rewriter, VectorizationState &state, | |
Operation *read = rewriter.create<vector::TransferReadOp>( | ||
loc, readType, opOperand->get(), indices, readMap, | ||
ArrayRef<bool>(inBounds)); | ||
read = state.maskOperation(rewriter, read, linalgOp, maskingMap); | ||
read = state.maskOperation(rewriter, read, linalgOp, indexingMap); | ||
Value readValue = read->getResult(0); | ||
|
||
// 3.b. If masked, set in-bounds to true. Masking guarantees that the access | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think indexing map would be misleading here as it's a term very tight to linalg ops and here we may apply it to arbitrary ops. Both will evolve in different ways for more complex vectorization scenarios, like those with control flow.
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.
Thanks for reminding me about this!
Right now we are actually passing the indexing map whenever this hook is used - this is just to documenting the state today. So let me leave it as is.
I will send a follow-on PR in which I'll try to clarify the difference between the two maps.