Skip to content

Commit 1c3b15e

Browse files
[llvm-exegesis] Use LLVM Support to get thread ID
This patch switches from manually using the Linux syscall to get the current thread ID to using the relevant LLVM Support libraries that abstract over the low level system details.
1 parent 8003f55 commit 1c3b15e

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class SubProcessFunctionExecutorImpl
301301
if (AddMemDefError)
302302
return AddMemDefError;
303303

304-
long ParentTID = SubprocessMemory::getCurrentTID();
304+
long ParentTID = get_threadid();
305305
pid_t ParentOrChildPID = fork();
306306

307307
if (ParentOrChildPID == -1) {

llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Error.h"
1111
#include "llvm/Support/Error.h"
1212
#include "llvm/Support/FormatVariadic.h"
13+
#include "llvm/Support/Threading.h"
1314
#include <cerrno>
1415

1516
#ifdef __linux__
@@ -24,21 +25,14 @@ namespace exegesis {
2425

2526
#if defined(__linux__) && !defined(__ANDROID__)
2627

27-
long SubprocessMemory::getCurrentTID() {
28-
// We're using the raw syscall here rather than the gettid() function provided
29-
// by most libcs for compatibility as gettid() was only added to glibc in
30-
// version 2.30.
31-
return syscall(SYS_gettid);
32-
}
33-
3428
Error SubprocessMemory::initializeSubprocessMemory(pid_t ProcessID) {
3529
// Add the PID to the shared memory name so that if we're running multiple
3630
// processes at the same time, they won't interfere with each other.
3731
// This comes up particularly often when running the exegesis tests with
3832
// llvm-lit. Additionally add the TID so that downstream consumers
3933
// using multiple threads don't run into conflicts.
4034
std::string AuxiliaryMemoryName =
41-
formatv("/{0}auxmem{1}", getCurrentTID(), ProcessID);
35+
formatv("/{0}auxmem{1}", get_threadid(), ProcessID);
4236
int AuxiliaryMemoryFD = shm_open(AuxiliaryMemoryName.c_str(),
4337
O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
4438
if (AuxiliaryMemoryFD == -1)
@@ -59,7 +53,7 @@ Error SubprocessMemory::addMemoryDefinition(
5953
SharedMemoryNames.reserve(MemoryDefinitions.size());
6054
for (auto &[Name, MemVal] : MemoryDefinitions) {
6155
std::string SharedMemoryName =
62-
formatv("/{0}t{1}memdef{2}", ProcessPID, getCurrentTID(), MemVal.Index);
56+
formatv("/{0}t{1}memdef{2}", ProcessPID, get_threadid(), MemVal.Index);
6357
SharedMemoryNames.push_back(SharedMemoryName);
6458
int SharedMemoryFD =
6559
shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
@@ -93,7 +87,7 @@ Error SubprocessMemory::addMemoryDefinition(
9387

9488
Expected<int> SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
9589
std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
96-
pid_t ParentPID, long ParentTID, int CounterFileDescriptor) {
90+
pid_t ParentPID, uint64_t ParentTID, int CounterFileDescriptor) {
9791
std::string AuxiliaryMemoryName =
9892
formatv("/{0}auxmem{1}", ParentTID, ParentPID);
9993
int AuxiliaryMemoryFileDescriptor =
@@ -145,7 +139,7 @@ Error SubprocessMemory::addMemoryDefinition(
145139

146140
Expected<int> SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
147141
std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
148-
pid_t ParentPID, long ParentTID, int CounterFileDescriptor) {
142+
pid_t ParentPID, uint64_t ParentTID, int CounterFileDescriptor) {
149143
return make_error<Failure>(
150144
"setupAuxiliaryMemoryInSubprocess is only supported on Linux");
151145
}

llvm/tools/llvm-exegesis/lib/SubprocessMemory.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ class SubprocessMemory {
3535
static constexpr const size_t AuxiliaryMemoryOffset = 1;
3636
static constexpr const size_t AuxiliaryMemorySize = 4096;
3737

38-
// Gets the thread ID for the calling thread.
39-
static long getCurrentTID();
40-
4138
Error initializeSubprocessMemory(pid_t ProcessID);
4239

4340
// The following function sets up memory definitions. It creates shared
@@ -57,7 +54,7 @@ class SubprocessMemory {
5754
// section.
5855
static Expected<int> setupAuxiliaryMemoryInSubprocess(
5956
std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
60-
pid_t ParentPID, long ParentTID, int CounterFileDescriptor);
57+
pid_t ParentPID, uint64_t ParentTID, int CounterFileDescriptor);
6158

6259
~SubprocessMemory();
6360

0 commit comments

Comments
 (0)