MLIR  21.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 #include <optional>
15 
16 namespace mlir {
17 
18 class AffineExpr;
19 class IRMapping;
20 class UnknownLoc;
21 class FileLineColLoc;
22 class FileLineColRange;
23 class Type;
24 class IntegerType;
25 class FloatType;
26 class FunctionType;
27 class IndexType;
28 class MemRefType;
29 class VectorType;
30 class RankedTensorType;
31 class UnrankedTensorType;
32 class TupleType;
33 class NoneType;
34 class BoolAttr;
35 class IntegerAttr;
36 class FloatAttr;
37 class StringAttr;
38 class TypeAttr;
39 class ArrayAttr;
40 class SymbolRefAttr;
41 class ElementsAttr;
42 class DenseElementsAttr;
43 class DenseIntElementsAttr;
44 class AffineMapAttr;
45 class AffineMap;
46 class UnitAttr;
47 
48 /// This class is a general helper class for creating context-global objects
49 /// like types, attributes, and affine expressions.
50 class Builder {
51 public:
53  explicit Builder(Operation *op) : Builder(op->getContext()) {}
54 
55  MLIRContext *getContext() const { return context; }
56 
57  // Locations.
60  Attribute metadata = Attribute());
61 
62  // Types.
63  FloatType getBF16Type();
64  FloatType getF16Type();
65  FloatType getTF32Type();
66  FloatType getF32Type();
67  FloatType getF64Type();
68  FloatType getF80Type();
69  FloatType getF128Type();
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);
104  DictionaryAttr getDictionaryAttr(ArrayRef<NamedAttribute> 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);
110  ArrayAttr getArrayAttr(ArrayRef<Attribute> value);
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  TypedAttr getZeroAttr(Type type);
116  // Returns a 1-valued attribute of the given `type`.
117  // Type constraints are the same as `getZeroAttr`.
118  TypedAttr getOneAttr(Type type);
119 
120  // Convenience methods for fixed types.
121  FloatAttr getF16FloatAttr(float value);
122  FloatAttr getF32FloatAttr(float value);
123  FloatAttr getF64FloatAttr(double value);
124 
125  IntegerAttr getI8IntegerAttr(int8_t value);
126  IntegerAttr getI16IntegerAttr(int16_t value);
127  IntegerAttr getI32IntegerAttr(int32_t value);
128  IntegerAttr getI64IntegerAttr(int64_t value);
129  IntegerAttr getIndexAttr(int64_t value);
130 
131  /// Signed and unsigned integer attribute getters.
132  IntegerAttr getSI32IntegerAttr(int32_t value);
133  IntegerAttr getUI32IntegerAttr(uint32_t value);
134 
135  /// Vector-typed DenseIntElementsAttr getters. `values` must not be empty.
140 
143 
144  /// Tensor-typed DenseIntElementsAttr getters. `values` can be empty.
145  /// These are generally preferable for representing general lists of integers
146  /// as attributes.
150 
151  /// Tensor-typed DenseArrayAttr getters.
159 
160  ArrayAttr getAffineMapArrayAttr(ArrayRef<AffineMap> values);
161  ArrayAttr getBoolArrayAttr(ArrayRef<bool> values);
162  ArrayAttr getI32ArrayAttr(ArrayRef<int32_t> values);
163  ArrayAttr getI64ArrayAttr(ArrayRef<int64_t> values);
164  ArrayAttr getIndexArrayAttr(ArrayRef<int64_t> values);
165  ArrayAttr getF32ArrayAttr(ArrayRef<float> values);
166  ArrayAttr getF64ArrayAttr(ArrayRef<double> values);
167  ArrayAttr getStrArrayAttr(ArrayRef<StringRef> values);
168  ArrayAttr getTypeArrayAttr(TypeRange values);
169 
170  // Affine expressions and affine maps.
171  AffineExpr getAffineDimExpr(unsigned position);
172  AffineExpr getAffineSymbolExpr(unsigned position);
173  AffineExpr getAffineConstantExpr(int64_t constant);
174 
175  // Special cases of affine maps and integer sets
176  /// Returns a zero result affine map with no dimensions or symbols: () -> ().
178  /// Returns a single constant result affine map with 0 dimensions and 0
179  /// symbols. One constant result: () -> (val).
180  AffineMap getConstantAffineMap(int64_t val);
181  // One dimension id identity map: (i) -> (i).
183  // Multi-dimensional identity map: (d0, d1, d2) -> (d0, d1, d2).
184  AffineMap getMultiDimIdentityMap(unsigned rank);
185  // One symbol identity map: ()[s] -> (s).
187 
188  /// Returns a map that shifts its (single) input dimension by 'shift'.
189  /// (d0) -> (d0 + shift)
190  AffineMap getSingleDimShiftAffineMap(int64_t shift);
191 
192  /// Returns an affine map that is a translation (shift) of all result
193  /// expressions in 'map' by 'shift'.
194  /// Eg: input: (d0, d1)[s0] -> (d0, d1 + s0), shift = 2
195  /// returns: (d0, d1)[s0] -> (d0 + 2, d1 + s0 + 2)
196  AffineMap getShiftedAffineMap(AffineMap map, int64_t shift);
197 
198 protected:
200 };
201 
202 /// This class helps build Operations. Operations that are created are
203 /// automatically inserted at an insertion point. The builder is copyable.
204 class OpBuilder : public Builder {
205 public:
206  class InsertPoint;
207  struct Listener;
208 
209  /// Create a builder with the given context.
210  explicit OpBuilder(MLIRContext *ctx, Listener *listener = nullptr)
211  : Builder(ctx), listener(listener) {}
212 
213  /// Create a builder and set the insertion point to the start of the region.
214  explicit OpBuilder(Region *region, Listener *listener = nullptr)
215  : OpBuilder(region->getContext(), listener) {
216  if (!region->empty())
217  setInsertionPointToStart(&region->front());
218  }
219  explicit OpBuilder(Region &region, Listener *listener = nullptr)
220  : OpBuilder(&region, listener) {}
221 
222  /// Create a builder and set insertion point to the given operation, which
223  /// will cause subsequent insertions to go right before it.
224  explicit OpBuilder(Operation *op, Listener *listener = nullptr)
225  : OpBuilder(op->getContext(), listener) {
226  setInsertionPoint(op);
227  }
228 
229  OpBuilder(Block *block, Block::iterator insertPoint,
230  Listener *listener = nullptr)
231  : OpBuilder(block->getParent()->getContext(), listener) {
232  setInsertionPoint(block, insertPoint);
233  }
234 
235  /// Create a builder and set the insertion point to before the first operation
236  /// in the block but still inside the block.
237  static OpBuilder atBlockBegin(Block *block, Listener *listener = nullptr) {
238  return OpBuilder(block, block->begin(), listener);
239  }
240 
241  /// Create a builder and set the insertion point to after the last operation
242  /// in the block but still inside the block.
243  static OpBuilder atBlockEnd(Block *block, Listener *listener = nullptr) {
244  return OpBuilder(block, block->end(), listener);
245  }
246 
247  /// Create a builder and set the insertion point to before the block
248  /// terminator.
250  Listener *listener = nullptr) {
251  auto *terminator = block->getTerminator();
252  assert(terminator != nullptr && "the block has no terminator");
253  return OpBuilder(block, Block::iterator(terminator), listener);
254  }
255 
256  //===--------------------------------------------------------------------===//
257  // Listeners
258  //===--------------------------------------------------------------------===//
259 
260  /// Base class for listeners.
261  struct ListenerBase {
262  /// The kind of listener.
263  enum class Kind {
264  /// OpBuilder::Listener or user-derived class.
265  OpBuilderListener = 0,
266 
267  /// RewriterBase::Listener or user-derived class.
269  };
270 
271  Kind getKind() const { return kind; }
272 
273  protected:
275 
276  private:
277  const Kind kind;
278  };
279 
280  /// This class represents a listener that may be used to hook into various
281  /// actions within an OpBuilder.
282  struct Listener : public ListenerBase {
283  Listener() : ListenerBase(ListenerBase::Kind::OpBuilderListener) {}
284 
285  virtual ~Listener() = default;
286 
287  /// Notify the listener that the specified operation was inserted.
288  ///
289  /// * If the operation was moved, then `previous` is the previous location
290  /// of the op.
291  /// * If the operation was unlinked before it was inserted, then `previous`
292  /// is empty.
293  ///
294  /// Note: Creating an (unlinked) op does not trigger this notification.
295  virtual void notifyOperationInserted(Operation *op, InsertPoint previous) {}
296 
297  /// Notify the listener that the specified block was inserted.
298  ///
299  /// * If the block was moved, then `previous` and `previousIt` are the
300  /// previous location of the block.
301  /// * If the block was unlinked before it was inserted, then `previous`
302  /// is "nullptr".
303  ///
304  /// Note: Creating an (unlinked) block does not trigger this notification.
305  virtual void notifyBlockInserted(Block *block, Region *previous,
306  Region::iterator previousIt) {}
307 
308  protected:
310  };
311 
312  /// Sets the listener of this builder to the one provided.
313  void setListener(Listener *newListener) { listener = newListener; }
314 
315  /// Returns the current listener of this builder, or nullptr if this builder
316  /// doesn't have a listener.
317  Listener *getListener() const { return listener; }
318 
319  //===--------------------------------------------------------------------===//
320  // Insertion Point Management
321  //===--------------------------------------------------------------------===//
322 
323  /// This class represents a saved insertion point.
324  class InsertPoint {
325  public:
326  /// Creates a new insertion point which doesn't point to anything.
327  InsertPoint() = default;
328 
329  /// Creates a new insertion point at the given location.
330  InsertPoint(Block *insertBlock, Block::iterator insertPt)
331  : block(insertBlock), point(insertPt) {}
332 
333  /// Returns true if this insert point is set.
334  bool isSet() const { return (block != nullptr); }
335 
336  Block *getBlock() const { return block; }
337  Block::iterator getPoint() const { return point; }
338 
339  private:
340  Block *block = nullptr;
341  Block::iterator point;
342  };
343 
344  /// RAII guard to reset the insertion point of the builder when destroyed.
346  public:
348  : builder(&builder), ip(builder.saveInsertionPoint()) {}
349 
351  if (builder)
352  builder->restoreInsertionPoint(ip);
353  }
354 
355  InsertionGuard(const InsertionGuard &) = delete;
357 
358  /// Implement the move constructor to clear the builder field of `other`.
359  /// That way it does not restore the insertion point upon destruction as
360  /// that should be done exclusively by the just constructed InsertionGuard.
361  InsertionGuard(InsertionGuard &&other) noexcept
362  : builder(other.builder), ip(other.ip) {
363  other.builder = nullptr;
364  }
365 
367 
368  private:
369  OpBuilder *builder;
371  };
372 
373  /// Reset the insertion point to no location. Creating an operation without a
374  /// set insertion point is an error, but this can still be useful when the
375  /// current insertion point a builder refers to is being removed.
377  this->block = nullptr;
378  insertPoint = Block::iterator();
379  }
380 
381  /// Return a saved insertion point.
384  }
385 
386  /// Restore the insert point to a previously saved point.
388  if (ip.isSet())
389  setInsertionPoint(ip.getBlock(), ip.getPoint());
390  else
392  }
393 
394  /// Set the insertion point to the specified location.
395  void setInsertionPoint(Block *block, Block::iterator insertPoint) {
396  // TODO: check that insertPoint is in this rather than some other block.
397  this->block = block;
398  this->insertPoint = insertPoint;
399  }
400 
401  /// Sets the insertion point to the specified operation, which will cause
402  /// subsequent insertions to go right before it.
405  }
406 
407  /// Sets the insertion point to the node after the specified operation, which
408  /// will cause subsequent insertions to go right after it.
411  }
412 
413  /// Sets the insertion point to the node after the specified value. If value
414  /// has a defining operation, sets the insertion point to the node after such
415  /// defining operation. This will cause subsequent insertions to go right
416  /// after it. Otherwise, value is a BlockArgument. Sets the insertion point to
417  /// the start of its block.
419  if (Operation *op = val.getDefiningOp()) {
421  } else {
422  auto blockArg = llvm::cast<BlockArgument>(val);
423  setInsertionPointToStart(blockArg.getOwner());
424  }
425  }
426 
427  /// Sets the insertion point to the start of the specified block.
429  setInsertionPoint(block, block->begin());
430  }
431 
432  /// Sets the insertion point to the end of the specified block.
434  setInsertionPoint(block, block->end());
435  }
436 
437  /// Return the block the current insertion point belongs to. Note that the
438  /// insertion point is not necessarily the end of the block.
439  Block *getInsertionBlock() const { return block; }
440 
441  /// Returns the current insertion point of the builder.
442  Block::iterator getInsertionPoint() const { return insertPoint; }
443 
444  /// Returns the current block of the builder.
445  Block *getBlock() const { return block; }
446 
447  //===--------------------------------------------------------------------===//
448  // Block Creation
449  //===--------------------------------------------------------------------===//
450 
451  /// Add new block with 'argTypes' arguments and set the insertion point to the
452  /// end of it. The block is inserted at the provided insertion point of
453  /// 'parent'. `locs` contains the locations of the inserted arguments, and
454  /// should match the size of `argTypes`.
455  Block *createBlock(Region *parent, Region::iterator insertPt = {},
456  TypeRange argTypes = std::nullopt,
457  ArrayRef<Location> locs = std::nullopt);
458 
459  /// Add new block with 'argTypes' arguments and set the insertion point to the
460  /// end of it. The block is placed before 'insertBefore'. `locs` contains the
461  /// locations of the inserted arguments, and should match the size of
462  /// `argTypes`.
463  Block *createBlock(Block *insertBefore, TypeRange argTypes = std::nullopt,
464  ArrayRef<Location> locs = std::nullopt);
465 
466  //===--------------------------------------------------------------------===//
467  // Operation Creation
468  //===--------------------------------------------------------------------===//
469 
470  /// Insert the given operation at the current insertion point and return it.
471  Operation *insert(Operation *op);
472 
473  /// Creates an operation given the fields represented as an OperationState.
474  Operation *create(const OperationState &state);
475 
476  /// Creates an operation with the given fields.
477  Operation *create(Location loc, StringAttr opName, ValueRange operands,
478  TypeRange types = {},
479  ArrayRef<NamedAttribute> attributes = {},
480  BlockRange successors = {},
481  MutableArrayRef<std::unique_ptr<Region>> regions = {});
482 
483 private:
484  /// Helper for sanity checking preconditions for create* methods below.
485  template <typename OpT>
486  RegisteredOperationName getCheckRegisteredInfo(MLIRContext *ctx) {
487  std::optional<RegisteredOperationName> opName =
488  RegisteredOperationName::lookup(TypeID::get<OpT>(), ctx);
489  if (LLVM_UNLIKELY(!opName)) {
490  llvm::report_fatal_error(
491  "Building op `" + OpT::getOperationName() +
492  "` but it isn't known in this MLIRContext: the dialect may not "
493  "be loaded or this operation hasn't been added by the dialect. See "
494  "also https://mlir.llvm.org/getting_started/Faq/"
495  "#registered-loaded-dependent-whats-up-with-dialects-management");
496  }
497  return *opName;
498  }
499 
500 public:
501  /// Create an operation of specific op type at the current insertion point.
502  template <typename OpTy, typename... Args>
503  OpTy create(Location location, Args &&...args) {
504  OperationState state(location,
505  getCheckRegisteredInfo<OpTy>(location.getContext()));
506  OpTy::build(*this, state, std::forward<Args>(args)...);
507  auto *op = create(state);
508  auto result = dyn_cast<OpTy>(op);
509  assert(result && "builder didn't return the right type");
510  return result;
511  }
512 
513  /// Create an operation of specific op type at the current insertion point,
514  /// and immediately try to fold it. This functions populates 'results' with
515  /// the results of the operation.
516  template <typename OpTy, typename... Args>
518  Args &&...args) {
519  // Create the operation without using 'create' as we want to control when
520  // the listener is notified.
521  OperationState state(location,
522  getCheckRegisteredInfo<OpTy>(location.getContext()));
523  OpTy::build(*this, state, std::forward<Args>(args)...);
524  Operation *op = Operation::create(state);
525  if (block)
526  block->getOperations().insert(insertPoint, op);
527 
528  // Attempt to fold the operation.
529  if (succeeded(tryFold(op, results)) && !results.empty()) {
530  // Erase the operation, if the fold removed the need for this operation.
531  // Note: The fold already populated the results in this case.
532  op->erase();
533  return;
534  }
535 
536  ResultRange opResults = op->getResults();
537  results.assign(opResults.begin(), opResults.end());
538  if (block && listener)
539  listener->notifyOperationInserted(op, /*previous=*/{});
540  }
541 
542  /// Overload to create or fold a single result operation.
543  template <typename OpTy, typename... Args>
544  std::enable_if_t<OpTy::template hasTrait<OpTrait::OneResult>(), Value>
545  createOrFold(Location location, Args &&...args) {
546  SmallVector<Value, 1> results;
547  createOrFold<OpTy>(results, location, std::forward<Args>(args)...);
548  return results.front();
549  }
550 
551  /// Overload to create or fold a zero result operation.
552  template <typename OpTy, typename... Args>
553  std::enable_if_t<OpTy::template hasTrait<OpTrait::ZeroResults>(), OpTy>
554  createOrFold(Location location, Args &&...args) {
555  auto op = create<OpTy>(location, std::forward<Args>(args)...);
556  SmallVector<Value, 0> unused;
557  (void)tryFold(op.getOperation(), unused);
558 
559  // Folding cannot remove a zero-result operation, so for convenience we
560  // continue to return it.
561  return op;
562  }
563 
564  /// Attempts to fold the given operation and places new results within
565  /// `results`. Returns success if the operation was folded, failure otherwise.
566  /// If the fold was in-place, `results` will not be filled. Optionally, newly
567  /// materialized constant operations can be returned to the caller.
568  ///
569  /// Note: This function does not erase the operation on a successful fold.
570  LogicalResult
572  SmallVectorImpl<Operation *> *materializedConstants = nullptr);
573 
574  /// Creates a deep copy of the specified operation, remapping any operands
575  /// that use values outside of the operation using the map that is provided
576  /// ( leaving them alone if no entry is present). Replaces references to
577  /// cloned sub-operations to the corresponding operation that is copied,
578  /// and adds those mappings to the map.
579  Operation *clone(Operation &op, IRMapping &mapper);
580  Operation *clone(Operation &op);
581 
582  /// Creates a deep copy of this operation but keep the operation regions
583  /// empty. Operands are remapped using `mapper` (if present), and `mapper` is
584  /// updated to contain the results.
586  return insert(op.cloneWithoutRegions(mapper));
587  }
589  return insert(op.cloneWithoutRegions());
590  }
591  template <typename OpT>
592  OpT cloneWithoutRegions(OpT op) {
593  return cast<OpT>(cloneWithoutRegions(*op.getOperation()));
594  }
595 
596  /// Clone the blocks that belong to "region" before the given position in
597  /// another region "parent". The two regions must be different. The caller is
598  /// responsible for creating or updating the operation transferring flow of
599  /// control to the region and passing it the correct block arguments.
600  void cloneRegionBefore(Region &region, Region &parent,
601  Region::iterator before, IRMapping &mapping);
602  void cloneRegionBefore(Region &region, Region &parent,
603  Region::iterator before);
604  void cloneRegionBefore(Region &region, Block *before);
605 
606 protected:
607  /// The optional listener for events of this builder.
609 
610 private:
611  /// The current block this builder is inserting into.
612  Block *block = nullptr;
613  /// The insertion point within the block that this builder is inserting
614  /// before.
615  Block::iterator insertPoint;
616 };
617 
618 } // namespace mlir
619 
620 #endif
union mlir::linalg::@1194::ArityGroupAndKind::Kind kind
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:46
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Block represents an ordered list of Operations.
Definition: Block.h:33
OpListType::iterator iterator
Definition: Block.h:140
Operation * getTerminator()
Get the terminator operation of this block.
Definition: Block.cpp:246
OpListType & getOperations()
Definition: Block.h:137
iterator end()
Definition: Block.h:144
iterator begin()
Definition: Block.h:143
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:50
IntegerAttr getIndexAttr(int64_t value)
Definition: Builders.cpp:104
AffineMap getSingleDimShiftAffineMap(int64_t shift)
Returns a map that shifts its (single) input dimension by 'shift'.
Definition: Builders.cpp:397
IntegerType getI16Type()
Definition: Builders.cpp:61
UnitAttr getUnitAttr()
Definition: Builders.cpp:94
ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:283
DenseF64ArrayAttr getDenseF64ArrayAttr(ArrayRef< double > values)
Definition: Builders.cpp:171
IntegerType getI2Type()
Definition: Builders.cpp:55
FloatType getF80Type()
Definition: Builders.cpp:47
FloatType getF128Type()
Definition: Builders.cpp:49
DenseI8ArrayAttr getDenseI8ArrayAttr(ArrayRef< int8_t > values)
Definition: Builders.cpp:151
IntegerAttr getI32IntegerAttr(int32_t value)
Definition: Builders.cpp:196
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:159
DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)
Vector-typed DenseIntElementsAttr getters. values must not be empty.
Definition: Builders.cpp:112
FloatType getF32Type()
Definition: Builders.cpp:43
FloatType getTF32Type()
Definition: Builders.cpp:41
TupleType getTupleType(TypeRange elementTypes)
Definition: Builders.cpp:80
IntegerAttr getIntegerAttr(Type type, int64_t value)
Definition: Builders.cpp:224
FloatAttr getF64FloatAttr(double value)
Definition: Builders.cpp:238
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:403
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:272
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:163
FloatAttr getF16FloatAttr(float value)
Definition: Builders.cpp:246
AffineMap getDimIdentityMap()
Definition: Builders.cpp:379
AffineMap getMultiDimIdentityMap(unsigned rank)
Definition: Builders.cpp:383
IntegerAttr getI16IntegerAttr(int16_t value)
Definition: Builders.cpp:213
DenseI16ArrayAttr getDenseI16ArrayAttr(ArrayRef< int16_t > values)
Definition: Builders.cpp:155
AffineExpr getAffineSymbolExpr(unsigned position)
Definition: Builders.cpp:364
DenseFPElementsAttr getF32VectorAttr(ArrayRef< float > values)
Definition: Builders.cpp:136
FloatAttr getFloatAttr(Type type, double value)
Definition: Builders.cpp:250
AffineExpr getAffineConstantExpr(int64_t constant)
Definition: Builders.cpp:368
DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)
Tensor-typed DenseIntElementsAttr getters.
Definition: Builders.cpp:175
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
Definition: Builders.cpp:76
IntegerType getI64Type()
Definition: Builders.cpp:65
IntegerType getI32Type()
Definition: Builders.cpp:63
IntegerAttr getI64IntegerAttr(int64_t value)
Definition: Builders.cpp:108
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:67
NoneType getNoneType()
Definition: Builders.cpp:84
DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:182
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:96
IntegerType getI4Type()
Definition: Builders.cpp:57
StringAttr getStringAttr(const Twine &bytes)
Definition: Builders.cpp:258
MLIRContext * context
Definition: Builders.h:199
Builder(MLIRContext *context)
Definition: Builders.h:52
AffineMap getEmptyAffineMap()
Returns a zero result affine map with no dimensions or symbols: () -> ().
Definition: Builders.cpp:372
Builder(Operation *op)
Definition: Builders.h:53
IntegerAttr getSI32IntegerAttr(int32_t value)
Signed and unsigned integer attribute getters.
Definition: Builders.cpp:203
TypedAttr getZeroAttr(Type type)
Definition: Builders.cpp:320
FloatType getF16Type()
Definition: Builders.cpp:39
Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())
Definition: Builders.cpp:29
FloatType getBF16Type()
Definition: Builders.cpp:37
AffineExpr getAffineDimExpr(unsigned position)
Definition: Builders.cpp:360
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:189
AffineMap getConstantAffineMap(int64_t val)
Returns a single constant result affine map with 0 dimensions and 0 symbols.
Definition: Builders.cpp:374
MLIRContext * getContext() const
Definition: Builders.h:55
ArrayAttr getTypeArrayAttr(TypeRange values)
Definition: Builders.cpp:308
DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:118
DenseF32ArrayAttr getDenseF32ArrayAttr(ArrayRef< float > values)
Definition: Builders.cpp:167
DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:124
AffineMap getSymbolIdentityMap()
Definition: Builders.cpp:392
ArrayAttr getF64ArrayAttr(ArrayRef< double > values)
Definition: Builders.cpp:296
IntegerType getI1Type()
Definition: Builders.cpp:53
DenseFPElementsAttr getF64VectorAttr(ArrayRef< double > values)
Definition: Builders.cpp:141
Location getUnknownLoc()
Definition: Builders.cpp:27
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
Definition: Builders.cpp:262
DenseBoolArrayAttr getDenseBoolArrayAttr(ArrayRef< bool > values)
Tensor-typed DenseArrayAttr getters.
Definition: Builders.cpp:147
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:277
IndexType getIndexType()
Definition: Builders.cpp:51
IntegerType getI8Type()
Definition: Builders.cpp:59
FloatAttr getF32FloatAttr(float value)
Definition: Builders.cpp:242
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition: Builders.cpp:100
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition: Builders.cpp:90
IntegerAttr getUI32IntegerAttr(uint32_t value)
Definition: Builders.cpp:208
IntegerAttr getI8IntegerAttr(int8_t value)
Definition: Builders.cpp:217
ArrayAttr getF32ArrayAttr(ArrayRef< float > values)
Definition: Builders.cpp:290
FloatType getF64Type()
Definition: Builders.cpp:45
ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)
Definition: Builders.cpp:266
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition: Builders.cpp:302
DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:130
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
Definition: Builders.cpp:314
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition: Builders.h:95
TypedAttr getOneAttr(Type type)
Definition: Builders.cpp:338
An attribute that represents a reference to a dense float vector or tensor object.
An attribute that represents a reference to a dense integer vector or tensor object.
This is a utility class for mapping one set of IR entities to another.
Definition: IRMapping.h:26
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
MLIRContext * getContext() const
Return the context this location is uniqued in.
Definition: Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:164
This class represents a saved insertion point.
Definition: Builders.h:324
InsertPoint()=default
Creates a new insertion point which doesn't point to anything.
Block::iterator getPoint() const
Definition: Builders.h:337
bool isSet() const
Returns true if this insert point is set.
Definition: Builders.h:334
InsertPoint(Block *insertBlock, Block::iterator insertPt)
Creates a new insertion point at the given location.
Definition: Builders.h:330
Block * getBlock() const
Definition: Builders.h:336
RAII guard to reset the insertion point of the builder when destroyed.
Definition: Builders.h:345
InsertionGuard(OpBuilder &builder)
Definition: Builders.h:347
InsertionGuard(InsertionGuard &&other) noexcept
Implement the move constructor to clear the builder field of other.
Definition: Builders.h:361
InsertionGuard(const InsertionGuard &)=delete
InsertionGuard & operator=(InsertionGuard &&other)=delete
InsertionGuard & operator=(const InsertionGuard &)=delete
This class helps build Operations.
Definition: Builders.h:204
InsertPoint saveInsertionPoint() const
Return a saved insertion point.
Definition: Builders.h:382
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:237
OpBuilder(Region *region, Listener *listener=nullptr)
Create a builder and set the insertion point to the start of the region.
Definition: Builders.h:214
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Definition: Builders.h:442
Operation * cloneWithoutRegions(Operation &op)
Definition: Builders.h:588
OpT cloneWithoutRegions(OpT op)
Definition: Builders.h:592
OpBuilder(MLIRContext *ctx, Listener *listener=nullptr)
Create a builder with the given context.
Definition: Builders.h:210
void clearInsertionPoint()
Reset the insertion point to no location.
Definition: Builders.h:376
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:243
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
Definition: Builders.cpp:549
void setListener(Listener *newListener)
Sets the listener of this builder to the one provided.
Definition: Builders.h:313
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition: Builders.h:428
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition: Builders.h:395
void setInsertionPoint(Operation *op)
Sets the insertion point to the specified operation, which will cause subsequent insertions to go rig...
Definition: Builders.h:403
static OpBuilder atBlockTerminator(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the block terminator.
Definition: Builders.h:249
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition: Builders.h:433
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
Definition: Builders.h:387
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn't have a listener.
Definition: Builders.h:317
OpBuilder(Region &region, Listener *listener=nullptr)
Definition: Builders.h:219
void cloneRegionBefore(Region &region, Region &parent, Region::iterator before, IRMapping &mapping)
Clone the blocks that belong to "region" before the given position in another region "parent".
Definition: Builders.cpp:576
OpBuilder(Block *block, Block::iterator insertPoint, Listener *listener=nullptr)
Definition: Builders.h:229
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
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:224
LogicalResult tryFold(Operation *op, SmallVectorImpl< Value > &results, SmallVectorImpl< Operation * > *materializedConstants=nullptr)
Attempts to fold the given operation and places new results within results.
Definition: Builders.cpp:469
Listener * listener
The optional listener for events of this builder.
Definition: Builders.h:608
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:426
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:554
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:517
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:545
Block * getBlock() const
Returns the current block of the builder.
Definition: Builders.h:445
void setInsertionPointAfterValue(Value val)
Sets the insertion point to the node after the specified value.
Definition: Builders.h:418
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition: Builders.h:409
Operation * insert(Operation *op)
Insert the given operation at the current insertion point and return it.
Definition: Builders.cpp:417
Operation * cloneWithoutRegions(Operation &op, IRMapping &mapper)
Creates a deep copy of this operation but keep the operation regions empty.
Definition: Builders.h:585
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Definition: Builders.h:439
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, OpaqueProperties properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Definition: Operation.cpp:67
Block * getBlock()
Returns the operation block that contains this operation.
Definition: Operation.h:213
Operation * cloneWithoutRegions(IRMapping &mapper)
Create a partial copy of this operation without traversing into attached regions.
Definition: Operation.cpp:705
result_range getResults()
Definition: Operation.h:415
void erase()
Remove this operation from its parent block and delete it.
Definition: Operation.cpp:539
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 std::optional< RegisteredOperationName > lookup(StringRef name, MLIRContext *ctx)
Lookup the registered operation information for the given operation.
This class implements the result iterators for the Operation class.
Definition: ValueRange.h:247
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:37
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:96
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.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
Base class for listeners.
Definition: Builders.h:261
Kind
The kind of listener.
Definition: Builders.h:263
@ RewriterBaseListener
RewriterBase::Listener or user-derived class.
@ OpBuilderListener
OpBuilder::Listener or user-derived class.
This class represents a listener that may be used to hook into various actions within an OpBuilder.
Definition: Builders.h:282
virtual void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)
Notify the listener that the specified block was inserted.
Definition: Builders.h:305
virtual ~Listener()=default
virtual void notifyOperationInserted(Operation *op, InsertPoint previous)
Notify the listener that the specified operation was inserted.
Definition: Builders.h:295
This represents an operation in an abstracted form, suitable for use with the builder APIs.