MLIR: lib/IR/Builders.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

18 #include "llvm/ADT/SmallVectorExtras.h"

19 #include "llvm/Support/raw_ostream.h"

20

21 using namespace mlir;

22

23

24

25

26

28

31 }

32

33

34

35

36

38

40

42

44

46

48

50

52

54

56

58

60

62

64

66

68

71 }

72

76 }

77

80 }

81

84 }

85

87

88

89

90

91

94 }

95

97

100 }

101

104 }

105

108 }

109

112 }

113

117 values);

118 }

119

123 values);

124 }

125

129 values);

130 }

131

135 values);

136 }

137

141 }

142

146 values);

147 }

148

151 }

152

155 }

156

159 }

160

163 }

164

167 }

168

171 }

172

175 }

176

181 values);

182 }

183

188 values);

189 }

190

195 values);

196 }

197

199

200

202 APInt(32, value, true));

203 }

204

207 APInt(32, value, true));

208 }

209

212 APInt(32, (uint64_t)value, false));

213 }

214

217 }

218

220

221

223 APInt(8, value, true));

224 }

225

229

230

233 true));

234 }

235

238 }

239

242 }

243

246 }

247

250 }

251

254 }

255

258 }

259

262 }

263

266 }

267

269 auto attrs = llvm::map_to_vector<8>(

272 }

273

275 auto attrs = llvm::map_to_vector<8>(

278 }

280 auto attrs = llvm::map_to_vector<8>(

283 }

284

286 auto attrs = llvm::map_to_vector<8>(values, [this](int64_t v) -> Attribute {

288 });

290 }

291

293 auto attrs = llvm::map_to_vector<8>(

296 }

297

299 auto attrs = llvm::map_to_vector<8>(

302 }

303

305 auto attrs = llvm::map_to_vector<8>(

308 }

309

311 auto attrs = llvm::map_to_vector<8>(

314 }

315

317 auto attrs = llvm::map_to_vector<8>(

320 }

321

323 if (llvm::isa(type))

325 if (llvm::isa(type))

327 if (llvm::dyn_cast(type))

329 APInt(llvm::cast(type).getWidth(), 0));

330 if (llvm::isa<RankedTensorType, VectorType>(type)) {

331 auto vtType = llvm::cast(type);

332 auto element = getZeroAttr(vtType.getElementType());

333 if (!element)

334 return {};

336 }

337 return {};

338 }

339

341 if (llvm::isa(type))

343 if (llvm::isa(type))

345 if (llvm::dyn_cast(type))

347 APInt(llvm::cast(type).getWidth(), 1));

348 if (llvm::isa<RankedTensorType, VectorType>(type)) {

349 auto vtType = llvm::cast(type);

350 auto element = getOneAttr(vtType.getElementType());

351 if (!element)

352 return {};

354 }

355 return {};

356 }

357

358

359

360

361

364 }

365

368 }

369

372 }

373

375

377 return AffineMap::get(0, 0,

379 }

380

383 }

384

387 dimExprs.reserve(rank);

388 for (unsigned i = 0; i < rank; ++i)

390 return AffineMap::get(rank, 0, dimExprs,

392 }

393

395 return AffineMap::get(0, 1,

397 }

398

400

402 return AffineMap::get(1, 0, expr);

403 }

404

408 for (auto resultExpr : map.getResults())

409 shiftedResults.push_back(resultExpr + shift);

412 }

413

414

415

416

417

418

420 if (block) {

424 }

425 return op;

426 }

427

430 assert(parent && "expected valid parent region");

431 assert(argTypes.size() == locs.size() && "argument location mismatch");

433 insertPt = parent->end();

434

437 parent->getBlocks().insert(insertPt, b);

439

442 return b;

443 }

444

445

446

449 assert(insertBefore && "expected valid insertion block");

451 argTypes, locs);

452 }

453

454

457 }

458

459

465 OperationState state(loc, opName, operands, types, attributes, successors,

466 regions);

467 return create(state);

468 }

469

470 LogicalResult

473 assert(results.empty() && "expected empty results");

476 results.reserve(opResults.size());

477 auto cleanupFailure = [&] {

478 results.clear();

479 return failure();

480 };

481

482

484 return cleanupFailure();

485

486

488 if (failed(op->fold(foldResults)))

