Skip to content

Commit 529662a

Browse files
[mlir] Allow accessing DialectResourceBlobManager::blobMap (#142352)
Add a new API to access all blobs that are stored in the blob manager. The main purpose (as of now) is to allow users of dialect resources to iterate over all blobs, especially when the blobs are no longer used in IR (e.g. the operation that uses the blob is deleted) and thus cannot be easily accessed without manual tracking of keys.
1 parent 2dcf436 commit 529662a

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

mlir/include/mlir/IR/DialectResourceBlobManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ class DialectResourceBlobManager {
9393
return HandleT(&entry, dialect);
9494
}
9595

96+
/// Provide access to all the registered blobs via a callable. During access
97+
/// the blob map is guaranteed to remain unchanged.
98+
void getBlobMap(llvm::function_ref<void(const llvm::StringMap<BlobEntry> &)>
99+
accessor) const;
100+
96101
private:
97102
/// A mutex to protect access to the blob map.
98-
llvm::sys::SmartRWMutex<true> blobMapLock;
103+
mutable llvm::sys::SmartRWMutex<true> blobMapLock;
99104

100105
/// The internal map of tracked blobs. StringMap stores entries in distinct
101106
/// allocations, so we can freely take references to the data without fear of

mlir/lib/IR/DialectResourceBlobManager.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ auto DialectResourceBlobManager::insert(StringRef name,
6363
nameStorage.resize(name.size() + 1);
6464
} while (true);
6565
}
66+
67+
void DialectResourceBlobManager::getBlobMap(
68+
llvm::function_ref<void(const llvm::StringMap<BlobEntry> &)> accessor)
69+
const {
70+
llvm::sys::SmartScopedReader<true> reader(blobMapLock);
71+
72+
accessor(blobMap);
73+
}

mlir/unittests/IR/BlobManagerTest.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===- mlir/unittest/IR/BlobManagerTest.cpp - Blob management unit tests --===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "../../test/lib/Dialect/Test/TestDialect.h"
10+
#include "mlir/IR/DialectResourceBlobManager.h"
11+
#include "mlir/Parser/Parser.h"
12+
13+
#include "gtest/gtest.h"
14+
15+
using namespace mlir;
16+
17+
namespace {
18+
19+
StringLiteral moduleStr = R"mlir(
20+
"test.use1"() {attr = dense_resource<blob1> : tensor<1xi64> } : () -> ()
21+
22+
{-#
23+
dialect_resources: {
24+
builtin: {
25+
blob1: "0x08000000ABCDABCDABCDABCE"
26+
}
27+
}
28+
#-}
29+
)mlir";
30+
31+
TEST(DialectResourceBlobManagerTest, Lookup) {
32+
MLIRContext context;
33+
context.loadDialect<test::TestDialect>();
34+
35+
OwningOpRef<ModuleOp> m = parseSourceString<ModuleOp>(moduleStr, &context);
36+
ASSERT_TRUE(m);
37+
38+
const auto &dialectManager =
39+
mlir::DenseResourceElementsHandle::getManagerInterface(&context);
40+
ASSERT_NE(dialectManager.getBlobManager().lookup("blob1"), nullptr);
41+
}
42+
43+
TEST(DialectResourceBlobManagerTest, GetBlobMap) {
44+
MLIRContext context;
45+
context.loadDialect<test::TestDialect>();
46+
47+
OwningOpRef<ModuleOp> m = parseSourceString<ModuleOp>(moduleStr, &context);
48+
ASSERT_TRUE(m);
49+
50+
Block *block = m->getBody();
51+
auto &op = block->getOperations().front();
52+
auto resourceAttr = op.getAttrOfType<DenseResourceElementsAttr>("attr");
53+
ASSERT_NE(resourceAttr, nullptr);
54+
55+
const auto &dialectManager =
56+
resourceAttr.getRawHandle().getManagerInterface(&context);
57+
58+
bool blobsArePresent = false;
59+
dialectManager.getBlobManager().getBlobMap(
60+
[&](const llvm::StringMap<DialectResourceBlobManager::BlobEntry>
61+
&blobMap) { blobsArePresent = blobMap.contains("blob1"); });
62+
ASSERT_TRUE(blobsArePresent);
63+
64+
// remove operations that use resources - resources must still be accessible
65+
block->clear();
66+
67+
blobsArePresent = false;
68+
dialectManager.getBlobManager().getBlobMap(
69+
[&](const llvm::StringMap<DialectResourceBlobManager::BlobEntry>
70+
&blobMap) { blobsArePresent = blobMap.contains("blob1"); });
71+
ASSERT_TRUE(blobsArePresent);
72+
}
73+
74+
} // end anonymous namespace

mlir/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_mlir_unittest(MLIRIRTests
1818
TypeAttrNamesTest.cpp
1919
OpPropertiesTest.cpp
2020
ValueTest.cpp
21+
BlobManagerTest.cpp
2122

2223
DEPENDS
2324
MLIRTestInterfaceIncGen

0 commit comments

Comments
 (0)