Skip to content

Commit f7010ea

Browse files
[mlir][Transforms][NFC] Turn in-place op modifications into RewriteActions
This commit simplifies the internal state of the dialect conversion. A separate field for the previous state of in-place op modifications is no longer needed. BEGIN_PUBLIC No public commit message needed for presubmit. END_PUBLIC
1 parent ebfaca6 commit f7010ea

File tree

2 files changed

+56
-71
lines changed

2 files changed

+56
-71
lines changed

mlir/include/mlir/Transforms/DialectConversion.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,8 @@ class ConversionPatternRewriter final : public PatternRewriter {
721721

722722
/// PatternRewriter hook for updating the given operation in-place.
723723
/// Note: These methods only track updates to the given operation itself,
724-
/// and not nested regions. Updates to regions will still require
725-
/// notification through other more specific hooks above.
724+
/// and not nested regions. Updates to regions will still require notification
725+
/// through other more specific hooks above.
726726
void startOpModification(Operation *op) override;
727727

728728
/// PatternRewriter hook for updating the given operation in-place.

mlir/lib/Transforms/Utils/DialectConversion.cpp

+54-69
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,12 @@ namespace {
154154
struct RewriterState {
155155
RewriterState(unsigned numCreatedOps, unsigned numUnresolvedMaterializations,
156156
unsigned numReplacements, unsigned numArgReplacements,
157-
unsigned numRewrites, unsigned numIgnoredOperations,
158-
unsigned numRootUpdates)
157+
unsigned numRewrites, unsigned numIgnoredOperations)
159158
: numCreatedOps(numCreatedOps),
160159
numUnresolvedMaterializations(numUnresolvedMaterializations),
161160
numReplacements(numReplacements),
162161
numArgReplacements(numArgReplacements), numRewrites(numRewrites),
163-
numIgnoredOperations(numIgnoredOperations),
164-
numRootUpdates(numRootUpdates) {}
162+
numIgnoredOperations(numIgnoredOperations) {}
165163

166164
/// The current number of created operations.
167165
unsigned numCreatedOps;
@@ -180,44 +178,6 @@ struct RewriterState {
180178

181179
/// The current number of ignored operations.
182180
unsigned numIgnoredOperations;
183-
184-
/// The current number of operations that were updated in place.
185-
unsigned numRootUpdates;
186-
};
187-
188-
//===----------------------------------------------------------------------===//
189-
// OperationTransactionState
190-
191-
/// The state of an operation that was updated by a pattern in-place. This
192-
/// contains all of the necessary information to reconstruct an operation that
193-
/// was updated in place.
194-
class OperationTransactionState {
195-
public:
196-
OperationTransactionState() = default;
197-
OperationTransactionState(Operation *op)
198-
: op(op), loc(op->getLoc()), attrs(op->getAttrDictionary()),
199-
operands(op->operand_begin(), op->operand_end()),
200-
successors(op->successor_begin(), op->successor_end()) {}
201-
202-
/// Discard the transaction state and reset the state of the original
203-
/// operation.
204-
void resetOperation() const {
205-
op->setLoc(loc);
206-
op->setAttrs(attrs);
207-
op->setOperands(operands);
208-
for (const auto &it : llvm::enumerate(successors))
209-
op->setSuccessor(it.value(), it.index());
210-
}
211-
212-
/// Return the original operation of this state.
213-
Operation *getOperation() const { return op; }
214-
215-
private:
216-
Operation *op;
217-
LocationAttr loc;
218-
DictionaryAttr attrs;
219-
SmallVector<Value, 8> operands;
220-
SmallVector<Block *, 2> successors;
221181
};
222182

223183
//===----------------------------------------------------------------------===//
@@ -761,7 +721,8 @@ class IRRewrite {
761721
MoveBlock,
762722
SplitBlock,
763723
BlockTypeConversion,
764-
MoveOperation
724+
MoveOperation,
725+
ModifyOperation
765726
};
766727

767728
virtual ~IRRewrite() = default;
@@ -992,7 +953,7 @@ class OperationRewrite : public IRRewrite {
992953

993954
static bool classof(const IRRewrite *rewrite) {
994955
return rewrite->getKind() >= Kind::MoveOperation &&
995-
rewrite->getKind() <= Kind::MoveOperation;
956+
rewrite->getKind() <= Kind::ModifyOperation;
996957
}
997958

998959
protected:
@@ -1031,6 +992,34 @@ class MoveOperationRewrite : public OperationRewrite {
1031992
// this operation was the only operation in the region.
1032993
Operation *insertBeforeOp;
1033994
};
995+
996+
/// In-place modification of an op. This rewrite is immediately reflected in
997+
/// the IR. The previous state of the operation is stored in this object.
998+
class ModifyOperationRewrite : public OperationRewrite {
999+
public:
1000+
ModifyOperationRewrite(ConversionPatternRewriterImpl &rewriterImpl,
1001+
Operation *op)
1002+
: OperationRewrite(Kind::ModifyOperation, rewriterImpl, op),
1003+
loc(op->getLoc()), attrs(op->getAttrDictionary()),
1004+
operands(op->operand_begin(), op->operand_end()),
1005+
successors(op->successor_begin(), op->successor_end()) {}
1006+
1007+
/// Discard the transaction state and reset the state of the original
1008+
/// operation.
1009+
void rollback() override {
1010+
op->setLoc(loc);
1011+
op->setAttrs(attrs);
1012+
op->setOperands(operands);
1013+
for (const auto &it : llvm::enumerate(successors))
1014+
op->setSuccessor(it.value(), it.index());
1015+
}
1016+
1017+
private:
1018+
LocationAttr loc;
1019+
DictionaryAttr attrs;
1020+
SmallVector<Value, 8> operands;
1021+
SmallVector<Block *, 2> successors;
1022+
};
10341023
} // namespace
10351024

10361025
//===----------------------------------------------------------------------===//
@@ -1184,9 +1173,6 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
11841173
/// operation was ignored.
11851174
SetVector<Operation *> ignoredOps;
11861175

1187-
/// A transaction state for each of operations that were updated in-place.
1188-
SmallVector<OperationTransactionState, 4> rootUpdates;
1189-
11901176
/// A vector of indices into `replacements` of operations that were replaced
11911177
/// with values with different result types than the original operation, e.g.
11921178
/// 1->N conversion of some kind.
@@ -1238,10 +1224,6 @@ static void detachNestedAndErase(Operation *op) {
12381224
}
12391225

12401226
void ConversionPatternRewriterImpl::discardRewrites() {
1241-
// Reset any operations that were updated in place.
1242-
for (auto &state : rootUpdates)
1243-
state.resetOperation();
1244-
12451227
undoRewrites();
12461228

12471229
// Remove any newly created ops.
@@ -1316,15 +1298,10 @@ void ConversionPatternRewriterImpl::applyRewrites() {
13161298
RewriterState ConversionPatternRewriterImpl::getCurrentState() {
13171299
return RewriterState(createdOps.size(), unresolvedMaterializations.size(),
13181300
replacements.size(), argReplacements.size(),
1319-
rewrites.size(), ignoredOps.size(), rootUpdates.size());
1301+
rewrites.size(), ignoredOps.size());
13201302
}
13211303

13221304
void ConversionPatternRewriterImpl::resetState(RewriterState state) {
1323-
// Reset any operations that were updated in place.
1324-
for (unsigned i = state.numRootUpdates, e = rootUpdates.size(); i != e; ++i)
1325-
rootUpdates[i].resetOperation();
1326-
rootUpdates.resize(state.numRootUpdates);
1327-
13281305
// Reset any replaced arguments.
13291306
for (BlockArgument replacedArg :
13301307
llvm::drop_begin(argReplacements, state.numArgReplacements))
@@ -1750,7 +1727,7 @@ void ConversionPatternRewriter::startOpModification(Operation *op) {
17501727
#ifndef NDEBUG
17511728
impl->pendingRootUpdates.insert(op);
17521729
#endif
1753-
impl->rootUpdates.emplace_back(op);
1730+
impl->appendRewrite<ModifyOperationRewrite>(op);
17541731
}
17551732

17561733
void ConversionPatternRewriter::finalizeOpModification(Operation *op) {
@@ -1769,13 +1746,15 @@ void ConversionPatternRewriter::cancelOpModification(Operation *op) {
17691746
"operation did not have a pending in-place update");
17701747
#endif
17711748
// Erase the last update for this operation.
1772-
auto stateHasOp = [op](const auto &it) { return it.getOperation() == op; };
1773-
auto &rootUpdates = impl->rootUpdates;
1774-
auto it = llvm::find_if(llvm::reverse(rootUpdates), stateHasOp);
1775-
assert(it != rootUpdates.rend() && "no root update started on op");
1776-
(*it).resetOperation();
1777-
int updateIdx = std::prev(rootUpdates.rend()) - it;
1778-
rootUpdates.erase(rootUpdates.begin() + updateIdx);
1749+
auto it = llvm::find_if(
1750+
llvm::reverse(impl->rewrites), [&](std::unique_ptr<IRRewrite> &rewrite) {
1751+
auto *modifyRewrite = dyn_cast<ModifyOperationRewrite>(rewrite.get());
1752+
return modifyRewrite && modifyRewrite->getOperation() == op;
1753+
});
1754+
assert(it != impl->rewrites.rend() && "no root update started on op");
1755+
(*it)->rollback();
1756+
int updateIdx = std::prev(impl->rewrites.rend()) - it;
1757+
impl->rewrites.erase(impl->rewrites.begin() + updateIdx);
17791758
}
17801759

17811760
detail::ConversionPatternRewriterImpl &ConversionPatternRewriter::getImpl() {
@@ -2128,8 +2107,11 @@ OperationLegalizer::legalizePatternResult(Operation *op, const Pattern &pattern,
21282107
};
21292108
auto updatedRootInPlace = [&] {
21302109
return llvm::any_of(
2131-
llvm::drop_begin(impl.rootUpdates, curState.numRootUpdates),
2132-
[op](auto &state) { return state.getOperation() == op; });
2110+
llvm::drop_begin(impl.rewrites, curState.numRewrites),
2111+
[op](auto &rewrite) {
2112+
auto *modifyRewrite = dyn_cast<ModifyOperationRewrite>(rewrite.get());
2113+
return modifyRewrite && modifyRewrite->getOperation() == op;
2114+
});
21332115
};
21342116
(void)replacedRoot;
21352117
(void)updatedRootInPlace;
@@ -2221,8 +2203,11 @@ LogicalResult OperationLegalizer::legalizePatternCreatedOperations(
22212203
LogicalResult OperationLegalizer::legalizePatternRootUpdates(
22222204
ConversionPatternRewriter &rewriter, ConversionPatternRewriterImpl &impl,
22232205
RewriterState &state, RewriterState &newState) {
2224-
for (int i = state.numRootUpdates, e = newState.numRootUpdates; i != e; ++i) {
2225-
Operation *op = impl.rootUpdates[i].getOperation();
2206+
for (int i = state.numRewrites, e = newState.numRewrites; i != e; ++i) {
2207+
auto *rewrite = dyn_cast<ModifyOperationRewrite>(impl.rewrites[i].get());
2208+
if (!rewrite)
2209+
continue;
2210+
Operation *op = rewrite->getOperation();
22262211
if (failed(legalize(op, rewriter))) {
22272212
LLVM_DEBUG(logFailure(
22282213
impl.logger, "failed to legalize operation updated in-place '{0}'",

0 commit comments

Comments
 (0)