176 lines · cpp
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-exception6//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 23using namespace llvm;24using namespace llvm::orc;25 26namespace {27 28const llvm::StringRef FooSrc = R"(29 define void @foo() {30 ret void31 }32)";33 34static std::unique_ptr<Module>35parseModuleRaw(llvm::StringRef Source, llvm::StringRef Name, LLVMContext &Ctx) {36 SMDiagnostic Err;37 auto M = parseIR(MemoryBufferRef(Source, Name), Err, Ctx);38 if (!M) {39 Err.print("Testcase source failed to parse: ", errs());40 exit(1);41 }42 return M;43}44 45static ThreadSafeModule parseModule(llvm::StringRef Source,46 llvm::StringRef Name) {47 auto Ctx = std::make_unique<LLVMContext>();48 auto M = parseModuleRaw(Source, Name, *Ctx);49 return ThreadSafeModule(std::move(M), std::move(Ctx));50}51 52TEST(ThreadSafeModuleTest, ContextWhollyOwnedByOneModule) {53 // Test that ownership of a context can be transferred to a single54 // ThreadSafeModule.55 auto Ctx = std::make_unique<LLVMContext>();56 auto M = std::make_unique<Module>("M", *Ctx);57 ThreadSafeModule TSM(std::move(M), std::move(Ctx));58}59 60TEST(ThreadSafeModuleTest, ContextOwnershipSharedByTwoModules) {61 // Test that ownership of a context can be shared between more than one62 // ThreadSafeModule.63 auto Ctx = std::make_unique<LLVMContext>();64 65 auto M1 = std::make_unique<Module>("M1", *Ctx);66 auto M2 = std::make_unique<Module>("M2", *Ctx);67 68 ThreadSafeContext TSCtx(std::move(Ctx));69 ThreadSafeModule TSM1(std::move(M1), TSCtx);70 ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx));71}72 73TEST(ThreadSafeModuleTest, ContextOwnershipSharedWithClient) {74 // Test that ownership of a context can be shared with a client-held75 // ThreadSafeContext so that it can be re-used for new modules.76 ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());77 78 {79 // Create and destroy a module.80 auto M1 = TSCtx.withContextDo(81 [](LLVMContext *Ctx) { return std::make_unique<Module>("M1", *Ctx); });82 ThreadSafeModule TSM1(std::move(M1), TSCtx);83 }84 85 // Verify that the context is still available for re-use.86 auto M2 = TSCtx.withContextDo(87 [](LLVMContext *Ctx) { return std::make_unique<Module>("M2", *Ctx); });88 ThreadSafeModule TSM2(std::move(M2), std::move(TSCtx));89}90 91TEST(ThreadSafeModuleTest, ThreadSafeModuleMoveAssignment) {92 // Move assignment needs to move the module before the context (opposite93 // to the field order) to ensure that overwriting with an empty94 // ThreadSafeModule does not destroy the context early.95 ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());96 auto M = TSCtx.withContextDo(97 [](LLVMContext *Ctx) { return std::make_unique<Module>("M", *Ctx); });98 ThreadSafeModule TSM(std::move(M), std::move(TSCtx));99 TSM = ThreadSafeModule();100}101 102TEST(ThreadSafeModuleTest, WithContextDoPreservesContext) {103 // Test that withContextDo passes through the LLVMContext that was used104 // to create the ThreadSafeContext.105 106 auto Ctx = std::make_unique<LLVMContext>();107 LLVMContext *OriginalCtx = Ctx.get();108 ThreadSafeContext TSCtx(std::move(Ctx));109 TSCtx.withContextDo(110 [&](LLVMContext *ClosureCtx) { EXPECT_EQ(ClosureCtx, OriginalCtx); });111}112 113TEST(ThreadSafeModuleTest, WithModuleDo) {114 // Test non-const version of withModuleDo.115 auto Ctx = std::make_unique<LLVMContext>();116 auto M = std::make_unique<Module>("M", *Ctx);117 ThreadSafeModule TSM(std::move(M), std::move(Ctx));118 TSM.withModuleDo([](Module &M) {});119}120 121TEST(ThreadSafeModuleTest, WithModuleDoConst) {122 // Test const version of withModuleDo.123 auto Ctx = std::make_unique<LLVMContext>();124 auto M = std::make_unique<Module>("M", *Ctx);125 const ThreadSafeModule TSM(std::move(M), std::move(Ctx));126 TSM.withModuleDo([](const Module &M) {});127}128 129TEST(ThreadSafeModuleTest, ConsumingModuleDo) {130 // Test consumingModuleDo.131 auto Ctx = std::make_unique<LLVMContext>();132 auto M = std::make_unique<Module>("M", *Ctx);133 ThreadSafeModule TSM(std::move(M), std::move(Ctx));134 TSM.consumingModuleDo([](std::unique_ptr<Module> M) {});135}136 137TEST(ThreadSafeModuleTest, CloneExternalModuleToNewContext) {138 auto Ctx = std::make_unique<LLVMContext>();139 auto M = parseModuleRaw(FooSrc, "foo.ll", *Ctx);140 auto TSCtx = ThreadSafeContext(std::make_unique<LLVMContext>());141 auto TSM = cloneExternalModuleToContext(*M, TSCtx);142 TSM.withModuleDo([&](Module &NewM) {143 EXPECT_NE(&NewM.getContext(), Ctx.get());144 TSCtx.withContextDo(145 [&](LLVMContext *NewCtx) { EXPECT_EQ(&NewM.getContext(), NewCtx); });146 EXPECT_FALSE(NewM.empty());147 EXPECT_FALSE(verifyModule(NewM, &errs()));148 });149}150 151TEST(ThreadSafeModuleTest, CloneToNewContext) {152 auto TSM1 = parseModule(FooSrc, "foo.ll");153 auto TSM2 = cloneToNewContext(TSM1);154 TSM2.withModuleDo([&](Module &NewM) {155 EXPECT_FALSE(verifyModule(NewM, &errs()));156 TSM1.withModuleDo([&](Module &OrigM) {157 EXPECT_NE(&NewM.getContext(), &OrigM.getContext());158 });159 });160}161 162TEST(ObjectFormatsTest, CloneToContext) {163 auto TSM1 = parseModule(FooSrc, "foo.ll");164 165 auto TSCtx = ThreadSafeContext(std::make_unique<LLVMContext>());166 auto TSM2 = cloneToContext(TSM1, TSCtx);167 168 TSM2.withModuleDo([&](Module &M) {169 EXPECT_FALSE(verifyModule(M, &errs()));170 TSCtx.withContextDo(171 [&](LLVMContext *Ctx) { EXPECT_EQ(&M.getContext(), Ctx); });172 });173}174 175} // end anonymous namespace176