MLIR  20.0.0git
RotateWhileLoop.cpp
Go to the documentation of this file.
1 //===- RotateWhileLoop.cpp - scf.while loop rotation ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Rotates `scf.while` loops.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
16 
17 using namespace mlir;
18 
19 namespace {
20 struct RotateWhileLoopPattern : OpRewritePattern<scf::WhileOp> {
22 
23  LogicalResult matchAndRewrite(scf::WhileOp whileOp,
24  PatternRewriter &rewriter) const final {
25  // Setting this option would lead to infinite recursion on a greedy driver
26  // as 'do-while' loops wouldn't be skipped.
27  constexpr bool forceCreateCheck = false;
28  FailureOr<scf::WhileOp> result =
29  scf::wrapWhileLoopInZeroTripCheck(whileOp, rewriter, forceCreateCheck);
30  // scf::wrapWhileLoopInZeroTripCheck hasn't yet implemented a failure
31  // mechanism. 'do-while' loops are simply returned unmodified. In order to
32  // stop recursion, we check input and output operations differ.
33  return success(succeeded(result) && *result != whileOp);
34  }
35 };
36 } // namespace
37 
38 namespace mlir {
39 namespace scf {
41  patterns.add<RotateWhileLoopPattern>(patterns.getContext());
42 }
43 } // namespace scf
44 } // namespace mlir
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:785
MLIRContext * getContext() const
Definition: PatternMatch.h:823
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Definition: PatternMatch.h:847
void populateSCFRotateWhileLoopPatterns(RewritePatternSet &patterns)
Populate patterns to rotate scf.while ops, constructing do-while loops from while loops.
FailureOr< WhileOp > wrapWhileLoopInZeroTripCheck(WhileOp whileOp, RewriterBase &rewriter, bool forceCreateCheck=false)
Create zero-trip-check around a while op and return the new loop op in the check.
Include the generated interface declarations.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:358