MLIR: include/mlir/IR/Builders.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9 #ifndef MLIR_IR_BUILDERS_H
10 #define MLIR_IR_BUILDERS_H
11
13 #include "llvm/Support/Compiler.h"
14 #include
15
16 namespace mlir {
17
18 class AffineExpr;
19 class IRMapping;
20 class UnknownLoc;
21 class FileLineColLoc;
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
49
51 public:
54
56
57
61
62
71
73
82 IntegerType getIntegerType(unsigned width, bool isSigned);
86
87
88 template <typename Ty, typename... Args>
91 }
92
93
94
95 template <typename Attr, typename... Args>
98 }
99
100
102
112
113
114
115
117
118
120
121
125
131
132
135
136
141
144
145
146
147
151
152
160
170
171
175
176
177
179
180
182
184
186
188
189
190
192
193
194
195
196
198
199 protected:
201 };
202
203
204
206 public:
209
210
213
214
217 if (!region->empty())
219 }
222
223
224
228 }
229
234 }
235
236
237
240 }
241
242
243
246 }
247
248
249
253 assert(terminator != nullptr && "the block has no terminator");
255 }
256
257
258
259
260
261
263
265
267
268
270 };
271
273
274 protected:
276
277 private:
278 const Kind kind;
279 };
280
281
282
285
287
288
289
290
291
292
293
294
295
297
298
299
300
301
302
303
304
305
308
309 protected:
311 };
312
313
315
316
317
319
320
321
322
323
324
326 public:
327
329
330
332 : block(insertBlock), point(insertPt) {}
333
334
335 bool isSet() const { return (block != nullptr); }
336
339
340 private:
341 Block *block = nullptr;
343 };
344
345
347 public:
350
352 if (builder)
354 }
355
358
359
360
361
363 : builder(other.builder), ip(other.ip) {
364 other.builder = nullptr;
365 }
366
368
369 private:
372 };
373
374
375
376
378 this->block = nullptr;
380 }
381
382
385 }
386
387
391 else
393 }
394
395
397
398 this->block = block;
399 this->insertPoint = insertPoint;
400 }
401
402
403
406 }
407
408
409
412 }
413
414
415
416
417
418
422 } else {
423 auto blockArg = llvm::cast(val);
425 }
426 }
427
428
431 }
432
433
436 }
437
438
439
441
442
444
445
447
448
449
450
451
452
453
454
455
457 TypeRange argTypes = std::nullopt,
458 ArrayRef locs = std::nullopt);
459
460
461
462
463
464 Block *createBlock(Block *insertBefore, TypeRange argTypes = std::nullopt,
465 ArrayRef locs = std::nullopt);
466
467
468
469
470
471
472 Operation *insert(Operation *op);
473
474
475 Operation *create(const OperationState &state);
476
477
478 Operation *create(Location loc, StringAttr opName, ValueRange operands,
479 TypeRange types = {},
480 ArrayRef attributes = {},
481 BlockRange successors = {},
482 MutableArrayRef<std::unique_ptr> regions = {});
483
484 private:
485
486 template
487 RegisteredOperationName getCheckRegisteredInfo(MLIRContext *ctx) {
488 std::optional opName =
490 if (LLVM_UNLIKELY(!opName)) {
491 llvm::report_fatal_error(
492 "Building op `" + OpT::getOperationName() +
493 "` but it isn't known in this MLIRContext: the dialect may not "
494 "be loaded or this operation hasn't been added by the dialect. See "
495 "also https://mlir.llvm.org/getting\_started/Faq/"
496 "#registered-loaded-dependent-whats-up-with-dialects-management");
497 }
498 return *opName;
499 }
500
501 public:
502
503 template <typename OpTy, typename... Args>
506 getCheckRegisteredInfo(location.getContext()));
507 OpTy::build(*this, state, std::forward(args)...);
508 auto *op = create(state);
509 auto result = dyn_cast(op);
510 assert(result && "builder didn't return the right type");
511 return result;
512 }
513
514
515
516
517 template <typename OpTy, typename... Args>
519 Args &&...args) {
520
521
523 getCheckRegisteredInfo(location.getContext()));
524 OpTy::build(*this, state, std::forward(args)...);
526 if (block)
528
529
530 if (succeeded(tryFold(op, results)) && !results.empty()) {
531
532
534 return;
535 }
536
538 results.assign(opResults.begin(), opResults.end());
541 }
542
543
544 template <typename OpTy, typename... Args>
545 std::enable_if_t<OpTy::template hasTraitOpTrait::OneResult(), Value>
548 createOrFold(results, location, std::forward(args)...);
549 return results.front();
550 }
551
552
553 template <typename OpTy, typename... Args>
554 std::enable_if_t<OpTy::template hasTraitOpTrait::ZeroResults(), OpTy>
556 auto op = create(location, std::forward(args)...);
558 (void)tryFold(op.getOperation(), unused);
559
560
561
562 return op;
563 }
564
565
566
567
568
569
570
571 LogicalResult
574
575
576
577
578
579
582
583
584
585
588 }
591 }
592 template
595 }
596
597
598
599
600
606
607 protected:
608
610
611 private:
612
613 Block *block = nullptr;
614
615
617 };
618
619 }
620
621 #endif
union mlir::linalg::@1203::ArityGroupAndKind::Kind kind
Base type for affine expression.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
OpListType::iterator iterator
Operation * getTerminator()
Get the terminator operation of this block.
OpListType & getOperations()
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,...
IntegerAttr getIndexAttr(int64_t value)
AffineMap getSingleDimShiftAffineMap(int64_t shift)
Returns a map that shifts its (single) input dimension by 'shift'.
ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)
DenseF64ArrayAttr getDenseF64ArrayAttr(ArrayRef< double > values)
DenseI8ArrayAttr getDenseI8ArrayAttr(ArrayRef< int8_t > values)
IntegerAttr getI32IntegerAttr(int32_t value)
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)
Vector-typed DenseIntElementsAttr getters. values must not be empty.
TupleType getTupleType(TypeRange elementTypes)
IntegerAttr getIntegerAttr(Type type, int64_t value)
FloatAttr getF64FloatAttr(double value)
AffineMap getShiftedAffineMap(AffineMap map, int64_t shift)
Returns an affine map that is a translation (shift) of all result expressions in 'map' by 'shift'.
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
FloatAttr getF16FloatAttr(float value)
AffineMap getDimIdentityMap()
AffineMap getMultiDimIdentityMap(unsigned rank)
IntegerAttr getI16IntegerAttr(int16_t value)
DenseI16ArrayAttr getDenseI16ArrayAttr(ArrayRef< int16_t > values)
AffineExpr getAffineSymbolExpr(unsigned position)
DenseFPElementsAttr getF32VectorAttr(ArrayRef< float > values)
FloatAttr getFloatAttr(Type type, double value)
AffineExpr getAffineConstantExpr(int64_t constant)
DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)
Tensor-typed DenseIntElementsAttr getters.
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
IntegerAttr getI64IntegerAttr(int64_t value)
IntegerType getIntegerType(unsigned width)
DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)
Ty getType(Args &&...args)
Get or construct an instance of the type Ty with provided arguments.
BoolAttr getBoolAttr(bool value)
StringAttr getStringAttr(const Twine &bytes)
Builder(MLIRContext *context)
AffineMap getEmptyAffineMap()
Returns a zero result affine map with no dimensions or symbols: () -> ().
IntegerAttr getSI32IntegerAttr(int32_t value)
Signed and unsigned integer attribute getters.
TypedAttr getZeroAttr(Type type)
Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())
AffineExpr getAffineDimExpr(unsigned position)
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
AffineMap getConstantAffineMap(int64_t val)
Returns a single constant result affine map with 0 dimensions and 0 symbols.
MLIRContext * getContext() const
ArrayAttr getTypeArrayAttr(TypeRange values)
FloatType getF8E8M0Type()
DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)
DenseF32ArrayAttr getDenseF32ArrayAttr(ArrayRef< float > values)
DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)
AffineMap getSymbolIdentityMap()
ArrayAttr getF64ArrayAttr(ArrayRef< double > values)
DenseFPElementsAttr getF64VectorAttr(ArrayRef< double > values)
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
DenseBoolArrayAttr getDenseBoolArrayAttr(ArrayRef< bool > values)
Tensor-typed DenseArrayAttr getters.
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
FloatAttr getF32FloatAttr(float value)
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
NamedAttribute getNamedAttr(StringRef name, Attribute val)
IntegerAttr getUI32IntegerAttr(uint32_t value)
IntegerAttr getI8IntegerAttr(int8_t value)
ArrayAttr getF32ArrayAttr(ArrayRef< float > values)
ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
TypedAttr getOneAttr(Type type)
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.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext * getContext() const
Return the context this location is uniqued in.
MLIRContext is the top-level object for a collection of MLIR operations.
NamedAttribute represents a combination of a name and an Attribute value.
This class represents a saved insertion point.
InsertPoint()=default
Creates a new insertion point which doesn't point to anything.
Block::iterator getPoint() const
bool isSet() const
Returns true if this insert point is set.
InsertPoint(Block *insertBlock, Block::iterator insertPt)
Creates a new insertion point at the given location.
RAII guard to reset the insertion point of the builder when destroyed.
InsertionGuard(OpBuilder &builder)
InsertionGuard(InsertionGuard &&other) noexcept
Implement the move constructor to clear the builder field of other.
InsertionGuard(const InsertionGuard &)=delete
InsertionGuard & operator=(InsertionGuard &&other)=delete
InsertionGuard & operator=(const InsertionGuard &)=delete
This class helps build Operations.
InsertPoint saveInsertionPoint() const
Return a saved insertion point.
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...
OpBuilder(Region *region, Listener *listener=nullptr)
Create a builder and set the insertion point to the start of the region.
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
Operation * cloneWithoutRegions(Operation &op)
OpT cloneWithoutRegions(OpT op)
OpBuilder(MLIRContext *ctx, Listener *listener=nullptr)
Create a builder with the given context.
void clearInsertionPoint()
Reset the insertion point to no location.
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...
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
void setListener(Listener *newListener)
Sets the listener of this builder to the one provided.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
void setInsertionPoint(Operation *op)
Sets the insertion point to the specified operation, which will cause subsequent insertions to go rig...
static OpBuilder atBlockTerminator(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the block terminator.
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn't have a listener.
OpBuilder(Region ®ion, Listener *listener=nullptr)
void cloneRegionBefore(Region ®ion, Region &parent, Region::iterator before, IRMapping &mapping)
Clone the blocks that belong to "region" before the given position in another region "parent".
OpBuilder(Block *block, Block::iterator insertPoint, Listener *listener=nullptr)
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
OpBuilder(Operation *op, Listener *listener=nullptr)
Create a builder and set insertion point to the given operation, which will cause subsequent insertio...
LogicalResult tryFold(Operation *op, SmallVectorImpl< Value > &results, SmallVectorImpl< Operation * > *materializedConstants=nullptr)
Attempts to fold the given operation and places new results within results.
Listener * listener
The optional listener for events of this builder.
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.
std::enable_if_t< OpTy::template hasTrait< OpTrait::ZeroResults >), OpTy > createOrFold(Location location, Args &&...args)
Overload to create or fold a zero result operation.
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...
std::enable_if_t< OpTy::template hasTrait< OpTrait::OneResult >), Value > createOrFold(Location location, Args &&...args)
Overload to create or fold a single result operation.
Block * getBlock() const
Returns the current block of the builder.
void setInsertionPointAfterValue(Value val)
Sets the insertion point to the node after the specified value.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Operation * insert(Operation *op)
Insert the given operation at the current insertion point and return it.
Operation * cloneWithoutRegions(Operation &op, IRMapping &mapper)
Creates a deep copy of this operation but keep the operation regions empty.
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Operation is the basic unit of execution within MLIR.
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.
Block * getBlock()
Returns the operation block that contains this operation.
Operation * cloneWithoutRegions(IRMapping &mapper)
Create a partial copy of this operation without traversing into attached regions.
result_range getResults()
void erase()
Remove this operation from its parent block and delete it.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
BlockListType::iterator iterator
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.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
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.
Kind
The kind of listener.
@ 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.
virtual void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)
Notify the listener that the specified block was inserted.
virtual ~Listener()=default
virtual void notifyOperationInserted(Operation *op, InsertPoint previous)
Notify the listener that the specified operation was inserted.
This represents an operation in an abstracted form, suitable for use with the builder APIs.