489 return cleanupFailure();

490

491

492 if (foldResults.empty())

493 return success();

494

495

498

499

501 for (auto [foldResult, expectedType] :

502 llvm::zip_equal(foldResults, opResults.getTypes())) {

503

504

505 if (auto value = llvm::dyn_cast_if_present(foldResult)) {

506 results.push_back(value);

507 continue;

508 }

509

510

511 if (!dialect)

512 return cleanupFailure();

513

514

515 Attribute attr = cast(foldResult);

516 auto *constOp = dialect->materializeConstant(cstBuilder, attr, expectedType,

518 if (!constOp) {

519

520 for (Operation *cst : generatedConstants)

522 return cleanupFailure();

523 }

525

526 generatedConstants.push_back(constOp);

527 results.push_back(constOp->getResult(0));

528 }

529

530

531 for (Operation *cst : generatedConstants)

533

534

535 if (materializedConstants)

536 *materializedConstants = std::move(generatedConstants);

537

538 return success();

539 }

540

541

542

546 for (Block &b : r.getBlocks())

548 {});

549 }

550

553 newOp = insert(newOp);

554

555

556

557

559

560

562 auto walkFn = [&](Operation *walkedOp) {

565 };

568 }

569

570 return newOp;

571 }

572

575 return clone(op, mapper);

576 }

577

580 region.cloneInto(&parent, before, mapping);

581

582

584 return;

585

586

587 for (auto it = mapping.lookup(&region.front())->getIterator(); it != before;

588 ++it) {

590 {});

594 });

595 }

596 }

597

602 }

603

606 }

static void notifyBlockInsertions(Operation *op, OpBuilder::Listener *listener)

Helper function that sends block insertion notifications for every block that is directly nested in t...

Base type for affine expression.

A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.

static AffineMap get(MLIRContext *context)

Returns a zero result affine map with no dimensions or symbols: () -> ().

unsigned getNumSymbols() const

unsigned getNumDims() const

ArrayRef< AffineExpr > getResults() const

unsigned getNumResults() const

Attributes are known-constant values of operations.

This class provides an abstraction over the different types of ranges over Blocks.

Block represents an ordered list of Operations.

iterator_range< args_iterator > addArguments(TypeRange types, ArrayRef< Location > locs)

Add one argument to the argument list for each type specified in the list.

Region * getParent() const

Provide a 'getParent' method for ilist_node_with_parent methods.

OpListType & getOperations()

Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.

static BoolAttr get(MLIRContext *context, bool value)

IntegerAttr getIndexAttr(int64_t value)

AffineMap getSingleDimShiftAffineMap(int64_t shift)

Returns a map that shifts its (single) input dimension by 'shift'.

ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)

DenseF64ArrayAttr getDenseF64ArrayAttr(ArrayRef< double > values)

DenseI8ArrayAttr getDenseI8ArrayAttr(ArrayRef< int8_t > values)

IntegerAttr getI32IntegerAttr(int32_t value)

DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)

DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)

Vector-typed DenseIntElementsAttr getters. values must not be empty.

TupleType getTupleType(TypeRange elementTypes)

IntegerAttr getIntegerAttr(Type type, int64_t value)

FloatAttr getF64FloatAttr(double value)

AffineMap getShiftedAffineMap(AffineMap map, int64_t shift)

Returns an affine map that is a translation (shift) of all result expressions in 'map' by 'shift'.

ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)

DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)

FloatAttr getF16FloatAttr(float value)

AffineMap getDimIdentityMap()

AffineMap getMultiDimIdentityMap(unsigned rank)

IntegerAttr getI16IntegerAttr(int16_t value)

DenseI16ArrayAttr getDenseI16ArrayAttr(ArrayRef< int16_t > values)

AffineExpr getAffineSymbolExpr(unsigned position)

DenseFPElementsAttr getF32VectorAttr(ArrayRef< float > values)

FloatAttr getFloatAttr(Type type, double value)

AffineExpr getAffineConstantExpr(int64_t constant)

DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)

Tensor-typed DenseIntElementsAttr getters.

FunctionType getFunctionType(TypeRange inputs, TypeRange results)

IntegerAttr getI64IntegerAttr(int64_t value)

IntegerType getIntegerType(unsigned width)

DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)

