MLIR  15.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, std::forward<Args>(args)...);
85  }
86 
87  /// Get or construct an instance of the attribute `Attr` with provided
88  /// arguments.
89  template <typename Attr, typename... Args>
90  Attr getAttr(Args &&...args) {
91  return Attr::get(context, std::forward<Args>(args)...);
92  }
93 
94  // Attributes.
95  NamedAttribute getNamedAttr(StringRef name, Attribute val);
96 
97  UnitAttr getUnitAttr();
99  DictionaryAttr getDictionaryAttr(ArrayRef<NamedAttribute> value);
100  IntegerAttr getIntegerAttr(Type type, int64_t value);
101  IntegerAttr getIntegerAttr(Type type, const APInt &value);
102  FloatAttr getFloatAttr(Type type, double value);
103  FloatAttr getFloatAttr(Type type, const APFloat &value);
104  StringAttr getStringAttr(const Twine &bytes);
105  ArrayAttr getArrayAttr(ArrayRef<Attribute> value);
106 
107  // Returns a 0-valued attribute of the given `type`. This function only
108  // supports boolean, integer, and 16-/32-/64-bit float types, and vector or
109  // ranked tensor of them. Returns null attribute otherwise.
110  Attribute getZeroAttr(Type type);
111 
112  // Convenience methods for fixed types.
113  FloatAttr getF16FloatAttr(float value);
114  FloatAttr getF32FloatAttr(float value);
115  FloatAttr getF64FloatAttr(double value);
116 
117  IntegerAttr getI8IntegerAttr(int8_t value);
118  IntegerAttr getI16IntegerAttr(int16_t value);
119  IntegerAttr getI32IntegerAttr(int32_t value);
120  IntegerAttr getI64IntegerAttr(int64_t value);
121  IntegerAttr getIndexAttr(int64_t value);
122 
123  /// Signed and unsigned integer attribute getters.
124  IntegerAttr getSI32IntegerAttr(int32_t value);
125  IntegerAttr getUI32IntegerAttr(uint32_t value);
126 
127  /// Vector-typed DenseIntElementsAttr getters. `values` must not be empty.
132 
133  /// Tensor-typed DenseIntElementsAttr getters. `values` can be empty.
134  /// These are generally preferable for representing general lists of integers
135  /// as attributes.
139 
140  ArrayAttr getAffineMapArrayAttr(ArrayRef<AffineMap> values);
141  ArrayAttr getBoolArrayAttr(ArrayRef<bool> values);
142  ArrayAttr getI32ArrayAttr(ArrayRef<int32_t> values);
143  ArrayAttr getI64ArrayAttr(ArrayRef<int64_t> values);
144  ArrayAttr getIndexArrayAttr(ArrayRef<int64_t> values);
145  ArrayAttr getF32ArrayAttr(ArrayRef<float> values);
146  ArrayAttr getF64ArrayAttr(ArrayRef<double> values);
147  ArrayAttr getStrArrayAttr(ArrayRef<StringRef> values);
148  ArrayAttr getTypeArrayAttr(TypeRange values);
149 
150  // Affine expressions and affine maps.
151  AffineExpr getAffineDimExpr(unsigned position);
152  AffineExpr getAffineSymbolExpr(unsigned position);
153  AffineExpr getAffineConstantExpr(int64_t constant);
154 
155  // Special cases of affine maps and integer sets
156  /// Returns a zero result affine map with no dimensions or symbols: () -> ().
158  /// Returns a single constant result affine map with 0 dimensions and 0
159  /// symbols. One constant result: () -> (val).
160  AffineMap getConstantAffineMap(int64_t val);
161  // One dimension id identity map: (i) -> (i).
163  // Multi-dimensional identity map: (d0, d1, d2) -> (d0, d1, d2).
164  AffineMap getMultiDimIdentityMap(unsigned rank);
165  // One symbol identity map: ()[s] -> (s).
167 
168  /// Returns a map that shifts its (single) input dimension by 'shift'.
169  /// (d0) -> (d0 + shift)
170  AffineMap getSingleDimShiftAffineMap(int64_t shift);
171 
172  /// Returns an affine map that is a translation (shift) of all result
173  /// expressions in 'map' by 'shift'.
174  /// Eg: input: (d0, d1)[s0] -> (d0, d1 + s0), shift = 2
175  /// returns: (d0, d1)[s0] -> (d0 + 2, d1 + s0 + 2)
176  AffineMap getShiftedAffineMap(AffineMap map, int64_t shift);
177 
178 protected:
180 };
181 
182 /// This class helps build Operations. Operations that are created are
183 /// automatically inserted at an insertion point. The builder is copyable.
184 class OpBuilder : public Builder {
185 public:
186  struct Listener;
187 
188  /// Create a builder with the given context.
189  explicit OpBuilder(MLIRContext *ctx, Listener *listener = nullptr)
190  : Builder(ctx), listener(listener) {}
191 
192  /// Create a builder and set the insertion point to the start of the region.
193  explicit OpBuilder(Region *region, Listener *listener = nullptr)
194  : OpBuilder(region->getContext(), listener) {
195  if (!region->empty())
196  setInsertionPoint(&region->front(), region->front().begin());
197  }
198  explicit OpBuilder(Region &region, Listener *listener = nullptr)
199  : OpBuilder(&region, listener) {}
200 
201  /// Create a builder and set insertion point to the given operation, which
202  /// will cause subsequent insertions to go right before it.
203  explicit OpBuilder(Operation *op, Listener *listener = nullptr)
204  : OpBuilder(op->getContext(), listener) {
205  setInsertionPoint(op);
206  }
207 
208  OpBuilder(Block *block, Block::iterator insertPoint,
209  Listener *listener = nullptr)
210  : OpBuilder(block->getParent()->getContext(), listener) {
211  setInsertionPoint(block, insertPoint);
212  }
213 
214  /// Create a builder and set the insertion point to before the first operation
215  /// in the block but still inside the block.
216  static OpBuilder atBlockBegin(Block *block, Listener *listener = nullptr) {
217  return OpBuilder(block, block->begin(), listener);
218  }
219 
220  /// Create a builder and set the insertion point to after the last operation
221  /// in the block but still inside the block.
222  static OpBuilder atBlockEnd(Block *block, Listener *listener = nullptr) {
223  return OpBuilder(block, block->end(), listener);
224  }
225 
226  /// Create a builder and set the insertion point to before the block
227  /// terminator.
229  Listener *listener = nullptr) {
230  auto *terminator = block->getTerminator();
231  assert(terminator != nullptr && "the block has no terminator");
232  return OpBuilder(block, Block::iterator(terminator), listener);
233  }
234 
235  //===--------------------------------------------------------------------===//
236  // Listeners
237  //===--------------------------------------------------------------------===//
238 
239  /// This class represents a listener that may be used to hook into various
240  /// actions within an OpBuilder.
241  struct Listener {
242  virtual ~Listener();
243 
244  /// Notification handler for when an operation is inserted into the builder.
245  /// `op` is the operation that was inserted.
246  virtual void notifyOperationInserted(Operation *op) {}
247 
248  /// Notification handler for when a block is created using the builder.
249  /// `block` is the block that was created.
250  virtual void notifyBlockCreated(Block *block) {}
251  };
252 
253  /// Sets the listener of this builder to the one provided.
254  void setListener(Listener *newListener) { listener = newListener; }
255 
256  /// Returns the current listener of this builder, or nullptr if this builder
257  /// doesn't have a listener.
258  Listener *getListener() const { return listener; }
259 
260  //===--------------------------------------------------------------------===//
261  // Insertion Point Management
262  //===--------------------------------------------------------------------===//
263 
264  /// This class represents a saved insertion point.
265  class InsertPoint {
266  public:
267  /// Creates a new insertion point which doesn't point to anything.
268  InsertPoint() = default;
269 
270  /// Creates a new insertion point at the given location.
271  InsertPoint(Block *insertBlock, Block::iterator insertPt)
272  : block(insertBlock), point(insertPt) {}
273 
274  /// Returns true if this insert point is set.
275  bool isSet() const { return (block != nullptr); }
276 
277  Block *getBlock() const { return block; }
278  Block::iterator getPoint() const { return point; }
279 
280  private:
281  Block *block = nullptr;
282  Block::iterator point;
283  };
284 
285  /// RAII guard to reset the insertion point of the builder when destroyed.
287  public:
289  : builder(&builder), ip(builder.saveInsertionPoint()) {}
290 
292  if (builder)
293  builder->restoreInsertionPoint(ip);
294  }
295 
296  InsertionGuard(const InsertionGuard &) = delete;
297  InsertionGuard &operator=(const InsertionGuard &) = delete;
298 
299  /// Implement the move constructor to clear the builder field of `other`.
300  /// That way it does not restore the insertion point upon destruction as
301  /// that should be done exclusively by the just constructed InsertionGuard.
302  InsertionGuard(InsertionGuard &&other) noexcept
303  : builder(other.builder), ip(other.ip) {
304  other.builder = nullptr;
305  }
306 
307  InsertionGuard &operator=(InsertionGuard &&other) = delete;
308 
309  private:
310  OpBuilder *builder;
312  };
313 
314  /// Reset the insertion point to no location. Creating an operation without a
315  /// set insertion point is an error, but this can still be useful when the
316  /// current insertion point a builder refers to is being removed.
318  this->block = nullptr;
319  insertPoint = Block::iterator();
320  }
321 
322  /// Return a saved insertion point.
324  return InsertPoint(getInsertionBlock(), getInsertionPoint());
325  }
326 
327  /// Restore the insert point to a previously saved point.
329  if (ip.isSet())
330  setInsertionPoint(ip.getBlock(), ip.getPoint());
331  else
332  clearInsertionPoint();
333  }
334 
335  /// Set the insertion point to the specified location.
336  void setInsertionPoint(Block *block, Block::iterator insertPoint) {
337  // TODO: check that insertPoint is in this rather than some other block.
338  this->block = block;
339  this->insertPoint = insertPoint;
340  }
341 
342  /// Sets the insertion point to the specified operation, which will cause
343  /// subsequent insertions to go right before it.
345  setInsertionPoint(op->getBlock(), Block::iterator(op));
346  }
347 
348  /// Sets the insertion point to the node after the specified operation, which
349  /// will cause subsequent insertions to go right after it.
351  setInsertionPoint(op->getBlock(), ++Block::iterator(op));
352  }
353 
354  /// Sets the insertion point to the node after the specified value. If value
355  /// has a defining operation, sets the insertion point to the node after such
356  /// defining operation. This will cause subsequent insertions to go right
357  /// after it. Otherwise, value is a BlockArgument. Sets the insertion point to
358  /// the start of its block.
360  if (Operation *op = val.getDefiningOp()) {
362  } else {
363  auto blockArg = val.cast<BlockArgument>();
364  setInsertionPointToStart(blockArg.getOwner());
365  }
366  }
367 
368  /// Sets the insertion point to the start of the specified block.
370  setInsertionPoint(block, block->begin());
371  }
372 
373  /// Sets the insertion point to the end of the specified block.
375  setInsertionPoint(block, block->end());
376  }
377 
378  /// Return the block the current insertion point belongs to. Note that the
379  /// the insertion point is not necessarily the end of the block.
380  Block *getInsertionBlock() const { return block; }
381 
382  /// Returns the current insertion point of the builder.
383  Block::iterator getInsertionPoint() const { return insertPoint; }
384 
385  /// Returns the current block of the builder.
386  Block *getBlock() const { return block; }
387 
388  //===--------------------------------------------------------------------===//
389  // Block Creation
390  //===--------------------------------------------------------------------===//
391 
392  /// Add new block with 'argTypes' arguments and set the insertion point to the
393  /// end of it. The block is inserted at the provided insertion point of
394  /// 'parent'. `locs` contains the locations of the inserted arguments, and
395  /// should match the size of `argTypes`.
396  Block *createBlock(Region *parent, Region::iterator insertPt = {},
397  TypeRange argTypes = llvm::None,
398  ArrayRef<Location> locs = llvm::None);
399 
400  /// Add new block with 'argTypes' arguments and set the insertion point to the
401  /// end of it. The block is placed before 'insertBefore'. `locs` contains the
402  /// locations of the inserted arguments, and should match the size of
403  /// `argTypes`.
404  Block *createBlock(Block *insertBefore, TypeRange argTypes = llvm::None,
405  ArrayRef<Location> locs = llvm::None);
406 
407  //===--------------------------------------------------------------------===//
408  // Operation Creation
409  //===--------------------------------------------------------------------===//
410 
411  /// Insert the given operation at the current insertion point and return it.
412  Operation *insert(Operation *op);
413 
414  /// Creates an operation given the fields represented as an OperationState.
415  Operation *create(const OperationState &state);
416 
417  /// Creates an operation with the given fields.
418  Operation *create(Location loc, StringAttr opName, ValueRange operands,
419  TypeRange types = {},
420  ArrayRef<NamedAttribute> attributes = {},
421  BlockRange successors = {},
423 
424 private:
425  /// Helper for sanity checking preconditions for create* methods below.
426  template <typename OpT>
427  RegisteredOperationName getCheckRegisteredInfo(MLIRContext *ctx) {
429  RegisteredOperationName::lookup(OpT::getOperationName(), ctx);
430  if (LLVM_UNLIKELY(!opName)) {
431  llvm::report_fatal_error(
432  "Building op `" + OpT::getOperationName() +
433  "` but it isn't registered in this MLIRContext: the dialect may not "
434  "be loaded or this operation isn't registered by the dialect. See "
435  "also https://mlir.llvm.org/getting_started/Faq/"
436  "#registered-loaded-dependent-whats-up-with-dialects-management");
437  }
438  return *opName;
439  }
440 
441 public:
442  /// Create an operation of specific op type at the current insertion point.
443  template <typename OpTy, typename... Args>
444  OpTy create(Location location, Args &&...args) {
445  OperationState state(location,
446  getCheckRegisteredInfo<OpTy>(location.getContext()));
447  OpTy::build(*this, state, std::forward<Args>(args)...);
448  auto *op = create(state);
449  auto result = dyn_cast<OpTy>(op);
450  assert(result && "builder didn't return the right type");
451  return result;
452  }
453 
454  /// Create an operation of specific op type at the current insertion point,
455  /// and immediately try to fold it. This functions populates 'results' with
456  /// the results after folding the operation.
457  template <typename OpTy, typename... Args>
459  Args &&...args) {
460  // Create the operation without using 'create' as we don't want to
461  // insert it yet.
462  OperationState state(location,
463  getCheckRegisteredInfo<OpTy>(location.getContext()));
464  OpTy::build(*this, state, std::forward<Args>(args)...);
465  Operation *op = Operation::create(state);
466 
467  // Fold the operation. If successful destroy it, otherwise insert it.
468  if (succeeded(tryFold(op, results)))
469  op->destroy();
470  else
471  insert(op);
472  }
473 
474  /// Overload to create or fold a single result operation.
475  template <typename OpTy, typename... Args>
476  typename std::enable_if<OpTy::template hasTrait<OpTrait::OneResult>(),
477  Value>::type
478  createOrFold(Location location, Args &&...args) {
479  SmallVector<Value, 1> results;
480  createOrFold<OpTy>(results, location, std::forward<Args>(args)...);
481  return results.front();
482  }
483 
484  /// Overload to create or fold a zero result operation.
485  template <typename OpTy, typename... Args>
486  typename std::enable_if<OpTy::template hasTrait<OpTrait::ZeroResults>(),
487  OpTy>::type
488  createOrFold(Location location, Args &&...args) {
489  auto op = create<OpTy>(location, std::forward<Args>(args)...);
490  SmallVector<Value, 0> unused;
491  (void)tryFold(op.getOperation(), unused);
492 
493  // Folding cannot remove a zero-result operation, so for convenience we
494  // continue to return it.
495  return op;
496  }
497 
498  /// Attempts to fold the given operation and places new results within
499  /// 'results'. Returns success if the operation was folded, failure otherwise.
500  /// Note: This function does not erase the operation on a successful fold.
501  LogicalResult tryFold(Operation *op, SmallVectorImpl<Value> &results);
502 
503  /// Creates a deep copy of the specified operation, remapping any operands
504  /// that use values outside of the operation using the map that is provided
505  /// ( leaving them alone if no entry is present). Replaces references to
506  /// cloned sub-operations to the corresponding operation that is copied,
507  /// and adds those mappings to the map.
508  Operation *clone(Operation &op, BlockAndValueMapping &mapper);
509  Operation *clone(Operation &op);
510 
511  /// Creates a deep copy of this operation but keep the operation regions
512  /// empty. Operands are remapped using `mapper` (if present), and `mapper` is
513  /// updated to contain the results.
515  return insert(op.cloneWithoutRegions(mapper));
516  }
518  return insert(op.cloneWithoutRegions());
519  }
520  template <typename OpT>
521  OpT cloneWithoutRegions(OpT op) {
522  return cast<OpT>(cloneWithoutRegions(*op.getOperation()));
523  }
524 
525 private:
526  /// The current block this builder is inserting into.
527  Block *block = nullptr;
528  /// The insertion point within the block that this builder is inserting
529  /// before.
530  Block::iterator insertPoint;
531  /// The optional listener for events of this builder.
532  Listener *listener;
533 };
534 
535 } // namespace mlir
536 
537 #endif
Location getUnknownLoc()
Definition: Builders.cpp:26
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
Definition: Builders.h:444
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:288
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:458
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:380
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
OpBuilder(Region &region, Listener *listener=nullptr)
Definition: Builders.h:198
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:278
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:336
Operation * cloneWithoutRegions(Operation &op)
Definition: Builders.h:517
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:203
Operation * cloneWithoutRegions(Operation &op, BlockAndValueMapping &mapper)
Creates a deep copy of this operation but keep the operation regions empty.
Definition: Builders.h:514
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:222
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:246
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition: Builders.cpp:81
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:220
An inlay hint that for a type annotation.
Block * getBlock()
Returns the operation block that contains this operation.
Definition: Operation.h:151
void destroy()
Destroys this operation and its subclass data.
Definition: Operation.cpp:171
Ty getType(Args &&...args)
Get or construct an instance of the type Ty with provided arguments.
Definition: Builders.h:83
This class represents a listener that may be used to hook into various actions within an OpBuilder...
Definition: Builders.h:241
virtual void notifyBlockCreated(Block *block)
Notification handler for when a block is created using the builder.
Definition: Builders.h:250
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:216
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:144
Block * getBlock() const
Returns the current block of the builder.
Definition: Builders.h:386
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition: Builders.h:350
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:302
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:193
InsertPoint(Block *insertBlock, Block::iterator insertPt)
Creates a new insertion point at the given location.
Definition: Builders.h:271
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:478
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:521
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:189
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:359
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:258
A multi-dimensional affine map Affine map&#39;s are immutable like Type&#39;s, and they are uniqued...
Definition: AffineMap.h:41
Block * getBlock() const
Definition: Builders.h:277
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:323
FloatType getBF16Type()
Definition: Builders.cpp:36
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition: Builders.h:90
This class represents an argument of a Block.
Definition: Value.h:300
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:344
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
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:85
void clearInsertionPoint()
Reset the insertion point to no location.
Definition: Builders.h:317
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:369
AffineExpr getAffineConstantExpr(int64_t constant)
Definition: Builders.cpp:293
RAII guard to reset the insertion point of the builder when destroyed.
Definition: Builders.h:286
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:228
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:265
void setListener(Listener *newListener)
Sets the listener of this builder to the one provided.
Definition: Builders.h:254
OpBuilder(Block *block, Block::iterator insertPoint, Listener *listener=nullptr)
Definition: Builders.h:208
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
This class provides an abstraction over the different types of ranges over Blocks.
Definition: BlockSupport.h:104
AffineExpr getAffineDimExpr(unsigned position)
Definition: Builders.cpp:285
U cast() const
Definition: Value.h:108
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition: Builders.h:374
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:179
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:383
FloatAttr getF32FloatAttr(float value)
Definition: Builders.cpp:185
std::enable_if< OpTy::template hasTrait< OpTrait::ZeroResults >), OpTy >::type createOrFold(Location location, Args &&...args)
Overload to create or fold a zero result operation.
Definition: Builders.h:488
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
Definition: Builders.h:328
This class helps build Operations.
Definition: Builders.h:184
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
Definition: Builders.cpp:205
This class provides an abstraction over the different types of ranges over Values.
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:550
IntegerType getI32Type()
Definition: Builders.cpp:54
bool isSet() const
Returns true if this insert point is set.
Definition: Builders.h:275
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