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