Skip to content

Commit dd612d6

Browse files
committed
[mlir][IR] Add getPropertyFromAttr and setPropertyFromAttr methods.
1 parent 4b99eb2 commit dd612d6

File tree

8 files changed

+364
-24
lines changed

8 files changed

+364
-24
lines changed

mlir/include/mlir/IR/ExtensibleDialect.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,17 @@ class DynamicOpDefinition : public OperationName::Impl {
493493
return failure();
494494
}
495495
Attribute getPropertiesAsAttr(Operation *op) final { return {}; }
496+
497+
LogicalResult
498+
setPropertyFromAttr(OperationName opName, OpaqueProperties properties,
499+
StringRef name, Attribute attr,
500+
function_ref<InFlightDiagnostic()> emitError) final {
501+
emitError() << "extensible Dialects don't support properties";
502+
return failure();
503+
}
504+
FailureOr<Attribute> getPropertyAsAttr(Operation *op, StringRef name) final {
505+
return failure();
506+
}
496507
void copyProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {}
497508
bool compareProperties(OpaqueProperties, OpaqueProperties) final { return false; }
498509
llvm::hash_code hashProperties(OpaqueProperties prop) final { return {}; }

mlir/include/mlir/IR/OpDefinition.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,17 @@ class Op : public OpState, public Traits<ConcreteType>... {
17581758
function_ref<InFlightDiagnostic()> emitError) {
17591759
return setPropertiesFromAttribute(prop, attr, emitError);
17601760
}
1761+
/// Convert the provided attribute to a property and assigned it to the
1762+
/// corresponding property. This default implementation forwards to a free
1763+
/// function `setPropertiesFromAttribute` that can be looked up with ADL in
1764+
/// the namespace where the properties are defined. It can also be overridden
1765+
/// in the derived ConcreteOp.
1766+
template <typename PropertiesTy>
1767+
static LogicalResult
1768+
setPropertyFromAttr(PropertiesTy &prop, StringRef name, Attribute attr,
1769+
function_ref<InFlightDiagnostic()> emitError) {
1770+
return setPropertyFromAttribute(prop, name, attr, emitError);
1771+
}
17611772
/// Convert the provided properties to an attribute. This default
17621773
/// implementation forwards to a free function `getPropertiesAsAttribute` that
17631774
/// can be looked up with ADL in the namespace where the properties are
@@ -1767,6 +1778,16 @@ class Op : public OpState, public Traits<ConcreteType>... {
17671778
const PropertiesTy &prop) {
17681779
return getPropertiesAsAttribute(ctx, prop);
17691780
}
1781+
/// Convert the provided named property to an attribute. This default
1782+
/// implementation forwards to a free function `getPropertiesAsAttribute` that
1783+
/// can be looked up with ADL in the namespace where the properties are
1784+
/// defined. It can also be overridden in the derived ConcreteOp.
1785+
template <typename PropertiesTy>
1786+
static FailureOr<Attribute> getPropertyAsAttr(MLIRContext *ctx,
1787+
const PropertiesTy &prop,
1788+
StringRef name) {
1789+
return getPropertyAsAttribute(ctx, prop, name);
1790+
}
17701791
/// Hash the provided properties. This default implementation forwards to a
17711792
/// free function `computeHash` that can be looked up with ADL in the
17721793
/// namespace where the properties are defined. It can also be overridden in

mlir/include/mlir/IR/Operation.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,12 @@ class alignas(8) Operation final
920920
/// operation. Returns an empty attribute if no properties are present.
921921
Attribute getPropertiesAsAttribute();
922922

