Skip to content

[mlir][IR] Add getPropertyAsAttr 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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions mlir/include/mlir/IR/ExtensibleDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 failure();
}
void copyProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {}
bool compareProperties(OpaqueProperties, OpaqueProperties) final { return false; }
llvm::hash_code hashProperties(OpaqueProperties prop) final { return {}; }
Expand Down
21 changes: 21 additions & 0 deletions mlir/include/mlir/IR/OpDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions mlir/include/mlir/IR/Operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
45 changes: 45 additions & 0 deletions mlir/include/mlir/IR/OperationSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 *>();
Expand Down
14 changes: 14 additions & 0 deletions mlir/lib/IR/MLIRContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *>();
Expand Down
15 changes: 15 additions & 0 deletions mlir/lib/IR/Operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
Loading