-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[AMDGPU] fold memref.subview
into amdgpu.gather_to_lds
#149851
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
lialan
wants to merge
7
commits into
llvm:main
Choose a base branch
from
lialan:fold_subviews
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.
+317
−93
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f6afe1
[AMDGPU] fold memref.subview into amdgpu.gather_to_lds
lialan 71fe3aa
Update mlir/lib/Dialect/AMDGPU/Transforms/FoldSubviewOps.cpp
lialan bd4ade5
Update mlir/include/mlir/Dialect/AMDGPU/Transforms/Passes.td
lialan 9552f4e
linting
lialan 5d6483d
updating tests
lialan cf50f5f
Support Expandshape and collapse shape.
lialan 3db555d
update tests.
lialan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//===- FoldSubviewOps.cpp - AMDGPU fold subview ops ---------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/AMDGPU/Transforms/Passes.h" | ||
|
||
#include "mlir/Dialect/AMDGPU/IR/AMDGPUDialect.h" | ||
#include "mlir/Dialect/Affine/ViewLikeInterfaceUtils.h" | ||
#include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
#include "mlir/Dialect/MemRef/Utils/MemRefUtils.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include "llvm/ADT/TypeSwitch.h" | ||
|
||
namespace mlir::amdgpu { | ||
#define GEN_PASS_DEF_AMDGPUFOLDSUBVIEWOPSPASS | ||
#include "mlir/Dialect/AMDGPU/Transforms/Passes.h.inc" | ||
|
||
struct AmdgpuFoldSubviewOpsPass | ||
: public amdgpu::impl::AmdgpuFoldSubviewOpsPassBase< | ||
AmdgpuFoldSubviewOpsPass> { | ||
void runOnOperation() override { | ||
RewritePatternSet patterns(&getContext()); | ||
populateAmdgpuFoldSubviewOpsPatterns(patterns); | ||
if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) | ||
signalPassFailure(); | ||
} | ||
}; | ||
|
||
struct FoldSubviewIntoGatherToLDSOp final : OpRewritePattern<GatherToLDSOp> { | ||
using OpRewritePattern::OpRewritePattern; | ||
LogicalResult matchAndRewrite(GatherToLDSOp op, | ||
PatternRewriter &rewriter) const override { | ||
Location loc = op.getLoc(); | ||
|
||
Value memrefSource; | ||
SmallVector<Value> sourceIndices; | ||
auto foldResult = | ||
llvm::TypeSwitch<Operation *, LogicalResult>( | ||
op.getSrc().getDefiningOp()) | ||
.Case<memref::SubViewOp>([&](memref::SubViewOp subviewOp) { | ||
// If the source is a SubViewOp, we can directly rewrite the | ||
// GatherToLDSOp. | ||
mlir::affine::resolveIndicesIntoOpWithOffsetsAndStrides( | ||
rewriter, loc, subviewOp.getMixedOffsets(), | ||
subviewOp.getMixedStrides(), subviewOp.getDroppedDims(), | ||
op.getSrcIndices(), sourceIndices); | ||
memrefSource = subviewOp.getSource(); | ||
return success(); | ||
}) | ||
.Case<memref::ExpandShapeOp>( | ||
[&](memref::ExpandShapeOp expandShapeOp) { | ||
if (failed(mlir::memref::resolveSourceIndicesExpandShape( | ||
loc, rewriter, expandShapeOp, op.getSrcIndices(), | ||
sourceIndices, false))) { | ||
return failure(); | ||
} | ||
memrefSource = expandShapeOp.getViewSource(); | ||
return success(); | ||
}) | ||
.Case<memref::CollapseShapeOp>( | ||
[&](memref::CollapseShapeOp collapseShapeOp) { | ||
if (failed(mlir::memref::resolveSourceIndicesCollapseShape( | ||
loc, rewriter, collapseShapeOp, op.getSrcIndices(), | ||
sourceIndices))) { | ||
return failure(); | ||
} | ||
memrefSource = collapseShapeOp.getViewSource(); | ||
return success(); | ||
}) | ||
.Default([&](Operation *op) { | ||
// If the source is not a SubViewOp, ExpandShapeOp, or | ||
// CollapseShapeOp, we cannot fold the GatherToLDSOp. | ||
return rewriter.notifyMatchFailure( | ||
op, | ||
"source producer is not one of SubViewOp, ExpandShapeOp, or " | ||
"CollapseShapeOp"); | ||
}); | ||
|
||
if (failed(foldResult)) { | ||
return failure(); | ||
} | ||
|
||
rewriter.replaceOpWithNewOp<GatherToLDSOp>(op, memrefSource, sourceIndices, | ||
op.getDst(), op.getDstIndices(), | ||
op.getTransferType()); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
void populateAmdgpuFoldSubviewOpsPatterns(RewritePatternSet &patterns, | ||
PatternBenefit benefit) { | ||
patterns.add<FoldSubviewIntoGatherToLDSOp>(patterns.getContext(), benefit); | ||
} | ||
} // namespace mlir::amdgpu |
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.
Uh oh!
There was an error while loading. Please reload this page.