[mlir] fix Operation::getDiscardableAttrs in absence of properties (#76816)
When properties are not enabled in an operation, inherent attributes are stored in the common dictionary with discardable attributes. However, `getDiscardableAttrs` and `getDiscardableAttrDictionary` were returning the entire dictionary, making the caller mistakenly believe that all inherent attributes are discardable. Fix this by filtering out attributes whose names are registered with the operation, i.e., inherent attributes. This requires an API change so `getDiscardableAttrs` returns a filter range.
This commit is contained in:
committed by
GitHub
parent
82e33d6203
commit
c1eab57673
@@ -475,19 +475,33 @@ public:
|
||||
return removeDiscardableAttr(StringAttr::get(getContext(), name));
|
||||
}
|
||||
|
||||
/// Return all of the discardable attributes on this operation.
|
||||
ArrayRef<NamedAttribute> getDiscardableAttrs() { return attrs.getValue(); }
|
||||
/// Return a range of all of discardable attributes on this operation. Note
|
||||
/// that for unregistered operations that are not storing inherent attributes
|
||||
/// as properties, all attributes are considered discardable.
|
||||
auto getDiscardableAttrs() {
|
||||
std::optional<RegisteredOperationName> opName = getRegisteredInfo();
|
||||
ArrayRef<StringAttr> attributeNames =
|
||||
opName ? getRegisteredInfo()->getAttributeNames()
|
||||
: ArrayRef<StringAttr>();
|
||||
return llvm::make_filter_range(
|
||||
attrs.getValue(),
|
||||
[this, attributeNames](const NamedAttribute attribute) {
|
||||
return getPropertiesStorage() ||
|
||||
!llvm::is_contained(attributeNames, attribute.getName());
|
||||
});
|
||||
}
|
||||
|
||||
/// Return all of the discardable attributes on this operation as a
|
||||
/// DictionaryAttr.
|
||||
DictionaryAttr getDiscardableAttrDictionary() { return attrs; }
|
||||
DictionaryAttr getDiscardableAttrDictionary() {
|
||||
if (getPropertiesStorage())
|
||||
return attrs;
|
||||
return DictionaryAttr::get(getContext(),
|
||||
llvm::to_vector(getDiscardableAttrs()));
|
||||
}
|
||||
|
||||
/// Return all of the attributes on this operation.
|
||||
ArrayRef<NamedAttribute> getAttrs() {
|
||||
if (!getPropertiesStorage())
|
||||
return getDiscardableAttrs();
|
||||
return getAttrDictionary().getValue();
|
||||
}
|
||||
ArrayRef<NamedAttribute> getAttrs() { return getAttrDictionary().getValue(); }
|
||||
|
||||
/// Return all of the attributes on this operation as a DictionaryAttr.
|
||||
DictionaryAttr getAttrDictionary();
|
||||
|
||||
@@ -613,12 +613,14 @@ void mlirOperationSetInherentAttributeByName(MlirOperation op,
|
||||
}
|
||||
|
||||
intptr_t mlirOperationGetNumDiscardableAttributes(MlirOperation op) {
|
||||
return static_cast<intptr_t>(unwrap(op)->getDiscardableAttrs().size());
|
||||
return static_cast<intptr_t>(
|
||||
llvm::range_size(unwrap(op)->getDiscardableAttrs()));
|
||||
}
|
||||
|
||||
MlirNamedAttribute mlirOperationGetDiscardableAttribute(MlirOperation op,
|
||||
intptr_t pos) {
|
||||
NamedAttribute attr = unwrap(op)->getDiscardableAttrs()[pos];
|
||||
NamedAttribute attr =
|
||||
*std::next(unwrap(op)->getDiscardableAttrs().begin(), pos);
|
||||
return MlirNamedAttribute{wrap(attr.getName()), wrap(attr.getValue())};
|
||||
}
|
||||
|
||||
|
||||
@@ -3543,7 +3543,7 @@ void OperationPrinter::printGenericOp(Operation *op, bool printOpName) {
|
||||
}
|
||||
|
||||
auto attrs = op->getDiscardableAttrs();
|
||||
printOptionalAttrDict(attrs);
|
||||
printOptionalAttrDict(llvm::to_vector(attrs));
|
||||
|
||||
// Print the type signature of the operation.
|
||||
os << " : ";
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "mlir/IR/Attributes.h"
|
||||
#include "mlir/IR/OpDefinition.h"
|
||||
#include "mlir/Parser/Parser.h"
|
||||
#include "gtest/gtest.h"
|
||||
@@ -132,6 +133,23 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/// A custom operation for the purpose of showcasing how discardable attributes
|
||||
/// are handled in absence of properties.
|
||||
class OpWithoutProperties : public Op<OpWithoutProperties> {
|
||||
public:
|
||||
// Begin boilerplate.
|
||||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(OpWithoutProperties)
|
||||
using Op::Op;
|
||||
static ArrayRef<StringRef> getAttributeNames() {
|
||||
static StringRef attributeNames[] = {StringRef("inherent_attr")};
|
||||
return ArrayRef(attributeNames);
|
||||
};
|
||||
static StringRef getOperationName() {
|
||||
return "test_op_properties.op_without_properties";
|
||||
}
|
||||
// End boilerplate.
|
||||
};
|
||||
|
||||
// A trivial supporting dialect to register the above operation.
|
||||
class TestOpPropertiesDialect : public Dialect {
|
||||
public:
|
||||
@@ -142,7 +160,7 @@ public:
|
||||
explicit TestOpPropertiesDialect(MLIRContext *context)
|
||||
: Dialect(getDialectNamespace(), context,
|
||||
TypeID::get<TestOpPropertiesDialect>()) {
|
||||
addOperations<OpWithProperties>();
|
||||
addOperations<OpWithProperties, OpWithoutProperties>();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -359,4 +377,24 @@ TEST(OpPropertiesTest, getOrAddProperties) {
|
||||
op->erase();
|
||||
}
|
||||
|
||||
constexpr StringLiteral withoutPropertiesAttrsSrc = R"mlir(
|
||||
"test_op_properties.op_without_properties"()
|
||||
{inherent_attr = 42, other_attr = 56} : () -> ()
|
||||
)mlir";
|
||||
|
||||
TEST(OpPropertiesTest, withoutPropertiesDiscardableAttrs) {
|
||||
MLIRContext context;
|
||||
context.getOrLoadDialect<TestOpPropertiesDialect>();
|
||||
ParserConfig config(&context);
|
||||
OwningOpRef<Operation *> op =
|
||||
parseSourceString(withoutPropertiesAttrsSrc, config);
|
||||
ASSERT_EQ(llvm::range_size(op->getDiscardableAttrs()), 1u);
|
||||
EXPECT_EQ(op->getDiscardableAttrs().begin()->getName().getValue(),
|
||||
"other_attr");
|
||||
|
||||
EXPECT_EQ(op->getAttrs().size(), 2u);
|
||||
EXPECT_TRUE(op->getInherentAttr("inherent_attr") != std::nullopt);
|
||||
EXPECT_TRUE(op->getDiscardableAttr("other_attr") != Attribute());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user