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