diff --git a/llvm/include/llvm/PassSupport.h b/llvm/include/llvm/PassSupport.h index 6cb6516412e8..7c3d49f02e8f 100644 --- a/llvm/include/llvm/PassSupport.h +++ b/llvm/include/llvm/PassSupport.h @@ -26,7 +26,7 @@ #include "llvm/PassInfo.h" #include "llvm/PassRegistry.h" #include "llvm/Support/Atomic.h" -#include "llvm/Support/Valgrind.h" +#include "llvm/Support/Compiler.h" #include namespace llvm { diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h index 1c0ef75f5298..9f98bc398fec 100644 --- a/llvm/include/llvm/Support/Compiler.h +++ b/llvm/include/llvm/Support/Compiler.h @@ -337,6 +337,45 @@ # define LLVM_ADDRESS_SANITIZER_BUILD 0 #endif +/// \macro LLVM_THREAD_SANITIZER_BUILD +/// \brief Whether LLVM itself is built with ThreadSanitizer instrumentation. +#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__) +# define LLVM_THREAD_SANITIZER_BUILD 1 +#else +# define LLVM_THREAD_SANITIZER_BUILD 0 +#endif + +#if LLVM_THREAD_SANITIZER_BUILD +// Thread Sanitizer is a tool that finds races in code. +// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations . +// tsan detects these exact functions by name. +extern "C" { +void AnnotateHappensAfter(const char *file, int line, const volatile void *cv); +void AnnotateHappensBefore(const char *file, int line, const volatile void *cv); +void AnnotateIgnoreWritesBegin(const char *file, int line); +void AnnotateIgnoreWritesEnd(const char *file, int line); +} + +// This marker is used to define a happens-before arc. The race detector will +// infer an arc from the begin to the end when they share the same pointer +// argument. +# define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv) + +// This marker defines the destination of a happens-before arc. +# define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv) + +// Ignore any races on writes between here and the next TsanIgnoreWritesEnd. +# define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__) + +// Resume checking for racy writes. +# define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__) +#else +# define TsanHappensBefore(cv) +# define TsanHappensAfter(cv) +# define TsanIgnoreWritesBegin() +# define TsanIgnoreWritesEnd() +#endif + /// \brief Mark debug helper function definitions like dump() that should not be /// stripped from debug builds. // FIXME: Move this to a private config.h as it's not usable in public headers. diff --git a/llvm/include/llvm/Support/ManagedStatic.h b/llvm/include/llvm/Support/ManagedStatic.h index addd34e704bc..2e131e47177d 100644 --- a/llvm/include/llvm/Support/ManagedStatic.h +++ b/llvm/include/llvm/Support/ManagedStatic.h @@ -15,8 +15,8 @@ #define LLVM_SUPPORT_MANAGEDSTATIC_H #include "llvm/Support/Atomic.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/Threading.h" -#include "llvm/Support/Valgrind.h" namespace llvm { diff --git a/llvm/include/llvm/Support/Valgrind.h b/llvm/include/llvm/Support/Valgrind.h index cebf75c49c19..12b0dc961daa 100644 --- a/llvm/include/llvm/Support/Valgrind.h +++ b/llvm/include/llvm/Support/Valgrind.h @@ -20,17 +20,6 @@ #include "llvm/Support/Compiler.h" #include -#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG) -// tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact -// functions by name. -extern "C" { -void AnnotateHappensAfter(const char *file, int line, const volatile void *cv); -void AnnotateHappensBefore(const char *file, int line, const volatile void *cv); -void AnnotateIgnoreWritesBegin(const char *file, int line); -void AnnotateIgnoreWritesEnd(const char *file, int line); -} -#endif - namespace llvm { namespace sys { // True if Valgrind is controlling this process. @@ -39,34 +28,6 @@ namespace sys { // Discard valgrind's translation of code in the range [Addr .. Addr + Len). // Otherwise valgrind may continue to execute the old version of the code. void ValgrindDiscardTranslations(const void *Addr, size_t Len); - -#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG) - // Thread Sanitizer is a valgrind tool that finds races in code. - // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations . - - // This marker is used to define a happens-before arc. The race detector will - // infer an arc from the begin to the end when they share the same pointer - // argument. - #define TsanHappensBefore(cv) \ - AnnotateHappensBefore(__FILE__, __LINE__, cv) - - // This marker defines the destination of a happens-before arc. - #define TsanHappensAfter(cv) \ - AnnotateHappensAfter(__FILE__, __LINE__, cv) - - // Ignore any races on writes between here and the next TsanIgnoreWritesEnd. - #define TsanIgnoreWritesBegin() \ - AnnotateIgnoreWritesBegin(__FILE__, __LINE__) - - // Resume checking for racy writes. - #define TsanIgnoreWritesEnd() \ - AnnotateIgnoreWritesEnd(__FILE__, __LINE__) -#else - #define TsanHappensBefore(cv) - #define TsanHappensAfter(cv) - #define TsanIgnoreWritesBegin() - #define TsanIgnoreWritesEnd() -#endif } } diff --git a/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp index b8fb2841e525..9868207b14ff 100644 --- a/llvm/lib/Support/ManagedStatic.cpp +++ b/llvm/lib/Support/ManagedStatic.cpp @@ -14,6 +14,7 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Config/config.h" #include "llvm/Support/Atomic.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/Mutex.h" #include "llvm/Support/MutexGuard.h" #include diff --git a/llvm/lib/Support/Statistic.cpp b/llvm/lib/Support/Statistic.cpp index 56c3b0f5659f..ee6c1305cdbe 100644 --- a/llvm/lib/Support/Statistic.cpp +++ b/llvm/lib/Support/Statistic.cpp @@ -24,6 +24,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Format.h" #include "llvm/Support/ManagedStatic.h" diff --git a/llvm/lib/Support/Valgrind.cpp b/llvm/lib/Support/Valgrind.cpp index facf8d927ecd..703448524ed9 100644 --- a/llvm/lib/Support/Valgrind.cpp +++ b/llvm/lib/Support/Valgrind.cpp @@ -52,23 +52,3 @@ void llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) { } #endif // !HAVE_VALGRIND_VALGRIND_H - -// These functions require no implementation, tsan just looks at the arguments -// they're called with. However, they are required to be weak as some other -// application or library may already be providing these definitions for the -// same reason we are. -extern "C" { -LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line, - const volatile void *cv); -void AnnotateHappensAfter(const char *file, int line, const volatile void *cv) { -} -LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line, - const volatile void *cv); -void AnnotateHappensBefore(const char *file, int line, - const volatile void *cv) {} -LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line); -void AnnotateIgnoreWritesBegin(const char *file, int line) {} -LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line); -void AnnotateIgnoreWritesEnd(const char *file, int line) {} -} -