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