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