MLIR  17.0.0git
SCF.h
Go to the documentation of this file.
1 //===- SCFOps.h - Structured Control Flow -----------------------*- 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 // This file defines structured control flow operations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_SCF_SCF_H
14 #define MLIR_DIALECT_SCF_SCF_H
15 
18 #include "mlir/IR/Builders.h"
19 #include "mlir/IR/BuiltinTypes.h"
27 
28 namespace mlir {
29 namespace scf {
30 void buildTerminatedBody(OpBuilder &builder, Location loc);
31 } // namespace scf
32 } // namespace mlir
33 
34 #include "mlir/Dialect/SCF/IR/SCFOpsDialect.h.inc"
35 
36 #define GET_OP_CLASSES
37 #include "mlir/Dialect/SCF/IR/SCFOps.h.inc"
38 
39 namespace mlir {
40 namespace scf {
41 
42 // Insert `loop.yield` at the end of the only region's only block if it
43 // does not have a terminator already. If a new `loop.yield` is inserted,
44 // the location is specified by `loc`. If the region is empty, insert a new
45 // block first.
46 void ensureLoopTerminator(Region &region, Builder &builder, Location loc);
47 
48 /// Returns the loop parent of an induction variable. If the provided value is
49 /// not an induction variable, then return nullptr.
51 
52 /// Returns the parallel loop parent of an induction variable. If the provided
53 /// value is not an induction variable, then return nullptr.
55 
56 /// Returns the ForallOp parent of an thread index variable.
57 /// If the provided value is not a thread index variable, then return nullptr.
59 
60 /// Return true if ops a and b (or their ancestors) are in mutually exclusive
61 /// regions/blocks of an IfOp.
62 // TODO: Consider moving this functionality to RegionBranchOpInterface.
64 
65 /// Promotes the loop body of a scf::ForallOp to its containing block if the
66 /// loop was known to have a single iteration.
68  scf::ForallOp forallOp);
69 
70 /// Promotes the loop body of a scf::ForallOp to its containing block.
71 void promote(PatternRewriter &rewriter, scf::ForallOp forallOp);
72 
73 /// An owning vector of values, handy to return from functions.
76 struct LoopNest {
79 };
80 
81 /// Creates a perfect nest of "for" loops, i.e. all loops but the innermost
82 /// contain only another loop and a terminator. The lower, upper bounds and
83 /// steps are provided as `lbs`, `ubs` and `steps`, which are expected to be of
84 /// the same size. `iterArgs` points to the initial values of the loop iteration
85 /// arguments, which will be forwarded through the nest to the innermost loop.
86 /// The body of the loop is populated using `bodyBuilder`, which accepts an
87 /// ordered list of induction variables of all loops, followed by a list of
88 /// iteration arguments of the innermost loop, in the same order as provided to
89 /// `iterArgs`. This function is expected to return as many values as
90 /// `iterArgs`, of the same type and in the same order, that will be treated as
91 /// yielded from the loop body and forwarded back through the loop nest. If the
92 /// function is not provided, the loop nest is not expected to have iteration
93 /// arguments, the body of the innermost loop will be left empty, containing
94 /// only the zero-operand terminator. Returns the LoopNest containing the list
95 /// of perfectly nest scf::ForOp build during the call.
96 /// If bound arrays are empty, the body builder will be called
97 /// once to construct the IR outside of the loop with an empty list of induction
98 /// variables.
100  OpBuilder &builder, Location loc, ValueRange lbs, ValueRange ubs,
101  ValueRange steps, ValueRange iterArgs,
103  bodyBuilder = nullptr);
104 
105 /// A convenience version for building loop nests without iteration arguments
106 /// (like for reductions). Does not take the initial value of reductions or
107 /// expect the body building functions to return their current value.
108 /// The built nested scf::For are captured in `capturedLoops` when non-null.
110  ValueRange ubs, ValueRange steps,
112  bodyBuilder = nullptr);
113 
114 } // namespace scf
115 } // namespace mlir
116 #endif // MLIR_DIALECT_SCF_SCF_H
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
This class helps build Operations.
Definition: Builders.h:202
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:700
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:370
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
ParallelOp getParallelForInductionVarOwner(Value val)
Returns the parallel loop parent of an induction variable.
Definition: SCF.cpp:2845
LogicalResult promoteIfSingleIteration(PatternRewriter &rewriter, scf::ForallOp forallOp)
Promotes the loop body of a scf::ForallOp to its containing block if the loop was known to have a sin...
Definition: SCF.cpp:541
SmallVector< scf::ForOp > LoopVector
Definition: SCF.h:75
void promote(PatternRewriter &rewriter, scf::ForallOp forallOp)
Promotes the loop body of a scf::ForallOp to its containing block.
Definition: SCF.cpp:556
void buildTerminatedBody(OpBuilder &builder, Location loc)
Default callback for IfOp builders. Inserts a yield without arguments.
Definition: SCF.cpp:79
LoopNest buildLoopNest(OpBuilder &builder, Location loc, ValueRange lbs, ValueRange ubs, ValueRange steps, ValueRange iterArgs, function_ref< ValueVector(OpBuilder &, Location, ValueRange, ValueRange)> bodyBuilder=nullptr)
Creates a perfect nest of "for" loops, i.e.
Definition: SCF.cpp:594
bool insideMutuallyExclusiveBranches(Operation *a, Operation *b)
Return true if ops a and b (or their ancestors) are in mutually exclusive regions/blocks of an IfOp.
Definition: SCF.cpp:1784
ForOp getForInductionVarOwner(Value val)
Returns the loop parent of an induction variable.
Definition: SCF.cpp:497
void ensureLoopTerminator(Region &region, Builder &builder, Location loc)
ForallOp getForallOpThreadIndexOwner(Value val)
Returns the ForallOp parent of an thread index variable.
Definition: SCF.cpp:1452
SmallVector< Value > ValueVector
An owning vector of values, handy to return from functions.
Definition: SCF.h:74
This header declares functions that assit transformations in the MemRef dialect.
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
ValueVector results
Definition: SCF.h:78
LoopVector loops
Definition: SCF.h:77