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