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