| 1 | //===- ResourceTest.cpp -----------------------------------------*- C++ -*-===// |
|---|---|
| 2 | // |
| 3 | // This file is licensed 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/TestAttributes.h" |
| 10 | #include "../../test/lib/Dialect/Test/TestDialect.h" |
| 11 | #include "mlir/Parser/Parser.h" |
| 12 | |
| 13 | #include "gmock/gmock.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | namespace { |
| 18 | TEST(MLIRParser, ResourceKeyConflict) { |
| 19 | std::string moduleStr = R"mlir( |
| 20 | "test.use1"() {attr = #test.e1di64_elements<blob1> : tensor<3xi64> } : () -> () |
| 21 | |
| 22 | {-# |
| 23 | dialect_resources: { |
| 24 | test: { |
| 25 | blob1: "0x08000000010000000000000002000000000000000300000000000000" |
| 26 | } |
| 27 | } |
| 28 | #-} |
| 29 | )mlir"; |
| 30 | std::string moduleStr2 = R"mlir( |
| 31 | "test.use2"() {attr = #test.e1di64_elements<blob1> : tensor<3xi64> } : () -> () |
| 32 | |
| 33 | {-# |
| 34 | dialect_resources: { |
| 35 | test: { |
| 36 | blob1: "0x08000000040000000000000005000000000000000600000000000000" |
| 37 | } |
| 38 | } |
| 39 | #-} |
| 40 | )mlir"; |
| 41 | |
| 42 | MLIRContext context; |
| 43 | context.loadDialect<test::TestDialect>(); |
| 44 | |
| 45 | // Parse both modules into the same context so that we ensure the conflicting |
| 46 | // resources have been loaded. |
| 47 | OwningOpRef<ModuleOp> module1 = |
| 48 | parseSourceString<ModuleOp>(sourceStr: moduleStr, config: &context); |
| 49 | OwningOpRef<ModuleOp> module2 = |
| 50 | parseSourceString<ModuleOp>(sourceStr: moduleStr2, config: &context); |
| 51 | ASSERT_TRUE(module1 && module2); |
| 52 | |
| 53 | // Merge the two modules so that we can test printing the remapped resources. |
| 54 | Block *block = module1->getBody(); |
| 55 | block->getOperations().splice(block->end(), |
| 56 | module2->getBody()->getOperations()); |
| 57 | |
| 58 | // Check that conflicting resources were remapped. |
| 59 | std::string outputStr; |
| 60 | { |
| 61 | llvm::raw_string_ostream os(outputStr); |
| 62 | module1->print(os); |
| 63 | } |
| 64 | StringRef output(outputStr); |
| 65 | EXPECT_TRUE( |
| 66 | output.contains("\"test.use1\"() {attr = #test.e1di64_elements<blob1>")); |
| 67 | EXPECT_TRUE(output.contains( |
| 68 | "blob1: \"0x08000000010000000000000002000000000000000300000000000000\"")); |
| 69 | EXPECT_TRUE(output.contains( |
| 70 | "\"test.use2\"() {attr = #test.e1di64_elements<blob1_1>")); |
| 71 | EXPECT_TRUE(output.contains( |
| 72 | "blob1_1: " |
| 73 | "\"0x08000000040000000000000005000000000000000600000000000000\"")); |
| 74 | } |
| 75 | } // namespace |
| 76 |
