-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][scf] Implement conversion from scf.forall to scf.parallel #94109
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
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
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,86 @@ | ||
//===- ForallToParallel.cpp - scf.forall to scf.parallel loop 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Transforms SCF.ForallOp's into SCF.ParallelOps's. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/SCF/IR/SCF.h" | ||
#include "mlir/Dialect/SCF/Transforms/Passes.h" | ||
#include "mlir/Dialect/SCF/Transforms/Transforms.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
|
||
namespace mlir { | ||
#define GEN_PASS_DEF_SCFFORALLTOPARALLELLOOP | ||
#include "mlir/Dialect/SCF/Transforms/Passes.h.inc" | ||
} // namespace mlir | ||
|
||
using namespace mlir; | ||
|
||
LogicalResult mlir::scf::forallToParallelLoop(RewriterBase &rewriter, | ||
adam-smnk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
scf::ForallOp forallOp, | ||
scf::ParallelOp *result) { | ||
OpBuilder::InsertionGuard guard(rewriter); | ||
rewriter.setInsertionPoint(forallOp); | ||
|
||
Location loc = forallOp.getLoc(); | ||
if (!forallOp.getOutputs().empty()) | ||
return rewriter.notifyMatchFailure( | ||
forallOp, | ||
"only fully bufferized scf.forall ops can be lowered to scf.parallel"); | ||
|
||
// Convert mixed bounds and steps to SSA values. | ||
SmallVector<Value> lbs = getValueOrCreateConstantIndexOp( | ||
rewriter, loc, forallOp.getMixedLowerBound()); | ||
SmallVector<Value> ubs = getValueOrCreateConstantIndexOp( | ||
rewriter, loc, forallOp.getMixedUpperBound()); | ||
SmallVector<Value> steps = | ||
getValueOrCreateConstantIndexOp(rewriter, loc, forallOp.getMixedStep()); | ||
|
||
// Create empty scf.parallel op. | ||
auto parallelOp = rewriter.create<scf::ParallelOp>(loc, lbs, ubs, steps); | ||
rewriter.eraseBlock(¶llelOp.getRegion().front()); | ||
rewriter.inlineRegionBefore(forallOp.getRegion(), parallelOp.getRegion(), | ||
parallelOp.getRegion().begin()); | ||
// Replace the terminator. | ||
rewriter.setInsertionPointToEnd(¶llelOp.getRegion().front()); | ||
rewriter.replaceOpWithNewOp<scf::ReduceOp>( | ||
parallelOp.getRegion().front().getTerminator()); | ||
|
||
// If the mapping attribute is present, propagate to the new parallelOp. | ||
if (forallOp.getMapping()) | ||
parallelOp->setAttr("mapping", *forallOp.getMapping()); | ||
|
||
// Erase the scf.forall op. | ||
rewriter.replaceOp(forallOp, parallelOp); | ||
|
||
if (result) | ||
*result = parallelOp; | ||
|
||
return success(); | ||
} | ||
|
||
namespace { | ||
struct ForallToParallelLoop final | ||
: public impl::SCFForallToParallelLoopBase<ForallToParallelLoop> { | ||
void runOnOperation() override { | ||
Operation *parentOp = getOperation(); | ||
IRRewriter rewriter(parentOp->getContext()); | ||
|
||
parentOp->walk([&](scf::ForallOp forallOp) { | ||
if (failed(scf::forallToParallelLoop(rewriter, forallOp))) { | ||
return signalPassFailure(); | ||
} | ||
}); | ||
} | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<Pass> mlir::createForallToParallelLoopPass() { | ||
return std::make_unique<ForallToParallelLoop>(); | ||
} |
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,80 @@ | ||
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(scf-forall-to-parallel))' -split-input-file | FileCheck %s | ||
|
||
func.func private @callee(%i: index, %j: index) | ||
|
||
// CHECK-LABEL: @two_iters | ||
// CHECK-SAME: %[[UB1:.+]]: index, %[[UB2:.+]]: index | ||
func.func @two_iters(%ub1: index, %ub2: index) { | ||
scf.forall (%i, %j) in (%ub1, %ub2) { | ||
func.call @callee(%i, %j) : (index, index) -> () | ||
} | ||
|
||
// CHECK: scf.parallel (%[[IV1:.+]], %[[IV2:.+]]) = (%{{.*}}, %{{.*}}) to (%[[UB1]], %[[UB2]]) | ||
// CHECK: func.call @callee(%[[IV1]], %[[IV2]]) : (index, index) -> () | ||
// CHECK: scf.reduce | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func.func private @callee(%i: index, %j: index) | ||
|
||
// CHECK-LABEL: @repeated | ||
// CHECK-SAME: %[[UB1:.+]]: index, %[[UB2:.+]]: index | ||
func.func @repeated(%ub1: index, %ub2: index) { | ||
scf.forall (%i, %j) in (%ub1, %ub2) { | ||
func.call @callee(%i, %j) : (index, index) -> () | ||
} | ||
|
||
// CHECK: scf.parallel (%[[IV1:.+]], %[[IV2:.+]]) = (%{{.*}}, %{{.*}}) to (%[[UB1]], %[[UB2]]) | ||
// CHECK: func.call @callee(%[[IV1]], %[[IV2]]) : (index, index) -> () | ||
// CHECK: scf.reduce | ||
scf.forall (%i, %j) in (%ub1, %ub2) { | ||
func.call @callee(%i, %j) : (index, index) -> () | ||
} | ||
|
||
// CHECK: scf.parallel (%[[IV3:.+]], %[[IV4:.+]]) = (%{{.*}}, %{{.*}}) to (%[[UB1]], %[[UB2]]) | ||
// CHECK: func.call @callee(%[[IV3]], %[[IV4]]) | ||
// CHECK: scf.reduce | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
func.func private @callee(%i: index, %j: index, %k: index, %l: index) | ||
|
||
// CHECK-LABEL: @nested | ||
// CHECK-SAME: %[[UB1:.+]]: index, %[[UB2:.+]]: index, %[[UB3:.+]]: index, %[[UB4:.+]]: index | ||
func.func @nested(%ub1: index, %ub2: index, %ub3: index, %ub4: index) { | ||
// CHECK: scf.parallel (%[[IV1:.+]], %[[IV2:.+]]) = (%{{.*}}, %{{.*}}) to (%[[UB1]], %[[UB2]]) step (%{{.*}}, %{{.*}}) { | ||
// CHECK: scf.parallel (%[[IV3:.+]], %[[IV4:.+]]) = (%{{.*}}, %{{.*}}) to (%[[UB3]], %[[UB4]]) step (%{{.*}}, %{{.*}}) { | ||
// CHECK: func.call @callee(%[[IV1]], %[[IV2]], %[[IV3]], %[[IV4]]) | ||
// CHECK: scf.reduce | ||
// CHECK: } | ||
// CHECK: scf.reduce | ||
// CHECK: } | ||
scf.forall (%i, %j) in (%ub1, %ub2) { | ||
scf.forall (%k, %l) in (%ub3, %ub4) { | ||
func.call @callee(%i, %j, %k, %l) : (index, index, index, index) -> () | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: @mapping_attr | ||
func.func @mapping_attr() -> () { | ||
// CHECK: scf.parallel | ||
// CHECK: scf.reduce | ||
// CHECK: {mapping = [#gpu.thread<x>]} | ||
|
||
%num_threads = arith.constant 100 : index | ||
|
||
scf.forall (%thread_idx) in (%num_threads) { | ||
scf.forall.in_parallel { | ||
} | ||
} {mapping = [#gpu.thread<x>]} | ||
return | ||
|
||
} |
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.