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