Skip to content

Commit c5bde8b

Browse files
authored
Merge pull request #167 from hewj03/ocl-open-80
Add llvm patch to fix ManagedStatic memleak
2 parents e7c2fbc + c3754c2 commit c5bde8b

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,18 @@ set(TARGET_NAME ${COMMON_CLANG_LIBRARY_NAME}${BUILD_PLATFORM} )
8080

8181
if(NOT USE_PREBUILT_LLVM)
8282
set(TARGET_BRANCH "ocl-open-80")
83+
set(LLVM_BASE_REVISION release_80)
84+
8385
set(CLANG_SOURCE_DIR ${LLVM_SOURCE_DIR}/tools/clang)
8486
set(CLANG_BASE_REVISION release_80)
8587

8688
set(SPIRV_SOURCE_DIR ${LLVM_SOURCE_DIR}/projects/llvm-spirv)
8789
set(SPIRV_BASE_REVISION llvm_release_80)
8890

91+
apply_patches(${LLVM_SOURCE_DIR}
92+
${CMAKE_CURRENT_SOURCE_DIR}/patches/llvm
93+
${LLVM_BASE_REVISION}
94+
${TARGET_BRANCH})
8995
apply_patches(${CLANG_SOURCE_DIR}
9096
${CMAKE_CURRENT_SOURCE_DIR}/patches/clang
9197
${CLANG_BASE_REVISION}

common_clang.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ static llvm::sys::Mutex lazyCCInitMutex;
8787

8888
static llvm::ManagedStatic<llvm::sys::SmartMutex<true>> compileMutex;
8989

90-
void CommonClangTerminate() { llvm::llvm_shutdown(); }
90+
void CommonClangTerminate() {
91+
llvm::llvm_shutdown();
92+
#ifndef USE_PREBUILT_LLVM
93+
llvm::deleteManagedStaticMutex();
94+
#endif
95+
}
9196

9297
// This function mustn't be invoked from a static object constructor,
9398
// from a DllMain function (Windows specific), or from a function
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
From f1a4b51f4f2438993e8ad8913f5f70481001b240 Mon Sep 17 00:00:00 2001
2+
From: Viktoria Maksimova <[email protected]>
3+
Date: Wed, 9 Sep 2020 09:42:45 +0800
4+
Subject: [PATCH] Adding llvm::deleteManagedStaticMutex
5+
6+
---
7+
include/llvm/Support/ManagedStatic.h | 8 ++++++++
8+
lib/Support/ManagedStatic.cpp | 5 +++++
9+
2 files changed, 13 insertions(+)
10+
11+
diff --git a/include/llvm/Support/ManagedStatic.h b/include/llvm/Support/ManagedStatic.h
12+
index b4bf3210cc7..1ebbaaa852b 100644
13+
--- a/include/llvm/Support/ManagedStatic.h
14+
+++ b/include/llvm/Support/ManagedStatic.h
15+
@@ -85,6 +85,14 @@ public:
16+
/// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
17+
void llvm_shutdown();
18+
19+
+/// Purpose of this function is to free memory allocated for ManagedStaticMutex.
20+
+/// One might want to do that to avoid memory leaks in case LLVM is loaded as a
21+
+/// shared library (or dll) at runtime.
22+
+/// This function is not thread safe and should be called only if there are no
23+
+/// threads which are using the mutex now or will use the mutex in the future.
24+
+/// This means deleteManagedStaticMutex can be called only after llvm_shutdown.
25+
+void deleteManagedStaticMutex();
26+
+
27+
/// llvm_shutdown_obj - This is a simple helper class that calls
28+
/// llvm_shutdown() when it is destroyed.
29+
struct llvm_shutdown_obj {
30+
diff --git a/lib/Support/ManagedStatic.cpp b/lib/Support/ManagedStatic.cpp
31+
index 74f71a38502..ad124e1f0a6 100644
32+
--- a/lib/Support/ManagedStatic.cpp
33+
+++ b/lib/Support/ManagedStatic.cpp
34+
@@ -83,3 +83,8 @@ void llvm::llvm_shutdown() {
35+
while (StaticList)
36+
StaticList->destroy();
37+
}
38+
+
39+
+void llvm::deleteManagedStaticMutex() {
40+
+ assert(StaticList == nullptr && "llvm_shutdown() must be called first!");
41+
+ delete ManagedStaticMutex;
42+
+}
43+
\ No newline at end of file
44+
--
45+
2.18.1
46+

0 commit comments

Comments
 (0)