MLIR  15.0.0git
ImplicitLocOpBuilder.h
Go to the documentation of this file.
1 //===- ImplicitLocOpBuilder.h - Convenience OpBuilder -----------*- 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 // Helper class to create ops with a modally set location.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_IMPLICITLOCOPBUILDER_H
14 #define MLIR_IR_IMPLICITLOCOPBUILDER_H
15 
16 #include "mlir/IR/Builders.h"
17 
18 namespace mlir {
19 
20 /// ImplicitLocOpBuilder maintains a 'current location', allowing use of the
21 /// create<> method without specifying the location. It is otherwise the same
22 /// as OpBuilder.
24 public:
25  /// OpBuilder has a bunch of convenience constructors - we support them all
26  /// with the additional Location.
27  template <typename... T>
28  ImplicitLocOpBuilder(Location loc, T &&...operands)
29  : OpBuilder(std::forward<T>(operands)...), curLoc(loc) {}
30 
31  /// Create a builder and set the insertion point to before the first operation
32  /// in the block but still inside the block.
34  Listener *listener = nullptr) {
35  return ImplicitLocOpBuilder(loc, block, block->begin(), listener);
36  }
37 
38  /// Create a builder and set the insertion point to after the last operation
39  /// in the block but still inside the block.
41  Listener *listener = nullptr) {
42  return ImplicitLocOpBuilder(loc, block, block->end(), listener);
43  }
44 
45  /// Create a builder and set the insertion point to before the block
46  /// terminator.
48  Listener *listener = nullptr) {
49  auto *terminator = block->getTerminator();
50  assert(terminator != nullptr && "the block has no terminator");
51  return ImplicitLocOpBuilder(loc, block, Block::iterator(terminator),
52  listener);
53  }
54 
55  /// Accessors for the implied location.
56  Location getLoc() const { return curLoc; }
57  void setLoc(Location loc) { curLoc = loc; }
58 
59  // We allow clients to use the explicit-loc version of create as well.
60  using OpBuilder::create;
62 
63  /// Create an operation of specific op type at the current insertion point and
64  /// location.
65  template <typename OpTy, typename... Args>
66  OpTy create(Args &&...args) {
67  return OpBuilder::create<OpTy>(curLoc, std::forward<Args>(args)...);
68  }
69 
70  /// Create an operation of specific op type at the current insertion point,
71  /// and immediately try to fold it. This functions populates 'results' with
72  /// the results after folding the operation.
73  template <typename OpTy, typename... Args>
74  void createOrFold(llvm::SmallVectorImpl<Value> &results, Args &&...args) {
75  OpBuilder::createOrFold<OpTy>(results, curLoc, std::forward<Args>(args)...);
76  }
77 
78  /// Overload to create or fold a single result operation.
79  template <typename OpTy, typename... Args>
80  typename std::enable_if<OpTy::template hasTrait<mlir::OpTrait::OneResult>(),
81  Value>::type
82  createOrFold(Args &&...args) {
83  return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
84  }
85 
86  /// Overload to create or fold a zero result operation.
87  template <typename OpTy, typename... Args>
88  typename std::enable_if<OpTy::template hasTrait<mlir::OpTrait::ZeroResults>(),
89  OpTy>::type
90  createOrFold(Args &&...args) {
91  return OpBuilder::createOrFold<OpTy>(curLoc, std::forward<Args>(args)...);
92  }
93 
94  /// This builder can also be used to emit diagnostics to the current location.
96  emitError(const llvm::Twine &message = llvm::Twine()) {
97  return mlir::emitError(curLoc, message);
98  }
100  emitWarning(const llvm::Twine &message = llvm::Twine()) {
101  return mlir::emitWarning(curLoc, message);
102  }
104  emitRemark(const llvm::Twine &message = llvm::Twine()) {
105  return mlir::emitRemark(curLoc, message);
106  }
107 
108 private:
109  Location curLoc;
110 };
111 
112 } // namespace mlir
113 
114 #endif // MLIR_IR_IMPLICITLOCOPBUILDER_H
Include the generated interface declarations.
iterator begin()
Definition: Block.h:134
void createOrFold(SmallVectorImpl< Value > &results, Location location, Args &&...args)
Create an operation of specific op type at the current insertion point, and immediately try to fold i...
Definition: Builders.h:451
static ImplicitLocOpBuilder atBlockEnd(Location loc, Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to after the last operation in the block but still insid...
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:311
Block represents an ordered list of Operations.
Definition: Block.h:29
static ImplicitLocOpBuilder atBlockBegin(Location loc, Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the first operation in the block but still ins...
This class represents a listener that may be used to hook into various actions within an OpBuilder...
Definition: Builders.h:234
InFlightDiagnostic emitWarning(Location loc)
Utility method to emit a warning message using this location.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:380
OpListType::iterator iterator
Definition: Block.h:131
iterator end()
Definition: Block.h:135
mlir::InFlightDiagnostic emitRemark(const llvm::Twine &message=llvm::Twine())
void createOrFold(llvm::SmallVectorImpl< Value > &results, Args &&...args)
Create an operation of specific op type at the current insertion point, and immediately try to fold i...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:85
Operation * getTerminator()
Get the terminator operation of this block.
Definition: Block.cpp:230
OpTy create(Args &&...args)
Create an operation of specific op type at the current insertion point and location.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
std::enable_if< OpTy::template hasTrait< mlir::OpTrait::OneResult >), Value >::type createOrFold(Args &&...args)
Overload to create or fold a single result operation.
mlir::InFlightDiagnostic emitWarning(const llvm::Twine &message=llvm::Twine())
ImplicitLocOpBuilder maintains a &#39;current location&#39;, allowing use of the create<> method without spec...
mlir::InFlightDiagnostic emitError(const llvm::Twine &message=llvm::Twine())
This builder can also be used to emit diagnostics to the current location.
InFlightDiagnostic emitRemark(Location loc)
Utility method to emit a remark message using this location.
std::enable_if< OpTy::template hasTrait< mlir::OpTrait::ZeroResults >), OpTy >::type createOrFold(Args &&...args)
Overload to create or fold a zero result operation.
This class helps build Operations.
Definition: Builders.h:177
ImplicitLocOpBuilder(Location loc, T &&...operands)
OpBuilder has a bunch of convenience constructors - we support them all with the additional Location...
Location getLoc() const
Accessors for the implied location.
static ImplicitLocOpBuilder atBlockTerminator(Location loc, Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the block terminator.