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