| 1 |
//=== DistinctAttributeAllocatorTest.cpp - DistinctAttr storage alloc test ===// |
| 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 "gtest/gtest.h" |
| 10 |
|
| 11 |
#include "mlir/IR/Builders.h" |
| 12 |
#include "mlir/IR/BuiltinAttributes.h" |
| 13 |
#include "mlir/IR/MLIRContext.h" |
| 14 |
#include "llvm/Support/CrashRecoveryContext.h" |
| 15 |
#include <thread> |
| 16 |
|
| 17 |
using namespace mlir; |
| 18 |
|
| 19 |
// |
| 20 |
// Test that a DistinctAttr that is created on a separate thread does |
| 21 |
// not have its storage deleted when the thread joins. |
| 22 |
// |
| 23 |
TEST(DistinctAttributeAllocatorTest, TestAttributeWellFormedAfterThreadJoin) { |
| 24 |
MLIRContext ctx; |
| 25 |
OpBuilder builder(&ctx); |
| 26 |
DistinctAttr attr; |
| 27 |
|
| 28 |
std::thread t([&ctx, &attr]() { |
| 29 |
attr = DistinctAttr::create(UnitAttr::get(&ctx)); |
| 30 |
ASSERT_TRUE(attr); |
| 31 |
}); |
| 32 |
t.join(); |
| 33 |
|
| 34 |
// If the attribute storage got deleted after the thread joins (which we don't |
| 35 |
// want) then trying to access it triggers an assert in Debug mode, and a |
| 36 |
// crash otherwise. Run this in a CrashRecoveryContext to avoid bringing down |
| 37 |
// the whole test suite if this test fails. Additionally, MSAN and/or TSAN |
| 38 |
// should raise failures here if the attribute storage was deleted. |
| 39 |
llvm::CrashRecoveryContext crc; |
| 40 |
EXPECT_TRUE(crc.RunSafely([attr]() { (void)attr.getAbstractAttribute(); })); |
| 41 |
EXPECT_TRUE( |
| 42 |
crc.RunSafely([attr]() { (void)*cast<Attribute>(attr).getImpl(); })); |
| 43 |
|
| 44 |
ASSERT_TRUE(attr); |
| 45 |
} |