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