Skip to content

[mlir] fix Operation::getDiscardableAttrs in absence of properties #76816

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
merged 3 commits into from
Jan 3, 2024
Merged
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
30 changes: 22 additions & 8 deletions mlir/include/mlir/IR/Operation.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,19 +475,33 @@ class alignas(8) Operation final
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();
Expand Down
6 changes: 4 additions & 2 deletions mlir/lib/CAPI/IR/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())};
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << " : ";
Expand Down
40 changes: 39 additions & 1 deletion mlir/unittests/IR/OpPropertiesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "mlir/IR/Attributes.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/Parser/Parser.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -132,6 +133,23 @@ class OpWithProperties : public Op<OpWithProperties> {
}
};

/// 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:
Expand All @@ -142,7 +160,7 @@ class TestOpPropertiesDialect : public Dialect {
explicit TestOpPropertiesDialect(MLIRContext *context)
: Dialect(getDialectNamespace(), context,
TypeID::get<TestOpPropertiesDialect>()) {
addOperations<OpWithProperties>();
addOperations<OpWithProperties, OpWithoutProperties>();
}
};

Expand Down Expand Up @@ -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