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