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