-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR] Create GPU utils library & move distribution utils #119264
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83a48ce
[MLIR] Create GPU utils library & move distribution utils
kurapov-peter 3c7d9aa
fix formatting
kurapov-peter e5b53de
hide helper methods behind a pattern interface
kurapov-peter ec5015f
get rid of template parameter
kurapov-peter 0e76378
cleanup
kurapov-peter e733600
address review comments
kurapov-peter e57e8c3
address more comments
kurapov-peter f141a26
remove const
kurapov-peter cc5e22e
address review comments
kurapov-peter 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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| //===- DistributionUtils.h - Distribution Utilities -------------*- 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_DIALECT_GPU_TRANSFORMS_DISTRIBUTIONUTILS_H_ | ||
| #define MLIR_DIALECT_GPU_TRANSFORMS_DISTRIBITIONUTILS_H_ | ||
|
|
||
| #include "mlir/Dialect/Affine/IR/AffineOps.h" | ||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/Dialect/GPU/IR/GPUDialect.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/IR/Value.h" | ||
|
|
||
| namespace mlir::gpu { | ||
| struct WarpDistributionPattern : OpRewritePattern<WarpExecuteOnLane0Op> { | ||
| using OpRewritePattern::OpRewritePattern; | ||
| using Base = WarpDistributionPattern; | ||
|
|
||
| virtual LogicalResult | ||
| matchAndRewrite(WarpExecuteOnLane0Op op, | ||
| PatternRewriter &rewriter) const override = 0; | ||
|
|
||
| protected: | ||
| /// Return a value yielded by `warpOp` which statifies the filter lamdba | ||
| /// condition and is not dead. | ||
| OpOperand *getWarpResult(WarpExecuteOnLane0Op warpOp, | ||
| llvm::function_ref<bool(Operation *)> fn) const; | ||
|
|
||
| /// Helper to create a new WarpExecuteOnLane0Op with different signature. | ||
| WarpExecuteOnLane0Op moveRegionToNewWarpOpAndReplaceReturns( | ||
| RewriterBase &rewriter, WarpExecuteOnLane0Op warpOp, | ||
| ValueRange newYieldedValues, TypeRange newReturnTypes) const; | ||
|
|
||
| /// Helper to create a new WarpExecuteOnLane0Op region with extra outputs. | ||
| /// `indices` return the index of each new output. | ||
| WarpExecuteOnLane0Op moveRegionToNewWarpOpAndAppendReturns( | ||
| RewriterBase &rewriter, WarpExecuteOnLane0Op warpOp, | ||
| ValueRange newYieldedValues, TypeRange newReturnTypes, | ||
| SmallVector<size_t> &indices) const; | ||
|
|
||
| /// Delinearize the given `laneId` into multiple dimensions, where each | ||
| /// dimension's size is determined by `originalShape` and `distributedShape` | ||
| /// together. This function expects the total numbers of threads needed for | ||
| /// distribution is equal to `warpSize`. Returns true and updates | ||
| /// `delinearizedIds` if so. | ||
| bool delinearizeLaneId(OpBuilder &builder, Location loc, | ||
| ArrayRef<int64_t> originalShape, | ||
| ArrayRef<int64_t> distributedShape, int64_t warpSize, | ||
| Value laneId, | ||
| SmallVectorImpl<Value> &delinearizedIds) const; | ||
| }; | ||
|
|
||
| } // namespace mlir::gpu | ||
|
|
||
| #endif // MLIR_DIALECT_GPU_TRANSFORMS_DISTRIBUTIONUTILS_H_ |
File renamed without changes.
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,14 @@ | ||
| add_mlir_dialect_library(MLIRGPUUtils | ||
| Utils.cpp | ||
| DistributionUtils.cpp | ||
|
|
||
| ADDITIONAL_HEADER_DIRS | ||
| ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/GPU/Utils | ||
|
|
||
| LINK_LIBS PUBLIC | ||
| MLIRArithDialect | ||
| MLIRAffineDialect | ||
| MLIRGPUDialect | ||
| MLIRSupport | ||
| MLIRIR | ||
| ) |
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,144 @@ | ||
| //===- DistributionUtils.cpp - Distribution tools for GPUOps --------------===// | ||
| // | ||
| // Part of the MLIR 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file implements distribution utility methods. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Dialect/GPU/Utils/DistributionUtils.h" | ||
| #include "mlir/Dialect/Affine/IR/AffineOps.h" | ||
| #include "mlir/Dialect/Arith/IR/Arith.h" | ||
| #include "mlir/IR/Value.h" | ||
|
|
||
| #include <numeric> | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::gpu; | ||
|
|
||
| WarpExecuteOnLane0Op | ||
| WarpDistributionPattern::moveRegionToNewWarpOpAndReplaceReturns( | ||
| RewriterBase &rewriter, WarpExecuteOnLane0Op warpOp, | ||
| ValueRange newYieldedValues, TypeRange newReturnTypes) const { | ||
| // Create a new op before the existing one, with the extra operands. | ||
| OpBuilder::InsertionGuard g(rewriter); | ||
| rewriter.setInsertionPoint(warpOp); | ||
| auto newWarpOp = rewriter.create<WarpExecuteOnLane0Op>( | ||
| warpOp.getLoc(), newReturnTypes, warpOp.getLaneid(), warpOp.getWarpSize(), | ||
| warpOp.getArgs(), warpOp.getBody()->getArgumentTypes()); | ||
|
|
||
| Region &opBody = warpOp.getBodyRegion(); | ||
| Region &newOpBody = newWarpOp.getBodyRegion(); | ||
| Block &newOpFirstBlock = newOpBody.front(); | ||
| rewriter.inlineRegionBefore(opBody, newOpBody, newOpBody.begin()); | ||
| rewriter.eraseBlock(&newOpFirstBlock); | ||
| assert(newWarpOp.getWarpRegion().hasOneBlock() && | ||
| "expected WarpOp with single block"); | ||
|
|
||
| auto yield = | ||
| cast<gpu::YieldOp>(newOpBody.getBlocks().begin()->getTerminator()); | ||
|
|
||
| rewriter.modifyOpInPlace( | ||
| yield, [&]() { yield.getValuesMutable().assign(newYieldedValues); }); | ||
| return newWarpOp; | ||
| } | ||
|
|
||
| WarpExecuteOnLane0Op | ||
| WarpDistributionPattern::moveRegionToNewWarpOpAndAppendReturns( | ||
| RewriterBase &rewriter, WarpExecuteOnLane0Op warpOp, | ||
| ValueRange newYieldedValues, TypeRange newReturnTypes, | ||
| SmallVector<size_t> &indices) const { | ||
| SmallVector<Type> types(warpOp.getResultTypes().begin(), | ||
| warpOp.getResultTypes().end()); | ||
| auto yield = cast<gpu::YieldOp>( | ||
| warpOp.getBodyRegion().getBlocks().begin()->getTerminator()); | ||
| llvm::SmallSetVector<Value, 32> yieldValues(yield.getOperands().begin(), | ||
| yield.getOperands().end()); | ||
| for (auto [value, type] : llvm::zip_equal(newYieldedValues, newReturnTypes)) { | ||
| if (yieldValues.insert(value)) { | ||
| types.push_back(type); | ||
| indices.push_back(yieldValues.size() - 1); | ||
| } else { | ||
| // If the value already exit the region don't create a new output. | ||
| for (auto [idx, yieldOperand] : | ||
| llvm::enumerate(yieldValues.getArrayRef())) { | ||
| if (yieldOperand == value) { | ||
| indices.push_back(idx); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| yieldValues.insert(newYieldedValues.begin(), newYieldedValues.end()); | ||
| WarpExecuteOnLane0Op newWarpOp = moveRegionToNewWarpOpAndReplaceReturns( | ||
| rewriter, warpOp, yieldValues.getArrayRef(), types); | ||
| rewriter.replaceOp(warpOp, | ||
| newWarpOp.getResults().take_front(warpOp.getNumResults())); | ||
| return newWarpOp; | ||
| } | ||
|
|
||
| OpOperand *WarpDistributionPattern::getWarpResult( | ||
| WarpExecuteOnLane0Op warpOp, | ||
| llvm::function_ref<bool(Operation *)> fn) const { | ||
| auto yield = cast<gpu::YieldOp>( | ||
| warpOp.getBodyRegion().getBlocks().begin()->getTerminator()); | ||
| for (OpOperand &yieldOperand : yield->getOpOperands()) { | ||
| Value yieldValues = yieldOperand.get(); | ||
| Operation *definedOp = yieldValues.getDefiningOp(); | ||
| if (definedOp && fn(definedOp)) { | ||
| if (!warpOp.getResult(yieldOperand.getOperandNumber()).use_empty()) | ||
| return &yieldOperand; | ||
| } | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| bool WarpDistributionPattern::delinearizeLaneId( | ||
| OpBuilder &builder, Location loc, ArrayRef<int64_t> originalShape, | ||
| ArrayRef<int64_t> distributedShape, int64_t warpSize, Value laneId, | ||
| SmallVectorImpl<Value> &delinearizedIds) const { | ||
| // If the original shape and the distributed shape is the same, we don't | ||
| // distribute at all--every thread is handling the whole. For such case, we | ||
| // should not rely on lane IDs later. So just return an empty lane ID vector. | ||
| if (originalShape == distributedShape) { | ||
| delinearizedIds.clear(); | ||
| return true; | ||
| } | ||
|
|
||
| SmallVector<int64_t> sizes; | ||
| for (auto [large, small] : llvm::zip_equal(originalShape, distributedShape)) { | ||
| if (large % small != 0) | ||
| return false; | ||
| sizes.push_back(large / small); | ||
| } | ||
| if (std::accumulate(sizes.begin(), sizes.end(), 1, | ||
| std::multiplies<int64_t>()) != warpSize) | ||
| return false; | ||
|
|
||
| AffineExpr s0, s1; | ||
| bindSymbols(builder.getContext(), s0, s1); | ||
|
|
||
| int64_t usedThreads = 1; | ||
|
|
||
| Value zero = builder.create<arith::ConstantIndexOp>(loc, 0); | ||
| delinearizedIds.assign(sizes.size(), zero); | ||
|
|
||
| for (int i = sizes.size() - 1; i >= 0; --i) { | ||
| usedThreads *= sizes[i]; | ||
| if (usedThreads == warpSize) { | ||
| // We've used up all available threads. Don't need to perform modulo | ||
| // anymore. And we can stop the calculation for further dimensions. | ||
| delinearizedIds[i] = laneId; | ||
| break; | ||
| } | ||
| delinearizedIds[i] = | ||
| affine::makeComposedAffineApply(builder, loc, s0 % sizes[i], {laneId}); | ||
| laneId = affine::makeComposedAffineApply( | ||
| builder, loc, s0.floorDiv(usedThreads), {laneId}); | ||
| } | ||
| return true; | ||
| } | ||
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.
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.