-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][transform] Fix crash when op is erased during transform.foreach #66357
Merged
matthias-springer
merged 1 commit into
llvm:main
from
matthias-springer:transform_foreach_erase
Sep 14, 2023
Merged
[mlir][transform] Fix crash when op is erased during transform.foreach #66357
matthias-springer
merged 1 commit into
llvm:main
from
matthias-springer:transform_foreach_erase
Sep 14, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-mlir-core @llvm/pr-subscribers-mlir ChangesFixes a crash when an op, that is mapped to handle that a `transform.foreach` iterates over, was erased (through the `TrackingRewriter`). Erasing an op removes it from all mappings and invalidates iterators. This is already taken care of when an op is iterating over payload ops in its `apply` method, but not when another transform op is erasing a tracked payload op. -- Full diff: https://github.com//pull/66357.diff5 Files Affected:
diff --git a/mlir/include/mlir/Dialect/Transform/IR/TransformInterfaces.h b/mlir/include/mlir/Dialect/Transform/IR/TransformInterfaces.h index 114d79555dcef50..efd8d573936c332 100644 --- a/mlir/include/mlir/Dialect/Transform/IR/TransformInterfaces.h +++ b/mlir/include/mlir/Dialect/Transform/IR/TransformInterfaces.h @@ -1156,6 +1156,11 @@ bool isHandleConsumed(Value handle, transform::TransformOpInterface transform); void modifiesPayload(SmallVectorImpl<MemoryEffects::EffectInstance> &effects); void onlyReadsPayload(SmallVectorImpl<MemoryEffects::EffectInstance> &effects); +/// Checks whether the transform op modifies the payload. +bool doesModifyPayload(transform::TransformOpInterface transform); +/// Checks whether the transform op reads the payload. +bool doesReadPayload(transform::TransformOpInterface transform); + /// Populates `consumedArguments` with positions of `block` arguments that are /// consumed by the operations in the `block`. void getConsumedBlockArguments( diff --git a/mlir/lib/Dialect/Transform/IR/TransformInterfaces.cpp b/mlir/lib/Dialect/Transform/IR/TransformInterfaces.cpp index ed987ac4b51646c..00450a1ff8f36cf 100644 --- a/mlir/lib/Dialect/Transform/IR/TransformInterfaces.cpp +++ b/mlir/lib/Dialect/Transform/IR/TransformInterfaces.cpp @@ -1904,6 +1904,20 @@ void transform::onlyReadsPayload( effects.emplace_back(MemoryEffects::Read::get(), PayloadIRResource::get()); } +bool transform::doesModifyPayload(transform::TransformOpInterface transform) { + auto iface = cast<MemoryEffectOpInterface>(transform.getOperation()); + SmallVector<MemoryEffects::EffectInstance> effects; + iface.getEffects(effects); + return ::hasEffect<MemoryEffects::Write, PayloadIRResource>(effects); +} + +bool transform::doesReadPayload(transform::TransformOpInterface transform) { + auto iface = cast<MemoryEffectOpInterface>(transform.getOperation()); + SmallVector<MemoryEffects::EffectInstance> effects; + iface.getEffects(effects); + return ::hasEffect<MemoryEffects::Read, PayloadIRResource>(effects); +} + void transform::getConsumedBlockArguments( Block &block, llvm::SmallDenseSet<unsigned int> &consumedArguments) { SmallVector<MemoryEffects::EffectInstance> effects; diff --git a/mlir/lib/Dialect/Transform/IR/TransformOps.cpp b/mlir/lib/Dialect/Transform/IR/TransformOps.cpp index 7bbbbba4134b184..0c9a80699aed09b 100644 --- a/mlir/lib/Dialect/Transform/IR/TransformOps.cpp +++ b/mlir/lib/Dialect/Transform/IR/TransformOps.cpp @@ -1121,8 +1121,11 @@ transform::ForeachOp::apply(transform::TransformRewriter &rewriter, transform::TransformResults &results, transform::TransformState &state) { SmallVector<SmallVector<Operation *>> resultOps(getNumResults(), {}); - - for (Operation *op : state.getPayloadOps(getTarget())) { + // Store payload ops in a vector because ops may be removed from the mapping + // by the TrackingRewriter while the iteration is in progress. + auto it = state.getPayloadOps(getTarget()); + SmallVector<Operation *> targets(it.begin(), it.end()); + for (Operation *op : targets) { auto scope = state.make_region_scope(getBody()); if (failed(state.mapBlockArguments(getIterationVariable(), {op}))) return DiagnosedSilenceableFailure::definiteFailure(); @@ -1152,6 +1155,7 @@ void transform::ForeachOp::getEffects( SmallVectorImpl<MemoryEffects::EffectInstance> &effects) { BlockArgument iterVar = getIterationVariable(); if (any_of(getBody().front().without_terminator(), [&](Operation &op) { + return isHandleConsumed(iterVar, cast<TransformOpInterface>(&op)); })) { consumesHandle(getTarget(), effects); @@ -1159,6 +1163,16 @@ void transform::ForeachOp::getEffects( onlyReadsHandle(getTarget(), effects); } + if (any_of(getBody().front().without_terminator(), [&](Operation &op) { + return doesModifyPayload(cast<TransformOpInterface>(&op)); + })) { + modifiesPayload(effects); + } else if (any_of(getBody().front().without_terminator(), [&](Operation &op) { + return doesReadPayload(cast<TransformOpInterface>(&op)); + })) { + onlyReadsPayload(effects); + } + for (Value result : getResults()) producesHandle(result, effects); } diff --git a/mlir/test/Dialect/Transform/test-interpreter.mlir b/mlir/test/Dialect/Transform/test-interpreter.mlir index db97d0a0887576f..68e3a4851539690 100644 --- a/mlir/test/Dialect/Transform/test-interpreter.mlir +++ b/mlir/test/Dialect/Transform/test-interpreter.mlir @@ -691,6 +691,28 @@ transform.with_pdl_patterns { // ----- +// CHECK-LABEL: func @consume_in_foreach() +// CHECK-NEXT: return +func.func @consume_in_foreach() { + %0 = arith.constant 0 : index + %1 = arith.constant 1 : index + %2 = arith.constant 2 : index + %3 = arith.constant 3 : index + return +} + +transform.sequence failures(propagate) { +^bb1(%arg1: !transform.any_op): + %f = transform.structured.match ops{["arith.constant"]} in %arg1 : (!transform.any_op) -> !transform.any_op + transform.foreach %f : !transform.any_op { + ^bb2(%arg2: !transform.any_op): + // expected-remark @below {{erasing}} + transform.test_emit_remark_and_erase_operand %arg2, "erasing" : !transform.any_op + } +} + +// ----- + func.func @bar() { scf.execute_region { // expected-remark @below {{transform applied}} diff --git a/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp b/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp index afd5011f17c6d2b..21f9ff5999a5ed5 100644 --- a/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp +++ b/mlir/test/lib/Dialect/Transform/TestTransformDialectExtension.cpp @@ -390,7 +390,7 @@ DiagnosedSilenceableFailure mlir::test::TestEmitRemarkAndEraseOperandOp::apply( transform::TransformResults &results, transform::TransformState &state) { emitRemark() << getRemark(); for (Operation *op : state.getPayloadOps(getTarget())) - op->erase(); + rewriter.eraseOp(op); if (getFailAfterErase()) return emitSilenceableError() << "silenceable error"; |
ftynse
approved these changes
Sep 14, 2023
Fixes a crash when an op, that is mapped to handle that a `transform.foreach` iterates over, was erased (through the `TrackingRewriter`). Erasing an op removes it from all mappings and invalidates iterators. This is already taken care of when an op is iterating over payload ops in its `apply` method, but not when another transform op is erasing a tracked payload op.
9b69e8b
to
95ecc96
Compare
Awesome, thanks! |
kstoimenov
pushed a commit
to kstoimenov/llvm-project
that referenced
this pull request
Sep 14, 2023
llvm#66357) Fixes a crash when an op, that is mapped to handle that a `transform.foreach` iterates over, was erased (through the `TrackingRewriter`). Erasing an op removes it from all mappings and invalidates iterators. This is already taken care of when an op is iterating over payload ops in its `apply` method, but not when another transform op is erasing a tracked payload op.
This was referenced Sep 14, 2023
ZijunZhaoCCK
pushed a commit
to ZijunZhaoCCK/llvm-project
that referenced
this pull request
Sep 19, 2023
llvm#66357) Fixes a crash when an op, that is mapped to handle that a `transform.foreach` iterates over, was erased (through the `TrackingRewriter`). Erasing an op removes it from all mappings and invalidates iterators. This is already taken care of when an op is iterating over payload ops in its `apply` method, but not when another transform op is erasing a tracked payload op.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes a crash when an op, that is mapped to handle that a
transform.foreach
iterates over, was erased (through theTrackingRewriter
). Erasing an op removes it from all mappings and invalidates iterators. This is already taken care of when an op is iterating over payload ops in itsapply
method, but not when another transform op is erasing a tracked payload op.