MLIR 24.0.0git
Builders.cpp
Go to the documentation of this file.
1//===- Builders.cpp - Helpers for constructing MLIR Classes ---------------===//
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#include "mlir/IR/Builders.h"
10#include "mlir/IR/AffineExpr.h"
11#include "mlir/IR/AffineMap.h"
13#include "mlir/IR/Dialect.h"
14#include "mlir/IR/IRMapping.h"
15#include "mlir/IR/Matchers.h"
16#include "llvm/ADT/SmallVectorExtras.h"
17#include "llvm/Support/DebugLog.h"
18
19using namespace mlir;
20
21//===----------------------------------------------------------------------===//
22// Locations.
23//===----------------------------------------------------------------------===//
24
25Location Builder::getUnknownLoc() { return UnknownLoc::get(context); }
26
28 return FusedLoc::get(locs, metadata, context);
29}
30
31//===----------------------------------------------------------------------===//
32// Types.
33//===----------------------------------------------------------------------===//
34
35FloatType Builder::getF8E8M0Type() { return Float8E8M0FNUType::get(context); }
36
38 return Float8E5M3FNUType::get(context);
39}
40
41FloatType Builder::getF8E4M3FNType() { return Float8E4M3FNType::get(context); }
42
43FloatType Builder::getF8E5M2Type() { return Float8E5M2Type::get(context); }
44
45FloatType Builder::getBF16Type() { return BFloat16Type::get(context); }
46
47FloatType Builder::getF16Type() { return Float16Type::get(context); }
48
49FloatType Builder::getTF32Type() { return FloatTF32Type::get(context); }
50
51FloatType Builder::getF32Type() { return Float32Type::get(context); }
52
53FloatType Builder::getF64Type() { return Float64Type::get(context); }
54
55FloatType Builder::getF80Type() { return Float80Type::get(context); }
56
57FloatType Builder::getF128Type() { return Float128Type::get(context); }
58
59IndexType Builder::getIndexType() { return IndexType::get(context); }
60
61IntegerType Builder::getI1Type() { return IntegerType::get(context, 1); }
62
63IntegerType Builder::getI2Type() { return IntegerType::get(context, 2); }
64
65IntegerType Builder::getI4Type() { return IntegerType::get(context, 4); }
66
67IntegerType Builder::getI8Type() { return IntegerType::get(context, 8); }
68
69IntegerType Builder::getI16Type() { return IntegerType::get(context, 16); }
70
71IntegerType Builder::getI32Type() { return IntegerType::get(context, 32); }
72
73IntegerType Builder::getI64Type() { return IntegerType::get(context, 64); }
74
75IntegerType Builder::getIntegerType(unsigned width) {
76 return IntegerType::get(context, width);
77}
78
79IntegerType Builder::getIntegerType(unsigned width, bool isSigned) {
80 return IntegerType::get(
81 context, width, isSigned ? IntegerType::Signed : IntegerType::Unsigned);
82}
83
84FunctionType Builder::getFunctionType(TypeRange inputs, TypeRange results) {
85 return FunctionType::get(context, inputs, results);
86}
87
88GraphType Builder::getGraphType(TypeRange inputs, TypeRange results) {
89 return GraphType::get(context, inputs, results);
90}
91
92TupleType Builder::getTupleType(TypeRange elementTypes) {
93 return TupleType::get(context, elementTypes);
94}
95
96NoneType Builder::getNoneType() { return NoneType::get(context); }
97
98//===----------------------------------------------------------------------===//
99// Attributes.
100//===----------------------------------------------------------------------===//
101
103 return NamedAttribute(name, val);
104}
105
106UnitAttr Builder::getUnitAttr() { return UnitAttr::get(context); }
107
109 return BoolAttr::get(context, value);
110}
111
113 return DictionaryAttr::get(context, value);
114}
115
116IntegerAttr Builder::getIndexAttr(int64_t value) {
117 return IntegerAttr::get(getIndexType(), APInt(64, value));
118}
119
121 return IntegerAttr::get(getIntegerType(64), APInt(64, value));
122}
123
126 VectorType::get(static_cast<int64_t>(values.size()), getI1Type()),
127 values);
128}
129
132 VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(32)),
133 values);
134}
135
138 VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(64)),
139 values);
140}
141
144 VectorType::get(static_cast<int64_t>(values.size()), getIndexType()),
145 values);
146}
147
150 VectorType::get(static_cast<float>(values.size()), getF32Type()), values);
151}
152
155 VectorType::get(static_cast<double>(values.size()), getF64Type()),
156 values);
157}
158
162
166
170
174
178
182
186
189 RankedTensorType::get(static_cast<int64_t>(values.size()),
190 getIntegerType(32)),
191 values);
192}
193
196 RankedTensorType::get(static_cast<int64_t>(values.size()),
197 getIntegerType(64)),
198 values);
199}
200
203 RankedTensorType::get(static_cast<int64_t>(values.size()),
204 getIndexType()),
205 values);
206}
207
208IntegerAttr Builder::getI32IntegerAttr(int32_t value) {
209 // The APInt always uses isSigned=true here because we accept the value
210 // as int32_t.
211 return IntegerAttr::get(getIntegerType(32),
212 APInt(32, value, /*isSigned=*/true));
213}
214
215IntegerAttr Builder::getSI32IntegerAttr(int32_t value) {
216 return IntegerAttr::get(getIntegerType(32, /*isSigned=*/true),
217 APInt(32, value, /*isSigned=*/true));
218}
219
220IntegerAttr Builder::getUI32IntegerAttr(uint32_t value) {
221 return IntegerAttr::get(getIntegerType(32, /*isSigned=*/false),
222 APInt(32, (uint64_t)value, /*isSigned=*/false));
223}
224
225IntegerAttr Builder::getI16IntegerAttr(int16_t value) {
226 return IntegerAttr::get(getIntegerType(16),
227 APInt(16, value, /*isSigned=*/true));
228}
229
230IntegerAttr Builder::getI8IntegerAttr(int8_t value) {
231 // The APInt always uses isSigned=true here because we accept the value
232 // as int8_t.
233 return IntegerAttr::get(getIntegerType(8),
234 APInt(8, value, /*isSigned=*/true));
235}
236
237IntegerAttr Builder::getIntegerAttr(Type type, int64_t value) {
238 if (type.isIndex())
239 return IntegerAttr::get(type, APInt(64, value));
240 // TODO: Avoid implicit trunc?
241 // See https://github.com/llvm/llvm-project/issues/112510.
242 return IntegerAttr::get(type, APInt(type.getIntOrFloatBitWidth(), value,
243 type.isSignedInteger(),
244 /*implicitTrunc=*/true));
245}
246
247IntegerAttr Builder::getIntegerAttr(Type type, const APInt &value) {
248 return IntegerAttr::get(type, value);
249}
250
251FloatAttr Builder::getF64FloatAttr(double value) {
252 return FloatAttr::get(getF64Type(), APFloat(value));
253}
254
255FloatAttr Builder::getF32FloatAttr(float value) {
256 return FloatAttr::get(getF32Type(), APFloat(value));
257}
258
259FloatAttr Builder::getF16FloatAttr(float value) {
260 return FloatAttr::get(getF16Type(), value);
261}
262
263FloatAttr Builder::getFloatAttr(Type type, double value) {
264 return FloatAttr::get(type, value);
265}
266
267FloatAttr Builder::getFloatAttr(Type type, const APFloat &value) {
268 return FloatAttr::get(type, value);
269}
270
271StringAttr Builder::getStringAttr(const Twine &bytes) {
272 return StringAttr::get(context, bytes);
273}
274
276 return ArrayAttr::get(context, value);
277}
278
280 auto attrs = llvm::map_to_vector<8>(
281 values, [this](bool v) -> Attribute { return getBoolAttr(v); });
282 return getArrayAttr(attrs);
283}
284
286 auto attrs = llvm::map_to_vector<8>(
287 values, [this](int32_t v) -> Attribute { return getI32IntegerAttr(v); });
288 return getArrayAttr(attrs);
289}
291 auto attrs = llvm::map_to_vector<8>(
292 values, [this](int64_t v) -> Attribute { return getI64IntegerAttr(v); });
293 return getArrayAttr(attrs);
294}
295
297 auto attrs = llvm::map_to_vector<8>(values, [this](int64_t v) -> Attribute {
298 return getIntegerAttr(IndexType::get(getContext()), v);
299 });
300 return getArrayAttr(attrs);
301}
302
304 auto attrs = llvm::map_to_vector<8>(
305 values, [this](float v) -> Attribute { return getF32FloatAttr(v); });
306 return getArrayAttr(attrs);
307}
308
310 auto attrs = llvm::map_to_vector<8>(
311 values, [this](double v) -> Attribute { return getF64FloatAttr(v); });
312 return getArrayAttr(attrs);
313}
314
316 auto attrs = llvm::map_to_vector<8>(
317 values, [this](StringRef v) -> Attribute { return getStringAttr(v); });
318 return getArrayAttr(attrs);
319}
320
322 auto attrs = llvm::map_to_vector<8>(
323 values, [](Type v) -> Attribute { return TypeAttr::get(v); });
324 return getArrayAttr(attrs);
325}
326
328 auto attrs = llvm::map_to_vector<8>(
329 values, [](AffineMap v) -> Attribute { return AffineMapAttr::get(v); });
330 return getArrayAttr(attrs);
331}
332
333TypedAttr Builder::getZeroAttr(Type type) {
334 if (llvm::isa<FloatType>(type))
335 return getFloatAttr(type, 0.0);
336 if (llvm::isa<IndexType>(type))
337 return getIndexAttr(0);
338 if (llvm::dyn_cast<IntegerType>(type))
339 return getIntegerAttr(type,
340 APInt(llvm::cast<IntegerType>(type).getWidth(), 0));
341 if (llvm::isa<RankedTensorType, VectorType>(type)) {
342 auto vtType = llvm::cast<ShapedType>(type);
343 auto element = getZeroAttr(vtType.getElementType());
344 if (!element)
345 return {};
346 return DenseElementsAttr::get(vtType, element);
347 }
348 return {};
349}
350
351TypedAttr Builder::getOneAttr(Type type) {
352 if (llvm::isa<FloatType>(type))
353 return getFloatAttr(type, 1.0);
354 if (llvm::isa<IndexType>(type))
355 return getIndexAttr(1);
356 if (llvm::dyn_cast<IntegerType>(type))
357 return getIntegerAttr(type,
358 APInt(llvm::cast<IntegerType>(type).getWidth(), 1));
359 if (llvm::isa<RankedTensorType, VectorType>(type)) {
360 auto vtType = llvm::cast<ShapedType>(type);
361 auto element = getOneAttr(vtType.getElementType());
362 if (!element)
363 return {};
364 return DenseElementsAttr::get(vtType, element);
365 }
366 return {};
367}
368
369//===----------------------------------------------------------------------===//
370// Affine Expressions, Affine Maps, and Integer Sets.
371//===----------------------------------------------------------------------===//
372
374 return mlir::getAffineDimExpr(position, context);
375}
376
378 return mlir::getAffineSymbolExpr(position, context);
379}
380
384
386
388 return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0,
390}
391
393 return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, getAffineDimExpr(0));
394}
395
398 dimExprs.reserve(rank);
399 for (unsigned i = 0; i < rank; ++i)
400 dimExprs.push_back(getAffineDimExpr(i));
401 return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs,
402 context);
403}
404
406 return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1,
408}
409
411 // expr = d0 + shift.
412 auto expr = getAffineDimExpr(0) + shift;
413 return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, expr);
414}
415
417 SmallVector<AffineExpr, 4> shiftedResults;
418 shiftedResults.reserve(map.getNumResults());
419 for (auto resultExpr : map.getResults())
420 shiftedResults.push_back(resultExpr + shift);
421 return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults,
422 context);
423}
424
425//===----------------------------------------------------------------------===//
426// OpBuilder
427//===----------------------------------------------------------------------===//
428
429/// Insert the given operation at the current insertion point and return it.
431 if (block) {
432 block->getOperations().insert(insertPoint, op);
433 if (listener)
434 listener->notifyOperationInserted(op, /*previous=*/{});
435 }
436 return op;
437}
438
440 TypeRange argTypes, ArrayRef<Location> locs) {
441 assert(parent && "expected valid parent region");
442 assert(argTypes.size() == locs.size() && "argument location mismatch");
443 if (insertPt == Region::iterator())
444 insertPt = parent->end();
445
446 Block *b = new Block();
447 b->addArguments(argTypes, locs);
448 parent->getBlocks().insert(insertPt, b);
450
451 if (listener)
452 listener->notifyBlockInserted(b, /*previous=*/nullptr, /*previousIt=*/{});
453 return b;
454}
455
456/// Add new block with 'argTypes' arguments and set the insertion point to the
457/// end of it. The block is placed before 'insertBefore'.
459 ArrayRef<Location> locs) {
460 assert(insertBefore && "expected valid insertion block");
461 return createBlock(insertBefore->getParent(), Region::iterator(insertBefore),
462 argTypes, locs);
463}
464
465/// Create an operation given the fields represented as an OperationState.
467 return insert(Operation::create(state));
468}
469
470/// Creates an operation with the given fields.
471Operation *OpBuilder::create(Location loc, StringAttr opName,
472 ValueRange operands, TypeRange types,
473 ArrayRef<NamedAttribute> attributes,
474 BlockRange successors,
475 MutableArrayRef<std::unique_ptr<Region>> regions) {
476 OperationState state(loc, opName, operands, types, attributes, successors,
477 regions);
478 return create(state);
480
481LogicalResult
483 SmallVectorImpl<Operation *> *materializedConstants) {
484 assert(results.empty() && "expected empty results");
485 ResultRange opResults = op->getResults();
486
487 results.reserve(opResults.size());
488 auto cleanupFailure = [&] {
489 results.clear();
490 return failure();
491 };
492
493 // If this operation is already a constant, there is nothing to do.
494 if (matchPattern(op, m_Constant()))
495 return cleanupFailure();
496
497 // Try to fold the operation.
499 LDBG() << "Trying to fold: "
500 << OpWithFlags(op, OpPrintingFlags().skipRegions());
501 if (failed(op->fold(foldResults)))
502 return cleanupFailure();
503
504 int count = 0;
505 do {
506 LDBG() << "Folded in place #" << count
507 << " times: " << OpWithFlags(op, OpPrintingFlags().skipRegions());
508 count++;
509 } while (foldResults.empty() && succeeded(op->fold(foldResults)));
510
511 // An in-place fold does not require generation of any constants.
512 if (foldResults.empty())
513 return success();
514
515 // A temporary builder used for creating constants during folding.
516 OpBuilder cstBuilder(context);
517 SmallVector<Operation *, 1> generatedConstants;
518
519 // Populate the results with the folded results.
520 Dialect *dialect = op->getDialect();
521 for (auto [foldResult, expectedType] :
522 llvm::zip_equal(foldResults, opResults.getTypes())) {
523
524 // Normal values get pushed back directly.
525 if (auto value = llvm::dyn_cast_if_present<Value>(foldResult)) {
526 results.push_back(value);
527 continue;
528 }
529
530 // Otherwise, try to materialize a constant operation.
531 if (!dialect)
532 return cleanupFailure();
533
534 // Ask the dialect to materialize a constant operation for this value.
535 Attribute attr = cast<Attribute>(foldResult);
536 auto *constOp = dialect->materializeConstant(cstBuilder, attr, expectedType,
537 op->getLoc());
538 if (!constOp) {
539 // Erase any generated constants.
540 for (Operation *cst : generatedConstants)
541 cst->erase();
542 return cleanupFailure();
543 }
544 assert(matchPattern(constOp, m_Constant()));
545
546 generatedConstants.push_back(constOp);
547 results.push_back(constOp->getResult(0));
548 }
549
550 // If we were successful, insert any generated constants.
551 for (Operation *cst : generatedConstants)
552 insert(cst);
553
554 // Return materialized constant operations.
555 if (materializedConstants)
556 *materializedConstants = std::move(generatedConstants);
557
558 return success();
559}
560
561/// Helper function that sends block insertion notifications for every block
562/// that is directly nested in the given op.
564 OpBuilder::Listener *listener) {
565 for (Region &r : op->getRegions())
566 for (Block &b : r.getBlocks())
567 listener->notifyBlockInserted(&b, /*previous=*/nullptr,
568 /*previousIt=*/{});
569}
570
572 Operation *newOp = op.clone(mapper);
573 newOp = insert(newOp);
574
575 // The `insert` call above handles the notification for inserting `newOp`
576 // itself. But if `newOp` has any regions, we need to notify the listener
577 // about any ops that got inserted inside those regions as part of cloning.
578 if (listener) {
579 // The `insert` call above notifies about op insertion, but not about block
580 // insertion.
582 auto walkFn = [&](Operation *walkedOp) {
583 listener->notifyOperationInserted(walkedOp, /*previous=*/{});
585 };
586 for (Region &region : newOp->getRegions())
587 region.walk<WalkOrder::PreOrder>(walkFn);
588 }
589
590 return newOp;
591}
592
594 IRMapping mapper;
595 return clone(op, mapper);
596}
597
599 Region::iterator before, IRMapping &mapping) {
600 region.cloneInto(&parent, before, mapping);
601
602 // Fast path: If no listener is attached, there is no more work to do.
603 if (!listener)
604 return;
605
606 // Notify about op/block insertion.
607 for (auto it = mapping.lookup(&region.front())->getIterator(); it != before;
608 ++it) {
609 listener->notifyBlockInserted(&*it, /*previous=*/nullptr,
610 /*previousIt=*/{});
611 it->walk<WalkOrder::PreOrder>([&](Operation *walkedOp) {
612 listener->notifyOperationInserted(walkedOp, /*previous=*/{});
614 });
615 }
616}
617
619 Region::iterator before) {
620 IRMapping mapping;
621 cloneRegionBefore(region, parent, before, mapping);
622}
623
625 cloneRegionBefore(region, *before->getParent(), before->getIterator());
626}
return success()
static void notifyBlockInsertions(Operation *op, OpBuilder::Listener *listener)
Helper function that sends block insertion notifications for every block that is directly nested in t...
Definition Builders.cpp:563
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
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
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
unsigned getNumSymbols() const
unsigned getNumDims() const
ArrayRef< AffineExpr > getResults() const
unsigned getNumResults() const
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class provides an abstraction over the different types of ranges over Blocks.
Block represents an ordered list of Operations.
Definition Block.h:33
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition Block.cpp:27
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
static BoolAttr get(MLIRContext *context, bool value)
IntegerAttr getIndexAttr(int64_t value)
Definition Builders.cpp:116
AffineMap getSingleDimShiftAffineMap(int64_t shift)
Returns a map that shifts its (single) input dimension by 'shift'.
Definition Builders.cpp:410
IntegerType getI16Type()
Definition Builders.cpp:69
UnitAttr getUnitAttr()
Definition Builders.cpp:106
ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:296
DenseF64ArrayAttr getDenseF64ArrayAttr(ArrayRef< double > values)
Definition Builders.cpp:183
IntegerType getI2Type()
Definition Builders.cpp:63
FloatType getF80Type()
Definition Builders.cpp:55
FloatType getF128Type()
Definition Builders.cpp:57
DenseI8ArrayAttr getDenseI8ArrayAttr(ArrayRef< int8_t > values)
Definition Builders.cpp:163
IntegerAttr getI32IntegerAttr(int32_t value)
Definition Builders.cpp:208
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition Builders.cpp:171
DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)
Vector-typed DenseIntElementsAttr getters. values must not be empty.
Definition Builders.cpp:124
FloatType getF32Type()
Definition Builders.cpp:51
FloatType getTF32Type()
Definition Builders.cpp:49
TupleType getTupleType(TypeRange elementTypes)
Definition Builders.cpp:92
IntegerAttr getIntegerAttr(Type type, int64_t value)
Definition Builders.cpp:237
FloatAttr getF64FloatAttr(double value)
Definition Builders.cpp:251
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:416
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
Definition Builders.cpp:285
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:175
FloatAttr getF16FloatAttr(float value)
Definition Builders.cpp:259
FloatType getF8E5M2Type()
Definition Builders.cpp:43
AffineMap getDimIdentityMap()
Definition Builders.cpp:392
AffineMap getMultiDimIdentityMap(unsigned rank)
Definition Builders.cpp:396
IntegerAttr getI16IntegerAttr(int16_t value)
Definition Builders.cpp:225
DenseI16ArrayAttr getDenseI16ArrayAttr(ArrayRef< int16_t > values)
Definition Builders.cpp:167
AffineExpr getAffineSymbolExpr(unsigned position)
Definition Builders.cpp:377
DenseFPElementsAttr getF32VectorAttr(ArrayRef< float > values)
Definition Builders.cpp:148
FloatAttr getFloatAttr(Type type, double value)
Definition Builders.cpp:263
AffineExpr getAffineConstantExpr(int64_t constant)
Definition Builders.cpp:381
DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)
Tensor-typed DenseIntElementsAttr getters.
Definition Builders.cpp:187
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
Definition Builders.cpp:84
IntegerType getI64Type()
Definition Builders.cpp:73
IntegerType getI32Type()
Definition Builders.cpp:71
IntegerAttr getI64IntegerAttr(int64_t value)
Definition Builders.cpp:120
IntegerType getIntegerType(unsigned width)
Definition Builders.cpp:75
NoneType getNoneType()
Definition Builders.cpp:96
DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:194
BoolAttr getBoolAttr(bool value)
Definition Builders.cpp:108
IntegerType getI4Type()
Definition Builders.cpp:65
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:271
MLIRContext * context
Definition Builders.h:205
AffineMap getEmptyAffineMap()
Returns a zero result affine map with no dimensions or symbols: () -> ().
Definition Builders.cpp:385
IntegerAttr getSI32IntegerAttr(int32_t value)
Signed and unsigned integer attribute getters.
Definition Builders.cpp:215
GraphType getGraphType(TypeRange inputs, TypeRange results)
Definition Builders.cpp:88
TypedAttr getZeroAttr(Type type)
Definition Builders.cpp:333
FloatType getF16Type()
Definition Builders.cpp:47
Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())
Definition Builders.cpp:27
FloatType getBF16Type()
Definition Builders.cpp:45
AffineExpr getAffineDimExpr(unsigned position)
Definition Builders.cpp:373
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:201
AffineMap getConstantAffineMap(int64_t val)
Returns a single constant result affine map with 0 dimensions and 0 symbols.
Definition Builders.cpp:387
ArrayAttr getTypeArrayAttr(TypeRange values)
Definition Builders.cpp:321
FloatType getF8E8M0Type()
Definition Builders.cpp:35
DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)
Definition Builders.cpp:130
DenseF32ArrayAttr getDenseF32ArrayAttr(ArrayRef< float > values)
Definition Builders.cpp:179
FloatType getF8E5M3FNUType()
Definition Builders.cpp:37
DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:136
AffineMap getSymbolIdentityMap()
Definition Builders.cpp:405
ArrayAttr getF64ArrayAttr(ArrayRef< double > values)
Definition Builders.cpp:309
IntegerType getI1Type()
Definition Builders.cpp:61
DenseFPElementsAttr getF64VectorAttr(ArrayRef< double > values)
Definition Builders.cpp:153
Location getUnknownLoc()
Definition Builders.cpp:25
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
Definition Builders.cpp:275
MLIRContext * getContext() const
Definition Builders.h:56
DenseBoolArrayAttr getDenseBoolArrayAttr(ArrayRef< bool > values)
Tensor-typed DenseArrayAttr getters.
Definition Builders.cpp:159
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:290
IndexType getIndexType()
Definition Builders.cpp:59
IntegerType getI8Type()
Definition Builders.cpp:67
FloatAttr getF32FloatAttr(float value)
Definition Builders.cpp:255
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition Builders.cpp:112
FloatType getF8E4M3FNType()
Definition Builders.cpp:41
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition Builders.cpp:102
IntegerAttr getUI32IntegerAttr(uint32_t value)
Definition Builders.cpp:220
IntegerAttr getI8IntegerAttr(int8_t value)
Definition Builders.cpp:230
ArrayAttr getF32ArrayAttr(ArrayRef< float > values)
Definition Builders.cpp:303
FloatType getF64Type()
Definition Builders.cpp:53
ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)
Definition Builders.cpp:279
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition Builders.cpp:315
DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)
Definition Builders.cpp:142
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
Definition Builders.cpp:327
TypedAttr getOneAttr(Type type)
Definition Builders.cpp:351
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
An attribute that represents a reference to a dense float vector or tensor object.
static DenseFPElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseFPElementsAttr with the given arguments.
An attribute that represents a reference to a dense integer vector or tensor object.
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
virtual Operation * materializeConstant(OpBuilder &builder, Attribute value, Type type, Location loc)
Registered hook to materialize a single constant operation from a given attribute value with the desi...
Definition Dialect.h:83
This is a utility class for mapping one set of IR entities to another.
Definition IRMapping.h:26
auto lookup(T from) const
Lookup a mapped value within the map.
Definition IRMapping.h:72
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Definition Builders.cpp:439
OpBuilder(MLIRContext *ctx, Listener *listener=nullptr)
Create a builder with the given context.
Definition Builders.h:216
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:571
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition Builders.h:439
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:598
LogicalResult tryFold(Operation *op, SmallVectorImpl< Value > &results, SmallVectorImpl< Operation * > *materializedConstants=nullptr)
Attempts to fold the given operation and places new results within results.
Definition Builders.cpp:482
Listener * listener
The optional listener for events of this builder.
Definition Builders.h:620
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition Builders.cpp:466
Operation * insert(Operation *op)
Insert the given operation at the current insertion point and return it.
Definition Builders.cpp:430
Set of flags used to control the behavior of the various IR print methods (e.g.
A wrapper class that allows for printing an operation with a set of flags, useful to act as a "stream...
Definition Operation.h:1142
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
Definition Operation.h:237
LogicalResult fold(ArrayRef< Attribute > operands, SmallVectorImpl< OpFoldResult > &results)
Attempt to fold this operation with the specified constant operand values.
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition Operation.h:702
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, PropertyRef properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Definition Operation.cpp:65
result_range getResults()
Definition Operation.h:440
Operation * clone(IRMapping &mapper, const CloneOptions &options=CloneOptions::all())
Create a deep copy of this operation, remapping any operands that use values outside of the operation...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Block & front()
Definition Region.h:65
void cloneInto(Region *dest, IRMapping &mapper)
Clone the internal blocks from this region into dest.
Definition Region.cpp:70
iterator end()
Definition Region.h:56
BlockListType & getBlocks()
Definition Region.h:45
BlockListType::iterator iterator
Definition Region.h:52
This class implements the result iterators for the Operation class.
Definition ValueRange.h:248
type_range getTypes() const
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:40
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isSignedInteger() const
Return true if this is a signed integer type (with the specified width).
Definition Types.cpp:78
bool isIndex() const
Definition Types.cpp:56
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:124
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:389
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< bool > content)
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition Matchers.h:490
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
detail::DenseArrayAttrImpl< int8_t > DenseI8ArrayAttr
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
detail::DenseArrayAttrImpl< double > DenseF64ArrayAttr
detail::DenseArrayAttrImpl< bool > DenseBoolArrayAttr
detail::DenseArrayAttrImpl< float > DenseF32ArrayAttr
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition Matchers.h:369
AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)
These free functions allow clients of the API to not use classes in detail.
detail::DenseArrayAttrImpl< int16_t > DenseI16ArrayAttr
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
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
This represents an operation in an abstracted form, suitable for use with the builder APIs.