923+
/// Return a named property converted to an attribute.
924+
/// This is expensive, and mostly useful when dealing with unregistered
925+
/// operations or in language bindings. Returns failure if there's no property
926+
/// under such name.
927+
FailureOr<Attribute> getPropertyAsAttribute(StringRef name);
928+
923929
/// Set the properties from the provided attribute.
924930
/// This is an expensive operation that can fail if the attribute is not
925931
/// matching the expectations of the properties for this operation. This is
@@ -930,6 +936,17 @@ class alignas(8) Operation final
930936
setPropertiesFromAttribute(Attribute attr,
931937
function_ref<InFlightDiagnostic()> emitError);
932938

939+
/// Set a named property from the provided attribute.
940+
/// This is an expensive operation that can fail if the attribute is not
941+
/// matching the expectations of the properties for this operation. This is
942+
/// mostly useful for unregistered operations, used when parsing the
943+
/// generic format, or in language bindings. An optional diagnostic emitter
944+
/// can be passed in for richer errors, if none is passed then behavior is
945+
/// undefined in error case.
946+
LogicalResult
947+
setPropertyFromAttribute(StringRef name, Attribute attr,
948+
function_ref<InFlightDiagnostic()> emitError);
949+
933950
/// Copy properties from an existing other properties object. The two objects
934951
/// must be the same type.
935952
void copyProperties(OpaqueProperties rhs);

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ class OperationName {
139139
setPropertiesFromAttr(OperationName, OpaqueProperties, Attribute,
140140
function_ref<InFlightDiagnostic()> emitError) = 0;
141141
virtual Attribute getPropertiesAsAttr(Operation *) = 0;
142+
virtual LogicalResult
143+
setPropertyFromAttr(OperationName, OpaqueProperties, StringRef name,
144+
Attribute,
145+
function_ref<InFlightDiagnostic()> emitError) = 0;
146+
virtual FailureOr<Attribute> getPropertyAsAttr(Operation *,
147+
StringRef name) = 0;
142148
virtual void copyProperties(OpaqueProperties, OpaqueProperties) = 0;
143149
virtual bool compareProperties(OpaqueProperties, OpaqueProperties) = 0;
144150
virtual llvm::hash_code hashProperties(OpaqueProperties) = 0;
@@ -220,6 +226,11 @@ class OperationName {
220226
setPropertiesFromAttr(OperationName, OpaqueProperties, Attribute,
221227
function_ref<InFlightDiagnostic()> emitError) final;
222228
Attribute getPropertiesAsAttr(Operation *) final;
229+
LogicalResult
230+
setPropertyFromAttr(OperationName, OpaqueProperties, StringRef name,
231+
Attribute,
232+
function_ref<InFlightDiagnostic()> emitError) final;
233+
FailureOr<Attribute> getPropertyAsAttr(Operation *, StringRef name) final;
223234
void copyProperties(OpaqueProperties, OpaqueProperties) final;
224235
bool compareProperties(OpaqueProperties, OpaqueProperties) final;
225236
llvm::hash_code hashProperties(OpaqueProperties) final;
@@ -441,6 +452,20 @@ class OperationName {
441452
emitError);
442453
}
443454

455+
/// Return an op property converted to an Attribute.
456+
FailureOr<Attribute> getOpPropertyAsAttribute(Operation *op,
457+
StringRef name) const {
458+
return getImpl()->getPropertyAsAttr(op, name);
459+
}
460+
461+
/// Define an op property from the provided Attribute.
462+
LogicalResult setOpPropertyFromAttribute(
463+
OperationName opName, OpaqueProperties properties, StringRef name,
464+
Attribute attr, function_ref<InFlightDiagnostic()> emitError) const {
465+
return getImpl()->setPropertyFromAttr(opName, properties, name, attr,
466+
emitError);
467+
}
468+
444469
void copyOpProperties(OpaqueProperties lhs, OpaqueProperties rhs) const {
445470
return getImpl()->copyProperties(lhs, rhs);
446471
}
@@ -650,6 +675,26 @@ class RegisteredOperationName : public OperationName {
650675
}
651676
return {};
652677
}
678+
LogicalResult
679+
setPropertyFromAttr(OperationName opName, OpaqueProperties properties,
680+
StringRef name, Attribute attr,
681+
function_ref<InFlightDiagnostic()> emitError) final {
682+
if constexpr (hasProperties) {
683+
auto p = properties.as<Properties *>();
684+
return ConcreteOp::setPropertyFromAttr(*p, name, attr, emitError);
685+
}
686+
emitError() << "this operation does not support properties";
687+
return failure();
688+
}
689+
FailureOr<Attribute> getPropertyAsAttr(Operation *op,
690+
StringRef name) final {
691+
if constexpr (hasProperties) {
692+
auto concreteOp = cast<ConcreteOp>(op);
693+
return ConcreteOp::getPropertyAsAttr(concreteOp->getContext(),
694+
concreteOp.getProperties(), name);
695+
}
696+
return failure();
697+
}
653698
bool compareProperties(OpaqueProperties lhs, OpaqueProperties rhs) final {
654699
if constexpr (hasProperties) {
655700
return *lhs.as<Properties *>() == *rhs.as<Properties *>();

mlir/lib/IR/MLIRContext.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,20 @@ Attribute
901901
OperationName::UnregisteredOpModel::getPropertiesAsAttr(Operation *op) {
902902
return *op->getPropertiesStorage().as<Attribute *>();
903903
}
904+
LogicalResult OperationName::UnregisteredOpModel::setPropertyFromAttr(
905+
OperationName opName, OpaqueProperties properties, StringRef name,
906+
Attribute attr, function_ref<InFlightDiagnostic()> emitError) {
907+
assert(false &&
908+
"`setPropertyFromAttr` doesn't work with unregistered operations.");
909+
return failure();
910+
}
911+
FailureOr<Attribute>
912+
OperationName::UnregisteredOpModel::getPropertyAsAttr(Operation *op,
913+
StringRef name) {
914+
assert(false &&
915+
"`getPropertyAsAttr` doesn't work with unregistered operations.");
916+
return failure();
917+
}
904918
void OperationName::UnregisteredOpModel::copyProperties(OpaqueProperties lhs,
905919
OpaqueProperties rhs) {
906920
*lhs.as<Attribute *>() = *rhs.as<Attribute *>();

mlir/lib/IR/Operation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ Attribute Operation::getPropertiesAsAttribute() {
351351
return *getPropertiesStorage().as<Attribute *>();
352352
return info->getOpPropertiesAsAttribute(this);
353353
}
354+
FailureOr<Attribute> Operation::getPropertyAsAttribute(StringRef name) {
355+
std::optional<RegisteredOperationName> info = getRegisteredInfo();
356+
assert(info &&
357+
"`getPropertyAsAttribute` only works for registered operations.");
358+
return info->getOpPropertyAsAttribute(this, name);
359+
}
354360
LogicalResult Operation::setPropertiesFromAttribute(
355361
Attribute attr, function_ref<InFlightDiagnostic()> emitError) {
356362
std::optional<RegisteredOperationName> info = getRegisteredInfo();
@@ -361,6 +367,15 @@ LogicalResult Operation::setPropertiesFromAttribute(
361367
return info->setOpPropertiesFromAttribute(
362368
this->getName(), this->getPropertiesStorage(), attr, emitError);
363369
}
370+
LogicalResult Operation::setPropertyFromAttribute(
371+
StringRef name, Attribute attr,
372+
function_ref<InFlightDiagnostic()> emitError) {
373+
std::optional<RegisteredOperationName> info = getRegisteredInfo();
374+
assert(info &&
375+
"`setPropertyFromAttribute` only works for registered operations.");
376+
return info->setOpPropertyFromAttribute(
377+
this->getName(), this->getPropertiesStorage(), name, attr, emitError);
378+
}
364379

365380
void Operation::copyProperties(OpaqueProperties rhs) {
366381
name.copyOpProperties(getPropertiesStorage(), rhs);

0 commit comments

Comments
 (0)