Skip to content

[mlir-tblgen] trim method body to empty with only spaces to avoid crash #139568

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
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
6 changes: 4 additions & 2 deletions mlir/lib/TableGen/Interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ bool InterfaceMethod::isStatic() const {

// Return the body for this method if it has one.
std::optional<StringRef> InterfaceMethod::getBody() const {
auto value = def->getValueAsString("body");
// Trim leading and trailing spaces from the default implementation.
auto value = def->getValueAsString("body").trim();
return value.empty() ? std::optional<StringRef>() : value;
}

// Return the default implementation for this method if it has one.
std::optional<StringRef> InterfaceMethod::getDefaultImplementation() const {
auto value = def->getValueAsString("defaultBody");
// Trim leading and trailing spaces from the default implementation.
auto value = def->getValueAsString("defaultBody").trim();
return value.empty() ? std::optional<StringRef>() : value;
}

Expand Down
45 changes: 45 additions & 0 deletions mlir/test/mlir-tblgen/method-body-with-only-spaces.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: mlir-tblgen --gen-type-interface-decls -I %S/../../include %s | FileCheck %s

include "mlir/IR/OpBase.td"

def TestEmptyMethodBodyTypeInterface : TypeInterface<"TestEmptyMethodBodyTypeInterface"> {
let cppNamespace = "::TestEmptyMethodBodyTypeInterface";
let description = [{
Treat method body with only spaces as empty.
}];
let methods = [
InterfaceMethod<
/*desc=*/ [{ Trim spaces of method body and default implementation }],
/*returnType=*/ "StringRef",
/*methodName=*/ "trimSpaces",
/*args=*/ (ins),
// CHECK-LABEL: StringRef detail::TestEmptyMethodBodyTypeInterfaceInterfaceTraits::Model<ConcreteType>::trimSpaces
// CHECK-SAME: {
// CHECK-NEXT: return (::llvm::cast<ConcreteType>(tablegen_opaque_val)).trimSpaces();
// CHECK-NEXT: }
/*methodBody=*/ [{ }],
/*defaultImpl=*/ [{ return StringRef(); }]
>
];
}

def TestEmptyDefaultImplTypeInterface : TypeInterface<"TestEmptyDefaultImplTypeInterface"> {
let cppNamespace = "::TestEmptyDefaultImplTypeInterface";
let description = [{
Treat default implementation with only spaces as empty.
}];

let methods = [
InterfaceMethod<
/*desc=*/ [{ Trim spaces of default implementation }],
/*returnType=*/ "StringRef",
/*methodName=*/ "trimSpaces",
/*args=*/ (ins),
/*methodBody=*/ [{ return StringRef(); }],
// COM: Don't generate default implementation
// CHECK-NOT: StringRef detail::TestEmptyDefaultImplTypeInterfaceInterfaceTraits::ExternalModel<ConcreteModel, ConcreteType>::trimSpaces
/*defaultImpl=*/ [{
}]
>
];
}