-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ODS was still generating the old `Operation::setAttr` hooks for ODS methods for setting attributes, when the backing implementation of the attributes was changed to properties. No idea how this wasn't noticed until now.
@llvm/pr-subscribers-mlir-core @llvm/pr-subscribers-mlir Author: Jeff Niu (Mogball) ChangesODS was still generating the old Full diff: https://github.com/llvm/llvm-project/pull/87688.diff 2 Files Affected:
diff --git a/mlir/test/mlir-tblgen/op-properties.td b/mlir/test/mlir-tblgen/op-properties.td
new file mode 100644
index 00000000000000..a484f68fc4a11e
--- /dev/null
+++ b/mlir/test/mlir-tblgen/op-properties.td
@@ -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
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 3a697520dfad57..843760d57c99d3 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -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;
@@ -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;
}
@@ -1862,13 +1874,25 @@ 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()) {
@@ -1876,8 +1900,10 @@ void OpEmitter::genAttrSetters() {
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);
}
}
|
joker-eph
approved these changes
Apr 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ODS was still generating the old
Operation::setAttr
hooks for ODS methods for setting attributes, when the backing implementation of the attributes was changed to properties. No idea how this wasn't noticed until now.