MLIR 23.0.0git
MemorySlotUtils.cpp
Go to the documentation of this file.
1//===- MemorySlotUtils.cpp - Utilities for MemorySlot interfaces ----------===//
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// This file implements common utilities for implementing MemorySlot interfaces,
10// in particular PromotableRegionOpInterface.
11//
12//===----------------------------------------------------------------------===//
13
15
16using namespace mlir;
17
19 Block *block, Value defaultReachingDef,
20 const DenseMap<Block *, Value> &reachingAtBlockEnd) {
21 Value blockReachingDef = reachingAtBlockEnd.lookup(block);
22 if (!blockReachingDef)
23 blockReachingDef = defaultReachingDef;
24 Operation *terminator = block->getTerminator();
25 terminator->insertOperands(terminator->getNumOperands(), {blockReachingDef});
26}
27
29 Operation *op,
30 TypeRange resultTypes) {
31 RewriterBase::InsertionGuard guard(rewriter);
32 rewriter.setInsertionPoint(op);
33 OperationState state(op->getLoc(), op->getName(), op->getOperands(),
34 resultTypes, op->getAttrs());
36 unsigned numRegions = op->getNumRegions();
37 for (unsigned i = 0; i < numRegions; ++i)
38 state.addRegion();
39 Operation *newOp = rewriter.create(state);
40 rewriter.startOpModification(newOp);
41 rewriter.startOpModification(op);
42 for (unsigned i = 0; i < numRegions; ++i)
43 newOp->getRegion(i).takeBody(op->getRegion(i));
44 rewriter.finalizeOpModification(op);
45 rewriter.finalizeOpModification(newOp);
46
47 rewriter.replaceAllOpUsesWith(
48 op, newOp->getResults().take_front(op->getNumResults()));
49 rewriter.eraseOp(op);
50 return newOp;
51}
Block represents an ordered list of Operations.
Definition Block.h:33
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:249
RAII guard to reset the insertion point of the builder when destroyed.
Definition Builders.h:350
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:400
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition Builders.cpp:461
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition Operation.h:712
void insertOperands(unsigned index, ValueRange operands)
Insert the given operands into the operand list at the given 'index'.
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Definition Operation.h:538
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:700
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:241
unsigned getNumOperands()
Definition Operation.h:372
Attribute getPropertiesAsAttribute()
Return the properties converted to an attribute.
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:116
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:404
result_range getResults()
Definition Operation.h:441
unsigned getNumResults()
Return the number of results held by this operation.
Definition Operation.h:430
void takeBody(Region &other)
Takes body of another region (that region will have no body after this operation completes).
Definition Region.h:252
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void finalizeOpModification(Operation *op)
This method is used to signal the end of an in-place modification of the given operation.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
void replaceAllOpUsesWith(Operation *from, ValueRange to)
Find uses of from and replace them with to.
virtual void startOpModification(Operation *op)
This method is used to notify the rewriter that an in-place operation modification is about to happen...
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:40
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Operation * replaceWithNewResults(RewriterBase &rewriter, Operation *op, TypeRange resultTypes)
Creates a shallow copy of an operation with new result types, moving the regions out of the original ...
void updateTerminator(Block *block, Value defaultReachingDef, const DenseMap< Block *, Value > &reachingAtBlockEnd)
Appends the reaching definition for the given block as an operand to its terminator.
Include the generated interface declarations.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:120
This represents an operation in an abstracted form, suitable for use with the builder APIs.
Attribute propertiesAttr
This Attribute is used to opaquely construct the properties of the operation.
Region * addRegion()
Create a region that should be attached to the operation.