[Mlir-commits] [mlir] [mlir][func] Move return-type verification from ReturnOp to FuncOp (PR #184153)
Ingo Müller
llvmlistbot at llvm.org
Tue Mar 3 04:53:58 PST 2026
================
@@ -284,23 +284,37 @@ FuncOp FuncOp::clone() {
// ReturnOp
//===----------------------------------------------------------------------===//
-LogicalResult ReturnOp::verify() {
- auto function = cast<FuncOp>((*this)->getParentOp());
-
- // The operand number and types must match the function signature.
- const auto &results = function.getFunctionType().getResults();
- if (getNumOperands() != results.size())
- return emitOpError("has ")
- << getNumOperands() << " operands, but enclosing function (@"
- << function.getName() << ") returns " << results.size();
-
- for (unsigned i = 0, e = results.size(); i != e; ++i)
- if (getOperand(i).getType() != results[i])
- return emitError() << "type of return operand " << i << " ("
- << getOperand(i).getType()
- << ") doesn't match function result type ("
- << results[i] << ")"
- << " in function @" << function.getName();
+LogicalResult FuncOp::verify() {
+ // External declarations have no body to check.
+ if (isDeclaration())
+ return success();
+ // Hoist the result types once; they are the same for every return site.
+ auto resultTypes = getFunctionType().getResults();
+ for (Block &block : getBody()) {
+ if (block.empty())
+ continue;
+ // Check func.return or other return-like terminators ops (e.g.
+ // llvm.return, test.return).
+ auto returnOp = dyn_cast<RegionBranchTerminatorOpInterface>(&block.back());
----------------
ingomueller-net wrote:
I think that this isn't nice to have -- it's crucial for the verification to be correct. The current version breaks several downstream users with false negatives. One [example](https://github.com/tensorflow/tensorflow/blob/e9b73649/tensorflow/compiler/mlir/tensorflow/tests/tf-reduce-identity.mlir#L4) from Tensorflow:
```mlir
func.func @target_function() -> tensor<i32> {
%0 = "tf_device.cluster"() ({
// ...
tf_device.return %4 : tensor<i32>
}) {...} : () -> tensor<i32>
func.return %0 : tensor<i32>
}
```
The `tf_device.return` is casted to `returnOp` successfully, such that the remaining code verifies properties that don't need to hold.
https://github.com/llvm/llvm-project/pull/184153
More information about the Mlir-commits
mailing list