-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir] Add transformation to wrap scf::while in zero-trip-check #81050
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
7 commits
Select commit
Hold shift + click to select a range
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
132 changes: 132 additions & 0 deletions
132
mlir/lib/Dialect/SCF/Transforms/WrapInZeroTripCheck.cpp
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,132 @@ | ||
//===- WrapInZeroTripCheck.cpp - Loop transforms to add zero-trip-check ---===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
pzread marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "mlir/Dialect/SCF/IR/SCF.h" | ||
#include "mlir/Dialect/SCF/Transforms/Transforms.h" | ||
#include "mlir/IR/IRMapping.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
|
||
using namespace mlir; | ||
|
||
/// Create zero-trip-check around a `while` op and return the new loop op in the | ||
/// check. The while loop is rotated to avoid evaluating the condition twice. | ||
/// | ||
/// Given an example below: | ||
/// | ||
/// scf.while (%arg0 = %init) : (i32) -> i64 { | ||
/// %val = .., %arg0 : i64 | ||
/// %cond = arith.cmpi .., %arg0 : i32 | ||
/// scf.condition(%cond) %val : i64 | ||
/// } do { | ||
/// ^bb0(%arg1: i64): | ||
/// %next = .., %arg1 : i32 | ||
/// scf.yield %next : i32 | ||
/// } | ||
/// | ||
/// First clone before block to the front of the loop: | ||
/// | ||
/// %pre_val = .., %init : i64 | ||
/// %pre_cond = arith.cmpi .., %init : i32 | ||
/// scf.while (%arg0 = %init) : (i32) -> i64 { | ||
/// %val = .., %arg0 : i64 | ||
/// %cond = arith.cmpi .., %arg0 : i32 | ||
/// scf.condition(%cond) %val : i64 | ||
/// } do { | ||
/// ^bb0(%arg1: i64): | ||
/// %next = .., %arg1 : i32 | ||
/// scf.yield %next : i32 | ||
/// } | ||
/// | ||
/// Create `if` op with the condition, rotate and move the loop into the else | ||
/// branch: | ||
/// | ||
/// %pre_val = .., %init : i64 | ||
/// %pre_cond = arith.cmpi .., %init : i32 | ||
/// scf.if %pre_cond -> i64 { | ||
/// %res = scf.while (%arg1 = %va0) : (i64) -> i64 { | ||
/// // Original after block | ||
/// %next = .., %arg1 : i32 | ||
/// // Original before block | ||
/// %val = .., %next : i64 | ||
/// %cond = arith.cmpi .., %next : i32 | ||
/// scf.condition(%cond) %val : i64 | ||
/// } do { | ||
/// ^bb0(%arg2: i64): | ||
/// %scf.yield %arg2 : i32 | ||
/// } | ||
/// scf.yield %res : i64 | ||
/// } else { | ||
/// scf.yield %pre_val : i64 | ||
/// } | ||
FailureOr<scf::WhileOp> mlir::scf::wrapWhileLoopInZeroTripCheck( | ||
scf::WhileOp whileOp, RewriterBase &rewriter, bool forceCreateCheck) { | ||
// If the loop is in do-while form (after block only passes through values), | ||
// there is no need to create a zero-trip-check as before block is always run. | ||
if (!forceCreateCheck && isa<scf::YieldOp>(whileOp.getAfterBody()->front())) { | ||
return whileOp; | ||
} | ||
|
||
OpBuilder::InsertionGuard insertion_guard(rewriter); | ||
|
||
IRMapping mapper; | ||
Block *beforeBlock = whileOp.getBeforeBody(); | ||
// Clone before block before the loop for zero-trip-check. | ||
for (auto [arg, init] : | ||
llvm::zip_equal(beforeBlock->getArguments(), whileOp.getInits())) { | ||
mapper.map(arg, init); | ||
} | ||
rewriter.setInsertionPoint(whileOp); | ||
pzread marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (auto &op : *beforeBlock) { | ||
if (isa<scf::ConditionOp>(op)) { | ||
break; | ||
} | ||
// Safe to clone everything as in a single block all defs have been cloned | ||
// and added to mapper in order. | ||
rewriter.insert(op.clone(mapper)); | ||
} | ||
|
||
scf::ConditionOp condOp = whileOp.getConditionOp(); | ||
Value clonedCondition = mapper.lookupOrDefault(condOp.getCondition()); | ||
SmallVector<Value> clonedCondArgs = llvm::map_to_vector( | ||
condOp.getArgs(), [&](Value arg) { return mapper.lookupOrDefault(arg); }); | ||
|
||
// Create rotated while loop. | ||
auto newLoopOp = rewriter.create<scf::WhileOp>( | ||
whileOp.getLoc(), whileOp.getResultTypes(), clonedCondArgs, | ||
[&](OpBuilder &builder, Location loc, ValueRange args) { | ||
// Rotate and move the loop body into before block. | ||
auto newBlock = builder.getBlock(); | ||
rewriter.mergeBlocks(whileOp.getAfterBody(), newBlock, args); | ||
auto yieldOp = cast<scf::YieldOp>(newBlock->getTerminator()); | ||
rewriter.mergeBlocks(whileOp.getBeforeBody(), newBlock, | ||
yieldOp.getResults()); | ||
rewriter.eraseOp(yieldOp); | ||
}, | ||
[&](OpBuilder &builder, Location loc, ValueRange args) { | ||
// Pass through values. | ||
builder.create<scf::YieldOp>(loc, args); | ||
}); | ||
|
||
// Create zero-trip-check and move the while loop in. | ||
auto ifOp = rewriter.create<scf::IfOp>( | ||
whileOp.getLoc(), clonedCondition, | ||
[&](OpBuilder &builder, Location loc) { | ||
// Then runs the while loop. | ||
rewriter.moveOpBefore(newLoopOp, builder.getInsertionBlock(), | ||
builder.getInsertionPoint()); | ||
builder.create<scf::YieldOp>(loc, newLoopOp.getResults()); | ||
}, | ||
[&](OpBuilder &builder, Location loc) { | ||
// Else returns the results from precondition. | ||
builder.create<scf::YieldOp>(loc, clonedCondArgs); | ||
}); | ||
|
||
rewriter.replaceOp(whileOp, ifOp); | ||
|
||
return newLoopOp; | ||
} |
130 changes: 130 additions & 0 deletions
130
mlir/test/Dialect/SCF/wrap-while-loop-in-zero-trip-check.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,130 @@ | ||
// RUN: mlir-opt %s -test-wrap-scf-while-loop-in-zero-trip-check -split-input-file | FileCheck %s | ||
// RUN: mlir-opt %s -test-wrap-scf-while-loop-in-zero-trip-check='force-create-check=true' -split-input-file | FileCheck %s --check-prefix FORCE-CREATE-CHECK | ||
|
||
func.func @wrap_while_loop_in_zero_trip_check(%bound : i32) -> i32 { | ||
%cst0 = arith.constant 0 : i32 | ||
%cst5 = arith.constant 5 : i32 | ||
%res:2 = scf.while (%iter = %cst0) : (i32) -> (i32, i32) { | ||
%cond = arith.cmpi slt, %iter, %bound : i32 | ||
%inv = arith.addi %bound, %cst5 : i32 | ||
scf.condition(%cond) %iter, %inv : i32, i32 | ||
} do { | ||
^bb0(%arg1: i32, %arg2: i32): | ||
%next = arith.addi %arg1, %arg2 : i32 | ||
scf.yield %next : i32 | ||
} | ||
return %res#0 : i32 | ||
} | ||
|
||
// CHECK-LABEL: func.func @wrap_while_loop_in_zero_trip_check( | ||
// CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { | ||
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 | ||
// CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 | ||
// CHECK-DAG: %[[PRE_COND:.*]] = arith.cmpi slt, %[[C0]], %[[BOUND]] : i32 | ||
// CHECK-DAG: %[[PRE_INV:.*]] = arith.addi %[[BOUND]], %[[C5]] : i32 | ||
// CHECK: %[[IF:.*]]:2 = scf.if %[[PRE_COND]] -> (i32, i32) { | ||
// CHECK: %[[WHILE:.*]]:2 = scf.while ( | ||
// CHECK-SAME: %[[ARG1:.*]] = %[[C0]], %[[ARG2:.*]] = %[[PRE_INV]] | ||
// CHECK-SAME: ) : (i32, i32) -> (i32, i32) { | ||
// CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[ARG2]] : i32 | ||
// CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 | ||
// CHECK: %[[INV:.*]] = arith.addi %[[BOUND]], %[[C5]] : i32 | ||
// CHECK: scf.condition(%[[COND]]) %[[NEXT]], %[[INV]] : i32, i32 | ||
// CHECK: } do { | ||
// CHECK: ^bb0(%[[ARG3:.*]]: i32, %[[ARG4:.*]]: i32): | ||
// CHECK: scf.yield %[[ARG3]], %[[ARG4]] : i32, i32 | ||
// CHECK: } | ||
// CHECK: scf.yield %[[WHILE]]#0, %[[WHILE]]#1 : i32, i32 | ||
// CHECK: } else { | ||
// CHECK: scf.yield %[[C0]], %[[PRE_INV]] : i32, i32 | ||
pzread marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// CHECK: } | ||
// CHECK: return %[[IF]]#0 : i32 | ||
|
||
// ----- | ||
|
||
func.func @wrap_while_loop_with_minimal_before_block(%bound : i32) -> i32 { | ||
%cst0 = arith.constant 0 : i32 | ||
%true = arith.constant true | ||
%cst5 = arith.constant 5 : i32 | ||
%res = scf.while (%iter = %cst0, %arg0 = %true) : (i32, i1) -> i32 { | ||
scf.condition(%arg0) %iter : i32 | ||
} do { | ||
^bb0(%arg1: i32): | ||
%next = arith.addi %arg1, %cst5 : i32 | ||
%cond = arith.cmpi slt, %next, %bound : i32 | ||
scf.yield %next, %cond : i32, i1 | ||
} | ||
return %res : i32 | ||
} | ||
|
||
// CHECK-LABEL: func.func @wrap_while_loop_with_minimal_before_block( | ||
// CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { | ||
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 | ||
// CHECK-DAG: %[[TRUE:.*]] = arith.constant true | ||
// CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 | ||
// CHECK: %[[IF:.*]] = scf.if %[[TRUE]] -> (i32) { | ||
// CHECK: %[[WHILE:.*]] = scf.while (%[[ARG1:.*]] = %[[C0]]) : (i32) -> i32 { | ||
// CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[C5]] : i32 | ||
// CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 | ||
// CHECK: scf.condition(%[[COND]]) %[[NEXT]] : i32 | ||
// CHECK: } do { | ||
// CHECK: ^bb0(%[[ARG2:.*]]: i32): | ||
// CHECK: scf.yield %[[ARG2]] : i32 | ||
// CHECK: } | ||
// CHECK: scf.yield %[[WHILE]] : i32 | ||
// CHECK: } else { | ||
// CHECK: scf.yield %[[C0]] : i32 | ||
// CHECK: } | ||
// CHECK: return %[[IF]] : i32 | ||
|
||
// ----- | ||
|
||
func.func @wrap_do_while_loop_in_zero_trip_check(%bound : i32) -> i32 { | ||
%cst0 = arith.constant 0 : i32 | ||
%cst5 = arith.constant 5 : i32 | ||
%res = scf.while (%iter = %cst0) : (i32) -> i32 { | ||
%next = arith.addi %iter, %cst5 : i32 | ||
%cond = arith.cmpi slt, %next, %bound : i32 | ||
scf.condition(%cond) %next : i32 | ||
} do { | ||
^bb0(%arg1: i32): | ||
scf.yield %arg1 : i32 | ||
} | ||
return %res : i32 | ||
} | ||
|
||
// CHECK-LABEL: func.func @wrap_do_while_loop_in_zero_trip_check( | ||
// CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { | ||
// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 | ||
// CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 | ||
// CHECK-NOT: scf.if | ||
// CHECK: %[[WHILE:.*]] = scf.while (%[[ARG1:.*]] = %[[C0]]) : (i32) -> i32 { | ||
// CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[C5]] : i32 | ||
// CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 | ||
// CHECK: scf.condition(%[[COND]]) %[[NEXT]] : i32 | ||
// CHECK: } do { | ||
// CHECK: ^bb0(%[[ARG2:.*]]: i32): | ||
// CHECK: scf.yield %[[ARG2]] : i32 | ||
// CHECK: } | ||
// CHECK: return %[[WHILE]] : i32 | ||
|
||
// FORCE-CREATE-CHECK-LABEL: func.func @wrap_do_while_loop_in_zero_trip_check( | ||
// FORCE-CREATE-CHECK-SAME: %[[BOUND:.*]]: i32) -> i32 { | ||
// FORCE-CREATE-CHECK-DAG: %[[C0:.*]] = arith.constant 0 : i32 | ||
// FORCE-CREATE-CHECK-DAG: %[[C5:.*]] = arith.constant 5 : i32 | ||
// FORCE-CREATE-CHECK: %[[PRE_NEXT:.*]] = arith.addi %[[C0]], %[[C5]] : i32 | ||
// FORCE-CREATE-CHECK: %[[PRE_COND:.*]] = arith.cmpi slt, %[[PRE_NEXT]], %[[BOUND]] : i32 | ||
// FORCE-CREATE-CHECK: %[[IF:.*]] = scf.if %[[PRE_COND]] -> (i32) { | ||
// FORCE-CREATE-CHECK: %[[WHILE:.*]] = scf.while (%[[ARG1:.*]] = %[[PRE_NEXT]]) : (i32) -> i32 { | ||
// FORCE-CREATE-CHECK: %[[NEXT:.*]] = arith.addi %[[ARG1]], %[[C5]] : i32 | ||
// FORCE-CREATE-CHECK: %[[COND:.*]] = arith.cmpi slt, %[[NEXT]], %[[BOUND]] : i32 | ||
// FORCE-CREATE-CHECK: scf.condition(%[[COND]]) %[[NEXT]] : i32 | ||
// FORCE-CREATE-CHECK: } do { | ||
// FORCE-CREATE-CHECK: ^bb0(%[[ARG2:.*]]: i32): | ||
// FORCE-CREATE-CHECK: scf.yield %[[ARG2]] : i32 | ||
// FORCE-CREATE-CHECK: } | ||
// FORCE-CREATE-CHECK: scf.yield %[[WHILE]] : i32 | ||
// FORCE-CREATE-CHECK: } else { | ||
// FORCE-CREATE-CHECK: scf.yield %[[PRE_NEXT]] : i32 | ||
// FORCE-CREATE-CHECK: } | ||
// FORCE-CREATE-CHECK: return %[[IF]] : i32 |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to future proof, maybe a good idea to return the
if
generated as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO users can look into the parent op for the
if
they need.And in
do {} while (...)
case we actually don't generate theif
(and don't rotate the0 loop) because everything in the before block is always executed (there is a parameter to control this behavior)I don't have a strong opinion on how this method should be designed, but only returning the loop seems to be enough for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to land this as it is first. We can re-iterate on interface if needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extending it as needed sounds good to me!