-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[mlir] Introduce wrapInZeroTripCheck in LoopLikeOpInterface #80331
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c67e040
Add replaceWithZeroTripCheck to LoopLikeOpInterface
059df4e
Add tests
2cf88e0
Update comments
ab65656
Update comments
bd919fb
Add test pass
9c20950
Revert "Add tests"
0e90f39
Add missing mlir file
b3cf941
Improve comments
3d77beb
Rename to wrapInZeroTripCheck
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// RUN: mlir-opt %s -test-wrap-in-zero-trip-check -split-input-file | FileCheck %s | ||
|
||
func.func @no_wrap_scf_while_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 | ||
} | ||
|
||
// TODO(pzread): Update the test once the wrapInZeroTripCheck is implemented. | ||
// CHECK-LABEL: func.func @no_wrap_scf_while_in_zero_trip_check | ||
// CHECK-NOT: scf.if | ||
// CHECK: scf.while |
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
53 changes: 53 additions & 0 deletions
53
mlir/test/lib/Interfaces/LoopLikeInterface/TestWrapInZeroTripCheck.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,53 @@ | ||
//===- TestWrapInZeroTripCheck.cpp -- Pass to test wrapInZeroTripCheck ----===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements the passes to test wrapInZeroTripCheck of loop ops. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Interfaces/LoopLikeInterface.h" | ||
#include "mlir/Pass/Pass.h" | ||
|
||
using namespace mlir; | ||
|
||
namespace { | ||
|
||
struct TestWrapInZeroTripCheck | ||
: public PassWrapper<TestWrapInZeroTripCheck, OperationPass<func::FuncOp>> { | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestWrapInZeroTripCheck) | ||
|
||
StringRef getArgument() const final { return "test-wrap-in-zero-trip-check"; } | ||
StringRef getDescription() const final { | ||
return "test wrapInZeroTripCheck of loop ops"; | ||
} | ||
|
||
void runOnOperation() override { | ||
func::FuncOp func = getOperation(); | ||
MLIRContext *context = &getContext(); | ||
IRRewriter rewriter(context); | ||
func.walk([&](LoopLikeOpInterface op) { | ||
auto result = op.wrapInZeroTripCheck(rewriter); | ||
if (failed(result)) { | ||
// Ignore not implemented failure in tests. The expected output should | ||
// catch problems (e.g. transformation doesn't happen). | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace mlir { | ||
namespace test { | ||
void registerTestWrapInZeroTripCheckPass() { | ||
PassRegistration<TestWrapInZeroTripCheck>(); | ||
} | ||
} // namespace test | ||
} // namespace 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
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.
Instead of creating a "check" (it is not really clear what the "check" is; it could be an
scf.if
, it could be a new basic block with a conditional branch, etc.), this method could also return just the condition. (Which evaluates totrue
if the loop has at least one iteration.) That would keep the interface a bit simpler. What do you think?Uh oh!
There was an error while loading. Please reload this page.
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 think the tricky part of adding zero-trip-check is to do loop rotation when the loop condition has side-effects. So if we simply return the loop condition, callers will need to implement loop rotation by themselves for each loop op, which can be complicated.
If the kind of "check" op is a concern, maybe we can add a callback and let callers to create the check op in the callback by themselves?
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.
Would it make sense to start with
scf.if
and generalize later as needed?I think it's worth clarifying that loop rotation will only be possible for
scf.while
ops for now, that is, turning regular a "while-do" into a "do-while". That kind of rotation won't be possible at this level of abstraction forscf.for
, hence the comment about redundant first iteration check. I think the rotation responsibility belongs to the interface itself.If we want to make it extra configurable, we could separate the zero-trip-check method from the rotation method...
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.
This makes sense, I did not think of side effects.
The lambda sounds like a good idea, but I'm not sure what exactly it should look like. (Should it return a
Block *
into which the loop is moved?) We can generalize later if needed.I think it would also work for
scf.for
. The check condition islb < ub
.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 think whether the loop rotation is needed (or possible) is the implementation details of each type of loop.
scf.while
needs it because the before block is in the loop and can contain ops with side-effects.scf.for
is simpler as thelb < ub
should have no side-effect and lightweight, which can be run twice (forscf.if
and the for loop)