Skip to content

Commit 592df39

Browse files
yoneymeta-codesync[bot]
authored andcommitted
Allocator-local locking for JIT code allocators
Summary: Remove allocator-side `ThreadedCompileSerialize` and make the concrete allocator mutexes unconditional, so `CodeAllocatorCinder` and `MultipleSectionCodeAllocator` rely on their local mutexes for allocator state access on both GIL and FT builds. Reviewed By: DinoV Differential Revision: D97125443 fbshipit-source-id: 78b3ef4e4897c3625bf6cecfc21640d9de8ce9a2
1 parent 26199f0 commit 592df39

2 files changed

Lines changed: 1 addition & 17 deletions

File tree

cinderx/Jit/code_allocator.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "cinderx/Common/log.h"
66
#include "cinderx/Jit/config.h"
7-
#include "cinderx/Jit/threaded_compile.h"
87

98
#ifdef WIN32
109
#include <Windows.h>
@@ -136,10 +135,7 @@ CodeAllocatorCinder::~CodeAllocatorCinder() {
136135
}
137136

138137
AllocateResult CodeAllocatorCinder::addCode(asmjit::CodeHolder* code) {
139-
ThreadedCompileSerialize guard;
140-
#ifdef Py_GIL_DISABLED
141138
std::lock_guard lock{allocator_mutex_};
142-
#endif
143139

144140
PROPAGATE_ERROR(code->flatten());
145141
PROPAGATE_ERROR(code->resolveUnresolvedLinks());
@@ -197,9 +193,7 @@ asmjit::Error CodeAllocatorCinder::releaseCode([[maybe_unused]] void* code) {
197193
}
198194

199195
bool CodeAllocatorCinder::contains(const void* ptr) const {
200-
#ifdef Py_GIL_DISABLED
201196
std::lock_guard lock{allocator_mutex_};
202-
#endif
203197
for (std::span<uint8_t> alloc : allocations_) {
204198
if (alloc.data() <= ptr && ptr < alloc.data() + alloc.size()) {
205199
return true;
@@ -254,10 +248,7 @@ void MultipleSectionCodeAllocator::createSlabs() noexcept {
254248
}
255249

256250
AllocateResult MultipleSectionCodeAllocator::addCode(asmjit::CodeHolder* code) {
257-
ThreadedCompileSerialize guard;
258-
#ifdef Py_GIL_DISABLED
259251
std::lock_guard lock{allocator_mutex_};
260-
#endif
261252

262253
if (code_sections_.empty()) {
263254
createSlabs();
@@ -335,13 +326,10 @@ asmjit::Error MultipleSectionCodeAllocator::releaseCode(
335326
}
336327

337328
bool MultipleSectionCodeAllocator::contains(const void* ptr) const {
338-
// Have to check both the hot/cold slab and the asmjit allocator. The latter
329+
// Have to check both the hot/cold slab and the asmjit allocator. The latter
339330
// is already thread-safe.
340331
{
341-
ThreadedCompileSerialize guard;
342-
#ifdef Py_GIL_DISABLED
343332
std::lock_guard lock{allocator_mutex_};
344-
#endif
345333
if (code_alloc_ <= ptr && ptr < code_alloc_ + total_allocation_size_) {
346334
return true;
347335
}

cinderx/Jit/code_allocator.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,8 @@ class CodeAllocatorCinder : public CodeAllocator {
6969
bool contains(const void* ptr) const override;
7070

7171
private:
72-
#ifdef Py_GIL_DISABLED
7372
// Protects all allocator-owned state used by addCode()/contains().
7473
mutable std::mutex allocator_mutex_;
75-
#endif
7674

7775
// List of chunks allocated for use in deallocation
7876
std::vector<std::span<uint8_t>> allocations_;
@@ -102,10 +100,8 @@ class MultipleSectionCodeAllocator : public CodeAllocator {
102100
private:
103101
void createSlabs() noexcept;
104102

105-
#ifdef Py_GIL_DISABLED
106103
// Protects all allocator-owned state used by addCode()/contains().
107104
mutable std::mutex allocator_mutex_;
108-
#endif
109105

110106
std::unordered_map<codegen::CodeSection, uint8_t*> code_sections_;
111107
std::unordered_map<codegen::CodeSection, size_t> code_section_free_sizes_;

0 commit comments

Comments
 (0)