-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[mlir] Allow accessing DialectResourceBlobManager::blobMap #142352
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //===- mlir/unittest/IR/BlobManagerTest.cpp - Blob management unit tests --===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "../../test/lib/Dialect/Test/TestDialect.h" | ||
| #include "mlir/IR/DialectResourceBlobManager.h" | ||
| #include "mlir/Parser/Parser.h" | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
|
|
||
| StringLiteral moduleStr = R"mlir( | ||
| "test.use1"() {attr = dense_resource<blob1> : tensor<1xi64> } : () -> () | ||
|
|
||
| {-# | ||
| dialect_resources: { | ||
| builtin: { | ||
| blob1: "0x08000000ABCDABCDABCDABCE" | ||
| } | ||
| } | ||
| #-} | ||
| )mlir"; | ||
|
|
||
| TEST(DialectResourceBlobManagerTest, Lookup) { | ||
| MLIRContext context; | ||
| context.loadDialect<test::TestDialect>(); | ||
|
|
||
| OwningOpRef<ModuleOp> m = parseSourceString<ModuleOp>(moduleStr, &context); | ||
| ASSERT_TRUE(m); | ||
|
|
||
| const auto &dialectManager = | ||
| mlir::DenseResourceElementsHandle::getManagerInterface(&context); | ||
| ASSERT_NE(dialectManager.getBlobManager().lookup("blob1"), nullptr); | ||
| } | ||
|
|
||
| TEST(DialectResourceBlobManagerTest, GetBlobMap) { | ||
| MLIRContext context; | ||
| context.loadDialect<test::TestDialect>(); | ||
|
|
||
| OwningOpRef<ModuleOp> m = parseSourceString<ModuleOp>(moduleStr, &context); | ||
| ASSERT_TRUE(m); | ||
|
|
||
| Block *block = m->getBody(); | ||
| auto &op = block->getOperations().front(); | ||
| auto resourceAttr = op.getAttrOfType<DenseResourceElementsAttr>("attr"); | ||
| ASSERT_NE(resourceAttr, nullptr); | ||
|
|
||
| const auto &dialectManager = | ||
| resourceAttr.getRawHandle().getManagerInterface(&context); | ||
|
|
||
| bool blobsArePresent = false; | ||
| dialectManager.getBlobManager().getBlobMap( | ||
| [&](const llvm::StringMap<DialectResourceBlobManager::BlobEntry> | ||
| &blobMap) { blobsArePresent = blobMap.contains("blob1"); }); | ||
| ASSERT_TRUE(blobsArePresent); | ||
|
|
||
| // remove operations that use resources - resources must still be accessible | ||
|
||
| block->clear(); | ||
|
|
||
| blobsArePresent = false; | ||
| dialectManager.getBlobManager().getBlobMap( | ||
| [&](const llvm::StringMap<DialectResourceBlobManager::BlobEntry> | ||
| &blobMap) { blobsArePresent = blobMap.contains("blob1"); }); | ||
| ASSERT_TRUE(blobsArePresent); | ||
| } | ||
|
|
||
| } // end anonymous namespace | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: the API itself is a bit weird since we need to keep access protected by a mutex. without a larger redesign of this, it seems to be the only option?