Skip to content

[mlir][IR] Improve error message when return type could not be inferred #112336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mlir/include/mlir/Interfaces/InferTypeOpInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ inferReturnTensorTypes(ArrayRef<ShapedTypeComponents> retComponents,
/// Verifies that the inferred result types match the actual result types for
/// the op. Precondition: op implements InferTypeOpInterface.
LogicalResult verifyInferredResultTypes(Operation *op);

/// Report a fatal error indicating that the result types could not be
/// inferred.
void reportFatalInferReturnTypesError(OperationState &state);
} // namespace detail

namespace OpTrait {
Expand Down
14 changes: 14 additions & 0 deletions mlir/lib/Interfaces/InferTypeOpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,17 @@ LogicalResult mlir::detail::verifyInferredResultTypes(Operation *op) {

return result;
}

void mlir::detail::reportFatalInferReturnTypesError(OperationState &state) {
std::string buffer;
llvm::raw_string_ostream os(buffer);
os << "Failed to infer result type(s):\n";
os << "\"" << state.name << "\"(...) ";
os << state.attributes.getDictionary(state.location.getContext());
os << " : (";
llvm::interleaveComma(state.operands, os,
[&](Value val) { os << val.getType(); });
os << ") -> ( ??? )";
emitRemark(state.location, "location of op");
llvm::report_fatal_error(llvm::StringRef(buffer));
}
5 changes: 5 additions & 0 deletions mlir/test/mlir-tblgen/op-decl-and-defs.td
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ def NS_FOp : NS_Op<"op_with_all_types_constraint",
// CHECK-LABEL: class FOp :
// CHECK: static ::llvm::LogicalResult inferReturnTypes

// DEFS: void FOp::build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::Value a) {
// DEFS: if (::mlir::succeeded(FOp::inferReturnTypes(odsBuilder.getContext(),
// DEFS: else
// DEFS: ::mlir::detail::reportFatalInferReturnTypesError(odsState);

def NS_GOp : NS_Op<"op_with_fixed_return_type", []> {
let arguments = (ins AnyType:$a);
let results = (outs I32:$b);
Expand Down
3 changes: 2 additions & 1 deletion mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2503,7 +2503,8 @@ void OpEmitter::genSeparateArgParamBuilder() {
{1}.regions, inferredReturnTypes)))
{1}.addTypes(inferredReturnTypes);
else
::llvm::report_fatal_error("Failed to infer result type(s).");)",
::mlir::detail::reportFatalInferReturnTypesError({1});
)",
opClass.getClassName(), builderOpState);
return;
}
Expand Down
Loading