46 lines · cpp
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-exception6//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 17using namespace mlir;18 19//20// Test that a DistinctAttr that is created on a separate thread does21// not have its storage deleted when the thread joins.22//23TEST(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't35 // want) then trying to access it triggers an assert in Debug mode, and a36 // crash otherwise. Run this in a CrashRecoveryContext to avoid bringing down37 // the whole test suite if this test fails. Additionally, MSAN and/or TSAN38 // 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}46