Skip to content

[mlir] Fix allocateAndCopyWithAlign for immutable #108679

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 1 commit into from
Oct 11, 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
9 changes: 8 additions & 1 deletion mlir/include/mlir/IR/AsmState.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class AsmStateImpl;
//===----------------------------------------------------------------------===//
// Resource Entry

class HeapAsmResourceBlob;

/// This class represents a processed binary blob of data. A resource blob is
/// essentially a collection of data, potentially mutable, with an associated
/// deleter function (used if the data needs to be destroyed).
Expand Down Expand Up @@ -177,6 +179,8 @@ class AsmResourceBlob {

/// Whether the data is mutable.
bool dataIsMutable;

friend class HeapAsmResourceBlob;
};

/// This class provides a simple utility wrapper for creating heap allocated
Expand All @@ -196,8 +200,11 @@ class HeapAsmResourceBlob {
static AsmResourceBlob allocateAndCopyWithAlign(ArrayRef<char> data,
size_t align,
bool dataIsMutable = true) {
AsmResourceBlob blob = allocate(data.size(), align, dataIsMutable);
// This sets the blob to be mutable initially to allow writing
// (getMutableData) below.
AsmResourceBlob blob = allocate(data.size(), align, /*dataIsMutable=*/true);
std::memcpy(blob.getMutableData().data(), data.data(), data.size());
blob.dataIsMutable = dataIsMutable;
return blob;
}
template <typename T>
Expand Down
15 changes: 15 additions & 0 deletions mlir/unittests/IR/AttributeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ TEST(DenseResourceElementsAttrTest, CheckNoCast) {
EXPECT_FALSE(isa<DenseBoolResourceElementsAttr>(i32ResourceAttr));
}

TEST(DenseResourceElementsAttrTest, CheckNotMutableAllocateAndCopy) {
MLIRContext context;
Builder builder(&context);

// Create a i32 attribute.
std::vector<int32_t> data = {10, 20, 30};
auto type = RankedTensorType::get(data.size(), builder.getI32Type());
Attribute i32ResourceAttr = DenseI32ResourceElementsAttr::get(
type, "resource",
HeapAsmResourceBlob::allocateAndCopyInferAlign<int32_t>(
data, /*is_mutable=*/false));

EXPECT_TRUE(isa<DenseI32ResourceElementsAttr>(i32ResourceAttr));
}

TEST(DenseResourceElementsAttrTest, CheckInvalidData) {
MLIRContext context;
Builder builder(&context);
Expand Down
Loading