-
Notifications
You must be signed in to change notification settings - Fork 16
[LinalgToXeGPU] Support squeezable any-D memrefs #414
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
8 commits
Select commit
Hold shift + click to select a range
9dd135e
[LinalgToXeGPU] Support squeezable any-D memrefs
dchigarev 324168f
Fix linalg.fill case
dchigarev d8bf832
Add squeeze tests
dchigarev 7dbe433
avoid copy
dchigarev 8e793a9
do not return smallVector
dchigarev a05f541
address review comments
dchigarev 21ee00b
fix endless loop
dchigarev e3994c7
Merge remote-tracking branch 'origin/main' into xegpu-squeeze
dchigarev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,14 @@ | |
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include <numeric> | ||
|
|
||
| #include "mlir/Dialect/Affine/ViewLikeInterfaceUtils.h" | ||
| #include "mlir/Dialect/GPU/IR/GPUDialect.h" | ||
| #include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
| #include "mlir/Dialect/Linalg/IR/Linalg.h" | ||
| #include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
| #include "mlir/Dialect/MemRef/Utils/MemRefUtils.h" | ||
| #include "mlir/Dialect/Tensor/IR/Tensor.h" | ||
| #include "mlir/IR/Attributes.h" | ||
| #include "mlir/IR/Matchers.h" | ||
|
|
@@ -155,9 +159,10 @@ Value flattenMemref(PatternRewriter &rewriter, Location loc, Value srcMemref) { | |
| auto srcType = cast<MemRefType>(srcMemref.getType()); | ||
|
|
||
| assert(srcType && "Expected a memref type"); | ||
| assert(srcType.getRank() == 2 && "Expected a 2D memref"); | ||
|
|
||
| int64_t flatSize = srcType.getShape()[0] * srcType.getShape()[1]; | ||
| auto shapeNd = srcType.getShape(); | ||
| int64_t flatSize = | ||
| std::accumulate(shapeNd.begin(), shapeNd.end(), 1, std::multiplies<>()); | ||
dchigarev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Value offset = rewriter.create<arith::ConstantIndexOp>(loc, 0); | ||
| Value size = rewriter.create<arith::ConstantIndexOp>(loc, flatSize); | ||
|
|
@@ -193,5 +198,126 @@ bool hasSharedMemSpace(mlir::Value memref) { | |
| return false; | ||
| } | ||
|
|
||
| std::tuple<SmallVector<Value>, Value> | ||
| computeSubviewOffsets(PatternRewriter &rewriter, Location loc, Value memref) { | ||
| auto fillVal = rewriter.create<arith::ConstantIndexOp>(loc, 0); | ||
| auto origShape = dyn_cast<MemRefType>(memref.getType()).getShape(); | ||
|
|
||
| SmallVector<Value> resolvedOffsets(origShape.size(), fillVal); | ||
|
|
||
| while (auto subViewOp = memref.getDefiningOp<memref::SubViewOp>()) { | ||
| auto currentOffsets = getAsOpFoldResult(resolvedOffsets); | ||
| resolvedOffsets.clear(); | ||
|
|
||
| affine::resolveIndicesIntoOpWithOffsetsAndStrides( | ||
| rewriter, memref.getLoc(), subViewOp.getMixedOffsets(), | ||
| subViewOp.getMixedStrides(), subViewOp.getDroppedDims(), currentOffsets, | ||
| resolvedOffsets); | ||
| memref = subViewOp.getOperand(0); | ||
| } | ||
|
||
|
|
||
| return std::make_tuple(resolvedOffsets, memref); | ||
| } | ||
|
|
||
| SmallVector<OpFoldResult> getMemrefStrides(PatternRewriter &rewriter, | ||
| Location loc, Value memref) { | ||
| auto type = dyn_cast<MemRefType>(memref.getType()); | ||
|
|
||
| auto stridedLayout = dyn_cast<StridedLayoutAttr>(type.getLayout()); | ||
| if (stridedLayout) { | ||
| auto strides = stridedLayout.getStrides(); | ||
| return getMixedValues(strides, {}, rewriter); | ||
| } | ||
|
|
||
| auto sizes = getMixedValues(type.getShape(), {}, rewriter); | ||
| auto strides = memref::computeStridesIRBlock(loc, rewriter, sizes); | ||
| return strides; | ||
| } | ||
|
|
||
| FailureOr<Value> squeezeMemref(PatternRewriter &rewriter, Location loc, | ||
| Value memref, size_t maxDims = 2) { | ||
| auto type = dyn_cast<MemRefType>(memref.getType()); | ||
| auto shape = type.getShape(); | ||
|
|
||
| if (shape.size() <= maxDims) | ||
| return memref; | ||
|
|
||
| for (size_t i = 0; i < shape.size() - maxDims; i++) | ||
| if (shape[i] != 1) | ||
| return failure(); | ||
|
|
||
| auto offsets = | ||
| getMixedValues(SmallVector<int64_t>(shape.size(), 0), {}, rewriter); | ||
| auto sizes = getMixedValues(shape, {}, rewriter); | ||
| auto staticStrides = utils::getStaticStrides(memref).value(); | ||
| auto strides = | ||
| getMixedValues(SmallVector<int64_t>(shape.size(), 1), {}, rewriter); | ||
|
|
||
| SmallVector<int64_t> newShape(shape.begin() + shape.size() - maxDims, | ||
| shape.end()); | ||
| SmallVector<int64_t> newStrides( | ||
| staticStrides.begin() + shape.size() - maxDims, staticStrides.end()); | ||
|
|
||
| int64_t newOffset = 0; | ||
| if (auto memrefLayout = dyn_cast<StridedLayoutAttr>(type.getLayout())) | ||
| newOffset = memrefLayout.getOffset(); | ||
|
|
||
| auto newLayout = StridedLayoutAttr::get( | ||
| rewriter.getContext(), /*offset=*/newOffset, /*strides=*/newStrides); | ||
| MemRefType newMemRefType = MemRefType::get(newShape, type.getElementType(), | ||
| newLayout, type.getMemorySpace()); | ||
|
|
||
| auto squeezedSubview = | ||
| rewriter | ||
| .create<memref::SubViewOp>(loc, newMemRefType, memref, offsets, sizes, | ||
| strides) | ||
| .getResult(); | ||
| return squeezedSubview; | ||
| } | ||
|
|
||
| LogicalResult maybeSqueezeDims(PatternRewriter &rewriter, | ||
| linalg::LinalgOp linalgOp, size_t maxDims) { | ||
| SmallVector<std::pair<size_t, Value>> newOperands; | ||
| auto operands = linalgOp->getOperands(); | ||
| auto loc = linalgOp.getLoc(); | ||
|
|
||
| for (size_t i = 0; i < operands.size(); i++) { | ||
| auto operand = operands[i]; | ||
| auto type = dyn_cast<MemRefType>(operand.getType()); | ||
| if (!type) { | ||
| // Skip non-memref operands | ||
| continue; | ||
| } | ||
|
|
||
| if (type.getShape().size() <= maxDims) | ||
| continue; | ||
|
|
||
| auto res = squeezeMemref(rewriter, loc, operand, maxDims); | ||
| if (failed(res)) { | ||
| return rewriter.notifyMatchFailure( | ||
| linalgOp, "Can't squeeze memref to the desired number of dimensions"); | ||
| } | ||
|
|
||
| auto flatSubview = res.value(); | ||
| newOperands.emplace_back(i, flatSubview); | ||
| } | ||
|
|
||
| for (auto [i, operand] : newOperands) | ||
| linalgOp->setOperand(i, operand); | ||
dchigarev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return success(); | ||
| } | ||
|
|
||
| bool canSqueezeDims(llvm::ArrayRef<int64_t> shape, size_t maxDims) { | ||
| if (shape.size() <= maxDims) | ||
| return true; | ||
|
|
||
| for (size_t i = 0; i < shape.size() - maxDims; i++) | ||
| if (shape[i] != 1) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| } // namespace utils | ||
| } // namespace mlir | ||
61 changes: 61 additions & 0 deletions
61
test/mlir/test/gc/Transforms/GPU/linalg-to-xegpu-squeeze.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,61 @@ | ||
| // RUN: gc-opt %s -linalg-to-xegpu="dpas-tile=8,16,16 k-tile=16" -canonicalize -split-input-file -cse | FileCheck %s | ||
|
|
||
| !input_type = memref<2x4x8x16xf16> | ||
| !chunk_type = memref<1x1x8x16xf16, strided<[512, 128, 16, 1], offset: ?>> | ||
| !slm_chunk = memref<1x1x8x16xf16, strided<[128, 128, 16, 1], offset: ?>, 3> | ||
|
|
||
| // The map that computes an offset for SLM | ||
| // CHECK: #map = affine_map<(d0, d1) -> (d0 * 4 + d1)> | ||
| #map = affine_map<(xi, yi) -> (xi * 4 + yi)> | ||
|
|
||
| func.func @entry(%arg0: !input_type, %arg1: !input_type, %arg2: !input_type) { | ||
| %c1 = arith.constant 1 : index | ||
| %c2 = arith.constant 2 : index | ||
| %c4 = arith.constant 4 : index | ||
|
|
||
| gpu.launch blocks(%arg3, %arg4, %arg5) in (%arg9 = %c1, %arg10 = %c1, %arg11 = %c1) threads(%arg6, %arg7, %arg8) in (%arg12 = %c2, %arg13 = %c4, %arg14 = %c1) { | ||
| // CHECK: %[[ARG0_SB:.+]] = memref.subview %arg0[%arg6, %arg7, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] | ||
| %arg0_sb = memref.subview %arg0[%arg6, %arg7, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] : !input_type to !chunk_type | ||
| // CHECK: %[[ARG1_SB:.+]] = memref.subview %arg1[%arg6, %arg7, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] | ||
| %arg1_sb = memref.subview %arg1[%arg6, %arg7, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] : !input_type to !chunk_type | ||
| // CHECK: %[[ARG2_SB:.+]] = memref.subview %arg2[%arg6, %arg7, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] | ||
| %arg2_sb = memref.subview %arg2[%arg6, %arg7, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] : !input_type to !chunk_type | ||
|
|
||
| // CHECK: %[[SLM_BUFF:.+]] = memref.alloc() : memref<8x1x8x16xf16, 3> | ||
| %slm_root = memref.alloc() : memref<8x1x8x16xf16, 3> | ||
|
|
||
| %slm_idx = affine.apply #map(%arg6, %arg7) | ||
| %slm = memref.subview %slm_root[%slm_idx, 0, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] : memref<8x1x8x16xf16, 3> to !slm_chunk | ||
|
|
||
| // Squeezing the arguments of 'linalg.mul' | ||
| // CHECK: %[[ARG0_SQUEEZ:.+]] = memref.subview %[[ARG0_SB]][0, 0, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] : | ||
| // CHECK-SAME: memref<1x1x8x16xf16, strided<[512, 128, 16, 1], offset: ?>> to memref<8x16xf16, strided<[16, 1], offset: ?>> | ||
|
|
||
| // CHECK: %[[ARG1_SQUEEZ:.+]] = memref.subview %[[ARG1_SB]][0, 0, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] : | ||
| // CHECK-SAME: memref<1x1x8x16xf16, strided<[512, 128, 16, 1], offset: ?>> to memref<8x16xf16, strided<[16, 1], offset: ?>> | ||
|
|
||
| // Verify that tensor descriptors are created from the squeezed memrefs | ||
| // CHECK: xegpu.create_nd_tdesc %[[ARG0_SQUEEZ]] | ||
| // CHECK: xegpu.create_nd_tdesc %[[ARG1_SQUEEZ]] | ||
|
|
||
| // Verify that the SLM output of linalg.mul is squeezed correctly | ||
| // CHECK-NOT: .* = memref.subview %[[SLM_BUFF]] .* | ||
| // CHECK: %[[SLM_THREAD_OFF:.+]] = affine.apply #map(%arg6, %arg7) | ||
| // CHECK: %[[SLM_OFF:.+]] = arith.muli %[[SLM_THREAD_OFF]], %c128 : index | ||
| // CHECK: %[[FLAT_SLM:.+]] = memref.reinterpret_cast %[[SLM_BUFF]] to offset: [%c0], sizes: [%c1024], strides: [%c1] : memref<8x1x8x16xf16, 3> to memref<1024xf16, 3> | ||
| // CHECK: xegpu.create_tdesc %[[FLAT_SLM]] | ||
| linalg.mul ins(%arg0_sb, %arg1_sb : !chunk_type, !chunk_type) outs(%slm : !slm_chunk) | ||
|
|
||
| // Squeezing the result buffer of 'linalg.add' | ||
| // CHECK: %[[ARG2_SQUEEZ:.+]] = memref.subview %[[ARG2_SB]][0, 0, 0, 0] [1, 1, 8, 16] [1, 1, 1, 1] : | ||
| // CHECK-SAME: memref<1x1x8x16xf16, strided<[512, 128, 16, 1], offset: ?>> to memref<8x16xf16, strided<[16, 1], offset: ?>> | ||
|
|
||
| // Verify that tensor descriptors are created from the squeezed memrefs | ||
| // CHECK: xegpu.create_nd_tdesc %[[ARG2_SQUEEZ]] | ||
| linalg.add ins(%arg0_sb, %slm : !chunk_type, !slm_chunk) outs(%arg2_sb : !chunk_type) | ||
|
|
||
| gpu.terminator | ||
| } {SCFToGPU_visited} | ||
|
|
||
| return | ||
| } |
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.