@@ -154,14 +154,12 @@ namespace {
154
154
struct RewriterState {
155
155
RewriterState (unsigned numCreatedOps, unsigned numUnresolvedMaterializations,
156
156
unsigned numReplacements, unsigned numArgReplacements,
157
- unsigned numRewrites, unsigned numIgnoredOperations,
158
- unsigned numRootUpdates)
157
+ unsigned numRewrites, unsigned numIgnoredOperations)
159
158
: numCreatedOps(numCreatedOps),
160
159
numUnresolvedMaterializations (numUnresolvedMaterializations),
161
160
numReplacements(numReplacements),
162
161
numArgReplacements(numArgReplacements), numRewrites(numRewrites),
163
- numIgnoredOperations(numIgnoredOperations),
164
- numRootUpdates(numRootUpdates) {}
162
+ numIgnoredOperations(numIgnoredOperations) {}
165
163
166
164
// / The current number of created operations.
167
165
unsigned numCreatedOps;
@@ -180,44 +178,6 @@ struct RewriterState {
180
178
181
179
// / The current number of ignored operations.
182
180
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;
221
181
};
222
182
223
183
// ===----------------------------------------------------------------------===//
@@ -761,7 +721,8 @@ class IRRewrite {
761
721
MoveBlock,
762
722
SplitBlock,
763
723
BlockTypeConversion,
764
- MoveOperation
724
+ MoveOperation,
725
+ ModifyOperation
765
726
};
766
727
767
728
virtual ~IRRewrite () = default ;
@@ -992,7 +953,7 @@ class OperationRewrite : public IRRewrite {
992
953
993
954
static bool classof (const IRRewrite *rewrite) {
994
955
return rewrite->getKind () >= Kind::MoveOperation &&
995
- rewrite->getKind () <= Kind::MoveOperation ;
956
+ rewrite->getKind () <= Kind::ModifyOperation ;
996
957
}
997
958
998
959
protected:
@@ -1031,6 +992,34 @@ class MoveOperationRewrite : public OperationRewrite {
1031
992
// this operation was the only operation in the region.
1032
993
Operation *insertBeforeOp;
1033
994
};
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
+ };
1034
1023
} // namespace
1035
1024
1036
1025
// ===----------------------------------------------------------------------===//
@@ -1184,9 +1173,6 @@ struct ConversionPatternRewriterImpl : public RewriterBase::Listener {
1184
1173
// / operation was ignored.
1185
1174
SetVector<Operation *> ignoredOps;
1186
1175
1187
- // / A transaction state for each of operations that were updated in-place.
1188
- SmallVector<OperationTransactionState, 4 > rootUpdates;
1189
-
1190
1176
// / A vector of indices into `replacements` of operations that were replaced
1191
1177
// / with values with different result types than the original operation, e.g.
1192
1178
// / 1->N conversion of some kind.
@@ -1238,10 +1224,6 @@ static void detachNestedAndErase(Operation *op) {
1238
1224
}
1239
1225
1240
1226
void ConversionPatternRewriterImpl::discardRewrites () {
1241
- // Reset any operations that were updated in place.
1242
- for (auto &state : rootUpdates)
1243
- state.resetOperation ();
1244
-
1245
1227
undoRewrites ();
1246
1228
1247
1229
// Remove any newly created ops.
@@ -1316,15 +1298,10 @@ void ConversionPatternRewriterImpl::applyRewrites() {
1316
1298
RewriterState ConversionPatternRewriterImpl::getCurrentState () {
1317
1299
return RewriterState (createdOps.size (), unresolvedMaterializations.size (),
1318
1300
replacements.size (), argReplacements.size (),
1319
- rewrites.size (), ignoredOps.size (), rootUpdates. size () );
1301
+ rewrites.size (), ignoredOps.size ());
1320
1302
}
1321
1303
1322
1304
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
-
1328
1305
// Reset any replaced arguments.
1329
1306
for (BlockArgument replacedArg :
1330
1307
llvm::drop_begin (argReplacements, state.numArgReplacements ))
@@ -1750,7 +1727,7 @@ void ConversionPatternRewriter::startOpModification(Operation *op) {
1750
1727
#ifndef NDEBUG
1751
1728
impl->pendingRootUpdates .insert (op);
1752
1729
#endif
1753
- impl->rootUpdates . emplace_back (op);
1730
+ impl->appendRewrite <ModifyOperationRewrite> (op);
1754
1731
}
1755
1732
1756
1733
void ConversionPatternRewriter::finalizeOpModification (Operation *op) {
@@ -1769,13 +1746,15 @@ void ConversionPatternRewriter::cancelOpModification(Operation *op) {
1769
1746
" operation did not have a pending in-place update" );
1770
1747
#endif
1771
1748
// 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);
1779
1758
}
1780
1759
1781
1760
detail::ConversionPatternRewriterImpl &ConversionPatternRewriter::getImpl () {
@@ -2128,8 +2107,11 @@ OperationLegalizer::legalizePatternResult(Operation *op, const Pattern &pattern,
2128
2107
};
2129
2108
auto updatedRootInPlace = [&] {
2130
2109
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
+ });
2133
2115
};
2134
2116
(void )replacedRoot;
2135
2117
(void )updatedRootInPlace;
@@ -2221,8 +2203,11 @@ LogicalResult OperationLegalizer::legalizePatternCreatedOperations(
2221
2203
LogicalResult OperationLegalizer::legalizePatternRootUpdates (
2222
2204
ConversionPatternRewriter &rewriter, ConversionPatternRewriterImpl &impl,
2223
2205
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 ();
2226
2211
if (failed (legalize (op, rewriter))) {
2227
2212
LLVM_DEBUG (logFailure (
2228
2213
impl.logger , " failed to legalize operation updated in-place '{0}'" ,
0 commit comments