1 //===- Patterns.h - SCF dialect rewrite patterns ----------------*- C++ -*-===//
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 #ifndef MLIR_DIALECT_SCF_TRANSFORMS_PATTERNS_H
10 #define MLIR_DIALECT_SCF_TRANSFORMS_PATTERNS_H
11 
12 #include "mlir/Dialect/SCF/IR/SCF.h"
13 #include "mlir/Dialect/SCF/Transforms/Transforms.h"
14 #include "mlir/IR/PatternMatch.h"
15 
16 namespace mlir {
17 namespace scf {
18 /// Generate a pipelined version of the scf.for loop based on the schedule given
19 /// as option. This applies the mechanical transformation of changing the loop
20 /// and generating the prologue/epilogue for the pipelining and doesn't make any
21 /// decision regarding the schedule.
22 /// Based on the options the loop is split into several stages.
23 /// The transformation assumes that the scheduling given by user is valid.
24 /// For example if we break a loop into 3 stages named S0, S1, S2 we would
25 /// generate the following code with the number in parenthesis as the iteration
26 /// index:
27 /// S0(0)                        // Prologue
28 /// S0(1) S1(0)                  // Prologue
29 /// scf.for %I = %C0 to %N - 2 {
30 ///  S0(I+2) S1(I+1) S2(I)       // Pipelined kernel
31 /// }
32 /// S1(N) S2(N-1)                // Epilogue
33 /// S2(N)                        // Epilogue
34 class ForLoopPipeliningPattern : public OpRewritePattern<ForOp> {
35 public:
ForLoopPipeliningPattern(const PipeliningOption & options,MLIRContext * context)36   ForLoopPipeliningPattern(const PipeliningOption &options,
37                            MLIRContext *context)
38       : OpRewritePattern<ForOp>(context), options(options) {}
matchAndRewrite(ForOp forOp,PatternRewriter & rewriter)39   LogicalResult matchAndRewrite(ForOp forOp,
40                                 PatternRewriter &rewriter) const override {
41     return returningMatchAndRewrite(forOp, rewriter);
42   }
43 
44   FailureOr<ForOp> returningMatchAndRewrite(ForOp forOp,
45                                             PatternRewriter &rewriter) const;
46 
47 protected:
48   PipeliningOption options;
49 };
50 
51 } // namespace scf
52 } // namespace mlir
53 
54 #endif // MLIR_DIALECT_SCF_TRANSFORMS_PATTERNS_H
55