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"
23
24namespace mlir {
25namespace scf {
26void buildTerminatedBody(OpBuilder &builder, Location loc);
27} // namespace scf
28} // namespace mlir
29
30#define GET_OP_CLASSES
31#include "mlir/Dialect/SCF/IR/SCFOps.h.inc"
32
33namespace mlir {
34namespace scf {
35
36/// Returns the loop parent of an induction variable. If the provided value is
37/// not an induction variable, then return nullptr.
38ForOp getForInductionVarOwner(Value val);
39
40/// Returns the parallel loop parent of an induction variable. If the provided
41/// value is not an induction variable, then return nullptr.
42ParallelOp getParallelForInductionVarOwner(Value val);
43
44/// Returns the ForallOp parent of an thread index variable.
45/// If the provided value is not a thread index variable, then return nullptr.
46ForallOp getForallOpThreadIndexOwner(Value val);
47
48/// Return true if ops a and b (or their ancestors) are in mutually exclusive
49/// regions/blocks of an IfOp.
50// TODO: Consider moving this functionality to RegionBranchOpInterface.
51bool insideMutuallyExclusiveBranches(Operation *a, Operation *b);
52
53/// Promotes the loop body of a scf::ForallOp to its containing block.
54/// Only tensor.parallel_insert_slice combining ops are supported.
55/// Returns failure without modifying the IR if a combining op is unsupported.
56LogicalResult promote(RewriterBase &rewriter, scf::ForallOp forallOp);
57
58/// An owning vector of values, handy to return from functions.
65
66/// Creates a perfect nest of "for" loops, i.e. all loops but the innermost
67/// contain only another loop and a terminator. The lower, upper bounds and
68/// steps are provided as `lbs`, `ubs` and `steps`, which are expected to be of
69/// the same size. `iterArgs` points to the initial values of the loop iteration
70/// arguments, which will be forwarded through the nest to the innermost loop.
71/// The body of the loop is populated using `bodyBuilder`, which accepts an
72/// ordered list of induction variables of all loops, followed by a list of
73/// iteration arguments of the innermost loop, in the same order as provided to
74/// `iterArgs`. This function is expected to return as many values as
75/// `iterArgs`, of the same type and in the same order, that will be treated as
76/// yielded from the loop body and forwarded back through the loop nest. If the
77/// function is not provided, the loop nest is not expected to have iteration
78/// arguments, the body of the innermost loop will be left empty, containing
79/// only the zero-operand terminator. Returns the LoopNest containing the list
80/// of perfectly nest scf::ForOp build during the call.
81/// If bound arrays are empty, the body builder will be called
82/// once to construct the IR outside of the loop with an empty list of induction
83/// variables.
85 OpBuilder &builder, Location loc, ValueRange lbs, ValueRange ubs,
86 ValueRange steps, ValueRange iterArgs,
88 bodyBuilder = nullptr);
89
90/// A convenience version for building loop nests without iteration arguments
91/// (like for reductions). Does not take the initial value of reductions or
92/// expect the body building functions to return their current value.
93/// The built nested scf::For are captured in `capturedLoops` when non-null.
95 ValueRange ubs, ValueRange steps,
97 bodyBuilder = nullptr);
98
99/// Perform a replacement of one iter OpOperand of an scf.for to the
100/// `replacement` value with a different type. A callback is used to insert
101/// cast ops inside the block to account for type differences.
105 scf::ForOp forOp,
106 OpOperand &operand,
108 const ValueTypeCastFnTy &castFn);
109
110/// A simple, conservative analysis to determine if the loop is shape
111/// preserving. I.e., the arg-th yielded value has the same shape as the
112/// corresponding region iter_arg: it is the iter_arg, or derives from it
113/// through tied results of destination-style ops and of shape-preserving loops.
114bool isShapePreserving(ForOp forOp, int64_t arg);
115
116/// Helper function to compute the difference between two values. This is used
117/// by the loop implementations to compute the trip count.
118std::optional<llvm::APSInt> computeUbMinusLb(Value lb, Value ub, bool isSigned);
119
120} // namespace scf
121} // namespace mlir
122#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:256
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:392
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:60
ParallelOp getParallelForInductionVarOwner(Value val)
Returns the parallel loop parent of an induction variable.
Definition SCF.cpp:3065
void buildTerminatedBody(OpBuilder &builder, Location loc)
Default callback for IfOp builders. Inserts a yield without arguments.
Definition SCF.cpp:96
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:824
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:1984
std::optional< llvm::APSInt > computeUbMinusLb(Value lb, Value ub, bool isSigned)
Helper function to compute the difference between two values.
Definition SCF.cpp:117
ForOp getForInductionVarOwner(Value val)
Returns the loop parent of an induction variable.
Definition SCF.cpp:681
SmallVector< Value > ValueVector
An owning vector of values, handy to return from functions.
Definition SCF.h:59
bool isShapePreserving(ForOp forOp, int64_t arg)
A simple, conservative analysis to determine if the loop is shape preserving.
Definition SCF.cpp:978
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:102
ForallOp getForallOpThreadIndexOwner(Value val)
Returns the ForallOp parent of an thread index variable.
Definition SCF.cpp:1464
SmallVector< Value > replaceAndCastForOpIterArg(RewriterBase &rewriter, scf::ForOp forOp, OpOperand &operand, Value replacement, const ValueTypeCastFnTy &castFn)
Definition SCF.cpp:913
LogicalResult promote(RewriterBase &rewriter, scf::ForallOp forallOp)
Promotes the loop body of a scf::ForallOp to its containing block.
Definition SCF.cpp:753
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
ValueVector results
Definition SCF.h:63
LoopVector loops
Definition SCF.h:62