Add a verify method to FuncOp and check that the type signature matches the signature...
authorRiver Riddle <riverriddle@google.com>
Thu, 6 Jun 2019 01:12:16 +0000 (18:12 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sun, 9 Jun 2019 23:20:35 +0000 (16:20 -0700)
PiperOrigin-RevId: 251759848

mlir/include/mlir/IR/Function.h
mlir/lib/IR/Function.cpp
mlir/test/IR/invalid-func-op.mlir

index 9ffcda4..0ff93b6 100644 (file)
@@ -336,9 +336,10 @@ public:
   static void build(Builder *builder, OperationState *result, StringRef name,
                     FunctionType type, ArrayRef<NamedAttribute> attrs);
 
-  /// Parsing/Printing methods.
+  /// Operation hooks.
   static ParseResult parse(OpAsmParser *parser, OperationState *result);
   void print(OpAsmPrinter *p);
+  LogicalResult verify();
 
   /// Returns the name of this function.
   StringRef getName() { return getAttrOfType<StringAttr>("name").getValue(); }
index 78cce5f..234c804 100644 (file)
@@ -418,6 +418,29 @@ void FuncOp::print(OpAsmPrinter *p) {
   *p << '\n';
 }
 
+LogicalResult FuncOp::verify() {
+  // If this function is external there is nothing to do.
+  if (isExternal())
+    return success();
+
+  // Verify that the argument list of the function and the arg list of the entry
+  // block line up.
+  Block &entryBlock = front();
+  auto fnInputTypes = getType().getInputs();
+  if (fnInputTypes.size() != entryBlock.getNumArguments())
+    return emitOpError("entry block must have ")
+           << fnInputTypes.size() << " arguments to match function signature";
+
+  for (unsigned i = 0, e = entryBlock.getNumArguments(); i != e; ++i)
+    if (fnInputTypes[i] != entryBlock.getArgument(i)->getType())
+      return emitOpError("type of entry block argument #")
+             << i << '(' << entryBlock.getArgument(i)->getType()
+             << ") must match the type of the corresponding argument in "
+             << "function signature(" << fnInputTypes[i] << ')';
+
+  return success();
+}
+
 //===----------------------------------------------------------------------===//
 // Function Argument Attribute.
 //===----------------------------------------------------------------------===//
index a6e84f8..7dcd8d2 100644 (file)
@@ -35,3 +35,25 @@ func @func_op() {
   func @mixed_named_arguments(%a : i32)
   return
 }
+
+// -----
+
+func @func_op() {
+  // expected-error@+1 {{entry block must have 1 arguments to match function signature}}
+  func @mixed_named_arguments(f32) {
+  ^entry:
+    return
+  }
+  return
+}
+
+// -----
+
+func @func_op() {
+  // expected-error@+1 {{type of entry block argument #0('i32') must match the type of the corresponding argument in function signature('f32')}}
+  func @mixed_named_arguments(f32) {
+  ^entry(%arg : i32):
+    return
+  }
+  return
+}