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