-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[AMDGPU] fold memref.subview/expand_shape/collapse_shape
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 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 d6746b9
Rename and update
lialan 2d653d0
Fix according to comments
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===- 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/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
namespace mlir::amdgpu { | ||
#define GEN_PASS_DEF_AMDGPUFOLDSUBVIEWOPSPASS | ||
#include "mlir/Dialect/AMDGPU/Transforms/Passes.h.inc" | ||
} // namespace mlir::amdgpu | ||
|
||
using namespace mlir; | ||
using namespace mlir::amdgpu; | ||
|
||
lialan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
namespace { | ||
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 : public OpRewritePattern<GatherToLDSOp> { | ||
using OpRewritePattern<GatherToLDSOp>::OpRewritePattern; | ||
lialan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
LogicalResult matchAndRewrite(GatherToLDSOp op, | ||
PatternRewriter &rewriter) const override { | ||
Location loc = op.getLoc(); | ||
|
||
// Check if the source is a subview operation: | ||
auto subviewOp = dyn_cast<memref::SubViewOp>(op.getSrc().getDefiningOp()); | ||
if (!subviewOp) | ||
return rewriter.notifyMatchFailure( | ||
loc, "GatherToLDSOp folding is currently supported only when the source is a SubviewOp. This is one specific pattern, and other scenarios may be added in the future."); | ||
lialan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
SmallVector<Value> sourceIndices; | ||
mlir::affine::resolveIndicesIntoOpWithOffsetsAndStrides( | ||
rewriter, loc, subviewOp.getMixedOffsets(), subviewOp.getMixedStrides(), | ||
subviewOp.getDroppedDims(), op.getSrcIndices(), sourceIndices); | ||
|
||
rewriter.replaceOpWithNewOp<GatherToLDSOp>( | ||
op, subviewOp.getSource(), sourceIndices, op.getDst(), | ||
op.getDstIndices(), op.getTransferType()); | ||
|
||
return success(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
void mlir::amdgpu::populateAmdgpuFoldSubviewOpsPatterns( | ||
RewritePatternSet &patterns, PatternBenefit benefit) { | ||
patterns.add<FoldSubviewIntoGatherToLDSOp>(patterns.getContext(), benefit); | ||
} |
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,50 @@ | ||
// RUN: mlir-opt -amdgpu-fold-subview-ops -split-input-file %s | FileCheck %s | ||
lialan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
#gpu_lds_addrspace = 3 | ||
|
||
// CHECK: func @test_memref | ||
// CHECK-SAME: %[[ARG0:.*]]: index, %[[ARG1:.*]]: index | ||
func.func @test_memref(%offset_i: index, %offset_j: index) { | ||
// CHECK: %[[C0:.*]] = arith.constant 0 : index | ||
// CHECK: %[[LOCAL:.*]] = memref.alloc() : memref<64x64xf16, 3> | ||
// CHECK: %[[MEM:.*]] = memref.alloc() : memref<64x128xf16> | ||
// CHECK: %[[MEM]][%arg0, %arg1], %[[LOCAL]][%[[C0]], %[[C0]]] | ||
lialan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// CHECK-SAME: vector<8xf16>, memref<64x128xf16>, memref<64x64xf16, 3> | ||
|
||
%alloc = memref.alloc() : memref<64x64xf16, #gpu_lds_addrspace> | ||
%mem = memref.alloc() : memref<64x128xf16> | ||
%subview = memref.subview %mem[0, 0][32, 64][1, 1] : memref<64x128xf16> to memref<32x64xf16, strided<[128, 1]>> | ||
%c0 = arith.constant 0 : index | ||
amdgpu.gather_to_lds %subview[%offset_i, %offset_j], %alloc[%c0, %c0] | ||
: vector<8xf16>, memref<32x64xf16, strided<[128, 1]>>, memref<64x64xf16, #gpu_lds_addrspace> | ||
func.return | ||
} | ||
|
||
// ----- | ||
|
||
#gpu_lds_addrspace = 3 | ||
|
||
// CHECK: #[[MAP:.*]] = affine_map<()[s0] -> (s0 + 32)> | ||
// CHECK: #[[MAP1:.*]] = affine_map<()[s0] -> (s0 + 64)> | ||
|
||
// CHECK: func @subview_folding_offset | ||
// CHECK-SAME: %[[ARG0:.*]]: index, %[[ARG1:.*]]: index | ||
func.func @subview_folding_offset(%offset_i: index, %offset_j: index) { | ||
// CHECK: %[[C0:.*]] = arith.constant 0 : index | ||
// CHECK: %[[LOCAL:.*]] = memref.alloc() : memref<64x64xf16, 3> | ||
// CHECK: %[[MEM:.*]] = memref.alloc() : memref<64x128xf16> | ||
|
||
// CHECK: %[[IDX0:.*]] = affine.apply #[[MAP]]()[%[[ARG0]]] | ||
// CHECK: %[[IDX1:.*]] = affine.apply #[[MAP1]]()[%[[ARG1]]] | ||
|
||
// CHECK: %[[MEM]][%[[IDX0]], %[[IDX1]]], %[[LOCAL]][%[[C0]], %[[C0]]] | ||
// CHECK-SAME: vector<8xf16>, memref<64x128xf16>, memref<64x64xf16, 3> | ||
|
||
%alloc = memref.alloc() : memref<64x64xf16, #gpu_lds_addrspace> | ||
%mem = memref.alloc() : memref<64x128xf16> | ||
%subview = memref.subview %mem[32, 64][32, 64][1, 1] : memref<64x128xf16> to memref<32x64xf16, strided<[128, 1], offset: 4160>> | ||
%c0 = arith.constant 0 : index | ||
amdgpu.gather_to_lds %subview[%offset_i, %offset_j], %alloc[%c0, %c0] | ||
: vector<8xf16>, memref<32x64xf16, strided<[128, 1], offset: 4160>>, memref<64x64xf16, #gpu_lds_addrspace> | ||
func.return | ||
} |
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.