[Mlir-commits] [mlir] Make `FunctionOpInterface` check `ReturnLike` (PR #112615)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Fri Nov 8 07:38:34 PST 2024


================
@@ -108,6 +108,30 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface", [
         }
       }
 
+      // FunctionOpInterface is tied to a ReturnLike.
+      Operation *terminator = entryBlock.getTerminator();
+      if (!terminator->hasTrait<OpTrait::ReturnLike>()) {
+        return $_op.emitOpError("The body of a FunctionOpInterface must have ")
+              << "a ReturnLike terminator, but the current terminator does not "
+              << "have this trait.";
+      }
+
+      // Match ReturnLike's operand types and FunctionOpInterface's 
+      // result types.
+      auto returnOperandTypes = terminator->getOperandTypes();
+      auto funcResultTypes =  $_op->getResultTypes();
+      if (funcResultTypes.size() != returnOperandTypes.size()) {
+        return $_op.emitOpError("The number of a FunctionOpInterface's")
+              << "results must match that of the ReturnLike operands.";
+      }
+
+      for (unsigned i = 0; i < funcResultTypes.size(); ++i) {
+        if (funcResultTypes[i] != returnOperandTypes[i]) {
+          return $_op.emitOpError("The result types of a FunctionOpInterface")
+              << "must match the operand types of the ReturnLike.";
+        }
+      }
+
----------------
ftynse wrote:

This is way too long to be inline code in a `.td` file. Please move this into a helper function in a `.cpp` file and call it from here. Having code as code rather than inlined strings makes it analyzable and processable by tooling.

https://github.com/llvm/llvm-project/pull/112615


More information about the Mlir-commits mailing list