Skip to content

Commit 488d171

Browse files
committed
Re-apply "[OpenMP][Archer] Use dlsym rather than weak symbols for TSan annotations"
Explicitly link libdl this time. Differential Revision: https://reviews.llvm.org/D142378
1 parent 79649ea commit 488d171

File tree

2 files changed

+13
-48
lines changed

2 files changed

+13
-48
lines changed

openmp/tools/archer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if(LIBOMP_OMPT_SUPPORT)
1212
include_directories(${LIBOMP_INCLUDE_DIR})
1313

1414
add_library(archer SHARED ompt-tsan.cpp)
15+
target_link_libraries(archer ${CMAKE_DL_LIBS})
1516
add_library(archer_static STATIC ompt-tsan.cpp)
1617

1718
install(TARGETS archer archer_static

openmp/tools/archer/ompt-tsan.cpp

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
#include <unistd.h>
3030
#include <unordered_map>
3131
#include <vector>
32-
33-
#if (defined __APPLE__ && defined __MACH__)
3432
#include <dlfcn.h>
35-
#endif
3633

3734
#include "omp-tools.h"
3835

@@ -53,7 +50,6 @@
5350
#define KMP_FALLTHROUGH() ((void)0)
5451
#endif
5552

56-
static int runOnTsan;
5753
static int hasReductionCallback;
5854

5955
namespace {
@@ -148,7 +144,6 @@ static ArcherFlags *archer_flags;
148144
// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
149145
// tsan detects these exact functions by name.
150146
extern "C" {
151-
#if (defined __APPLE__ && defined __MACH__)
152147
static void (*AnnotateHappensAfter)(const char *, int, const volatile void *);
153148
static void (*AnnotateHappensBefore)(const char *, int, const volatile void *);
154149
static void (*AnnotateIgnoreWritesBegin)(const char *, int);
@@ -157,37 +152,7 @@ static void (*AnnotateNewMemory)(const char *, int, const volatile void *,
157152
size_t);
158153
static void (*__tsan_func_entry)(const void *);
159154
static void (*__tsan_func_exit)(void);
160-
161-
static int RunningOnValgrind() {
162-
int (*fptr)();
163-
164-
fptr = (int (*)())dlsym(RTLD_DEFAULT, "RunningOnValgrind");
165-
// If we found RunningOnValgrind other than this function, we assume
166-
// Annotation functions present in this execution and leave runOnTsan=1
167-
// otherwise we change to runOnTsan=0
168-
if (!fptr || fptr == RunningOnValgrind)
169-
runOnTsan = 0;
170-
return 0;
171-
}
172-
#else
173-
void __attribute__((weak))
174-
AnnotateHappensAfter(const char *file, int line, const volatile void *cv) {}
175-
void __attribute__((weak))
176-
AnnotateHappensBefore(const char *file, int line, const volatile void *cv) {}
177-
void __attribute__((weak))
178-
AnnotateIgnoreWritesBegin(const char *file, int line) {}
179-
void __attribute__((weak)) AnnotateIgnoreWritesEnd(const char *file, int line) {
180-
}
181-
void __attribute__((weak))
182-
AnnotateNewMemory(const char *file, int line, const volatile void *cv,
183-
size_t size) {}
184-
int __attribute__((weak)) RunningOnValgrind() {
185-
runOnTsan = 0;
186-
return 0;
187-
}
188-
void __attribute__((weak)) __tsan_func_entry(const void *call_pc) {}
189-
void __attribute__((weak)) __tsan_func_exit(void) {}
190-
#endif
155+
static int (*RunningOnValgrind)(void);
191156
}
192157

193158
// This marker is used to define a happens-before arc. The race detector will
@@ -1078,6 +1043,14 @@ static void ompt_tsan_mutex_released(ompt_mutex_t kind, ompt_wait_id_t wait_id,
10781043

10791044
#define SET_CALLBACK(event) SET_CALLBACK_T(event, event)
10801045

1046+
#define findTsanFunction(f, fSig) \
1047+
do { \
1048+
if (NULL == (f = fSig dlsym(RTLD_DEFAULT, #f))) \
1049+
printf("Unable to find TSan function " #f ".\n"); \
1050+
} while (0)
1051+
1052+
#define findTsanFunctionSilent(f, fSig) f = fSig dlsym(RTLD_DEFAULT, #f)
1053+
10811054
static int ompt_tsan_initialize(ompt_function_lookup_t lookup, int device_num,
10821055
ompt_data_t *tool_data) {
10831056
const char *options = getenv("TSAN_OPTIONS");
@@ -1099,13 +1072,6 @@ static int ompt_tsan_initialize(ompt_function_lookup_t lookup, int device_num,
10991072
exit(1);
11001073
}
11011074

1102-
#if (defined __APPLE__ && defined __MACH__)
1103-
#define findTsanFunction(f, fSig) \
1104-
do { \
1105-
if (NULL == (f = fSig dlsym(RTLD_DEFAULT, #f))) \
1106-
printf("Unable to find TSan function " #f ".\n"); \
1107-
} while (0)
1108-
11091075
findTsanFunction(AnnotateHappensAfter,
11101076
(void (*)(const char *, int, const volatile void *)));
11111077
findTsanFunction(AnnotateHappensBefore,
@@ -1117,7 +1083,6 @@ static int ompt_tsan_initialize(ompt_function_lookup_t lookup, int device_num,
11171083
(void (*)(const char *, int, const volatile void *, size_t)));
11181084
findTsanFunction(__tsan_func_entry, (void (*)(const void *)));
11191085
findTsanFunction(__tsan_func_exit, (void (*)(void)));
1120-
#endif
11211086

11221087
SET_CALLBACK(thread_begin);
11231088
SET_CALLBACK(thread_end);
@@ -1181,10 +1146,9 @@ ompt_start_tool(unsigned int omp_version, const char *runtime_version) {
11811146
// an implementation of the Annotation interface is available in the
11821147
// execution or disable the tool (by returning NULL).
11831148

1184-
runOnTsan = 1;
1185-
RunningOnValgrind();
1186-
if (!runOnTsan) // if we are not running on TSAN, give a different tool the
1187-
// chance to be loaded
1149+
findTsanFunctionSilent(RunningOnValgrind, (int (*)(void)));
1150+
if (!RunningOnValgrind) // if we are not running on TSAN, give a different
1151+
// tool the chance to be loaded
11881152
{
11891153
if (archer_flags->verbose)
11901154
std::cout << "Archer detected OpenMP application without TSan "

0 commit comments

Comments
 (0)