Skip to content

Commit 8fd46ea

Browse files
authored
Make thread_local variables constinit (#1206)
By declaring all thread_local variables that can easily be given static initialization constinit, and removing the destructor of `arena`, we can remove the call to `__tls_init` which is inserted into every single allocation, significantly speeding up the code. One consequence of this is that threads are responsible for calling munmap explicitly before exiting now; my understanding is they are pretty much always doing this currently via `free_all_kore_mem`, so this shouldn't break anything, but it needs testing to make sure.
1 parent 636ea36 commit 8fd46ea

File tree

13 files changed

+24
-32
lines changed

13 files changed

+24
-32
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 3.5)
1+
cmake_minimum_required (VERSION 3.12)
22

33
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
44

@@ -12,6 +12,9 @@ project(KLLVM CXX C)
1212

1313
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
1414
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
15+
set(CMAKE_CXX_STANDARD 20)
16+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
17+
set(CMAKE_CXX_EXTENSIONS OFF)
1518

1619
include(CTest)
1720

bin/llvm-kompile-clang

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ infer_library_name() {
5050
fi
5151
}
5252

53-
clangpp_args=("--std=c++17")
53+
clangpp_args=("--std=c++20")
5454
while [[ $# -gt 0 ]]; do
5555
if $output; then
5656
output=false

cmake/KLLVMCompilerFlags.cmake

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
set(CMAKE_CXX_STANDARD 20)
2-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3-
set(CMAKE_CXX_EXTENSIONS OFF)
4-
51
# Required to build the Python bindings as a static library that is then linked
62
# into a shared library with the interpreter library generated by llvm-kompile.
73
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

cmake/LLVMKompile.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
22
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
3-
set(CMAKE_CXX_STANDARD 14)
3+
set(CMAKE_CXX_STANDARD 20)
44
set(CMAKE_CXX_STANDARD_REQUIRED ON)
55
set(CMAKE_CXX_EXTENSIONS OFF)
66

include/runtime/arena.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,10 @@ size_t const MIN_SPACE = 1024 * 1024;
2929
// once.
3030
class arena {
3131
public:
32-
arena(char id, bool trigger_collection)
32+
constexpr arena(char id, bool trigger_collection)
3333
: allocation_semispace_id(id)
3434
, trigger_collection(trigger_collection) { }
3535

36-
~arena() {
37-
if (current_addr_ptr)
38-
munmap(current_addr_ptr, HYPERBLOCK_SIZE);
39-
if (collection_addr_ptr)
40-
munmap(collection_addr_ptr, HYPERBLOCK_SIZE);
41-
}
42-
4336
char *evacuate(char *scan_ptr);
4437

4538
// Allocates the requested number of bytes as a contiguous region and returns a
@@ -163,7 +156,7 @@ inline char arena::get_arena_semispace_id_of_object(void *ptr) {
163156
//
164157
extern bool time_for_collection;
165158
#else
166-
extern thread_local bool time_for_collection;
159+
extern thread_local constinit bool time_for_collection;
167160
#endif
168161

169162
size_t get_gc_threshold();

include/runtime/collect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ using set_node = set::iterator::node_t;
2626
using set_impl = set::iterator::tree_t;
2727

2828
extern "C" {
29-
extern thread_local size_t numBytesLiveAtCollection[1 << AGE_WIDTH];
30-
extern thread_local bool collect_old;
29+
extern thread_local constinit size_t numBytesLiveAtCollection[1 << AGE_WIDTH];
30+
extern thread_local constinit bool collect_old;
3131
size_t get_size(uint64_t, uint16_t);
3232
void migrate_static_roots(void);
3333
void migrate(block **block_ptr);

include/runtime/header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void hash_exit(void);
5353
//
5454
extern bool gc_enabled;
5555
#else
56-
extern thread_local bool gc_enabled;
56+
extern thread_local constinit bool gc_enabled;
5757
#endif
5858
}
5959

runtime/alloc/arena.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ extern size_t const VAR_BLOCK_SIZE = BLOCK_SIZE;
1919
bool time_for_collection = false;
2020
bool gc_enabled = true;
2121
#else
22-
thread_local bool time_for_collection = false;
23-
thread_local bool gc_enabled = true;
22+
thread_local constinit bool time_for_collection = false;
23+
thread_local constinit bool gc_enabled = true;
2424
#endif
2525

2626
void arena::initialize_semispace() {

runtime/arithmetic/int.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ void int_hash(mpz_t i, void *hasher) {
374374
}
375375

376376
thread_local gmp_randstate_t kllvm_rand_state;
377-
thread_local bool kllvm_rand_state_initialized = false;
377+
thread_local constinit bool kllvm_rand_state_initialized = false;
378378

379379
SortK hook_INT_srand(SortInt seed) {
380380
if (!kllvm_rand_state_initialized) {

runtime/collect/collect.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
#include <cstring>
1111

1212
extern "C" {
13-
extern thread_local arena youngspace;
14-
extern thread_local arena oldspace;
15-
extern thread_local arena alwaysgcspace;
13+
extern thread_local constinit arena youngspace;
14+
extern thread_local constinit arena oldspace;
15+
extern thread_local constinit arena alwaysgcspace;
1616

1717
char *youngspace_ptr(void);
1818
char *oldspace_ptr(void);
1919

2020
static thread_local bool is_gc = false;
21-
bool thread_local collect_old = false;
21+
bool thread_local constinit collect_old = false;
2222
#ifndef GC_DBG
2323
static thread_local uint8_t num_collection_only_young = 0;
2424
#else
2525
static thread_local char *last_alloc_ptr;
2626
#endif
2727

28-
size_t thread_local numBytesLiveAtCollection[1 << AGE_WIDTH];
28+
size_t thread_local constinit numBytesLiveAtCollection[1 << AGE_WIDTH];
2929

3030
bool during_gc() {
3131
return is_gc;

0 commit comments

Comments
 (0)