MLIR 24.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
19#include "mlir/IR/Builders.h"
30
31namespace mlir {
32namespace scf {
33void buildTerminatedBody(OpBuilder &builder, Location loc);
34} // namespace scf
35} // namespace mlir
36
37#define GET_OP_CLASSES
38#include "mlir/Dialect/SCF/IR/SCFOps.h.inc"
39
40namespace mlir {
41namespace scf {
42
43/// Returns the loop parent of an induction variable. If the provided value is
44/// not an induction variable, then return nullptr.
45ForOp getForInductionVarOwner(Value val);
46
47/// Returns the parallel loop parent of an induction variable. If the provided
48/// value is not an induction variable, then return nullptr.
49ParallelOp getParallelForInductionVarOwner(Value val);
50
51/// Returns the ForallOp parent of an thread index variable.
52/// If the provided value is not a thread index variable, then return nullptr.
53ForallOp getForallOpThreadIndexOwner(Value val);
54
55/// Return true if ops a and b (or their ancestors) are in mutually exclusive
56/// regions/blocks of an IfOp.
57// TODO: Consider moving this functionality to RegionBranchOpInterface.
58bool insideMutuallyExclusiveBranches(Operation *a, Operation *b);
59
60/// Promotes the loop body of a scf::ForallOp to its containing block.
61/// Only tensor.parallel_insert_slice combining ops are supported.
62/// Returns failure without modifying the IR if a combining op is unsupported.
63LogicalResult promote(RewriterBase &rewriter, scf::ForallOp forallOp);
64
65/// An owning vector of values, handy to return from functions.
72
73/// Creates a perfect nest of "for" loops, i.e. all loops but the innermost
74/// contain only another loop and a terminator. The lower, upper bounds and
75/// steps are provided as `lbs`, `ubs` and `steps`, which are expected to be of
76/// the same size. `iterArgs` points to the initial values of the loop iteration
77/// arguments, which will be forwarded through the nest to the innermost loop.
78/// The body of the loop is populated using `bodyBuilder`, which accepts an
79/// ordered list of induction variables of all loops, followed by a list of
80/// iteration arguments of the innermost loop, in the same order as provided to
81/// `iterArgs`. This function is expected to return as many values as
82/// `iterArgs`, of the same type and in the same order, that will be treated as
83/// yielded from the loop body and forwarded back through the loop nest. If the
84/// function is not provided, the loop nest is not expected to have iteration
85/// arguments, the body of the innermost loop will be left empty, containing
86/// only the zero-operand terminator. Returns the LoopNest containing the list
87/// of perfectly nest scf::ForOp build during the call.
88/// If bound arrays are empty, the body builder will be called
89/// once to construct the IR outside of the loop with an empty list of induction
90/// variables.
92 OpBuilder &builder, Location loc, ValueRange lbs, ValueRange ubs,
93 ValueRange steps, ValueRange iterArgs,
95 bodyBuilder = nullptr);
96
97/// A convenience version for building loop nests without iteration arguments
98/// (like for reductions). Does not take the initial value of reductions or
99/// expect the body building functions to return their current value.
100/// The built nested scf::For are captured in `capturedLoops` when non-null.
102 ValueRange ubs, ValueRange steps,
104 bodyBuilder = nullptr);
105
106/// Perform a replacement of one iter OpOperand of an scf.for to the
107/// `replacement` value with a different type. A callback is used to insert
108/// cast ops inside the block to account for type differences.
112 scf::ForOp forOp,
113 OpOperand &operand,
115 const ValueTypeCastFnTy &castFn);
116
117/// Helper function to compute the difference between two values. This is used
118/// by the loop implementations to compute the trip count.
119std::optional<llvm::APSInt> computeUbMinusLb(Value lb, Value ub, bool isSigned);
120
121} // namespace scf
122} // namespace mlir
123#endif // MLIR_DIALECT_SCF_SCF_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:210
This class represents an operand of an operation.
Definition Value.h:254
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:389
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
SmallVector< scf::ForOp > LoopVector
Definition SCF.h:67
ParallelOp getParallelForInductionVarOwner(Value val)
Returns the parallel loop parent of an induction variable.
Definition SCF.cpp:3006
void buildTerminatedBody(OpBuilder &builder, Location loc)
Default callback for IfOp builders. Inserts a yield without arguments.
Definition SCF.cpp:95
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:823
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:1946
std::optional< llvm::APSInt > computeUbMinusLb(Value lb, Value ub, bool isSigned)
Helper function to compute the difference between two values.
Definition SCF.cpp:116
ForOp getForInductionVarOwner(Value val)
Returns the loop parent of an induction variable.
Definition SCF.cpp:680
SmallVector< Value > ValueVector
An owning vector of values, handy to return from functions.
Definition SCF.h:66
llvm::function_ref< Value(OpBuilder &, Location loc, Type, Value)> ValueTypeCastFnTy
Perform a replacement of one iter OpOperand of an scf.for to the replacement value with a different t...
Definition SCF.h:109
ForallOp getForallOpThreadIndexOwner(Value val)
Returns the ForallOp parent of an thread index variable.
Definition SCF.cpp:1426
SmallVector< Value > replaceAndCastForOpIterArg(RewriterBase &rewriter, scf::ForOp forOp, OpOperand &operand, Value replacement, const ValueTypeCastFnTy &castFn)
Definition SCF.cpp:912
LogicalResult promote(RewriterBase &rewriter, scf::ForallOp forallOp)
Promotes the loop body of a scf::ForallOp to its containing block.
Definition SCF.cpp:752
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
ValueVector results
Definition SCF.h:70
LoopVector loops
Definition SCF.h:69