Skip to content

[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
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions mlir/include/mlir/Interfaces/LoopLikeInterface.td
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,30 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
/*defaultImplementation=*/[{
return ::mlir::failure();
}]
>,
InterfaceMethod<[{
Add a zero-trip-check around the loop to check if the loop body is ever
Copy link
Member

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 to true if the loop has at least one iteration.) That would keep the interface a bit simpler. What do you think?

Copy link
Member Author

@pzread pzread Feb 2, 2024

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?

Copy link
Contributor

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 for scf.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...

Copy link
Member

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 is lb < ub.

Copy link
Member Author

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 the lb < ub should have no side-effect and lightweight, which can be run twice (for scf.if and the for loop)

run and return the same loop (moved) or a new loop (replaced) inside the
check. Returns "failure" if the loop doesn't support the transformation.

After the transformation, the ops inserted to the parent region of the
loop are guaranteed to be run only if the loop body runs at least one
iteration.

Note: Ops in the loop body might be rearranged because of loop rotating
to maintain the semantic. Terminators might be removed/added during this
transformation. Also callers are not required to check the side-effect
of loop condition, so the transformation needs to consider that to make
sure the loop behavior is unchanged when moving the condition out of the
loop for the zero-trip-check.
}],
/*retTy=*/"::mlir::FailureOr<::mlir::LoopLikeOpInterface>",
/*methodName=*/"wrapInZeroTripCheck",
/*args=*/(ins "::mlir::RewriterBase &":$rewriter),
/*methodBody=*/"",
/*defaultImplementation=*/[{
return ::mlir::failure();
}]
>
];

Expand Down
21 changes: 21 additions & 0 deletions mlir/test/Dialect/SCF/wrap-in-zero-trip-check.mlir
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
1 change: 1 addition & 0 deletions mlir/test/lib/Interfaces/LoopLikeInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_mlir_library(MLIRLoopLikeInterfaceTestPasses
TestBlockInLoop.cpp
TestWrapInZeroTripCheck.cpp

EXCLUDE_FROM_LIBMLIR

Expand Down
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
2 changes: 2 additions & 0 deletions mlir/tools/mlir-opt/mlir-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void registerTestTensorTransforms();
void registerTestTopologicalSortAnalysisPass();
void registerTestTransformDialectEraseSchedulePass();
void registerTestTransformDialectInterpreterPass();
void registerTestWrapInZeroTripCheckPass();
void registerTestWrittenToPass();
void registerTestVectorLowerings();
void registerTestVectorReductionToSPIRVDotProd();
Expand Down Expand Up @@ -261,6 +262,7 @@ void registerTestPasses() {
mlir::test::registerTestVectorLowerings();
mlir::test::registerTestVectorReductionToSPIRVDotProd();
mlir::test::registerTestNvgpuLowerings();
mlir::test::registerTestWrapInZeroTripCheckPass();
mlir::test::registerTestWrittenToPass();
#if MLIR_ENABLE_PDL_IN_PATTERNMATCH
mlir::test::registerTestDialectConversionPasses();
Expand Down