Skip to content

Commit a659514

Browse files
committed
Add llvm patch to fix ManagedStatic memleak
1 parent 158058d commit a659514

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,9 +80,11 @@ set(TARGET_NAME ${COMMON_CLANG_LIBRARY_NAME}${BUILD_PLATFORM} )
8080
if(NOT USE_PREBUILT_LLVM)
8181

8282
if(NOT LLVM_EXTERNAL_CLANG_SOURCE_DIR)
83+
set(LLVM_BASE_REVISION release_11)
8384
set(CLANG_SOURCE_DIR ${LLVM_SOURCE_DIR}/tools/clang)
8485
set(CLANG_BASE_REVISION release_11)
8586
elseif(EXISTS "${LLVM_EXTERNAL_CLANG_SOURCE_DIR}/CMakeLists.txt")
87+
set(LLVM_BASE_REVISION release/11.x)
8688
set(CLANG_SOURCE_DIR "${LLVM_EXTERNAL_CLANG_SOURCE_DIR}")
8789
set(CLANG_BASE_REVISION release/11.x)
8890
endif()
@@ -123,6 +125,10 @@ if(NOT USE_PREBUILT_LLVM)
123125
set(SPIRV_BASE_REVISION llvm_release_110)
124126
set(TARGET_BRANCH "ocl-open-110")
125127

128+
apply_patches(${LLVM_SOURCE_DIR}
129+
${CMAKE_CURRENT_SOURCE_DIR}/patches/llvm
130+
${LLVM_BASE_REVISION}
131+
${TARGET_BRANCH})
126132
apply_patches(${CLANG_SOURCE_DIR}
127133
${CMAKE_CURRENT_SOURCE_DIR}/patches/clang
128134
${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 945d0c390566987832d76e161d62a82828ac40d8 Mon Sep 17 00:00:00 2001
2+
From: Viktoria Maksimova <[email protected]>
3+
Date: Wed, 9 Sep 2020 11:41:09 +0800
4+
Subject: [PATCH] Adding llvm::deleteManagedStaticMutex
5+
6+
---
7+
llvm/include/llvm/Support/ManagedStatic.h | 8 ++++++++
8+
llvm/lib/Support/ManagedStatic.cpp | 5 +++++
9+
2 files changed, 13 insertions(+)
10+
11+
diff --git a/llvm/include/llvm/Support/ManagedStatic.h b/llvm/include/llvm/Support/ManagedStatic.h
12+
index f2b41422f13..01049e72beb 100644
13+
--- a/llvm/include/llvm/Support/ManagedStatic.h
14+
+++ b/llvm/include/llvm/Support/ManagedStatic.h
15+
@@ -113,6 +113,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/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp
31+
index 053493f72fb..c843fabbe81 100644
32+
--- a/llvm/lib/Support/ManagedStatic.cpp
33+
+++ b/llvm/lib/Support/ManagedStatic.cpp
34+
@@ -81,3 +81,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)