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/IntegerSet.h"
16 #include "mlir/IR/Matchers.h"
17 #include "mlir/IR/SymbolTable.h"
18 #include "llvm/ADT/SmallVectorExtras.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace mlir;
22 
23 //===----------------------------------------------------------------------===//
24 // Locations.
25 //===----------------------------------------------------------------------===//
26 
28 
30  return FusedLoc::get(locs, metadata, context);
31 }
32 
33 //===----------------------------------------------------------------------===//
34 // Types.
35 //===----------------------------------------------------------------------===//
36 
38 
40 
42 
44 
46 
48 
50 
52 
53 IntegerType Builder::getI1Type() { return IntegerType::get(context, 1); }
54 
55 IntegerType Builder::getI2Type() { return IntegerType::get(context, 2); }
56 
57 IntegerType Builder::getI4Type() { return IntegerType::get(context, 4); }
58 
59 IntegerType Builder::getI8Type() { return IntegerType::get(context, 8); }
60 
61 IntegerType Builder::getI16Type() { return IntegerType::get(context, 16); }
62 
63 IntegerType Builder::getI32Type() { return IntegerType::get(context, 32); }
64 
65 IntegerType Builder::getI64Type() { return IntegerType::get(context, 64); }
66 
67 IntegerType Builder::getIntegerType(unsigned width) {
68  return IntegerType::get(context, width);
69 }
70 
71 IntegerType Builder::getIntegerType(unsigned width, bool isSigned) {
72  return IntegerType::get(
73  context, width, isSigned ? IntegerType::Signed : IntegerType::Unsigned);
74 }
75 
76 FunctionType Builder::getFunctionType(TypeRange inputs, TypeRange results) {
77  return FunctionType::get(context, inputs, results);
78 }
79 
80 TupleType Builder::getTupleType(TypeRange elementTypes) {
81  return TupleType::get(context, elementTypes);
82 }
83 
85 
86 //===----------------------------------------------------------------------===//
87 // Attributes.
88 //===----------------------------------------------------------------------===//
89 
91  return NamedAttribute(name, val);
92 }
93 
95 
97  return BoolAttr::get(context, value);
98 }
99 
101  return DictionaryAttr::get(context, value);
102 }
103 
104 IntegerAttr Builder::getIndexAttr(int64_t value) {
105  return IntegerAttr::get(getIndexType(), APInt(64, value));
106 }
107 
108 IntegerAttr Builder::getI64IntegerAttr(int64_t value) {
109  return IntegerAttr::get(getIntegerType(64), APInt(64, value));
110 }
111 
114  VectorType::get(static_cast<int64_t>(values.size()), getI1Type()),
115  values);
116 }
117 
120  VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(32)),
121  values);
122 }
123 
126  VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(64)),
127  values);
128 }
129 
132  VectorType::get(static_cast<int64_t>(values.size()), getIndexType()),
133  values);
134 }
135 
138  VectorType::get(static_cast<float>(values.size()), getF32Type()), values);
139 }
140 
143  VectorType::get(static_cast<double>(values.size()), getF64Type()),
144  values);
145 }
146 
148  return DenseBoolArrayAttr::get(context, values);
149 }
150 
152  return DenseI8ArrayAttr::get(context, values);
153 }
154 
156  return DenseI16ArrayAttr::get(context, values);
157 }
158 
160  return DenseI32ArrayAttr::get(context, values);
161 }
162 
164  return DenseI64ArrayAttr::get(context, values);
165 }
166 
168  return DenseF32ArrayAttr::get(context, values);
169 }
170 
172  return DenseF64ArrayAttr::get(context, values);
173 }
174 
177  RankedTensorType::get(static_cast<int64_t>(values.size()),
178  getIntegerType(32)),
179  values);
180 }
181 
184  RankedTensorType::get(static_cast<int64_t>(values.size()),
185  getIntegerType(64)),
186  values);
187 }
188 
191  RankedTensorType::get(static_cast<int64_t>(values.size()),
192  getIndexType()),
193  values);
194 }
195 
196 IntegerAttr Builder::getI32IntegerAttr(int32_t value) {
197  // The APInt always uses isSigned=true here because we accept the value
198  // as int32_t.
199  return IntegerAttr::get(getIntegerType(32),
200  APInt(32, value, /*isSigned=*/true));
201 }
202 
203 IntegerAttr Builder::getSI32IntegerAttr(int32_t value) {
204  return IntegerAttr::get(getIntegerType(32, /*isSigned=*/true),
205  APInt(32, value, /*isSigned=*/true));
206 }
207 
208 IntegerAttr Builder::getUI32IntegerAttr(uint32_t value) {
209  return IntegerAttr::get(getIntegerType(32, /*isSigned=*/false),
210  APInt(32, (uint64_t)value, /*isSigned=*/false));
211 }
212 
213 IntegerAttr Builder::getI16IntegerAttr(int16_t value) {
214  return IntegerAttr::get(getIntegerType(16), APInt(16, value));
215 }
216 
217 IntegerAttr Builder::getI8IntegerAttr(int8_t value) {
218  // The APInt always uses isSigned=true here because we accept the value
219  // as int8_t.
221  APInt(8, value, /*isSigned=*/true));
222 }
223 
224 IntegerAttr Builder::getIntegerAttr(Type type, int64_t value) {
225  if (type.isIndex())
226  return IntegerAttr::get(type, APInt(64, value));
227  // TODO: Avoid implicit trunc?
228  // See https://github.com/llvm/llvm-project/issues/112510.
229  return IntegerAttr::get(type, APInt(type.getIntOrFloatBitWidth(), value,
230  type.isSignedInteger(),
231  /*implicitTrunc=*/true));
232 }
233 
234 IntegerAttr Builder::getIntegerAttr(Type type, const APInt &value) {
235  return IntegerAttr::get(type, value);
236 }
237 
238 FloatAttr Builder::getF64FloatAttr(double value) {
239  return FloatAttr::get(getF64Type(), APFloat(value));
240 }
241 
242 FloatAttr Builder::getF32FloatAttr(float value) {
243  return FloatAttr::get(getF32Type(), APFloat(value));
244 }
245 
246 FloatAttr Builder::getF16FloatAttr(float value) {
247  return FloatAttr::get(getF16Type(), value);
248 }
249 
250 FloatAttr Builder::getFloatAttr(Type type, double value) {
251  return FloatAttr::get(type, value);
252 }
253 
254 FloatAttr Builder::getFloatAttr(Type type, const APFloat &value) {
255  return FloatAttr::get(type, value);
256 }
257 
258 StringAttr Builder::getStringAttr(const Twine &bytes) {
259  return StringAttr::get(context, bytes);
260 }
261 
263  return ArrayAttr::get(context, value);
264 }
265 
267  auto attrs = llvm::map_to_vector<8>(
268  values, [this](bool v) -> Attribute { return getBoolAttr(v); });
269  return getArrayAttr(attrs);
270 }
271 
273  auto attrs = llvm::map_to_vector<8>(
274  values, [this](int32_t v) -> Attribute { return getI32IntegerAttr(v); });
275  return getArrayAttr(attrs);
276 }
278  auto attrs = llvm::map_to_vector<8>(
279  values, [this](int64_t v) -> Attribute { return getI64IntegerAttr(v); });
280  return getArrayAttr(attrs);
281 }
282 
284  auto attrs = llvm::map_to_vector<8>(values, [this](int64_t v) -> Attribute {
286  });
287  return getArrayAttr(attrs);
288 }
289 
291  auto attrs = llvm::map_to_vector<8>(
292  values, [this](float v) -> Attribute { return getF32FloatAttr(v); });
293  return getArrayAttr(attrs);
294 }
295 
297  auto attrs = llvm::map_to_vector<8>(
298  values, [this](double v) -> Attribute { return getF64FloatAttr(v); });
299  return getArrayAttr(attrs);
300 }
301 
303  auto attrs = llvm::map_to_vector<8>(
304  values, [this](StringRef v) -> Attribute { return getStringAttr(v); });
305  return getArrayAttr(attrs);
306 }
307 
309  auto attrs = llvm::map_to_vector<8>(
310  values, [](Type v) -> Attribute { return TypeAttr::get(v); });
311  return getArrayAttr(attrs);
312 }
313 
315  auto attrs = llvm::map_to_vector<8>(
316  values, [](AffineMap v) -> Attribute { return AffineMapAttr::get(v); });
317  return getArrayAttr(attrs);
318 }
319 
320 TypedAttr Builder::getZeroAttr(Type type) {
321  if (llvm::isa<FloatType>(type))
322  return getFloatAttr(type, 0.0);
323  if (llvm::isa<IndexType>(type))
324  return getIndexAttr(0);
325  if (llvm::dyn_cast<IntegerType>(type))
326  return getIntegerAttr(type,
327  APInt(llvm::cast<IntegerType>(type).getWidth(), 0));
328  if (llvm::isa<RankedTensorType, VectorType>(type)) {
329  auto vtType = llvm::cast<ShapedType>(type);
330  auto element = getZeroAttr(vtType.getElementType());
331  if (!element)
332  return {};
333  return DenseElementsAttr::get(vtType, element);
334  }
335  return {};
336 }
337 
338 TypedAttr Builder::getOneAttr(Type type) {
339  if (llvm::isa<FloatType>(type))
340  return getFloatAttr(type, 1.0);
341  if (llvm::isa<IndexType>(type))
342  return getIndexAttr(1);
343  if (llvm::dyn_cast<IntegerType>(type))
344  return getIntegerAttr(type,
345  APInt(llvm::cast<IntegerType>(type).getWidth(), 1));
346  if (llvm::isa<RankedTensorType, VectorType>(type)) {
347  auto vtType = llvm::cast<ShapedType>(type);
348  auto element = getOneAttr(vtType.getElementType());
349  if (!element)
350  return {};
351  return DenseElementsAttr::get(vtType, element);
352  }
353  return {};
354 }
355 
356 //===----------------------------------------------------------------------===//
357 // Affine Expressions, Affine Maps, and Integer Sets.
358 //===----------------------------------------------------------------------===//
359 
361  return mlir::getAffineDimExpr(position, context);
362 }
363 
365  return mlir::getAffineSymbolExpr(position, context);
366 }
367 
369  return mlir::getAffineConstantExpr(constant, context);
370 }
371 
373 
375  return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0,
376  getAffineConstantExpr(val));
377 }
378 
380  return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, getAffineDimExpr(0));
381 }
382 
385  dimExprs.reserve(rank);
386  for (unsigned i = 0; i < rank; ++i)
387  dimExprs.push_back(getAffineDimExpr(i));
388  return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs,
389  context);
390 }
391 
393  return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1,
395 }
396 
398  // expr = d0 + shift.
399  auto expr = getAffineDimExpr(0) + shift;
400  return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, expr);
401 }
402 
404  SmallVector<AffineExpr, 4> shiftedResults;
405  shiftedResults.reserve(map.getNumResults());
406  for (auto resultExpr : map.getResults())
407  shiftedResults.push_back(resultExpr + shift);
408  return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults,
409  context);
410 }
411 
412 //===----------------------------------------------------------------------===//
413 // OpBuilder
414 //===----------------------------------------------------------------------===//
415 
416 /// Insert the given operation at the current insertion point and return it.
418  if (block) {
419  block->getOperations().insert(insertPoint, op);
420  if (listener)
421  listener->notifyOperationInserted(op, /*previous=*/{});
422  }
423  return op;
424 }
425 
427  TypeRange argTypes, ArrayRef<Location> locs) {
428  assert(parent && "expected valid parent region");
429  assert(argTypes.size() == locs.size() && "argument location mismatch");
430  if (insertPt == Region::iterator())
431  insertPt = parent->end();
432 
433  Block *b = new Block();
434  b->addArguments(argTypes, locs);
435  parent->getBlocks().insert(insertPt, b);
437 
438  if (listener)
439  listener->notifyBlockInserted(b, /*previous=*/nullptr, /*previousIt=*/{});
440  return b;
441 }
442 
443 /// Add new block with 'argTypes' arguments and set the insertion point to the
444 /// end of it. The block is placed before 'insertBefore'.
445 Block *OpBuilder::createBlock(Block *insertBefore, TypeRange argTypes,
446  ArrayRef<Location> locs) {
447  assert(insertBefore && "expected valid insertion block");
448  return createBlock(insertBefore->getParent(), Region::iterator(insertBefore),
449  argTypes, locs);
450 }
451 
452 /// Create an operation given the fields represented as an OperationState.
454  return insert(Operation::create(state));
455 }
456 
457 /// Creates an operation with the given fields.
458 Operation *OpBuilder::create(Location loc, StringAttr opName,
459  ValueRange operands, TypeRange types,
460  ArrayRef<NamedAttribute> attributes,
461  BlockRange successors,
462  MutableArrayRef<std::unique_ptr<Region>> regions) {
463  OperationState state(loc, opName, operands, types, attributes, successors,
464  regions);
465  return create(state);
466 }
467 
468 LogicalResult OpBuilder::tryFold(Operation *op,
469  SmallVectorImpl<Value> &results) {
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 success();
532 }
533 
534 /// Helper function that sends block insertion notifications for every block
535 /// that is directly nested in the given op.
537  OpBuilder::Listener *listener) {
538  for (Region &r : op->getRegions())
539  for (Block &b : r.getBlocks())
540  listener->notifyBlockInserted(&b, /*previous=*/nullptr,
541  /*previousIt=*/{});
542 }
543 
545  Operation *newOp = op.clone(mapper);
546  newOp = insert(newOp);
547 
548  // The `insert` call above handles the notification for inserting `newOp`
549  // itself. But if `newOp` has any regions, we need to notify the listener
550  // about any ops that got inserted inside those regions as part of cloning.
551  if (listener) {
552  // The `insert` call above notifies about op insertion, but not about block
553  // insertion.
555  auto walkFn = [&](Operation *walkedOp) {
556  listener->notifyOperationInserted(walkedOp, /*previous=*/{});
557  notifyBlockInsertions(walkedOp, listener);
558  };
559  for (Region &region : newOp->getRegions())
560  region.walk<WalkOrder::PreOrder>(walkFn);
561  }
562 
563  return newOp;
564 }
565 
567  IRMapping mapper;
568  return clone(op, mapper);
569 }
570 
572  Region::iterator before, IRMapping &mapping) {
573  region.cloneInto(&parent, before, mapping);
574 
575  // Fast path: If no listener is attached, there is no more work to do.
576  if (!listener)
577  return;
578 
579  // Notify about op/block insertion.
580  for (auto it = mapping.lookup(&region.front())->getIterator(); it != before;
581  ++it) {
582  listener->notifyBlockInserted(&*it, /*previous=*/nullptr,
583  /*previousIt=*/{});
584  it->walk<WalkOrder::PreOrder>([&](Operation *walkedOp) {
585  listener->notifyOperationInserted(walkedOp, /*previous=*/{});
586  notifyBlockInsertions(walkedOp, listener);
587  });
588  }
589 }
590 
592  Region::iterator before) {
593  IRMapping mapping;
594  cloneRegionBefore(region, parent, before, mapping);
595 }
596 
597 void OpBuilder::cloneRegionBefore(Region &region, Block *before) {
598  cloneRegionBefore(region, *before->getParent(), before->getIterator());
599 }
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:536
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:398
unsigned getNumDims() const
Definition: AffineMap.cpp:394
ArrayRef< AffineExpr > getResults() const
Definition: AffineMap.cpp:407
unsigned getNumResults() const
Definition: AffineMap.cpp:402
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:162
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition: Block.cpp:29
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:104
AffineMap getSingleDimShiftAffineMap(int64_t shift)
Returns a map that shifts its (single) input dimension by 'shift'.
Definition: Builders.cpp:397
IntegerType getI16Type()
Definition: Builders.cpp:61
UnitAttr getUnitAttr()
Definition: Builders.cpp:94
ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:283
DenseF64ArrayAttr getDenseF64ArrayAttr(ArrayRef< double > values)
Definition: Builders.cpp:171
IntegerType getI2Type()
Definition: Builders.cpp:55
FloatType getF80Type()
Definition: Builders.cpp:47
FloatType getF128Type()
Definition: Builders.cpp:49
DenseI8ArrayAttr getDenseI8ArrayAttr(ArrayRef< int8_t > values)
Definition: Builders.cpp:151
IntegerAttr getI32IntegerAttr(int32_t value)
Definition: Builders.cpp:196
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:159
DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)
Vector-typed DenseIntElementsAttr getters. values must not be empty.
Definition: Builders.cpp:112
FloatType getF32Type()
Definition: Builders.cpp:43
FloatType getTF32Type()
Definition: Builders.cpp:41
TupleType getTupleType(TypeRange elementTypes)
Definition: Builders.cpp:80
IntegerAttr getIntegerAttr(Type type, int64_t value)
Definition: Builders.cpp:224
FloatAttr getF64FloatAttr(double value)
Definition: Builders.cpp:238
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:403
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:272
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:163
FloatAttr getF16FloatAttr(float value)
Definition: Builders.cpp:246
AffineMap getDimIdentityMap()
Definition: Builders.cpp:379
AffineMap getMultiDimIdentityMap(unsigned rank)
Definition: Builders.cpp:383
IntegerAttr getI16IntegerAttr(int16_t value)
Definition: Builders.cpp:213
DenseI16ArrayAttr getDenseI16ArrayAttr(ArrayRef< int16_t > values)
Definition: Builders.cpp:155
AffineExpr getAffineSymbolExpr(unsigned position)
Definition: Builders.cpp:364
DenseFPElementsAttr getF32VectorAttr(ArrayRef< float > values)
Definition: Builders.cpp:136
FloatAttr getFloatAttr(Type type, double value)
Definition: Builders.cpp:250
AffineExpr getAffineConstantExpr(int64_t constant)
Definition: Builders.cpp:368
DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)
Tensor-typed DenseIntElementsAttr getters.
Definition: Builders.cpp:175
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
Definition: Builders.cpp:76
IntegerType getI64Type()
Definition: Builders.cpp:65
IntegerType getI32Type()
Definition: Builders.cpp:63
IntegerAttr getI64IntegerAttr(int64_t value)
Definition: Builders.cpp:108
IntegerType getIntegerType(unsigned width)
Definition: Builders.cpp:67
NoneType getNoneType()
Definition: Builders.cpp:84
DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:182
BoolAttr getBoolAttr(bool value)
Definition: Builders.cpp:96
IntegerType getI4Type()
Definition: Builders.cpp:57
StringAttr getStringAttr(const Twine &bytes)
Definition: Builders.cpp:258
MLIRContext * context
Definition: Builders.h:200
AffineMap getEmptyAffineMap()
Returns a zero result affine map with no dimensions or symbols: () -> ().
Definition: Builders.cpp:372
IntegerAttr getSI32IntegerAttr(int32_t value)
Signed and unsigned integer attribute getters.
Definition: Builders.cpp:203
TypedAttr getZeroAttr(Type type)
Definition: Builders.cpp:320
FloatType getF16Type()
Definition: Builders.cpp:39
Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())
Definition: Builders.cpp:29
FloatType getBF16Type()
Definition: Builders.cpp:37
AffineExpr getAffineDimExpr(unsigned position)
Definition: Builders.cpp:360
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:189
AffineMap getConstantAffineMap(int64_t val)
Returns a single constant result affine map with 0 dimensions and 0 symbols.
Definition: Builders.cpp:374
MLIRContext * getContext() const
Definition: Builders.h:56
ArrayAttr getTypeArrayAttr(TypeRange values)
Definition: Builders.cpp:308
DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:118
DenseF32ArrayAttr getDenseF32ArrayAttr(ArrayRef< float > values)
Definition: Builders.cpp:167
DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:124
AffineMap getSymbolIdentityMap()
Definition: Builders.cpp:392
ArrayAttr getF64ArrayAttr(ArrayRef< double > values)
Definition: Builders.cpp:296
IntegerType getI1Type()
Definition: Builders.cpp:53
DenseFPElementsAttr getF64VectorAttr(ArrayRef< double > values)
Definition: Builders.cpp:141
Location getUnknownLoc()
Definition: Builders.cpp:27
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
Definition: Builders.cpp:262
DenseBoolArrayAttr getDenseBoolArrayAttr(ArrayRef< bool > values)
Tensor-typed DenseArrayAttr getters.
Definition: Builders.cpp:147
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:277
IndexType getIndexType()
Definition: Builders.cpp:51
IntegerType getI8Type()
Definition: Builders.cpp:59
FloatAttr getF32FloatAttr(float value)
Definition: Builders.cpp:242
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition: Builders.cpp:100
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition: Builders.cpp:90
IntegerAttr getUI32IntegerAttr(uint32_t value)
Definition: Builders.cpp:208
IntegerAttr getI8IntegerAttr(int8_t value)
Definition: Builders.cpp:217
ArrayAttr getF32ArrayAttr(ArrayRef< float > values)
Definition: Builders.cpp:290
FloatType getF64Type()
Definition: Builders.cpp:45
ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)
Definition: Builders.cpp:266
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition: Builders.cpp:302
DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)
Definition: Builders.cpp:130
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
Definition: Builders.cpp:314
TypedAttr getOneAttr(Type type)
Definition: Builders.cpp:338
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:66
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:207
This class helps build Operations.
Definition: Builders.h:205
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:544
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:571
Listener * listener
The optional listener for events of this builder.
Definition: Builders.h:605
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Definition: Builders.cpp:426
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
LogicalResult tryFold(Operation *op, SmallVectorImpl< Value > &results)
Attempts to fold the given operation and places new results within results.
Definition: Builders.cpp:468
Operation * insert(Operation *op)
Insert the given operation at the current insertion point and return it.
Definition: Builders.cpp:417
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:632
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:717
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
void erase()
Remove this operation from its parent block and delete it.
Definition: Operation.cpp:539
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:242
type_range getTypes() const
Definition: ValueRange.cpp:35
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool isSignedInteger() const
Return true if this is a signed integer type (with the specified width).
Definition: Types.cpp:68
bool isIndex() const
Definition: Types.cpp:45
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition: Types.cpp:114
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
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:641
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:617
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
Definition: AffineExpr.cpp:627
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.