Skip to content

Commit 1305c2d

Browse files
committed
Add ReturnLike check for FunctionOpInterface
1 parent 3e34c1d commit 1305c2d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

mlir/include/mlir/Interfaces/FunctionInterfaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mlir/IR/SymbolTable.h"
2121
#include "mlir/IR/TypeUtilities.h"
2222
#include "mlir/Interfaces/CallInterfaces.h"
23+
#include "mlir/Interfaces/ControlFlowInterfaces.h"
2324
#include "llvm/ADT/BitVector.h"
2425
#include "llvm/ADT/SmallString.h"
2526

mlir/include/mlir/Interfaces/FunctionInterfaces.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface", [
108108
}
109109
}
110110

111+
// FunctionOpInterface is tied to a ReturnLike.
112+
Operation *terminator = entryBlock.getTerminator();
113+
if (terminator->hasTrait<OpTrait::ReturnLike>()) {
114+
return $_op.emitOpError("The body of a FunctionOpInterface must")
115+
<< "have a ReturnLike terminator.";
116+
}
117+
118+
// Match ReturnLike's operand types and FunctionOpInterface's
119+
// result types.
120+
auto returnOperandTypes = terminator->getOperandTypes();
121+
auto funcResultTypes = $_op->getResultTypes();
122+
if (funcResultTypes.size() != returnOperandTypes.size()) {
123+
return $_op.emitOpError("The number of a FunctionOpInterface's")
124+
<< "result must match that of the ReturnLike operands.";
125+
}
126+
127+
for (unsigned i = 0; i < funcResultTypes.size(); ++i) {
128+
if (funcResultTypes[i] != returnOperandTypes[i]) {
129+
return $_op.emitOpError("The result types of a FunctionOpInterface")
130+
<< "must match the operand types of the ReturnLike.";
131+
}
132+
}
133+
111134
return success();
112135
}]>,
113136
InterfaceMethod<[{

0 commit comments

Comments
 (0)