MLIR  14.0.0git
Builders.h
Go to the documentation of this file.
1 //===- Builders.h - Helpers for constructing MLIR Classes -------*- 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 #ifndef MLIR_IR_BUILDERS_H
10 #define MLIR_IR_BUILDERS_H
11 
12 #include "mlir/IR/OpDefinition.h"
13 #include "llvm/Support/Compiler.h"
14 
15 namespace mlir {
16 
17 class AffineExpr;
18 class BlockAndValueMapping;
19 class UnknownLoc;
20 class FileLineColLoc;
21 class Type;
22 class PrimitiveType;
23 class IntegerType;
24 class FloatType;
25 class FunctionType;
26 class IndexType;
27 class MemRefType;
28 class VectorType;
29 class RankedTensorType;
30 class UnrankedTensorType;
31 class TupleType;
32 class NoneType;
33 class BoolAttr;
34 class IntegerAttr;
35 class FloatAttr;
36 class StringAttr;
37 class TypeAttr;
38 class ArrayAttr;
39 class SymbolRefAttr;
40 class ElementsAttr;
41 class DenseElementsAttr;
42 class DenseIntElementsAttr;
43 class AffineMapAttr;
44 class AffineMap;
45 class UnitAttr;
46 
47 /// This class is a general helper class for creating context-global objects
48 /// like types, attributes, and affine expressions.
49 class Builder {
50 public:
51  explicit Builder(MLIRContext *context) : context(context) {}
52  explicit Builder(Operation *op) : Builder(op->getContext()) {}
53 
54  MLIRContext *getContext() const { return context; }
55 
56  // Locations.
59  Attribute metadata = Attribute());
60 
61  // Types.
68 
69  IndexType getIndexType();
70 
71  IntegerType getI1Type();
72  IntegerType getI8Type();
73  IntegerType getI32Type();
74  IntegerType getI64Type();
75  IntegerType getIntegerType(unsigned width);
76  IntegerType getIntegerType(unsigned width, bool isSigned);
77  FunctionType getFunctionType(TypeRange inputs, TypeRange results);
78  TupleType getTupleType(TypeRange elementTypes);
79  NoneType getNoneType();
80 
81  /// Get or construct an instance of the type 'ty' with provided arguments.
82  template <typename Ty, typename... Args>
83  Ty getType(Args... args) {
84  return Ty::get(context, args...);
85  }
86 
87  // Attributes.
88  NamedAttribute getNamedAttr(StringRef name, Attribute val);
89 
90  UnitAttr getUnitAttr();
92  DictionaryAttr getDictionaryAttr(ArrayRef<NamedAttribute> value);
93  IntegerAttr getIntegerAttr(Type type, int64_t value);
94  IntegerAttr getIntegerAttr(Type type, const APInt &value);
95  FloatAttr getFloatAttr(Type type, double value);
96  FloatAttr getFloatAttr(Type type, const APFloat &value);
97  StringAttr getStringAttr(const Twine &bytes);
98  ArrayAttr getArrayAttr(ArrayRef<Attribute> value);
99 
100  // Returns a 0-valued attribute of the given `type`. This function only
101  // supports boolean, integer, and 16-/32-/64-bit float types, and vector or
102  // ranked tensor of them. Returns null attribute otherwise.
103  Attribute getZeroAttr(Type type);
104 
105  // Convenience methods for fixed types.
106  FloatAttr getF16FloatAttr(float value);
107  FloatAttr getF32FloatAttr(float value);
108  FloatAttr getF64FloatAttr(double value);
109 
110  IntegerAttr getI8IntegerAttr(int8_t value);
111  IntegerAttr getI16IntegerAttr(int16_t value);
112  IntegerAttr getI32IntegerAttr(int32_t value);
113  IntegerAttr getI64IntegerAttr(int64_t value);
114  IntegerAttr getIndexAttr(int64_t value);
115 
116  /// Signed and unsigned integer attribute getters.
117  IntegerAttr getSI32IntegerAttr(int32_t value);
118  IntegerAttr getUI32IntegerAttr(uint32_t value);
119 
120  /// Vector-typed DenseIntElementsAttr getters. `values` must not be empty.
125 
126  /// Tensor-typed DenseIntElementsAttr getters. `values` can be empty.
127  /// These are generally preferable for representing general lists of integers
128  /// as attributes.
132 
133  ArrayAttr getAffineMapArrayAttr(ArrayRef<AffineMap> values);
134  ArrayAttr getBoolArrayAttr(ArrayRef<bool> values);
135  ArrayAttr getI32ArrayAttr(ArrayRef<int32_t> values);
136  ArrayAttr getI64ArrayAttr(ArrayRef<int64_t> values);
137  ArrayAttr getIndexArrayAttr(ArrayRef<int64_t> values);
138  ArrayAttr getF32ArrayAttr(ArrayRef<float> values);
139  ArrayAttr getF64ArrayAttr(ArrayRef<double> values);
140  ArrayAttr getStrArrayAttr(ArrayRef<StringRef> values);
141  ArrayAttr getTypeArrayAttr(TypeRange values);
142 
143  // Affine expressions and affine maps.
144  AffineExpr getAffineDimExpr(unsigned position);
145  AffineExpr getAffineSymbolExpr(unsigned position);
146  AffineExpr getAffineConstantExpr(int64_t constant);
147 
148  // Special cases of affine maps and integer sets
149  /// Returns a zero result affine map with no dimensions or symbols: () -> ().
151  /// Returns a single constant result affine map with 0 dimensions and 0
152  /// symbols. One constant result: () -> (val).
153  AffineMap getConstantAffineMap(int64_t val);
154  // One dimension id identity map: (i) -> (i).
156  // Multi-dimensional identity map: (d0, d1, d2) -> (d0, d1, d2).
157  AffineMap getMultiDimIdentityMap(unsigned rank);
158  // One symbol identity map: ()[s] -> (s).
160 
161  /// Returns a map that shifts its (single) input dimension by 'shift'.
162  /// (d0) -> (d0 + shift)
163  AffineMap getSingleDimShiftAffineMap(int64_t shift);
164 
165  /// Returns an affine map that is a translation (shift) of all result
166  /// expressions in 'map' by 'shift'.
167  /// Eg: input: (d0, d1)[s0] -> (d0, d1 + s0), shift = 2
168  /// returns: (d0, d1)[s0] -> (d0 + 2, d1 + s0 + 2)
169  AffineMap getShiftedAffineMap(AffineMap map, int64_t shift);
170 
171 protected:
173 };
174 
175 /// This class helps build Operations. Operations that are created are
176 /// automatically inserted at an insertion point. The builder is copyable.
177 class OpBuilder : public Builder {
178 public:
179  struct Listener;
180 
181  /// Create a builder with the given context.
182  explicit OpBuilder(MLIRContext *ctx, Listener *listener = nullptr)
183  : Builder(ctx), listener(listener) {}
184 
185  /// Create a builder and set the insertion point to the start of the region.
186  explicit OpBuilder(Region *region, Listener *listener = nullptr)
187  : OpBuilder(region->getContext(), listener) {
188  if (!region->empty())
189  setInsertionPoint(&region->front(), region->front().begin());
190  }
191  explicit OpBuilder(Region &region, Listener *listener = nullptr)
192  : OpBuilder(&region, listener) {}
193 
194  /// Create a builder and set insertion point to the given operation, which
195  /// will cause subsequent insertions to go right before it.
196  explicit OpBuilder(Operation *op, Listener *listener = nullptr)
197  : OpBuilder(op->getContext(), listener) {
198  setInsertionPoint(op);
199  }
200 
201  OpBuilder(Block *block, Block::iterator insertPoint,
202  Listener *listener = nullptr)
203  : OpBuilder(block->getParent()->getContext(), listener) {
204  setInsertionPoint(block, insertPoint);
205  }
206 
207  /// Create a builder and set the insertion point to before the first operation
208  /// in the block but still inside the block.
209  static OpBuilder atBlockBegin(Block *block, Listener *listener = nullptr) {
210  return OpBuilder(block, block->begin(), listener);
211  }
212 
213  /// Create a builder and set the insertion point to after the last operation
214  /// in the block but still inside the block.
215  static OpBuilder atBlockEnd(Block *block, Listener *listener = nullptr) {
216  return OpBuilder(block, block->end(), listener);
217  }
218 
219  /// Create a builder and set the insertion point to before the block
220  /// terminator.
222  Listener *listener = nullptr) {
223  auto *terminator = block->getTerminator();
224  assert(terminator != nullptr && "the block has no terminator");
225  return OpBuilder(block, Block::iterator(terminator), listener);
226  }
227 
228  //===--------------------------------------------------------------------===//
229  // Listeners
230  //===--------------------------------------------------------------------===//
231 
232  /// This class represents a listener that may be used to hook into various
233  /// actions within an OpBuilder.
234  struct Listener {
235  virtual ~Listener();
236 
237  /// Notification handler for when an operation is inserted into the builder.
238  /// `op` is the operation that was inserted.
239  virtual void notifyOperationInserted(Operation *op) {}
240 
241  /// Notification handler for when a block is created using the builder.
242  /// `block` is the block that was created.
243  virtual void notifyBlockCreated(Block *block) {}
244  };
245 
246  /// Sets the listener of this builder to the one provided.
247  void setListener(Listener *newListener) { listener = newListener; }
248 
249  /// Returns the current listener of this builder, or nullptr if this builder
250  /// doesn't have a listener.
251  Listener *getListener() const { return listener; }
252 
253  //===--------------------------------------------------------------------===//
254  // Insertion Point Management
255  //===--------------------------------------------------------------------===//
256 
257  /// This class represents a saved insertion point.
258  class InsertPoint {
259  public:
260  /// Creates a new insertion point which doesn't point to anything.
261  InsertPoint() = default;
262 
263  /// Creates a new insertion point at the given location.
264  InsertPoint(Block *insertBlock, Block::iterator insertPt)
265  : block(insertBlock), point(insertPt) {}
266 
267  /// Returns true if this insert point is set.
268  bool isSet() const { return (block != nullptr); }
269 
270  Block *getBlock() const { return block; }
271  Block::iterator getPoint() const { return point; }
272 
273  private:
274  Block *block = nullptr;
275  Block::iterator point;
276  };
277 
278  /// RAII guard to reset the insertion point of the builder when destroyed.
280  public:
282  : builder(&builder), ip(builder.saveInsertionPoint()) {}
283 
285  if (builder)
286  builder->restoreInsertionPoint(ip);
287  }
288 
289  InsertionGuard(const InsertionGuard &) = delete;
290  InsertionGuard &operator=(const InsertionGuard &) = delete;
291 
292  /// Implement the move constructor to clear the builder field of `other`.
293  /// That way it does not restore the insertion point upon destruction as
294  /// that should be done exclusively by the just constructed InsertionGuard.
295  InsertionGuard(InsertionGuard &&other) noexcept
296  : builder(other.builder), ip(other.ip) {
297  other.builder = nullptr;
298  }
299 
300  InsertionGuard &operator=(InsertionGuard &&other) = delete;
301 
302  private:
303  OpBuilder *builder;
305  };
306 
307  /// Reset the insertion point to no location. Creating an operation without a
308  /// set insertion point is an error, but this can still be useful when the
309  /// current insertion point a builder refers to is being removed.
311  this->block = nullptr;
312  insertPoint = Block::iterator();
313  }
314 
315  /// Return a saved insertion point.
317  return InsertPoint(getInsertionBlock(), getInsertionPoint());
318  }
319 
320  /// Restore the insert point to a previously saved point.
322  if (ip.isSet())
323  setInsertionPoint(ip.getBlock(), ip.getPoint());
324  else
325  clearInsertionPoint();
326  }
327 
328  /// Set the insertion point to the specified location.
329  void setInsertionPoint(Block *block, Block::iterator insertPoint) {
330  // TODO: check that insertPoint is in this rather than some other block.
331  this->block = block;
332  this->insertPoint = insertPoint;
333  }
334 
335  /// Sets the insertion point to the specified operation, which will cause
336  /// subsequent insertions to go right before it.
338  setInsertionPoint(op->getBlock(), Block::iterator(op));
339  }
340 
341  /// Sets the insertion point to the node after the specified operation, which
342  /// will cause subsequent insertions to go right after it.
344  setInsertionPoint(op->getBlock(), ++Block::iterator(op));
345  }
346 
347  /// Sets the insertion point to the node after the specified value. If value
348  /// has a defining operation, sets the insertion point to the node after such
349  /// defining operation. This will cause subsequent insertions to go right
350  /// after it. Otherwise, value is a BlockArgument. Sets the insertion point to
351  /// the start of its block.
353  if (Operation *op = val.getDefiningOp()) {
355  } else {
356  auto blockArg = val.cast<BlockArgument>();
357  setInsertionPointToStart(blockArg.getOwner());
358  }
359  }
360 
361  /// Sets the insertion point to the start of the specified block.
363  setInsertionPoint(block, block->begin());
364  }
365 
366  /// Sets the insertion point to the end of the specified block.
368  setInsertionPoint(block, block->end());
369  }
370 
371  /// Return the block the current insertion point belongs to. Note that the
372  /// the insertion point is not necessarily the end of the block.
373  Block *getInsertionBlock() const { return block; }
374 
375  /// Returns the current insertion point of the builder.
376  Block::iterator getInsertionPoint() const { return insertPoint; }
377 
378  /// Returns the current block of the builder.
379  Block *getBlock() const { return block; }
380 
381  //===--------------------------------------------------------------------===//
382  // Block Creation
383  //===--------------------------------------------------------------------===//
384 
385  /// Add new block with 'argTypes' arguments and set the insertion point to the
386  /// end of it. The block is inserted at the provided insertion point of
387  /// 'parent'. `locs` contains the locations of the inserted arguments, and
388  /// should match the size of `argTypes`.
389  Block *createBlock(Region *parent, Region::iterator insertPt = {},
390  TypeRange argTypes = llvm::None,
391  ArrayRef<Location> locs = llvm::None);
392 
393  /// Add new block with 'argTypes' arguments and set the insertion point to the
394  /// end of it. The block is placed before 'insertBefore'. `locs` contains the
395  /// locations of the inserted arguments, and should match the size of
396  /// `argTypes`.
397  Block *createBlock(Block *insertBefore, TypeRange argTypes = llvm::None,
398  ArrayRef<Location> locs = llvm::None);
399 
400  //===--------------------------------------------------------------------===//
401  // Operation Creation
402  //===--------------------------------------------------------------------===//
403 
404  /// Insert the given operation at the current insertion point and return it.
405  Operation *insert(Operation *op);
406 
407  /// Creates an operation given the fields represented as an OperationState.
408  Operation *createOperation(const OperationState &state);
409 
410 private:
411  /// Helper for sanity checking preconditions for create* methods below.
412  template <typename OpT>
413  RegisteredOperationName getCheckRegisteredInfo(MLIRContext *ctx) {
415  RegisteredOperationName::lookup(OpT::getOperationName(), ctx);
416  if (LLVM_UNLIKELY(!opName)) {
417  llvm::report_fatal_error(
418  "Building op `" + OpT::getOperationName() +
419  "` but it isn't registered in this MLIRContext: the dialect may not "
420  "be loaded or this operation isn't registered by the dialect. See "
421  "also https://mlir.llvm.org/getting_started/Faq/"
422  "#registered-loaded-dependent-whats-up-with-dialects-management");
423  }
424  return *opName;
425  }
426 
427 public:
428  /// Create an operation of specific op type at the current insertion point.
429  template <typename OpTy, typename... Args>
430  OpTy create(Location location, Args &&...args) {
431  OperationState state(location,
432  getCheckRegisteredInfo<OpTy>(location.getContext()));
433  OpTy::build(*this, state, std::forward<Args>(args)...);
434  auto *op = createOperation(state);
435  auto result = dyn_cast<OpTy>(op);
436  assert(result && "builder didn't return the right type");
437  return result;
438  }
439 
440  /// Create an operation of specific op type at the current insertion point,
441  /// and immediately try to fold it. This functions populates 'results' with
442  /// the results after folding the operation.
443  template <typename OpTy, typename... Args>
445  Args &&...args) {
446  // Create the operation without using 'createOperation' as we don't want to
447  // insert it yet.
448  OperationState state(location,
449  getCheckRegisteredInfo<OpTy>(location.getContext()));
450  OpTy::build(*this, state, std::forward<Args>(args)...);
451  Operation *op = Operation::create(state);
452 
453  // Fold the operation. If successful destroy it, otherwise insert it.
454  if (succeeded(tryFold(op, results)))
455  op->destroy();
456  else
457  insert(op);
458  }
459 
460  /// Overload to create or fold a single result operation.
461  template <typename OpTy, typename... Args>
462  typename std::enable_if<OpTy::template hasTrait<OpTrait::OneResult>(),
463  Value>::type
464  createOrFold(Location location, Args &&...args) {
465  SmallVector<Value, 1> results;
466  createOrFold<OpTy>(results, location, std::forward<Args>(args)...);
467  return results.front();
468  }
469 
470  /// Overload to create or fold a zero result operation.
471  template <typename OpTy, typename... Args>
472  typename std::enable_if<OpTy::template hasTrait<OpTrait::ZeroResult>(),
473  OpTy>::type
474  createOrFold(Location location, Args &&...args) {
475  auto op = create<OpTy>(location, std::forward<Args>(args)...);
476  SmallVector<Value, 0> unused;
477  (void)tryFold(op.getOperation(), unused);
478 
479  // Folding cannot remove a zero-result operation, so for convenience we
480  // continue to return it.
481  return op;
482  }
483 
484  /// Attempts to fold the given operation and places new results within
485  /// 'results'. Returns success if the operation was folded, failure otherwise.
486  /// Note: This function does not erase the operation on a successful fold.
487  LogicalResult tryFold(Operation *op, SmallVectorImpl<Value> &results);
488 
489  /// Creates a deep copy of the specified operation, remapping any operands
490  /// that use values outside of the operation using the map that is provided
491  /// ( leaving them alone if no entry is present). Replaces references to
492  /// cloned sub-operations to the corresponding operation that is copied,
493  /// and adds those mappings to the map.
494  Operation *clone(Operation &op, BlockAndValueMapping &mapper);
495  Operation *clone(Operation &op);
496 
497  /// Creates a deep copy of this operation but keep the operation regions
498  /// empty. Operands are remapped using `mapper` (if present), and `mapper` is
499  /// updated to contain the results.
501  return insert(op.cloneWithoutRegions(mapper));
502  }
504  return insert(op.cloneWithoutRegions());
505  }
506  template <typename OpT>
507  OpT cloneWithoutRegions(OpT op) {
508  return cast<OpT>(cloneWithoutRegions(*op.getOperation()));
509  }
510 
511 private:
512  /// The current block this builder is inserting into.
513  Block *block = nullptr;
514  /// The insertion point within the block that this builder is inserting
515  /// before.
516  Block::iterator insertPoint;
517  /// The optional listener for events of this builder.
518  Listener *listener;
519 };
520 
521 } // namespace mlir
522 
523 #endif
Location getUnknownLoc()
Definition: Builders.cpp:26
Include the generated interface declarations.
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
Definition: Builders.h:430
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, ArrayRef< NamedAttribute > attributes, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Definition: Operation.cpp:27
AffineMap getEmptyAffineMap()
Returns a zero result affine map with no dimensions or symbols: () -> ().
Definition: Builders.cpp:297
InsertionGuard(OpBuilder &builder)
Definition: Builders.h:281
This class contains a list of basic blocks and a link to the parent operation it is attached to...
Definition: Region.h:26
iterator begin()
Definition: Block.h:134
DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:115
MLIRContext * getContext() const
Definition: Builders.h:54
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:444
AffineMap getMultiDimIdentityMap(unsigned rank)
Definition: Builders.cpp:308
IntegerAttr getUI32IntegerAttr(uint32_t value)
Definition: Builders.cpp:157
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Definition: Builders.h:373
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
OpBuilder(Region &region, Listener *listener=nullptr)
Definition: Builders.h:191
Ty getType(Args... args)
Get or construct an instance of the type &#39;ty&#39; with provided arguments.
Definition: Builders.h:83
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:141
AffineMap getSingleDimShiftAffineMap(int64_t shift)
Returns a map that shifts its (single) input dimension by &#39;shift&#39;.
Definition: Builders.cpp:322
TupleType getTupleType(TypeRange elementTypes)
Definition: Builders.cpp:71
Block::iterator getPoint() const
Definition: Builders.h:271
Attribute getZeroAttr(Type type)
Definition: Builders.cpp:264
Block represents an ordered list of Operations.
Definition: Block.h:29
Block & front()
Definition: Region.h:65
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition: Builders.h:329
Operation * cloneWithoutRegions(Operation &op)
Definition: Builders.h:503
OpBuilder(Operation *op, Listener *listener=nullptr)
Create a builder and set insertion point to the given operation, which will cause subsequent insertio...
Definition: Builders.h:196
Operation * cloneWithoutRegions(Operation &op, BlockAndValueMapping &mapper)
Creates a deep copy of this operation but keep the operation regions empty.
Definition: Builders.h:500
AffineExpr getAffineSymbolExpr(unsigned position)
Definition: Builders.cpp:289
FloatType getF16Type()
Definition: Builders.cpp:38
BlockListType::iterator iterator
Definition: Region.h:52
NoneType getNoneType()
Definition: Builders.cpp:75
FloatAttr getF64FloatAttr(double value)
Definition: Builders.cpp:181
static OpBuilder atBlockEnd(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to after the last operation in the block but still insid...
Definition: Builders.h:215
Builder(Operation *op)
Definition: Builders.h:52
FloatType getF32Type()
Definition: Builders.cpp:40
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value...
Definition: LogicalResult.h:68
virtual void notifyOperationInserted(Operation *op)
Notification handler for when an operation is inserted into the builder.
Definition: Builders.h:239
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition: Builders.cpp:81
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:220
Block * getBlock()
Returns the operation block that contains this operation.
Definition: Operation.h:96
void destroy()
Destroys this operation and its subclass data.
Definition: Operation.cpp:171
This class represents a listener that may be used to hook into various actions within an OpBuilder...
Definition: Builders.h:234
virtual void notifyBlockCreated(Block *block)
Notification handler for when a block is created using the builder.
Definition: Builders.h:243
static constexpr const bool value
static OpBuilder atBlockBegin(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the first operation in the block but still ins...
Definition: Builders.h:209
IntegerAttr getI16IntegerAttr(int16_t value)
Definition: Builders.cpp:162
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
FloatAttr getF16FloatAttr(float value)
Definition: Builders.cpp:189
ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:226
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:137
Block * getBlock() const
Returns the current block of the builder.
Definition: Builders.h:379
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition: Builders.h:343
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
Definition: Builders.cpp:258
FloatAttr getFloatAttr(Type type, double value)
Definition: Builders.cpp:193
IntegerAttr getI32IntegerAttr(int32_t value)
Definition: Builders.cpp:148
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
InsertionGuard(InsertionGuard &&other) noexcept
Implement the move constructor to clear the builder field of other.
Definition: Builders.h:295
OpListType::iterator iterator
Definition: Block.h:131
bool empty()
Definition: Region.h:60
OpBuilder(Region *region, Listener *listener=nullptr)
Create a builder and set the insertion point to the start of the region.
Definition: Builders.h:186
InsertPoint(Block *insertBlock, Block::iterator insertPt)
Creates a new insertion point at the given location.
Definition: Builders.h:264
std::enable_if< OpTy::template hasTrait< OpTrait::OneResult >), Value >::type createOrFold(Location location, Args &&...args)
Overload to create or fold a single result operation.
Definition: Builders.h:464
IntegerAttr getIntegerAttr(Type type, int64_t value)
Definition: Builders.cpp:170
IntegerAttr getI64IntegerAttr(int64_t value)
Definition: Builders.cpp:99
iterator end()
Definition: Block.h:135
UnitAttr getUnitAttr()
Definition: Builders.cpp:85
IntegerAttr getI8IntegerAttr(int8_t value)
Definition: Builders.cpp:166
Attributes are known-constant values of operations.
Definition: Attributes.h:24
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition: Builders.cpp:91
OpT cloneWithoutRegions(OpT op)
Definition: Builders.h:507
IntegerAttr getSI32IntegerAttr(int32_t value)
Signed and unsigned integer attribute getters.
Definition: Builders.cpp:152
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:58
FloatType getF128Type()
Definition: Builders.cpp:46
Base type for affine expression.
Definition: AffineExpr.h:68
OpBuilder(MLIRContext *ctx, Listener *listener=nullptr)
Create a builder with the given context.
Definition: Builders.h:182
Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())
Definition: Builders.cpp:28
void setInsertionPointAfterValue(Value val)
Sets the insertion point to the node after the specified value.
Definition: Builders.h:352
IntegerType getI8Type()
Definition: Builders.cpp:52
DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)
Vector-typed DenseIntElementsAttr getters. values must not be empty.
Definition: Builders.cpp:103
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:38
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:215
IntegerType getI1Type()
Definition: Builders.cpp:50
This represents an operation in an abstracted form, suitable for use with the builder APIs...
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn&#39;t have a listener...
Definition: Builders.h:251
A multi-dimensional affine map Affine map&#39;s are immutable like Type&#39;s, and they are uniqued...
Definition: AffineMap.h:38
Block * getBlock() const
Definition: Builders.h:270
DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)
Tensor-typed DenseIntElementsAttr getters.
Definition: Builders.cpp:127
InsertPoint saveInsertionPoint() const
Return a saved insertion point.
Definition: Builders.h:316
FloatType getBF16Type()
Definition: Builders.cpp:36
This class represents an argument of a Block.
Definition: Value.h:298
Builder(MLIRContext *context)
Definition: Builders.h:51
void setInsertionPoint(Operation *op)
Sets the insertion point to the specified operation, which will cause subsequent insertions to go rig...
Definition: Builders.h:337
static Optional< RegisteredOperationName > lookup(StringRef name, MLIRContext *ctx)
Lookup the registered operation information for the given operation.
ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)
Definition: Builders.cpp:209
AffineMap getSymbolIdentityMap()
Definition: Builders.cpp:317
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
std::enable_if< OpTy::template hasTrait< OpTrait::ZeroResult >), OpTy >::type createOrFold(Location location, Args &&...args)
Overload to create or fold a zero result operation.
Definition: Builders.h:474
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:84
void clearInsertionPoint()
Reset the insertion point to no location.
Definition: Builders.h:310
ArrayAttr getF32ArrayAttr(ArrayRef< float > values)
Definition: Builders.cpp:234
IntegerType getI64Type()
Definition: Builders.cpp:56
Operation * getTerminator()
Get the terminator operation of this block.
Definition: Block.cpp:230
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition: Builders.h:362
AffineExpr getAffineConstantExpr(int64_t constant)
Definition: Builders.cpp:293
RAII guard to reset the insertion point of the builder when destroyed.
Definition: Builders.h:279
AffineMap getShiftedAffineMap(AffineMap map, int64_t shift)
Returns an affine map that is a translation (shift) of all result expressions in &#39;map&#39; by &#39;shift&#39;...
Definition: Builders.cpp:328
MLIRContext * getContext() const
Return the context this location is uniqued in.
Definition: Location.h:58
static OpBuilder atBlockTerminator(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the block terminator.
Definition: Builders.h:221
This class is a general helper class for creating context-global objects like types, attributes, and affine expressions.
Definition: Builders.h:49
IndexType getIndexType()
Definition: Builders.cpp:48
AffineMap getDimIdentityMap()
Definition: Builders.cpp:304
This class represents a saved insertion point.
Definition: Builders.h:258
void setListener(Listener *newListener)
Sets the listener of this builder to the one provided.
Definition: Builders.h:247
OpBuilder(Block *block, Block::iterator insertPoint, Listener *listener=nullptr)
Definition: Builders.h:201
DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:134
FloatType getF64Type()
Definition: Builders.cpp:42
ArrayAttr getTypeArrayAttr(TypeRange values)
Definition: Builders.cpp:252
BoolAttr getBoolAttr(bool value)
Definition: Builders.cpp:87
FloatType getF80Type()
Definition: Builders.cpp:44
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition: Value.cpp:20
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:109
AffineExpr getAffineDimExpr(unsigned position)
Definition: Builders.cpp:285
U cast() const
Definition: Value.h:107
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition: Builders.h:367
AffineMap getConstantAffineMap(int64_t val)
Returns a single constant result affine map with 0 dimensions and 0 symbols.
Definition: Builders.cpp:299
MLIRContext * context
Definition: Builders.h:172
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
Definition: Builders.cpp:67
This is a "type erased" representation of a registered operation.
static void setInsertionPointAfter(OpBuilder &b, Value value)
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Definition: Builders.h:376
FloatAttr getF32FloatAttr(float value)
Definition: Builders.cpp:185
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
Definition: Builders.h:321
This class helps build Operations.
Definition: Builders.h:177
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
Definition: Builders.cpp:205
IntegerAttr getIndexAttr(int64_t value)
Definition: Builders.cpp:95
StringAttr getStringAttr(const Twine &bytes)
Definition: Builders.cpp:201
DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:121
Operation * cloneWithoutRegions(BlockAndValueMapping &mapper)
Create a partial copy of this operation without traversing into attached regions. ...
Definition: Operation.cpp:529
IntegerType getI32Type()
Definition: Builders.cpp:54
bool isSet() const
Returns true if this insert point is set.
Definition: Builders.h:268
An attribute that represents a reference to a dense integer vector or tensor object.
ArrayAttr getF64ArrayAttr(ArrayRef< double > values)
Definition: Builders.cpp:240
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition: Builders.cpp:246