[MLIR] Fix walk() after PostOrderTraversal change#191357
Merged
aengelke merged 1 commit intoApr 10, 2026
Merged
Conversation
Created using spr 1.3.8-wip
Member
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core Author: Alexis Engelke (aengelke) Changesmake_early_inc_range doesn't keep the range alive, only the iterators. Fixup of #191047 / 691a130. Full diff: https://github.com/llvm/llvm-project/pull/191357.diff 1 Files Affected:
diff --git a/mlir/include/mlir/IR/Visitors.h b/mlir/include/mlir/IR/Visitors.h
index 893f66ae33deb..907f470c0d248 100644
--- a/mlir/include/mlir/IR/Visitors.h
+++ b/mlir/include/mlir/IR/Visitors.h
@@ -120,8 +120,9 @@ void walk(Operation *op, function_ref<void(Block *)> callback,
WalkOrder order) {
for (auto ®ion : Iterator::makeIterable(*op)) {
// Early increment here in the case where the block is erased.
- for (auto &block :
- llvm::make_early_inc_range(Iterator::makeIterable(region))) {
+ // PostOrderTraversal keeps state outside of iterators, so store it here.
+ auto &&It = Iterator::makeIterable(region);
+ for (auto &block : llvm::make_early_inc_range(It)) {
if (order == WalkOrder::PreOrder)
callback(&block);
for (auto &nestedOp : Iterator::makeIterable(block))
@@ -195,8 +196,9 @@ WalkResult walk(Operation *op, function_ref<WalkResult(Block *)> callback,
WalkOrder order) {
for (auto ®ion : Iterator::makeIterable(*op)) {
// Early increment here in the case where the block is erased.
- for (auto &block :
- llvm::make_early_inc_range(Iterator::makeIterable(region))) {
+ // PostOrderTraversal keeps state outside of iterators, so store it here.
+ auto &&It = Iterator::makeIterable(region);
+ for (auto &block : llvm::make_early_inc_range(It)) {
if (order == WalkOrder::PreOrder) {
WalkResult result = callback(&block);
if (result.wasSkipped())
|
nikic
approved these changes
Apr 10, 2026
Contributor
Author
|
(For reference: using C++23 (P2718R0) would fix this problem as well, but it'll be a long time until we can use C++23...) |
This file contains hidden or 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
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.
make_early_inc_range doesn't keep the range alive, only the iterators.
This breaks with the recent PostOrderTraversal change, which no longer
stores the state in the iterators. Store the range in a variable to keep
it alive for the entire loop.
Fixup of #191047 / 691a130.