CIRCT  19.0.0git
BackedgeBuilder.h
Go to the documentation of this file.
1 //===- BackedgeBuilder.h - Support for building backedges -------*- 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 // Backedges are operations/values which have to exist as operands before
10 // they are produced in a result. Since it isn't clear how to build backedges
11 // in MLIR, these helper classes set up a canonical way to do so.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef CIRCT_SUPPORT_BACKEDGEBUILDER_H
16 #define CIRCT_SUPPORT_BACKEDGEBUILDER_H
17 
18 #include "mlir/IR/Location.h"
19 #include "mlir/IR/Value.h"
20 #include "llvm/ADT/SmallVector.h"
21 
22 namespace mlir {
23 class OpBuilder;
24 class PatternRewriter;
25 class Operation;
26 } // namespace mlir
27 
28 namespace circt {
29 
30 class Backedge;
31 
32 /// Instantiate one of these and use it to build typed backedges. Backedges
33 /// which get used as operands must be assigned to with the actual value before
34 /// this class is destructed, usually at the end of a scope. It will check that
35 /// invariant then erase all the backedge ops during destruction.
36 ///
37 /// Example use:
38 /// ```
39 /// circt::BackedgeBuilder back(rewriter, loc);
40 /// circt::Backedge ready = back.get(rewriter.getI1Type());
41 /// // Use `ready` as a `Value`.
42 /// auto addOp = rewriter.create<addOp>(loc, ready);
43 /// // When the actual value is available,
44 /// ready.set(anotherOp.getResult(0));
45 /// ```
47  friend class Backedge;
48 
49 public:
50  /// To build a backedge op and manipulate it, we need a `PatternRewriter` and
51  /// a `Location`. Store them during construct of this instance and use them
52  /// when building.
53  BackedgeBuilder(mlir::OpBuilder &builder, mlir::Location loc);
54  BackedgeBuilder(mlir::PatternRewriter &rewriter, mlir::Location loc);
56 
57  /// Create a typed backedge. If no location is provided, the one passed to the
58  /// constructor will be used.
59  Backedge get(mlir::Type resultType, mlir::LocationAttr optionalLoc = {});
60 
61  /// Clear the backedges, erasing any remaining cursor ops. Returns `failure`
62  /// and emits diagnostic messages if a backedge is still active.
63  mlir::LogicalResult clearOrEmitError();
64 
65  /// Abandon the backedges, suppressing any diagnostics if they are still
66  /// active upon destruction of the backedge builder. Also, any currently
67  /// existing cursor ops will be abandoned.
68  void abandon();
69 
70 private:
71  mlir::OpBuilder &builder;
72  mlir::PatternRewriter *rewriter;
73  mlir::Location loc;
74  llvm::SmallVector<mlir::Operation *, 16> edges;
75 };
76 
77 /// `Backedge` is a wrapper class around a `Value`. When assigned another
78 /// `Value`, it replaces all uses of itself with the new `Value` then become a
79 /// wrapper around the new `Value`.
80 class Backedge {
81  friend class BackedgeBuilder;
82 
83  /// `Backedge` is constructed exclusively by `BackedgeBuilder`.
84  Backedge(mlir::Operation *op);
85 
86 public:
87  Backedge() {}
88 
89  explicit operator bool() const { return !!value; }
90  operator mlir::Value() const { return value; }
91  void setValue(mlir::Value);
92 
93 private:
94  mlir::Value value;
95  bool set = false;
96 };
97 
98 } // namespace circt
99 
100 #endif // CIRCT_SUPPORT_BACKEDGEBUILDER_H
Instantiate one of these and use it to build typed backedges.
void abandon()
Abandon the backedges, suppressing any diagnostics if they are still active upon destruction of the b...
llvm::SmallVector< mlir::Operation *, 16 > edges
BackedgeBuilder(mlir::PatternRewriter &rewriter, mlir::Location loc)
Backedge get(mlir::Type resultType, mlir::LocationAttr optionalLoc={})
Create a typed backedge.
mlir::OpBuilder & builder
mlir::PatternRewriter * rewriter
mlir::LogicalResult clearOrEmitError()
Clear the backedges, erasing any remaining cursor ops.
BackedgeBuilder(mlir::OpBuilder &builder, mlir::Location loc)
To build a backedge op and manipulate it, we need a PatternRewriter and a Location.
Backedge is a wrapper class around a Value.
mlir::Value value
void setValue(mlir::Value)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21