[Mlir-commits] [mlir] 4cef3f2 - Fix scf.while verifier crash (NFC)

Mehdi Amini llvmlistbot at llvm.org
Fri Jan 13 17:27:49 PST 2023


Author: Mehdi Amini
Date: 2023-01-14T01:27:38Z
New Revision: 4cef3f2232478c3fa65b4a77e141981fe875ac68

URL: https://github.com/llvm/llvm-project/commit/4cef3f2232478c3fa65b4a77e141981fe875ac68
DIFF: https://github.com/llvm/llvm-project/commit/4cef3f2232478c3fa65b4a77e141981fe875ac68.diff

LOG: Fix scf.while verifier crash (NFC)

Harden the verifier against invalid IR.

Added: 
    

Modified: 
    mlir/lib/Dialect/SCF/IR/SCF.cpp
    mlir/test/Dialect/SCF/invalid.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/SCF/IR/SCF.cpp b/mlir/lib/Dialect/SCF/IR/SCF.cpp
index a709d2c24b92d..4e6cd2e5e64bd 100644
--- a/mlir/lib/Dialect/SCF/IR/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/IR/SCF.cpp
@@ -2859,10 +2859,12 @@ static LogicalResult verifyTypeRangesMatch(OpTy op, TypeRange left,
 template <typename TerminatorTy>
 static TerminatorTy verifyAndGetTerminator(scf::WhileOp op, Region &region,
                                            StringRef errorMessage) {
-  Operation *terminatorOperation = region.front().getTerminator();
-  if (auto yield = dyn_cast_or_null<TerminatorTy>(terminatorOperation))
-    return yield;
-
+  Operation *terminatorOperation = nullptr;
+  if (!region.empty() && !region.front().empty()) {
+    terminatorOperation = &region.front().back();
+    if (auto yield = dyn_cast_or_null<TerminatorTy>(terminatorOperation))
+      return yield;
+  }
   auto diag = op.emitOpError(errorMessage);
   if (terminatorOperation)
     diag.attachNote(terminatorOperation->getLoc()) << "terminator here";

diff  --git a/mlir/test/Dialect/SCF/invalid.mlir b/mlir/test/Dialect/SCF/invalid.mlir
index c97514f3ed167..498a3bc28041e 100644
--- a/mlir/test/Dialect/SCF/invalid.mlir
+++ b/mlir/test/Dialect/SCF/invalid.mlir
@@ -460,6 +460,26 @@ func.func @while_bad_terminator() {
 
 // -----
 
+func.func @while_empty_region() {
+  // expected-error at +1 {{'scf.while' op region #0 ('before') failed to verify constraint: region with 1 blocks}}
+  scf.while : () -> () {
+  } do {
+  }
+}
+
+// -----
+
+func.func @while_empty_block() {
+  // expected-error at +1 {{expects the 'before' region to terminate with 'scf.condition'}}
+  scf.while : () -> () {
+   ^bb0:
+  } do {
+   ^bb0:
+  }
+}
+
+// -----
+
 func.func @while_cross_region_type_mismatch() {
   %true = arith.constant true
   // expected-error at +1 {{'scf.while' op  region control flow edge from Region #0 to Region #1: source has 0 operands, but target successor needs 1}}


        


More information about the Mlir-commits mailing list