1 //===--- ThreadSafeModuleTest.cpp - Test basic use of ThreadSafeModule ----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
10 #include "gtest/gtest.h"
17 using namespace llvm::orc
;
21 TEST(ThreadSafeModuleTest
, ContextWhollyOwnedByOneModule
) {
22 // Test that ownership of a context can be transferred to a single
24 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
25 auto M
= std::make_unique
<Module
>("M", *TSCtx
.getContext());
26 ThreadSafeModule
TSM(std::move(M
), std::move(TSCtx
));
29 TEST(ThreadSafeModuleTest
, ContextOwnershipSharedByTwoModules
) {
30 // Test that ownership of a context can be shared between more than one
32 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
34 auto M1
= std::make_unique
<Module
>("M1", *TSCtx
.getContext());
35 ThreadSafeModule
TSM1(std::move(M1
), TSCtx
);
37 auto M2
= std::make_unique
<Module
>("M2", *TSCtx
.getContext());
38 ThreadSafeModule
TSM2(std::move(M2
), std::move(TSCtx
));
41 TEST(ThreadSafeModuleTest
, ContextOwnershipSharedWithClient
) {
42 // Test that ownership of a context can be shared with a client-held
43 // ThreadSafeContext so that it can be re-used for new modules.
44 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
47 // Create and destroy a module.
48 auto M1
= std::make_unique
<Module
>("M1", *TSCtx
.getContext());
49 ThreadSafeModule
TSM1(std::move(M1
), TSCtx
);
52 // Verify that the context is still available for re-use.
53 auto M2
= std::make_unique
<Module
>("M2", *TSCtx
.getContext());
54 ThreadSafeModule
TSM2(std::move(M2
), std::move(TSCtx
));
57 TEST(ThreadSafeModuleTest
, ThreadSafeModuleMoveAssignment
) {
58 // Move assignment needs to move the module before the context (opposite
59 // to the field order) to ensure that overwriting with an empty
60 // ThreadSafeModule does not destroy the context early.
61 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
62 auto M
= std::make_unique
<Module
>("M", *TSCtx
.getContext());
63 ThreadSafeModule
TSM(std::move(M
), std::move(TSCtx
));
64 TSM
= ThreadSafeModule();
67 TEST(ThreadSafeModuleTest
, BasicContextLockAPI
) {
68 // Test that basic lock API calls work.
69 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
70 auto M
= std::make_unique
<Module
>("M", *TSCtx
.getContext());
71 ThreadSafeModule
TSM(std::move(M
), TSCtx
);
73 { auto L
= TSCtx
.getLock(); }
75 { auto L
= TSM
.getContext().getLock(); }
78 TEST(ThreadSafeModuleTest
, ContextLockPreservesContext
) {
79 // Test that the existence of a context lock preserves the attached
81 // The trick to verify this is a bit of a hack: We attach a Module
82 // (without the ThreadSafeModule wrapper) to the context, then verify
83 // that this Module destructs safely (which it will not if its context
84 // has been destroyed) even though all references to the context have
85 // been thrown away (apart from the lock).
87 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
88 auto L
= TSCtx
.getLock();
89 auto &Ctx
= *TSCtx
.getContext();
90 auto M
= std::make_unique
<Module
>("M", Ctx
);
91 TSCtx
= ThreadSafeContext();
94 TEST(ThreadSafeModuleTest
, WithModuleDo
) {
95 // Test non-const version of withModuleDo.
96 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
97 ThreadSafeModule
TSM(std::make_unique
<Module
>("M", *TSCtx
.getContext()),
99 TSM
.withModuleDo([](Module
&M
) {});
102 TEST(ThreadSafeModuleTest
, WithModuleDoConst
) {
103 // Test const version of withModuleDo.
104 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
105 const ThreadSafeModule
TSM(std::make_unique
<Module
>("M", *TSCtx
.getContext()),
107 TSM
.withModuleDo([](const Module
&M
) {});
110 TEST(ThreadSafeModuleTest
, ConsumingModuleDo
) {
111 // Test consumingModuleDo.
112 ThreadSafeContext
TSCtx(std::make_unique
<LLVMContext
>());
113 ThreadSafeModule
TSM(std::make_unique
<Module
>("M", *TSCtx
.getContext()),
115 TSM
.consumingModuleDo([](std::unique_ptr
<Module
> M
) {});
118 } // end anonymous namespace