-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR][AMDGPU] Adding Vector transfer_read to load rewrite pattern #131803
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f6d6ef
Adding Vector to AMDGPU conversion lowering
jerryyin 4610b01
Addressing review feedbacks
jerryyin f7ca23b
Addressing review feedbacks
jerryyin 2c292f8
Adding bazel target for VectorToAMDGPU
jerryyin 01beca8
Amend from conversion to dialect rewrite pattern
jerryyin 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
24 changes: 24 additions & 0 deletions
24
mlir/include/mlir/Conversion/VectorToAMDGPU/VectorToAMDGPU.h
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,24 @@ | ||
| //===- VectorToAMDGPU.h - Vector to AMDGPU dialect conversion ---*- C++ -*-===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef MLIR_CONVERSION_VECTORTOAMDGPU_VECTORTOAMDGPU_H | ||
| #define MLIR_CONVERSION_VECTORTOAMDGPU_VECTORTOAMDGPU_H | ||
|
|
||
| #include "mlir/IR/PatternMatch.h" | ||
|
|
||
| namespace mlir { | ||
| class RewritePatternSet; | ||
| class Pass; | ||
|
|
||
| #define GEN_PASS_DECL_CONVERTVECTORTOAMDGPUPASS | ||
| #include "mlir/Conversion/Passes.h.inc" | ||
|
|
||
| void populateVectorToAMDGPUConversionPatterns(RewritePatternSet &patterns); | ||
| } // namespace mlir | ||
|
|
||
| #endif // MLIR_CONVERSION_VECTORTOAMDGPU_VECTORTOAMDGPU_H |
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,18 @@ | ||
| add_mlir_conversion_library(MLIRVectorToAMDGPU | ||
| VectorToAMDGPU.cpp | ||
|
|
||
| ADDITIONAL_HEADER_DIRS | ||
| ${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/VectorToAMDGPU | ||
|
|
||
| DEPENDS | ||
| MLIRConversionPassIncGen | ||
|
|
||
| LINK_COMPONENTS | ||
| Core | ||
|
|
||
| LINK_LIBS PUBLIC | ||
| MLIRAMDGPUDialect | ||
| MLIRVectorDialect | ||
| MLIRPass | ||
| MLIRTransforms | ||
| ) | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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,147 @@ | ||
| //===- VectorToAMDGPU.cpp - Vector to AMDGPU dialect conversion ---------===// | ||
| // | ||
| // 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/Conversion/VectorToAMDGPU/VectorToAMDGPU.h" | ||
|
|
||
| #include "mlir/Dialect/AMDGPU/IR/AMDGPUDialect.h" | ||
| #include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
| #include "mlir/IR/BuiltinTypes.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/IR/TypeUtilities.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Support/LogicalResult.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
|
||
| namespace mlir { | ||
| #define GEN_PASS_DEF_CONVERTVECTORTOAMDGPUPASS | ||
| #include "mlir/Conversion/Passes.h.inc" | ||
| } // namespace mlir | ||
|
|
||
| using namespace mlir; | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /// This pattern supports lowering of: | ||
| /// `vector.transfer_read` to a combination of `vector.load`, `arith.select` and | ||
| /// `vector.broadcast` if all of the following hold: | ||
| /// - The transfer op is masked. | ||
| /// - The memref is in buffer address space. | ||
| /// - Stride of most minor memref dimension must be 1. | ||
| /// - Out-of-bounds masking is not required. | ||
| /// - If the memref's element type is a vector type then it coincides with the | ||
| /// result type. | ||
| /// - The permutation map doesn't perform permutation (broadcasting is allowed). | ||
| /// Note: those conditions mostly come from TransferReadToVectorLoadLowering | ||
| /// pass. | ||
| static LogicalResult | ||
| transferPreconditions(PatternRewriter &rewriter, | ||
| VectorTransferOpInterface xferOp, | ||
| SmallVector<unsigned> &broadcastedDims, | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| VectorType &unbroadcastedVectorType) { | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!xferOp.getMask()) | ||
| return rewriter.notifyMatchFailure(xferOp, "Only support masked transfer"); | ||
|
|
||
| // Permutations are handled by VectorToSCF or | ||
| // populateVectorTransferPermutationMapLoweringPatterns. | ||
| // We let the 0-d corner case pass-through as it is supported. | ||
| if (!xferOp.getPermutationMap().isMinorIdentityWithBroadcasting( | ||
| &broadcastedDims)) | ||
| return rewriter.notifyMatchFailure(xferOp, "not minor identity + bcast"); | ||
|
|
||
| auto memRefType = dyn_cast<MemRefType>(xferOp.getShapedType()); | ||
| if (!memRefType) | ||
| return rewriter.notifyMatchFailure(xferOp, "not a memref source"); | ||
|
|
||
| Attribute addrSpace = memRefType.getMemorySpace(); | ||
| if (!addrSpace || | ||
| llvm::dyn_cast<amdgpu::AddressSpaceAttr>(addrSpace).getValue() != | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| amdgpu::AddressSpace::FatRawBuffer) | ||
| return rewriter.notifyMatchFailure(xferOp, "not in buffer address space"); | ||
|
|
||
| // Non-unit strides are handled by VectorToSCF. | ||
| if (!memRefType.isLastDimUnitStride()) | ||
| return rewriter.notifyMatchFailure(xferOp, "!= 1 stride needs VectorToSCF"); | ||
|
|
||
| // If there is broadcasting involved then we first load the unbroadcasted | ||
| // vector, and then broadcast it with `vector.broadcast`. | ||
| ArrayRef<int64_t> vectorShape = xferOp.getVectorType().getShape(); | ||
| SmallVector<int64_t> unbroadcastedVectorShape(vectorShape); | ||
| for (unsigned i : broadcastedDims) | ||
| unbroadcastedVectorShape[i] = 1; | ||
| unbroadcastedVectorType = xferOp.getVectorType().cloneWith( | ||
| unbroadcastedVectorShape, xferOp.getVectorType().getElementType()); | ||
|
|
||
| // `vector.load` supports vector types as memref's elements only when the | ||
| // resulting vector type is the same as the element type. | ||
| auto memrefElTy = memRefType.getElementType(); | ||
| if (isa<VectorType>(memrefElTy) && memrefElTy != unbroadcastedVectorType) | ||
| return rewriter.notifyMatchFailure(xferOp, "incompatible element type"); | ||
|
|
||
| // Otherwise, element types of the memref and the vector must match. | ||
| if (!isa<VectorType>(memrefElTy) && | ||
| memrefElTy != xferOp.getVectorType().getElementType()) | ||
| return rewriter.notifyMatchFailure(xferOp, "non-matching element type"); | ||
|
|
||
| // Out-of-bounds dims are handled by MaterializeTransferMask. | ||
| if (xferOp.hasOutOfBoundsDim()) | ||
jerryyin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return rewriter.notifyMatchFailure(xferOp, "out-of-bounds needs mask"); | ||
|
|
||
| if (xferOp.getVectorType().getRank() != 1) | ||
| // vector.maskedload operates on 1-D vectors. | ||
| return rewriter.notifyMatchFailure( | ||
| xferOp, "vector type is not rank 1, can't create masked load, needs " | ||
| "VectorToSCF"); | ||
|
|
||
| return success(); | ||
| } | ||
|
|
||
| struct TransferReadLowering : public OpRewritePattern<vector::TransferReadOp> { | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using OpRewritePattern<vector::TransferReadOp>::OpRewritePattern; | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| LogicalResult matchAndRewrite(vector::TransferReadOp readOp, | ||
| PatternRewriter &rewriter) const override { | ||
|
|
||
| SmallVector<unsigned> broadcastedDims; | ||
| VectorType unbroadcastedVectorType; | ||
| if (failed(transferPreconditions(rewriter, readOp, broadcastedDims, | ||
| unbroadcastedVectorType))) { | ||
| return failure(); | ||
| } | ||
|
|
||
| Value fill = rewriter.create<vector::SplatOp>( | ||
| readOp.getLoc(), unbroadcastedVectorType, readOp.getPadding()); | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Value load = rewriter.create<vector::LoadOp>( | ||
| readOp.getLoc(), unbroadcastedVectorType, readOp.getSource(), | ||
| readOp.getIndices()); | ||
| Value res = rewriter.create<arith::SelectOp>( | ||
| readOp.getLoc(), unbroadcastedVectorType, readOp.getMask(), load, fill); | ||
|
|
||
| // Insert a broadcasting op if required. | ||
| if (!broadcastedDims.empty()) { | ||
| res = rewriter.create<vector::BroadcastOp>(readOp.getLoc(), | ||
| readOp.getVectorType(), res); | ||
| } | ||
|
|
||
| rewriter.replaceOp(readOp, res); | ||
|
|
||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| void mlir::populateVectorToAMDGPUConversionPatterns( | ||
| RewritePatternSet &patterns) { | ||
| patterns.add<TransferReadLowering>(patterns.getContext()); | ||
| } | ||
|
|
||
| struct ConvertVectorToAMDGPUPass | ||
| : public impl::ConvertVectorToAMDGPUPassBase<ConvertVectorToAMDGPUPass> { | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| void runOnOperation() override { | ||
| RewritePatternSet patterns(&getContext()); | ||
| populateVectorToAMDGPUConversionPatterns(patterns); | ||
| if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return signalPassFailure(); | ||
| } | ||
| }; | ||
68 changes: 68 additions & 0 deletions
68
mlir/test/Conversion/VectorToAMDGPU/vector-transfer-read-to-vector-load.mlir
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,68 @@ | ||
| // RUN: mlir-opt %s -convert-vector-to-amdgpu --split-input-file | FileCheck %s | ||
jerryyin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // CHECK-LABEL: func @transfer_to_maskedload_fatrawbuffer( | ||
| // CHECK-SAME: %[[ARG0:.*]]: memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>> | ||
| // CHECK-SAME: %[[ARG1:.*]]: index | ||
| // CHECK-SAME: %[[ARG2:.*]]: vector<4xi1> | ||
| func.func @transfer_to_maskedload_fatrawbuffer(%mem : memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>>, %idx : index, %mask : vector<4xi1>) -> vector<4xf32> { | ||
| %cf0 = arith.constant 0.0 : f32 | ||
| %res = vector.transfer_read %mem[%idx, %idx], %cf0, %mask {in_bounds = [true]} : memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>>, vector<4xf32> | ||
| return %res : vector<4xf32> | ||
| } | ||
| // CHECK: %[[CST:.*]] = arith.constant dense<0.000000e+00> | ||
| // CHECK: %[[LOAD:.*]] = vector.load %arg0[%arg1, %arg1] | ||
| // CHECK: %[[SELECT:.*]] = arith.select %arg2, %[[LOAD]], %[[CST]] | ||
| // CHECK: return %[[SELECT]] : vector<4xf32> | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: func @transfer_to_maskedload_regular( | ||
| // CHECK-SAME: %[[ARG0:.*]]: memref<8x8xf32> | ||
| // CHECK-SAME: %[[ARG1:.*]]: index | ||
| // CHECK-SAME: %[[ARG2:.*]]: vector<4xi1> | ||
| func.func @transfer_to_maskedload_regular(%mem : memref<8x8xf32>, %idx : index, %mask : vector<4xi1>) -> vector<4xf32> { | ||
| %cf0 = arith.constant 0.0 : f32 | ||
| %res = vector.transfer_read %mem[%idx, %idx], %cf0, %mask {in_bounds = [true]} : memref<8x8xf32>, vector<4xf32> | ||
| return %res : vector<4xf32> | ||
| } | ||
| // CHECK: %[[CST:.*]] = arith.constant 0.0 | ||
| // CHECK: %[[RES:.*]] = vector.transfer_read %arg0[%arg1, %arg1], %[[CST]], %arg2 {in_bounds = [true]} : memref<8x8xf32>, vector<4xf32> | ||
| // CHECK: return %[[RES]] : vector<4xf32> | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: func @transfer_broadcasting( | ||
| // CHECK-SAME: %[[ARG0:.*]]: memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>> | ||
| // CHECK-SAME: %[[ARG1:.*]]: index | ||
| // CHECK-SAME: %[[ARG2:.*]]: vector<1xi1> | ||
| #broadcast_1d = affine_map<(d0, d1) -> (0)> | ||
| func.func @transfer_broadcasting(%mem : memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>>, %idx : index, %mask : vector<1xi1>) -> vector<4xf32> { | ||
| %cf0 = arith.constant 0.0 : f32 | ||
| %res = vector.transfer_read %mem[%idx, %idx], %cf0, %mask | ||
| {in_bounds = [true], permutation_map = #broadcast_1d} | ||
| : memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>>, vector<4xf32> | ||
| return %res : vector<4xf32> | ||
| } | ||
| // CHECK: %[[CST:.*]] = arith.constant dense<0.000000e+00> | ||
| // CHECK: %[[LOAD:.*]] = vector.load %arg0[%arg1, %arg1] | ||
| // CHECK: %[[SELECT:.*]] = arith.select %arg2, %[[LOAD]], %[[CST]] | ||
| // CHECK: %[[BROADCAST:.*]] = vector.broadcast %[[SELECT]] : vector<1xf32> to vector<4xf32> | ||
| // CHECK: return %[[BROADCAST]] : vector<4xf32> | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: func @transfer_scalar( | ||
| // CHECK-SAME: %[[ARG0:.*]]: memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>> | ||
| // CHECK-SAME: %[[ARG1:.*]]: index | ||
| // CHECK-SAME: %[[ARG2:.*]]: vector<1xi1> | ||
| func.func @transfer_scalar(%mem : memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>>, %idx : index, %mask : vector<1xi1>) -> vector<1xf32> { | ||
| %cf0 = arith.constant 0.0 : f32 | ||
| %res = vector.transfer_read %mem[%idx, %idx], %cf0, %mask | ||
| {in_bounds = [true]} | ||
| : memref<8x8xf32, #amdgpu.address_space<fat_raw_buffer>>, vector<1xf32> | ||
| return %res : vector<1xf32> | ||
| } | ||
| // CHECK: %[[CST:.*]] = arith.constant dense<0.000000e+00> | ||
| // CHECK: %[[LOAD:.*]] = vector.load %arg0[%arg1, %arg1] | ||
| // CHECK: %[[SELECT:.*]] = arith.select %arg2, %[[LOAD]], %[[CST]] | ||
| // CHECK: return %[[SELECT]] : vector<1xf32> | ||
jerryyin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.