[Mlir-commits] [mlir] aac9efd - [mlir][Bytecode] Use explicit attribute APIs to split inherent/discardable attr access (#218880)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Aug 26 03:37:39 PDT 2026


Author: Mehdi Amini
Date: 2026-08-26T10:37:34Z
New Revision: aac9efdc468c2b2dd564e4333f11ddd88d04c4ae

URL: https://github.com/llvm/llvm-project/commit/aac9efdc468c2b2dd564e4333f11ddd88d04c4ae
DIFF: https://github.com/llvm/llvm-project/commit/aac9efdc468c2b2dd564e4333f11ddd88d04c4ae.diff

LOG: [mlir][Bytecode] Use explicit attribute APIs to split inherent/discardable attr access (#218880)

Use explicit discardable and inherent attribute access when numbering
and writing bytecode, and update the focused unit coverage.

Assisted-by: Codex

Added: 
    

Modified: 
    mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
    mlir/lib/Bytecode/Writer/IRNumbering.cpp
    mlir/unittests/Bytecode/BytecodeTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
index 90a1de6691d99..89aa731c5cca0 100644
--- a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
+++ b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp
@@ -1015,14 +1015,19 @@ LogicalResult BytecodeWriter::writeOp(EncodingEmitter &emitter, Operation *op) {
   emitter.emitVarInt(numberingState.getNumber(op->getLoc()), "op location");
 
   // Emit the attributes of this operation.
-  DictionaryAttr attrs = op->getDiscardableAttrDictionary();
+  DictionaryAttr attrs = op->getRawDictionaryAttrs();
   // Allow deployment to version <kNativePropertiesEncoding by merging inherent
   // attribute with the discardable ones. We should fail if there are any
   // conflicts. When properties are not used by the op, also store everything as
   // attributes.
-  if (config.bytecodeVersion < bytecode::kNativePropertiesEncoding ||
-      !op->getPropertiesStorage()) {
-    attrs = op->getAttrDictionary();
+  if (config.bytecodeVersion < bytecode::kNativePropertiesEncoding &&
+      op->getPropertiesStorage()) {
+    NamedAttrList allAttrs;
+    op->getName().walkInherentAttrs(op, [&](StringRef name, Attribute &attr) {
+      allAttrs.append(name, attr);
+    });
+    allAttrs.append(op->getDiscardableAttrDictionary().getValue());
+    attrs = allAttrs.getDictionary(op->getContext());
   }
   if (!attrs.empty()) {
     opEncodingMask |= bytecode::OpEncodingMask::kHasAttrs;

diff  --git a/mlir/lib/Bytecode/Writer/IRNumbering.cpp b/mlir/lib/Bytecode/Writer/IRNumbering.cpp
index 04625628fa5a6..dcc87871ead49 100644
--- a/mlir/lib/Bytecode/Writer/IRNumbering.cpp
+++ b/mlir/lib/Bytecode/Writer/IRNumbering.cpp
@@ -446,10 +446,18 @@ void IRNumberingState::number(Operation &op) {
   // not used, we need to number also the merged dictionary containing both the
   // inherent and discardable attribute.
   DictionaryAttr dictAttr;
-  if (config.getDesiredBytecodeVersion() >= bytecode::kNativePropertiesEncoding)
+  if (config.getDesiredBytecodeVersion() >=
+          bytecode::kNativePropertiesEncoding ||
+      !op.getPropertiesStorage())
     dictAttr = op.getRawDictionaryAttrs();
-  else
-    dictAttr = op.getAttrDictionary();
+  else {
+    NamedAttrList attrs;
+    op.getName().walkInherentAttrs(&op, [&](StringRef name, Attribute &attr) {
+      attrs.append(name, attr);
+    });
+    attrs.append(op.getDiscardableAttrDictionary().getValue());
+    dictAttr = attrs.getDictionary(op.getContext());
+  }
   // Only number the operation's dictionary if it isn't empty.
   if (!dictAttr.empty())
     number(dictAttr);

diff  --git a/mlir/unittests/Bytecode/BytecodeTest.cpp b/mlir/unittests/Bytecode/BytecodeTest.cpp
index d8f6510924698..7e11aa14fda44 100644
--- a/mlir/unittests/Bytecode/BytecodeTest.cpp
+++ b/mlir/unittests/Bytecode/BytecodeTest.cpp
@@ -222,7 +222,7 @@ TEST(Bytecode, OpWithoutProperties) {
   ASSERT_TRUE(succeeded(readBytecodeFile(
       llvm::MemoryBufferRef(bytecode, "string-buffer"), block.get(), config)));
   Operation *roundtripped = &block->front();
-  EXPECT_EQ(roundtripped->getAttrs().size(), 2u);
+  EXPECT_EQ(roundtripped->getRawDictionaryAttrs().size(), 2u);
   EXPECT_EQ(roundtripped->getInherentAttr("inherent_attr"), std::nullopt);
   EXPECT_NE(roundtripped->getDiscardableAttr("inherent_attr"), Attribute());
   EXPECT_NE(roundtripped->getDiscardableAttr("other_attr"), Attribute());
@@ -389,7 +389,7 @@ TEST(Bytecode, LocationElisionPreservesAttributes) {
   EXPECT_TRUE(isa<UnknownLoc>(innerOp->getLoc()));
 
   // 2. Verify that the semantic location attribute WAS PRESERVED.
-  Attribute semanticLocAttr = innerOp->getAttr("some_loc_attr");
+  Attribute semanticLocAttr = innerOp->getDiscardableAttr("some_loc_attr");
   ASSERT_TRUE(semanticLocAttr);
   auto locAttr = dyn_cast<LocationAttr>(semanticLocAttr);
   ASSERT_TRUE(locAttr);


        


More information about the Mlir-commits mailing list