BoolAttr getBoolAttr(bool value)

StringAttr getStringAttr(const Twine &bytes)

AffineMap getEmptyAffineMap()

Returns a zero result affine map with no dimensions or symbols: () -> ().

IntegerAttr getSI32IntegerAttr(int32_t value)

Signed and unsigned integer attribute getters.

TypedAttr getZeroAttr(Type type)

Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())

AffineExpr getAffineDimExpr(unsigned position)

DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)

AffineMap getConstantAffineMap(int64_t val)

Returns a single constant result affine map with 0 dimensions and 0 symbols.

MLIRContext * getContext() const

ArrayAttr getTypeArrayAttr(TypeRange values)

FloatType getF8E8M0Type()

DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)

DenseF32ArrayAttr getDenseF32ArrayAttr(ArrayRef< float > values)

DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)

AffineMap getSymbolIdentityMap()

ArrayAttr getF64ArrayAttr(ArrayRef< double > values)

DenseFPElementsAttr getF64VectorAttr(ArrayRef< double > values)

ArrayAttr getArrayAttr(ArrayRef< Attribute > value)

DenseBoolArrayAttr getDenseBoolArrayAttr(ArrayRef< bool > values)

Tensor-typed DenseArrayAttr getters.

ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)

FloatAttr getF32FloatAttr(float value)

DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)

NamedAttribute getNamedAttr(StringRef name, Attribute val)

IntegerAttr getUI32IntegerAttr(uint32_t value)

IntegerAttr getI8IntegerAttr(int8_t value)

ArrayAttr getF32ArrayAttr(ArrayRef< float > values)

ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)

ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)

DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)

ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)

TypedAttr getOneAttr(Type type)

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...

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...

This is a utility class for mapping one set of IR entities to another.

auto lookup(T from) const

Lookup a mapped value within the map.

This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...

NamedAttribute represents a combination of a name and an Attribute value.

This class helps build Operations.

Operation * clone(Operation &op, IRMapping &mapper)

Creates a deep copy of the specified operation, remapping any operands that use values outside of the...

void cloneRegionBefore(Region &region, Block *before)

void setInsertionPointToEnd(Block *block)

Sets the insertion point to the end of the specified block.

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".

LogicalResult tryFold(Operation *op, SmallVectorImpl< Value > &results, SmallVectorImpl< Operation * > *materializedConstants=nullptr)

Attempts to fold the given operation and places new results within results.

Listener * listener

The optional listener for events of this builder.

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.

Operation * create(const OperationState &state)

Creates an operation given the fields represented as an OperationState.

Operation * insert(Operation *op)

Insert the given operation at the current insertion point and return it.

Operation is the basic unit of execution within MLIR.

LogicalResult fold(ArrayRef< Attribute > operands, SmallVectorImpl< OpFoldResult > &results)

Attempt to fold this operation with the specified constant operand values.

Dialect * getDialect()

Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...

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...

Location getLoc()

The source location the operation was defined or derived from.

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.

MutableArrayRef< Region > getRegions()

Returns the regions held by this operation.

result_range getResults()

void erase()

Remove this operation from its parent block and delete it.

This class contains a list of basic blocks and a link to the parent operation it is attached to.

void cloneInto(Region *dest, IRMapping &mapper)

Clone the internal blocks from this region into dest.

BlockListType & getBlocks()

BlockListType::iterator iterator

This class implements the result iterators for the Operation class.

type_range getTypes() const

This class provides an abstraction over the various different ranges of value types.

Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...

bool isSignedInteger() const

Return true if this is a signed integer type (with the specified width).

unsigned getIntOrFloatBitWidth() const

Return the bit width of an integer or a float type, assert failure on other types.

This class provides an abstraction over the different types of ranges over Values.

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.

Include the generated interface declarations.

bool matchPattern(Value value, const Pattern &pattern)

Entry point for matching a pattern over a Value.

AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)

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.

AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)

These free functions allow clients of the API to not use classes in detail.

AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)

This class represents a listener that may be used to hook into various actions within an OpBuilder.

virtual void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)

Notify the listener that the specified block was inserted.

virtual void notifyOperationInserted(Operation *op, InsertPoint previous)

Notify the listener that the specified operation was inserted.

This represents an operation in an abstracted form, suitable for use with the builder APIs.