[llvm-commits] [llvm] r116492 - /llvm/trunk/include/llvm/PassSupport.h
Owen Anderson
resistor at mac.com
Thu Oct 14 10:59:03 PDT 2010
Author: resistor
Date: Thu Oct 14 12:59:03 2010
New Revision: 116492
URL: http://llvm.org/viewvc/llvm-project?rev=116492&view=rev
Log:
Try again at implementing thread-safe lazy pass initialization, without depending on static local initialization
being threadsafe AND ensuring that initialization is complete by the time the initializeFooPass method returns.
Modified:
llvm/trunk/include/llvm/PassSupport.h
Modified: llvm/trunk/include/llvm/PassSupport.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/PassSupport.h?rev=116492&r1=116491&r2=116492&view=diff
==============================================================================
--- llvm/trunk/include/llvm/PassSupport.h (original)
+++ llvm/trunk/include/llvm/PassSupport.h Thu Oct 14 12:59:03 2010
@@ -24,7 +24,7 @@
#include "Pass.h"
#include "llvm/PassRegistry.h"
#include "llvm/InitializePasses.h"
-#include "llvm/Support/Compiler.h"
+#include "llvm/System/Atomic.h"
#include <vector>
namespace llvm {
@@ -137,8 +137,20 @@
return PI; \
} \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- ATTRIBUTE_USED \
- static void* initialized = initialize##passName##PassOnce(Registry); \
+ static volatile sys::cas_flag initialized = 0; \
+ sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
+ if (old_val == 0) { \
+ initialize##passName##PassOnce(Registry); \
+ sys::MemoryFence(); \
+ initialized = 2; \
+ } else { \
+ sys::cas_flag tmp = initialized; \
+ sys::MemoryFence(); \
+ while (tmp != 2) { \
+ tmp = initialized; \
+ sys::MemoryFence(); \
+ } \
+ } \
} \
static RegisterPass<passName> passName ## _info(arg, name, cfg, analysis);
@@ -157,8 +169,20 @@
return PI; \
} \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- ATTRIBUTE_USED \
- static void* initialized = initialize##passName##PassOnce(Registry); \
+ static volatile sys::cas_flag initialized = 0; \
+ sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
+ if (old_val == 0) { \
+ initialize##passName##PassOnce(Registry); \
+ sys::MemoryFence(); \
+ initialized = 2; \
+ } else { \
+ sys::cas_flag tmp = initialized; \
+ sys::MemoryFence(); \
+ while (tmp != 2) { \
+ tmp = initialized; \
+ sys::MemoryFence(); \
+ } \
+ } \
} \
static RegisterPass<passName> passName ## _info(arg, name, cfg, analysis);
@@ -244,11 +268,24 @@
return AI; \
} \
void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
- ATTRIBUTE_USED static void* initialized = \
+ static volatile sys::cas_flag initialized = 0; \
+ sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
+ if (old_val == 0) { \
initialize##agName##AnalysisGroupOnce(Registry); \
+ sys::MemoryFence(); \
+ initialized = 2; \
+ } else { \
+ sys::cas_flag tmp = initialized; \
+ sys::MemoryFence(); \
+ while (tmp != 2) { \
+ tmp = initialized; \
+ sys::MemoryFence(); \
+ } \
+ } \
} \
static RegisterAnalysisGroup<agName> agName##_info (name);
+
#define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
@@ -260,12 +297,25 @@
return AI; \
} \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- ATTRIBUTE_USED \
- static void* initialized = initialize##passName##PassOnce(Registry); \
+ static volatile sys::cas_flag initialized = 0; \
+ sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
+ if (old_val == 0) { \
+ initialize##passName##PassOnce(Registry); \
+ sys::MemoryFence(); \
+ initialized = 2; \
+ } else { \
+ sys::cas_flag tmp = initialized; \
+ sys::MemoryFence(); \
+ while (tmp != 2) { \
+ tmp = initialized; \
+ sys::MemoryFence(); \
+ } \
+ } \
} \
static RegisterPass<passName> passName ## _info(arg, name, cfg, analysis); \
static RegisterAnalysisGroup<agName, def> passName ## _ag(passName ## _info);
+
#define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
static void* initialize##passName##PassOnce(PassRegistry &Registry) {
@@ -279,8 +329,20 @@
return AI; \
} \
void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
- ATTRIBUTE_USED \
- static void* initialized = initialize##passName##PassOnce(Registry); \
+ static volatile sys::cas_flag initialized = 0; \
+ sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
+ if (old_val == 0) { \
+ initialize##passName##PassOnce(Registry); \
+ sys::MemoryFence(); \
+ initialized = 2; \
+ } else { \
+ sys::cas_flag tmp = initialized; \
+ sys::MemoryFence(); \
+ while (tmp != 2) { \
+ tmp = initialized; \
+ sys::MemoryFence(); \
+ } \
+ } \
} \
static RegisterPass<passName> passName ## _info(arg, n, cfg, analysis); \
static RegisterAnalysisGroup<agName, def> passName ## _ag(passName ## _info);
More information about the llvm-commits
mailing list