summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/ExecutionEngine
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2018-09-26 18:50:01 +0000
committerLang Hames <lhames@gmail.com>2018-09-26 18:50:01 +0000
commitbcdfcbcb1d8694c0f0630e3c326cbda437b24c14 (patch)
tree298cef37ce2d21f6c437c4dcf293760d19135ffe /llvm/unittests/ExecutionEngine
parentd938d0d4f52aca68b27d622ce52f459fcea7a3c8 (diff)
downloadbcm5719-llvm-bcdfcbcb1d8694c0f0630e3c326cbda437b24c14.tar.gz
bcm5719-llvm-bcdfcbcb1d8694c0f0630e3c326cbda437b24c14.zip
[ORC] Change the field order of ThreadSafeModule to ensure the Module is
destroyed before its ThreadSharedContext. Destroying the context first is an error if this ThreadSafeModule is the only owner of its underlying context. Add a unit test for ThreadSafeModule/ThreadSafeContext to catch this and other basic usage issues. llvm-svn: 343129
Diffstat (limited to 'llvm/unittests/ExecutionEngine')
-rw-r--r--llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt1
-rw-r--r--llvm/unittests/ExecutionEngine/Orc/ThreadSafeModuleTest.cpp84
2 files changed, 85 insertions, 0 deletions
diff --git a/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt b/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt
index c18c9361cb0..3a47bfa20bf 100644
--- a/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt
+++ b/llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt
@@ -26,6 +26,7 @@ add_llvm_unittest(OrcJITTests
RTDyldObjectLinkingLayerTest.cpp
RTDyldObjectLinkingLayer2Test.cpp
SymbolStringPoolTest.cpp
+ ThreadSafeModuleTest.cpp
)
target_link_libraries(OrcJITTests PRIVATE ${ORC_JIT_TEST_LIBS})
diff --git a/llvm/unittests/ExecutionEngine/Orc/ThreadSafeModuleTest.cpp b/llvm/unittests/ExecutionEngine/Orc/ThreadSafeModuleTest.cpp
new file mode 100644
index 00000000000..40efd36160e
--- /dev/null
+++ b/llvm/unittests/ExecutionEngine/Orc/ThreadSafeModuleTest.cpp
@@ -0,0 +1,84 @@
+//===--- ThreadSafeModuleTest.cpp - Test basic use of ThreadSafeModule ----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
+#include "gtest/gtest.h"
+
+#include <atomic>
+#include <future>
+#include <thread>
+
+using namespace llvm;
+using namespace llvm::orc;
+
+namespace {
+
+TEST(ThreadSafeModuleTest, ContextWhollyOwnedByOneModule) {
+ // Test that ownership of a context can be transferred to a single
+ // ThreadSafeModule.
+ ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
+ ThreadSafeModule TSM(llvm::make_unique<Module>("M", *TSCtx.getContext()),
+ std::move(TSCtx));
+}
+
+TEST(ThreadSafeModuleTest, ContextOwnershipSharedByTwoModules) {
+ // Test that ownership of a context can be shared between more than one
+ // ThreadSafeModule.
+ ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
+
+ ThreadSafeModule TSM1(llvm::make_unique<Module>("M1", *TSCtx.getContext()),
+ TSCtx);
+ ThreadSafeModule TSM2(llvm::make_unique<Module>("M2", *TSCtx.getContext()),
+ std::move(TSCtx));
+}
+
+TEST(ThreadSafeModuleTest, ContextOwnershipSharedWithClient) {
+ // Test that ownership of a context can be shared with a client-held
+ // ThreadSafeContext so that it can be re-used for new modules.
+ ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
+
+ {
+ // Create and destroy a module.
+ ThreadSafeModule TSM1(llvm::make_unique<Module>("M1", *TSCtx.getContext()),
+ TSCtx);
+ }
+
+ // Verify that the context is still available for re-use.
+ ThreadSafeModule TSM2(llvm::make_unique<Module>("M2", *TSCtx.getContext()),
+ std::move(TSCtx));
+}
+
+TEST(ThreadSafeModuleTest, BasicContextLockAPI) {
+ // Test that basic lock API calls work.
+ ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
+ ThreadSafeModule TSM(llvm::make_unique<Module>("M", *TSCtx.getContext()),
+ TSCtx);
+
+ { auto L = TSCtx.getLock(); }
+
+ { auto L = TSM.getContextLock(); }
+}
+
+TEST(ThreadSafeModuleTest, ContextLockPreservesContext) {
+ // Test that the existence of a context lock preserves the attached
+ // context.
+ // The trick to verify this is a bit of a hack: We attach a Module
+ // (without the ThreadSafeModule wrapper) to the context, then verify
+ // that this Module destructs safely (which it will not if its context
+ // has been destroyed) even though all references to the context have
+ // been thrown away (apart from the lock).
+
+ ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
+ auto L = TSCtx.getLock();
+ auto &Ctx = *TSCtx.getContext();
+ auto M = llvm::make_unique<Module>("M", Ctx);
+ TSCtx = ThreadSafeContext();
+}
+
+} // end anonymous namespace
OpenPOWER on IntegriCloud