Fix scf.while verifier crash (NFC)
authorMehdi Amini <joker.eph@gmail.com>
Sat, 14 Jan 2023 01:22:05 +0000 (01:22 +0000)
committerMehdi Amini <joker.eph@gmail.com>
Sat, 14 Jan 2023 01:27:38 +0000 (01:27 +0000)
Harden the verifier against invalid IR.

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

index a709d2c..4e6cd2e 100644 (file)
@@ -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";
index c97514f..498a3bc 100644 (file)
@@ -460,6 +460,26 @@ func.func @while_bad_terminator() {
 
 // -----
 
+func.func @while_empty_region() {
+  // expected-error@+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@+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@+1 {{'scf.while' op  region control flow edge from Region #0 to Region #1: source has 0 operands, but target successor needs 1}}