-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][vector] Allow lowering multi-dim scatters to LLVM #132227
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
Open
Groverkss
wants to merge
1
commit into
llvm:main
Choose a base branch
from
Groverkss:unroll_scatter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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 |
---|---|---|
|
@@ -38,6 +38,7 @@ using namespace mlir; | |
using namespace mlir::vector; | ||
|
||
namespace { | ||
|
||
/// Unrolls 2 or more dimensional `vector.gather` ops by unrolling the | ||
/// outermost dimension. For example: | ||
/// ``` | ||
|
@@ -81,26 +82,72 @@ struct UnrollGather : OpRewritePattern<vector::GatherOp> { | |
VectorType subTy = VectorType::Builder(resultTy).dropDim(0); | ||
|
||
for (int64_t i = 0, e = resultTy.getShape().front(); i < e; ++i) { | ||
int64_t thisIdx[1] = {i}; | ||
|
||
Value indexSubVec = | ||
rewriter.create<vector::ExtractOp>(loc, indexVec, thisIdx); | ||
Value maskSubVec = | ||
rewriter.create<vector::ExtractOp>(loc, maskVec, thisIdx); | ||
Value indexSubVec = rewriter.create<vector::ExtractOp>(loc, indexVec, i); | ||
Value maskSubVec = rewriter.create<vector::ExtractOp>(loc, maskVec, i); | ||
Value passThruSubVec = | ||
rewriter.create<vector::ExtractOp>(loc, passThruVec, thisIdx); | ||
rewriter.create<vector::ExtractOp>(loc, passThruVec, i); | ||
Value subGather = rewriter.create<vector::GatherOp>( | ||
loc, subTy, op.getBase(), op.getIndices(), indexSubVec, maskSubVec, | ||
passThruSubVec); | ||
result = | ||
rewriter.create<vector::InsertOp>(loc, subGather, result, thisIdx); | ||
result = rewriter.create<vector::InsertOp>(loc, subGather, result, i); | ||
} | ||
|
||
rewriter.replaceOp(op, result); | ||
return success(); | ||
} | ||
}; | ||
|
||
/// Unrolls 2 or more dimensional `vector.scatter` ops by unrolling the | ||
/// outermost dimension. For example: | ||
/// ``` | ||
/// %g = vector.scatter %base[%c0][%v], %mask, %valueToStore : ... | ||
/// vector<2x3xf32> | ||
/// | ||
/// ==> | ||
/// | ||
/// %g0 = vector.extract %valueToStore[0] : vector<3xf32> from vector<2x3xf32> | ||
/// vector.scatter %base[%c0][%v0], %mask0, %g0 | ||
/// %g1 = vector.extract %valueToStore[1] : vector<3xf32> from vector<2x3xf32> | ||
/// vector.scatter %base[%c0][%v0], %mask0, %g1 | ||
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.
|
||
/// ``` | ||
/// | ||
/// When applied exhaustively, this will produce a sequence of 1-d scatter ops. | ||
/// | ||
/// Supports vector types with a fixed leading dimension. | ||
struct UnrollScatter : OpRewritePattern<vector::ScatterOp> { | ||
using OpRewritePattern::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(vector::ScatterOp op, | ||
PatternRewriter &rewriter) const override { | ||
VectorType vectorTy = op.getVectorType(); | ||
if (vectorTy.getRank() < 2) | ||
return rewriter.notifyMatchFailure(op, "already 1-D"); | ||
|
||
// Unrolling doesn't take vscale into account. Pattern is disabled for | ||
// vectors with leading scalable dim(s). | ||
if (vectorTy.getScalableDims().front()) | ||
return rewriter.notifyMatchFailure(op, "cannot unroll scalable dim"); | ||
|
||
Location loc = op.getLoc(); | ||
Value indexVec = op.getIndexVec(); | ||
Value maskVec = op.getMask(); | ||
Value valueToStoreVec = op.getValueToStore(); | ||
|
||
for (int64_t i = 0, e = vectorTy.getShape().front(); i < e; ++i) { | ||
Value indexSubVec = rewriter.create<vector::ExtractOp>(loc, indexVec, i); | ||
Value maskSubVec = rewriter.create<vector::ExtractOp>(loc, maskVec, i); | ||
Value valueToStoreSubVec = | ||
rewriter.create<vector::ExtractOp>(loc, valueToStoreVec, i); | ||
rewriter.create<vector::ScatterOp>(loc, op.getBase(), op.getIndices(), | ||
indexSubVec, maskSubVec, | ||
valueToStoreSubVec); | ||
} | ||
|
||
rewriter.eraseOp(op); | ||
return success(); | ||
} | ||
}; | ||
|
||
/// Rewrites a vector.gather of a strided MemRef as a gather of a non-strided | ||
/// MemRef with updated indices that model the strided access. | ||
/// | ||
|
@@ -268,9 +315,9 @@ struct Gather1DToConditionalLoads : OpRewritePattern<vector::GatherOp> { | |
}; | ||
} // namespace | ||
|
||
void mlir::vector::populateVectorGatherLoweringPatterns( | ||
void mlir::vector::populateVectorGatherScatterLoweringPatterns( | ||
RewritePatternSet &patterns, PatternBenefit benefit) { | ||
patterns.add<UnrollGather>(patterns.getContext(), benefit); | ||
patterns.add<UnrollGather, UnrollScatter>(patterns.getContext(), benefit); | ||
} | ||
|
||
void mlir::vector::populateVectorGatherToConditionalLoadPatterns( | ||
|
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
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.
Wonder if we could have a generic utility for this?