/home/arjun/llvm-project/mlir/include/mlir/Interfaces/ControlFlowInterfaces.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- ControlFlowInterfaces.h - ControlFlow Interfaces ---------*- 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 contains the definitions of the branch interfaces defined in |
10 | | // `ControlFlowInterfaces.td`. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #ifndef MLIR_INTERFACES_CONTROLFLOWINTERFACES_H |
15 | | #define MLIR_INTERFACES_CONTROLFLOWINTERFACES_H |
16 | | |
17 | | #include "mlir/IR/OpDefinition.h" |
18 | | |
19 | | namespace mlir { |
20 | | class BranchOpInterface; |
21 | | |
22 | | //===----------------------------------------------------------------------===// |
23 | | // BranchOpInterface |
24 | | //===----------------------------------------------------------------------===// |
25 | | |
26 | | namespace detail { |
27 | | /// Return the `BlockArgument` corresponding to operand `operandIndex` in some |
28 | | /// successor if `operandIndex` is within the range of `operands`, or None if |
29 | | /// `operandIndex` isn't a successor operand index. |
30 | | Optional<BlockArgument> |
31 | | getBranchSuccessorArgument(Optional<OperandRange> operands, |
32 | | unsigned operandIndex, Block *successor); |
33 | | |
34 | | /// Verify that the given operands match those of the given successor block. |
35 | | LogicalResult verifyBranchSuccessorOperands(Operation *op, unsigned succNo, |
36 | | Optional<OperandRange> operands); |
37 | | } // namespace detail |
38 | | |
39 | | //===----------------------------------------------------------------------===// |
40 | | // RegionBranchOpInterface |
41 | | //===----------------------------------------------------------------------===// |
42 | | |
43 | | /// This class represents a successor of a region. A region successor can either |
44 | | /// be another region, or the parent operation. If the successor is a region, |
45 | | /// this class accepts the destination region, as well as a set of arguments |
46 | | /// from that region that will be populated by values from the current region. |
47 | | /// If the successor is the parent operation, this class accepts an optional set |
48 | | /// of results that will be populated by values from the current region. |
49 | | class RegionSuccessor { |
50 | | public: |
51 | | /// Initialize a successor that branches to another region of the parent |
52 | | /// operation. |
53 | | RegionSuccessor(Region *region, Block::BlockArgListType regionInputs = {}) |
54 | 0 | : region(region), inputs(regionInputs) {} |
55 | | /// Initialize a successor that branches back to/out of the parent operation. |
56 | | RegionSuccessor(Optional<Operation::result_range> results = {}) |
57 | 0 | : region(nullptr), inputs(results ? ValueRange(*results) : ValueRange()) { |
58 | 0 | } |
59 | | |
60 | | /// Return the given region successor. Returns nullptr if the successor is the |
61 | | /// parent operation. |
62 | 0 | Region *getSuccessor() const { return region; } |
63 | | |
64 | | /// Return the inputs to the successor that are remapped by the exit values of |
65 | | /// the current region. |
66 | 0 | ValueRange getSuccessorInputs() const { return inputs; } |
67 | | |
68 | | private: |
69 | | Region *region; |
70 | | ValueRange inputs; |
71 | | }; |
72 | | |
73 | | //===----------------------------------------------------------------------===// |
74 | | // ControlFlow Interfaces |
75 | | //===----------------------------------------------------------------------===// |
76 | | |
77 | | #include "mlir/Interfaces/ControlFlowInterfaces.h.inc" |
78 | | |
79 | | //===----------------------------------------------------------------------===// |
80 | | // ControlFlow Traits |
81 | | //===----------------------------------------------------------------------===// |
82 | | |
83 | | namespace OpTrait { |
84 | | /// This trait indicates that a terminator operation is "return-like". This |
85 | | /// means that it exits its current region and forwards its operands as "exit" |
86 | | /// values to the parent region. Operations with this trait are not permitted to |
87 | | /// contain successors or produce results. |
88 | | template <typename ConcreteType> |
89 | | struct ReturnLike : public TraitBase<ConcreteType, ReturnLike> { |
90 | 0 | static LogicalResult verifyTrait(Operation *op) { |
91 | 0 | static_assert(ConcreteType::template hasTrait<IsTerminator>(), |
92 | 0 | "expected operation to be a terminator"); |
93 | 0 | static_assert(ConcreteType::template hasTrait<ZeroResult>(), |
94 | 0 | "expected operation to have zero results"); |
95 | 0 | static_assert(ConcreteType::template hasTrait<ZeroSuccessor>(), |
96 | 0 | "expected operation to have zero successors"); |
97 | 0 | return success(); |
98 | 0 | } |
99 | | }; |
100 | | } // namespace OpTrait |
101 | | |
102 | | } // end namespace mlir |
103 | | |
104 | | #endif // MLIR_INTERFACES_CONTROLFLOWINTERFACES_H |