Skip to content

Conversation

@matthias-springer
Copy link
Member

Use make_early_inc_range when erasing operations from a block to make sure that the iterator is not invalidated. The previous implementation happened to work on a "normal" dialect conversion because some IR modifications are delayed. It no longer works with a One-Shot Dialect Conversion. The new One-Shot Dialect Conversion API is more similar to the normal rewriter API.

@llvmbot
Copy link
Member

llvmbot commented Jul 3, 2025

@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)

Changes

Use make_early_inc_range when erasing operations from a block to make sure that the iterator is not invalidated. The previous implementation happened to work on a "normal" dialect conversion because some IR modifications are delayed. It no longer works with a One-Shot Dialect Conversion. The new One-Shot Dialect Conversion API is more similar to the normal rewriter API.


Full diff: https://github.com/llvm/llvm-project/pull/146892.diff

2 Files Affected:

  • (modified) mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp (+1-1)
  • (modified) mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp (+1-1)
diff --git a/mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp b/mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp
index de1eafc4d8678..22f75e042fe08 100644
--- a/mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp
+++ b/mlir/examples/toy/Ch6/mlir/LowerToLLVM.cpp
@@ -92,7 +92,7 @@ class PrintOpLowering : public ConversionPattern {
       auto step = rewriter.create<arith::ConstantIndexOp>(loc, 1);
       auto loop =
           rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step);
-      for (Operation &nested : *loop.getBody())
+      for (Operation &nested : make_early_inc_range(*loop.getBody()))
         rewriter.eraseOp(&nested);
       loopIvs.push_back(loop.getInductionVar());
 
diff --git a/mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp b/mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp
index a20a2c888a175..54eeb275ae85e 100644
--- a/mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp
+++ b/mlir/examples/toy/Ch7/mlir/LowerToLLVM.cpp
@@ -92,7 +92,7 @@ class PrintOpLowering : public ConversionPattern {
       auto step = rewriter.create<arith::ConstantIndexOp>(loc, 1);
       auto loop =
           rewriter.create<scf::ForOp>(loc, lowerBound, upperBound, step);
-      for (Operation &nested : *loop.getBody())
+      for (Operation &nested : make_early_inc_range(*loop.getBody()))
         rewriter.eraseOp(&nested);
       loopIvs.push_back(loop.getInductionVar());
 

@matthias-springer matthias-springer merged commit 34f124b into main Jul 3, 2025
11 checks passed
@matthias-springer matthias-springer deleted the users/matthias-springer/toy_erase branch July 3, 2025 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants