| 1 |
//===--- ThreadSafeModuleTest.cpp - Test basic use of ThreadSafeModule ----===// |
| 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 "llvm/ExecutionEngine/Orc/ThreadSafeModule.h" |
| 10 |
#include "llvm/IR/LLVMContext.h" |
| 11 |
#include "llvm/IR/Module.h" |
| 12 |
#include "llvm/IR/Verifier.h" |
| 13 |
#include "llvm/IRReader/IRReader.h" |
| 14 |
#include "llvm/Support/SourceMgr.h" |
| 15 |
#include "llvm/Support/raw_ostream.h" |
| 16 |
|
| 17 |
#include "gtest/gtest.h" |
| 18 |
|
| 19 |
#include <atomic> |
| 20 |
#include <future> |
| 21 |
#include <thread> |
| 22 |
|
| 23 |
using namespace llvm; |
| 24 |
using namespace llvm::orc; |
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
const llvm::StringRef FooSrc = R"( |
| 29 |
define void @foo() { |
| 30 |
ret void |
| 31 |
} |
| 32 |
)"; |
| 33 |
|
| 34 |
static ThreadSafeModule parseModule(llvm::StringRef Source, |
| 35 |
llvm::StringRef Name) { |
| 36 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 37 |
SMDiagnostic Err; |
| 38 |
auto M = parseIR(MemoryBufferRef(Source, Name), Err, *Ctx); |
| 39 |
if (!M) { |
| 40 |
Err.print("Testcase source failed to parse: ", errs()); |
| 41 |
exit(1); |
| 42 |
} |
| 43 |
return ThreadSafeModule(std::move(M), std::move(Ctx)); |
| 44 |
} |
| 45 |
|
| 46 |
TEST(ThreadSafeModuleTest, ContextWhollyOwnedByOneModule) { |
| 47 |
// Test that ownership of a context can be transferred to a single |
| 48 |
// ThreadSafeModule. |
| 49 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 50 |
auto M = std::make_unique<Module>("M", *Ctx); |
| 51 |
ThreadSafeModule TSM(std::move(M), std::move(Ctx)); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(ThreadSafeModuleTest, ContextOwnershipSharedByTwoModules) { |
| 55 |
// Test that ownership of a context can be shared between more than one |
| 56 |
// ThreadSafeModule. |
| 57 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 58 |
|
| 59 |
auto M1 = std::make_unique<Module>("M1", *Ctx); |
| 60 |
auto M2 = std::make_unique<Module>("M2", *Ctx); |
| 61 |
|
| 62 |
ThreadSafeContext TSCtx(std::move(Ctx)); |
| 63 |
ThreadSafeModule TSM1(std::move(M1), TSCtx); |
| 64 |
ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx)); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(ThreadSafeModuleTest, ContextOwnershipSharedWithClient) { |
| 68 |
// Test that ownership of a context can be shared with a client-held |
| 69 |
// ThreadSafeContext so that it can be re-used for new modules. |
| 70 |
ThreadSafeContext TSCtx(std::make_unique<LLVMContext>()); |
| 71 |
|
| 72 |
{ |
| 73 |
// Create and destroy a module. |
| 74 |
auto M1 = TSCtx.withContextDo( |
| 75 |
[](LLVMContext *Ctx) { return std::make_unique<Module>("M1", *Ctx); }); |
| 76 |
ThreadSafeModule TSM1(std::move(M1), TSCtx); |
| 77 |
} |
| 78 |
|
| 79 |
// Verify that the context is still available for re-use. |
| 80 |
auto M2 = TSCtx.withContextDo( |
| 81 |
[](LLVMContext *Ctx) { return std::make_unique<Module>("M2", *Ctx); }); |
| 82 |
ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx)); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(ThreadSafeModuleTest, ThreadSafeModuleMoveAssignment) { |
| 86 |
// Move assignment needs to move the module before the context (opposite |
| 87 |
// to the field order) to ensure that overwriting with an empty |
| 88 |
// ThreadSafeModule does not destroy the context early. |
| 89 |
ThreadSafeContext TSCtx(std::make_unique<LLVMContext>()); |
| 90 |
auto M = TSCtx.withContextDo( |
| 91 |
[](LLVMContext *Ctx) { return std::make_unique<Module>("M", *Ctx); }); |
| 92 |
ThreadSafeModule TSM(std::move(M), std::move(TSCtx)); |
| 93 |
TSM = ThreadSafeModule(); |
| 94 |
} |
| 95 |
|
| 96 |
TEST(ThreadSafeModuleTest, WithContextDoPreservesContext) { |
| 97 |
// Test that withContextDo passes through the LLVMContext that was used |
| 98 |
// to create the ThreadSafeContext. |
| 99 |
|
| 100 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 101 |
LLVMContext *OriginalCtx = Ctx.get(); |
| 102 |
ThreadSafeContext TSCtx(std::move(Ctx)); |
| 103 |
TSCtx.withContextDo( |
| 104 |
[&](LLVMContext *ClosureCtx) { EXPECT_EQ(ClosureCtx, OriginalCtx); }); |
| 105 |
} |
| 106 |
|
| 107 |
TEST(ThreadSafeModuleTest, WithModuleDo) { |
| 108 |
// Test non-const version of withModuleDo. |
| 109 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 110 |
auto M = std::make_unique<Module>("M", *Ctx); |
| 111 |
ThreadSafeModule TSM(std::move(M), std::move(Ctx)); |
| 112 |
TSM.withModuleDo([](Module &M) {}); |
| 113 |
} |
| 114 |
|
| 115 |
TEST(ThreadSafeModuleTest, WithModuleDoConst) { |
| 116 |
// Test const version of withModuleDo. |
| 117 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 118 |
auto M = std::make_unique<Module>("M", *Ctx); |
| 119 |
const ThreadSafeModule TSM(std::move(M), std::move(Ctx)); |
| 120 |
TSM.withModuleDo([](const Module &M) {}); |
| 121 |
} |
| 122 |
|
| 123 |
TEST(ThreadSafeModuleTest, ConsumingModuleDo) { |
| 124 |
// Test consumingModuleDo. |
| 125 |
auto Ctx = std::make_unique<LLVMContext>(); |
| 126 |
auto M = std::make_unique<Module>("M", *Ctx); |
| 127 |
ThreadSafeModule TSM(std::move(M), std::move(Ctx)); |
| 128 |
TSM.consumingModuleDo([](std::unique_ptr<Module> M) {}); |
| 129 |
} |
| 130 |
|
| 131 |
TEST(ThreadSafeModuleTest, CloneToNewContext) { |
| 132 |
auto TSM1 = parseModule(FooSrc, "foo.ll"); |
| 133 |
auto TSM2 = cloneToNewContext(TSM1); |
| 134 |
TSM2.withModuleDo([&](Module &NewM) { |
| 135 |
EXPECT_FALSE(verifyModule(NewM, &errs())); |
| 136 |
TSM1.withModuleDo([&](Module &OrigM) { |
| 137 |
EXPECT_NE(&NewM.getContext(), &OrigM.getContext()); |
| 138 |
}); |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(ObjectFormatsTest, CloneToContext) { |
| 143 |
auto TSM1 = parseModule(FooSrc, "foo.ll"); |
| 144 |
|
| 145 |
auto TSCtx = ThreadSafeContext(std::make_unique<LLVMContext>()); |
| 146 |
auto TSM2 = cloneToContext(TSM1, TSCtx); |
| 147 |
|
| 148 |
TSM2.withModuleDo([&](Module &M) { |
| 149 |
EXPECT_FALSE(verifyModule(M, &errs())); |
| 150 |
TSCtx.withContextDo( |
| 151 |
[&](LLVMContext *Ctx) { EXPECT_EQ(&M.getContext(), Ctx); }); |
| 152 |
}); |
| 153 |
} |
| 154 |
|
| 155 |
} // end anonymous namespace |