Skip to content

[mlir][ods] Fix attribute setter gen when properties are on #87688

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 1 commit into from
Apr 4, 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
21 changes: 21 additions & 0 deletions mlir/test/mlir-tblgen/op-properties.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: mlir-tblgen -gen-op-defs -I %S/../../include %s | FileCheck %s

include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/EnumAttr.td"
include "mlir/IR/OpBase.td"

def Test_Dialect : Dialect {
let name = "test";
let cppNamespace = "foobar";
}
class NS_Op<string mnemonic, list<Trait> traits = []> :
Op<Test_Dialect, mnemonic, traits>;

def OpWithAttr : NS_Op<"op_with_attr">{
let arguments = (ins AnyAttr:$attr, OptionalAttr<AnyAttr>:$optional);
}

// CHECK: void OpWithAttr::setAttrAttr(::mlir::Attribute attr)
// CHECK-NEXT: getProperties().attr = attr
// CHECK: void OpWithAttr::setOptionalAttr(::mlir::Attribute attr)
// CHECK-NEXT: getProperties().optional = attr
52 changes: 39 additions & 13 deletions mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1804,23 +1804,36 @@ void OpEmitter::genAttrGetters() {
}

void OpEmitter::genAttrSetters() {
bool useProperties = op.getDialect().usePropertiesForAttributes();

// Generate the code to set an attribute.
auto emitSetAttr = [&](Method *method, StringRef getterName,
StringRef attrName, StringRef attrVar) {
if (useProperties) {
method->body() << formatv(" getProperties().{0} = {1};", attrName,
attrVar);
} else {
method->body() << formatv(" (*this)->setAttr({0}AttrName(), {1});",
getterName, attrVar);
}
};

// Generate raw named setter type. This is a wrapper class that allows setting
// to the attributes via setters instead of having to use the string interface
// for better compile time verification.
auto emitAttrWithStorageType = [&](StringRef setterName, StringRef getterName,
Attribute attr) {
StringRef attrName, Attribute attr) {
auto *method =
opClass.addMethod("void", setterName + "Attr",
MethodParameter(attr.getStorageType(), "attr"));
if (method)
method->body() << formatv(" (*this)->setAttr({0}AttrName(), attr);",
getterName);
emitSetAttr(method, getterName, attrName, "attr");
};

// Generate a setter that accepts the underlying C++ type as opposed to the
// attribute type.
auto emitAttrWithReturnType = [&](StringRef setterName, StringRef getterName,
Attribute attr) {
StringRef attrName, Attribute attr) {
Attribute baseAttr = attr.getBaseAttr();
if (!canUseUnwrappedRawValue(baseAttr))
return;
Expand Down Expand Up @@ -1849,9 +1862,8 @@ void OpEmitter::genAttrSetters() {

// If the value isn't optional, just set it directly.
if (!isOptional) {
method->body() << formatv(
" (*this)->setAttr({0}AttrName(), {1});", getterName,
constBuildAttrFromParam(attr, fctx, "attrValue"));
emitSetAttr(method, getterName, attrName,
constBuildAttrFromParam(attr, fctx, "attrValue"));
return;
}

Expand All @@ -1862,22 +1874,36 @@ void OpEmitter::genAttrSetters() {
// optional but not in the same way as the others (i.e. it uses bool over
// std::optional<>).
StringRef paramStr = isUnitAttr ? "attrValue" : "*attrValue";
const char *optionalCodeBody = R"(
if (!useProperties) {
const char *optionalCodeBody = R"(
if (attrValue)
return (*this)->setAttr({0}AttrName(), {1});
(*this)->removeAttr({0}AttrName());)";
method->body() << formatv(
optionalCodeBody, getterName,
constBuildAttrFromParam(baseAttr, fctx, paramStr));
method->body() << formatv(
optionalCodeBody, getterName,
constBuildAttrFromParam(baseAttr, fctx, paramStr));
} else {
const char *optionalCodeBody = R"(
auto &odsProp = getProperties().{0};
if (attrValue)
odsProp = {1};
else
odsProp = nullptr;)";
method->body() << formatv(
optionalCodeBody, attrName,
constBuildAttrFromParam(baseAttr, fctx, paramStr));
}
};

for (const NamedAttribute &namedAttr : op.getAttributes()) {
if (namedAttr.attr.isDerivedAttr())
continue;
std::string setterName = op.getSetterName(namedAttr.name);
std::string getterName = op.getGetterName(namedAttr.name);
emitAttrWithStorageType(setterName, getterName, namedAttr.attr);
emitAttrWithReturnType(setterName, getterName, namedAttr.attr);
emitAttrWithStorageType(setterName, getterName, namedAttr.name,
namedAttr.attr);
emitAttrWithReturnType(setterName, getterName, namedAttr.name,
namedAttr.attr);
}
}

Expand Down