[Mlir-commits] [mlir] [mlir][SCF] Do not access erased op in `scf.while` lowering (PR #148356)

Matthias Springer llvmlistbot at llvm.org
Sat Jul 12 03:48:49 PDT 2025


https://github.com/matthias-springer created https://github.com/llvm/llvm-project/pull/148356

Do not access the erased `scf.condition` operation in the lowering pattern. That won't work anymore in a One-Shot Dialect Conversion and triggers a use-after-free sanitizer error.

After the One-Shot Dialect Conversion refactoring, a `ConversionPatternRewriter` will behave more like a normal `PatternRewriter`.


>From 647c06352c5b2e7cb37fb043f97147d9d8d2ea15 Mon Sep 17 00:00:00 2001
From: Matthias Springer <me at m-sp.org>
Date: Sat, 12 Jul 2025 10:46:26 +0000
Subject: [PATCH] [mlir][SCF] Do not access erased op in `scf.while` lowering

---
 mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp b/mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp
index 03c5c2239ed7a..0df91a243d07a 100644
--- a/mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp
+++ b/mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp
@@ -630,14 +630,16 @@ DoWhileLowering::matchAndRewrite(WhileOp whileOp,
   // Loop around the "before" region based on condition.
   rewriter.setInsertionPointToEnd(before);
   auto condOp = cast<ConditionOp>(before->getTerminator());
-  rewriter.replaceOpWithNewOp<cf::CondBranchOp>(condOp, condOp.getCondition(),
-                                                before, condOp.getArgs(),
-                                                continuation, ValueRange());
+  rewriter.create<cf::CondBranchOp>(condOp.getLoc(), condOp.getCondition(),
+                                    before, condOp.getArgs(), continuation,
+                                    ValueRange());
 
   // Replace the op with values "yielded" from the "before" region, which are
   // visible by dominance.
   rewriter.replaceOp(whileOp, condOp.getArgs());
 
+  // Erase the condition op.
+  rewriter.eraseOp(condOp);
   return success();
 }
 



More information about the Mlir-commits mailing list