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