-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[mlir][IR] Add getPropertyFromAttr
and setPropertyFromAttr
methods.
#150060
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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core Author: Fabian Mora (fabianmcg) ChangesThis patch adds the The reasoning behind adding these methods is that Also, while it would be possible to use This is also the first step in fixing #150009 . Patch is 24.80 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/150060.diff 8 Files Affected:
diff --git a/mlir/include/mlir/IR/ExtensibleDialect.h b/mlir/include/mlir/IR/ExtensibleDialect.h
index 955faaad9408b..204b3f59db1a5 100644
--- a/mlir/include/mlir/IR/ExtensibleDialect.h
+++ b/mlir/include/mlir/IR/ExtensibleDialect.h
@@ -493,6 +493,17 @@ class DynamicOpDefinition : public OperationName::Impl {
return failure();
}
Attribute getPropertiesAsAttr(Operation *op) final { return {}; }
+
+ LogicalResult
+ setPropertyFromAttr(OperationName opName, OpaqueProperties properties,
+ StringRef name, Attribute attr,
+ function_ref<InFlightDiagnostic()> emitError) final {
+ emitError() << "extensible Dialects don't support properties";
+ return failure();
+ }
+ FailureOr<Attribute> getPropertyAsAttr(Operation *op, StringRef name) final {
+ return {};
+ }
void copyProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {}
bool compareProperties(OpaqueProperties, OpaqueProperties) final { return false; }
llvm::hash_code hashProperties(OpaqueProperties prop) final { return {}; }
diff --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index 883ece32967e4..2e610ec21000c 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -1758,6 +1758,17 @@ class Op : public OpState, public Traits<ConcreteType>... {
function_ref<InFlightDiagnostic()> emitError) {
return setPropertiesFromAttribute(prop, attr, emitError);
}
+ /// Convert the provided attribute to a property and assigned it to the
+ /// corresponding property. This default implementation forwards to a free
+ /// function `setPropertiesFromAttribute` that can be looked up with ADL in
+ /// the namespace where the properties are defined. It can also be overridden
+ /// in the derived ConcreteOp.
+ template <typename PropertiesTy>
+ static LogicalResult
+ setPropertyFromAttr(PropertiesTy &prop, StringRef name, Attribute attr,
+ function_ref<InFlightDiagnostic()> emitError) {
+ return setPropertyFromAttribute(prop, name, attr, emitError);
+ }
/// Convert the provided properties to an attribute. This default
/// implementation forwards to a free function `getPropertiesAsAttribute` that
/// can be looked up with ADL in the namespace where the properties are
@@ -1767,6 +1778,16 @@ class Op : public OpState, public Traits<ConcreteType>... {
const PropertiesTy &prop) {
return getPropertiesAsAttribute(ctx, prop);
}
+ /// Convert the provided named property to an attribute. This default
+ /// implementation forwards to a free function `getPropertiesAsAttribute` that
+ /// can be looked up with ADL in the namespace where the properties are
+ /// defined. It can also be overridden in the derived ConcreteOp.
+ template <typename PropertiesTy>
+ static FailureOr<Attribute> getPropertyAsAttr(MLIRContext *ctx,
+ const PropertiesTy &prop,
+ StringRef name) {
+ return getPropertyAsAttribute(ctx, prop, name);
+ }
/// Hash the provided properties. This default implementation forwards to a
/// free function `computeHash` that can be looked up with ADL in the
/// namespace where the properties are defined. It can also be overridden in
diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h
index fa8a4873572ce..42ab61c055bc3 100644
--- a/mlir/include/mlir/IR/Operation.h
+++ b/mlir/include/mlir/IR/Operation.h
@@ -920,6 +920,12 @@ class alignas(8) Operation final
/// operation. Returns an empty attribute if no properties are present.
Attribute getPropertiesAsAttribute();
+ /// Return a named property converted to an attribute.
+ /// This is expensive, and mostly useful when dealing with unregistered
+ /// operations or in language bindings. Returns failure if there's no property
+ /// under such name.
+ FailureOr<Attribute> getPropertyAsAttribute(StringRef name);
+
/// Set the properties from the provided attribute.
/// This is an expensive operation that can fail if the attribute is not
/// matching the expectations of the properties for this operation. This is
@@ -930,6 +936,17 @@ class alignas(8) Operation final
setPropertiesFromAttribute(Attribute attr,
function_ref<InFlightDiagnostic()> emitError);
+ /// Set a named property from the provided attribute.
+ /// This is an expensive operation that can fail if the attribute is not
+ /// matching the expectations of the properties for this operation. This is
+ /// mostly useful for unregistered operations, used when parsing the
+ /// generic format, or in language bindings. An optional diagnostic emitter
+ /// can be passed in for richer errors, if none is passed then behavior is
+ /// undefined in error case.
+ LogicalResult
+ setPropertyFromAttribute(StringRef name, Attribute attr,
+ function_ref<InFlightDiagnostic()> emitError);
+
/// Copy properties from an existing other properties object. The two objects
/// must be the same type.
void copyProperties(OpaqueProperties rhs);
diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index 1ff7c56ddca38..7aadcca8f1232 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -139,6 +139,12 @@ class OperationName {
setPropertiesFromAttr(OperationName, OpaqueProperties, Attribute,
function_ref<InFlightDiagnostic()> emitError) = 0;
virtual Attribute getPropertiesAsAttr(Operation *) = 0;
+ virtual LogicalResult
+ setPropertyFromAttr(OperationName, OpaqueProperties, StringRef name,
+ Attribute,
+ function_ref<InFlightDiagnostic()> emitError) = 0;
+ virtual FailureOr<Attribute> getPropertyAsAttr(Operation *,
+ StringRef name) = 0;
virtual void copyProperties(OpaqueProperties, OpaqueProperties) = 0;
virtual bool compareProperties(OpaqueProperties, OpaqueProperties) = 0;
virtual llvm::hash_code hashProperties(OpaqueProperties) = 0;
@@ -220,6 +226,11 @@ class OperationName {
setPropertiesFromAttr(OperationName, OpaqueProperties, Attribute,
function_ref<InFlightDiagnostic()> emitError) final;
Attribute getPropertiesAsAttr(Operation *) final;
+ LogicalResult
+ setPropertyFromAttr(OperationName, OpaqueProperties, StringRef name,
+ Attribute,
+ function_ref<InFlightDiagnostic()> emitError) final;
+ FailureOr<Attribute> getPropertyAsAttr(Operation *, StringRef name) final;
void copyProperties(OpaqueProperties, OpaqueProperties) final;
bool compareProperties(OpaqueProperties, OpaqueProperties) final;
llvm::hash_code hashProperties(OpaqueProperties) final;
@@ -441,6 +452,20 @@ class OperationName {
emitError);
}
+ /// Return an op property converted to an Attribute.
+ FailureOr<Attribute> getOpPropertyAsAttribute(Operation *op,
+ StringRef name) const {
+ return getImpl()->getPropertyAsAttr(op, name);
+ }
+
+ /// Define an op property from the provided Attribute.
+ LogicalResult setOpPropertyFromAttribute(
+ OperationName opName, OpaqueProperties properties, StringRef name,
+ Attribute attr, function_ref<InFlightDiagnostic()> emitError) const {
+ return getImpl()->setPropertyFromAttr(opName, properties, name, attr,
+ emitError);
+ }
+
void copyOpProperties(OpaqueProperties lhs, OpaqueProperties rhs) const {
return getImpl()->copyProperties(lhs, rhs);
}
@@ -650,6 +675,26 @@ class RegisteredOperationName : public OperationName {
}
return {};
}
+ LogicalResult
+ setPropertyFromAttr(OperationName opName, OpaqueProperties properties,
+ StringRef name, Attribute attr,
+ function_ref<InFlightDiagnostic()> emitError) final {
+ if constexpr (hasProperties) {
+ auto p = properties.as<Properties *>();
+ return ConcreteOp::setPropertyFromAttr(*p, name, attr, emitError);
+ }
+ emitError() << "this operation does not support properties";
+ return failure();
+ }
+ FailureOr<Attribute> getPropertyAsAttr(Operation *op,
+ StringRef name) final {
+ if constexpr (hasProperties) {
+ auto concreteOp = cast<ConcreteOp>(op);
+ return ConcreteOp::getPropertyAsAttr(concreteOp->getContext(),
+ concreteOp.getProperties(), name);
+ }
+ return failure();
+ }
bool compareProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {
if constexpr (hasProperties) {
return *lhs.as<Properties *>() == *rhs.as<Properties *>();
diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp
index 06ec1c85fb4d5..7b49a945c549b 100644
--- a/mlir/lib/IR/MLIRContext.cpp
+++ b/mlir/lib/IR/MLIRContext.cpp
@@ -901,6 +901,20 @@ Attribute
OperationName::UnregisteredOpModel::getPropertiesAsAttr(Operation *op) {
return *op->getPropertiesStorage().as<Attribute *>();
}
+LogicalResult OperationName::UnregisteredOpModel::setPropertyFromAttr(
+ OperationName opName, OpaqueProperties properties, StringRef name,
+ Attribute attr, function_ref<InFlightDiagnostic()> emitError) {
+ assert(false &&
+ "`setPropertyFromAttr` doesn't work with unregistered operations.");
+ return failure();
+}
+FailureOr<Attribute>
+OperationName::UnregisteredOpModel::getPropertyAsAttr(Operation *op,
+ StringRef name) {
+ assert(false &&
+ "`getPropertyAsAttr` doesn't work with unregistered operations.");
+ return failure();
+}
void OperationName::UnregisteredOpModel::copyProperties(OpaqueProperties lhs,
OpaqueProperties rhs) {
*lhs.as<Attribute *>() = *rhs.as<Attribute *>();
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index 8bcfa465e4a22..edd2efedf5a45 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -351,6 +351,12 @@ Attribute Operation::getPropertiesAsAttribute() {
return *getPropertiesStorage().as<Attribute *>();
return info->getOpPropertiesAsAttribute(this);
}
+FailureOr<Attribute> Operation::getPropertyAsAttribute(StringRef name) {
+ std::optional<RegisteredOperationName> info = getRegisteredInfo();
+ assert(info &&
+ "`getPropertyAsAttribute` only works for registered operations.");
+ return info->getOpPropertyAsAttribute(this, name);
+}
LogicalResult Operation::setPropertiesFromAttribute(
Attribute attr, function_ref<InFlightDiagnostic()> emitError) {
std::optional<RegisteredOperationName> info = getRegisteredInfo();
@@ -361,6 +367,15 @@ LogicalResult Operation::setPropertiesFromAttribute(
return info->setOpPropertiesFromAttribute(
this->getName(), this->getPropertiesStorage(), attr, emitError);
}
+LogicalResult Operation::setPropertyFromAttribute(
+ StringRef name, Attribute attr,
+ function_ref<InFlightDiagnostic()> emitError) {
+ std::optional<RegisteredOperationName> info = getRegisteredInfo();
+ assert(info &&
+ "`setPropertyFromAttribute` only works for registered operations.");
+ return info->setOpPropertyFromAttribute(
+ this->getName(), this->getPropertiesStorage(), name, attr, emitError);
+}
void Operation::copyProperties(OpaqueProperties rhs) {
name.copyOpProperties(getPropertiesStorage(), rhs);
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index f35cfa6826388..31834554cdd43 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -1411,7 +1411,7 @@ void OpEmitter::genPropertiesSupport() {
attrOrProperties.push_back(&emitHelper.getOperandSegmentsSize().value());
if (emitHelper.getResultSegmentsSize())
attrOrProperties.push_back(&emitHelper.getResultSegmentsSize().value());
- auto &setPropMethod =
+ auto &setPropsMethod =
opClass
.addStaticMethod(
"::llvm::LogicalResult", "setPropertiesFromAttr",
@@ -1421,12 +1421,31 @@ void OpEmitter::genPropertiesSupport() {
"::llvm::function_ref<::mlir::InFlightDiagnostic()>",
"emitError"))
->body();
- auto &getPropMethod =
+ auto &getPropsMethod =
opClass
.addStaticMethod("::mlir::Attribute", "getPropertiesAsAttr",
MethodParameter("::mlir::MLIRContext *", "ctx"),
MethodParameter("const Properties &", "prop"))
->body();
+ auto &setPropMethod =
+ opClass
+ .addStaticMethod(
+ "::llvm::LogicalResult", "setPropertyFromAttr",
+ MethodParameter("Properties &", "prop"),
+ MethodParameter("llvm::StringRef", "name"),
+ MethodParameter("::mlir::Attribute", "attr"),
+ MethodParameter(
+ "::llvm::function_ref<::mlir::InFlightDiagnostic()>",
+ "emitError"))
+ ->body();
+ auto &getPropMethod =
+ opClass
+ .addStaticMethod("llvm::FailureOr<::mlir::Attribute>",
+ "getPropertyAsAttr",
+ MethodParameter("::mlir::MLIRContext *", "ctx"),
+ MethodParameter("const Properties &", "prop"),
+ MethodParameter("llvm::StringRef", "name"))
+ ->body();
auto &hashMethod =
opClass
.addStaticMethod("llvm::hash_code", "computePropertiesHash",
@@ -1468,7 +1487,7 @@ void OpEmitter::genPropertiesSupport() {
// Convert the property to the attribute form.
- setPropMethod << R"decl(
+ setPropsMethod << R"decl(
::mlir::DictionaryAttr dict = ::llvm::dyn_cast<::mlir::DictionaryAttr>(attr);
if (!dict) {
emitError() << "expected DictionaryAttr to set properties";
@@ -1480,9 +1499,9 @@ void OpEmitter::genPropertiesSupport() {
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) -> ::mlir::LogicalResult {{
{0}
};
- {1};
+ {1}
)decl";
- const char *attrGetNoDefaultFmt = R"decl(;
+ const char *attrGetNoDefaultFmt = R"decl(
if (attr && ::mlir::failed(setFromAttr(prop.{0}, attr, emitError)))
return ::mlir::failure();
)decl";
@@ -1515,22 +1534,33 @@ void OpEmitter::genPropertiesSupport() {
}
fctx.withBuilder(odsBuilder);
- setPropMethod << "{\n"
+ setPropsMethod << "{\n"
+ << formatv(
+ propFromAttrFmt,
+ tgfmt(prop.getConvertFromAttributeCall(),
+ &fctx.addSubst("_attr", propertyAttr)
+ .addSubst("_storage", propertyStorage)
+ .addSubst("_diag", propertyDiag)),
+ getAttr);
+ if (prop.hasStorageTypeValueOverride()) {
+ setPropsMethod << formatv(attrGetDefaultFmt, name,
+ prop.getStorageTypeValueOverride());
+ } else if (prop.hasDefaultValue()) {
+ setPropsMethod << formatv(attrGetDefaultFmt, name,
+ tgfmt(prop.getDefaultValue(), &fctx));
+ } else {
+ setPropsMethod << formatv(attrGetNoDefaultFmt, name);
+ }
+ setPropsMethod << " }\n";
+ setPropMethod << formatv(" if (name == \"{0}\") {{", name)
<< formatv(propFromAttrFmt,
tgfmt(prop.getConvertFromAttributeCall(),
&fctx.addSubst("_attr", propertyAttr)
.addSubst("_storage", propertyStorage)
.addSubst("_diag", propertyDiag)),
- getAttr);
- if (prop.hasStorageTypeValueOverride()) {
- setPropMethod << formatv(attrGetDefaultFmt, name,
- prop.getStorageTypeValueOverride());
- } else if (prop.hasDefaultValue()) {
- setPropMethod << formatv(attrGetDefaultFmt, name,
- tgfmt(prop.getDefaultValue(), &fctx));
- } else {
- setPropMethod << formatv(attrGetNoDefaultFmt, name);
- }
+ "");
+ setPropMethod << formatv(attrGetNoDefaultFmt, name);
+ setPropMethod << " return ::mlir::success();\n";
setPropMethod << " }\n";
} else {
const auto *namedAttr =
@@ -1548,7 +1578,7 @@ void OpEmitter::genPropertiesSupport() {
os << " if (!attr) attr = dict.get(\"result_segment_sizes\");";
}
- setPropMethod << formatv(R"decl(
+ setPropsMethod << formatv(R"decl(
{{
auto &propStorage = prop.{0};
{1}
@@ -1563,16 +1593,38 @@ void OpEmitter::genPropertiesSupport() {
}
}
)decl",
- name, getAttr);
+ name, getAttr);
+ setPropMethod << formatv(R"decl(
+ {{
+ auto &propStorage = prop.{0};
+ if (attr == nullptr) {{
+ propStorage = nullptr;
+ return ::mlir::success();
+ }
+ auto convertedAttr = ::llvm::dyn_cast<std::remove_reference_t<decltype(propStorage)>>(attr);
+ if (convertedAttr) {{
+ propStorage = convertedAttr;
+ return ::mlir::success();
+ } else {{
+ emitError() << "Invalid attribute `{0}` in property conversion: " << attr;
+ return ::mlir::failure();
}
}
- setPropMethod << " return ::mlir::success();\n";
+)decl",
+ name);
+ }
+ }
+ setPropsMethod << " return ::mlir::success();\n";
+ setPropMethod << " return emitError() << \"`\" << name << \"` is not an op "
+ "property\";\n";
// Convert the attribute form to the property.
- getPropMethod << " ::mlir::SmallVector<::mlir::NamedAttribute> attrs;\n"
- << " ::mlir::Builder odsBuilder{ctx};\n";
- const char *propToAttrFmt = R"decl(
+ getPropsMethod << " ::mlir::SmallVector<::mlir::NamedAttribute> attrs;\n"
+ << " ::mlir::Builder odsBuilder{ctx};\n";
+ getPropMethod << " ::mlir::Builder odsBuilder{ctx};\n"
+ << " (void)odsBuilder;\n";
+ const char *propsToAttrFmt = R"decl(
{
const auto &propStorage = prop.{0};
auto attr = [&]() -> ::mlir::Attribute {{
@@ -1580,6 +1632,15 @@ void OpEmitter::genPropertiesSupport() {
}();
attrs.push_back(odsBuilder.getNamedAttr("{0}", attr));
}
+)decl";
+ const char *propToAttrFmt = R"decl(
+ if (name == "{0}") {
+ const auto &propStorage = prop.{0};
+ auto attr = [&]() -> ::mlir::Attribute {{
+ {1}
+ }();
+ return attr;
+ }
)decl";
for (const auto &attrOrProp : attrOrProperties) {
if (const auto *namedProperty =
@@ -1587,6 +1648,11 @@ void OpEmitter::genPropertiesSupport() {
StringRef name = namedProperty->name;
auto &prop = namedProperty->prop;
FmtContext fctx;
+ getPropsMethod << formatv(
+ propsToAttrFmt, name,
+ tgfmt(prop.getConvertToAttributeCall(),
+ &fctx.addSubst("_ctxt", "ctx")
+ .addSubst("_storage", propertyStorage)));
getPropMethod << formatv(
propToAttrFmt, name,
tgfmt(prop.getConvertToAttributeCall(),
@@ -1597,21 +1663,28 @@ void OpEmitter::genPropertiesSupport() {
const auto *namedAttr =
llvm::dyn_cast_if_present<const AttributeMetadata *>(attrOrProp);
StringRef name = namedAttr->attrName;
- getPropMethod << formatv(R"decl(
+ getPropsMethod << formatv(R"decl(
{{
const auto &propStorage = prop.{0};
if (propStorage)
attrs.push_back(odsBuilder.getNamedAttr("{0}",
propStorage));
}
+)decl",
+ name);
+ getPropMethod << formatv(R"decl(
+ if (name == "{0}") {...
[truncated]
|
c912836
to
dd612d6
Compare
This patch adds the
getPropertyFromAttr
andsetPropertyFromAttr
methods to various places. These methods are similar togetPropertiesFromAttr
andsetPropertiesFromAttr
, in that they get or set a property to or from an attr, but they set or get props based on a name.The reasoning behind adding these methods is that
getPropertiesFromAttr
andsetPropertiesFromAttr
are meant for getting and setting collections of props, which is not always desirable specially in language bindings like the python bindings, and can be wasteful.Also, while it would be possible to use
setPropertiesFromAttr
to set a single prop, the method introduces additional logic to set default values if the value is not present, which is not something desirable once the op has been created. Further,getPropertiesFromAttr
always converts all properties, which might be a wasteful process for large props.This is also the first step in fixing #150009 .