Skip to content

Commit 9eee51f

Browse files
Dwight Guthrv-jenkins
andauthored
add flag to control whether gc should be enabled or not (#1082)
This is one of a sequence of PRs designed to make progress towards generating stack maps so that we can trigger the GC during allocation rather than in between rewrite steps only. The first few PRs will be preliminaries that add small features that will be used by future PRs. This PR adds a global variable that controls whether or not garbage collection is enabled. This boolean flag is disabled during certain call frames that it would be difficult to walk the stack of. In particular, we disable the flag during: * allocations that occur inside immer * calls to hooks * allocations that occur inside GMP and MPFR * construction of the initial configuration The boolean flag is currently unused. It will be used in a future pull request to determine whether the allocator is allowed to trigger garbage collection at the current time. Co-authored-by: rv-jenkins <[email protected]>
1 parent 92efc5d commit 9eee51f

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
lines changed

include/kllvm/codegen/CreateTerm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class create_term {
3535
bool populate_static_set(kore_pattern *pattern);
3636
std::pair<llvm::Value *, bool> create_allocation(
3737
kore_pattern *pattern, std::string const &location_stack = "");
38+
llvm::Value *disable_gc();
39+
void enable_gc(llvm::Value *was_enabled);
3840

3941
public:
4042
create_term(

include/runtime/header.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ size_t hash_k(block *);
111111
void k_hash(block *, void *);
112112
bool hash_enter(void);
113113
void hash_exit(void);
114+
115+
extern bool gc_enabled;
114116
}
115117

116118
__attribute__((always_inline)) constexpr uint64_t len_hdr(uint64_t hdr) {
@@ -225,7 +227,10 @@ struct kore_alloc_heap {
225227
if (during_gc()) {
226228
return ::operator new(size);
227229
}
230+
bool enabled = gc_enabled;
231+
gc_enabled = false;
228232
auto *result = (string *)kore_alloc_token(size + sizeof(blockheader));
233+
gc_enabled = enabled;
229234
init_with_len(result, size);
230235
return result->data;
231236
}

lib/codegen/CreateTerm.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,29 @@ llvm::Value *create_term::create_hook(
673673
}
674674
std::string hook_name = "hook_" + name.substr(0, name.find('.')) + "_"
675675
+ name.substr(name.find('.') + 1);
676-
return create_function_call(
676+
auto *old_val = disable_gc();
677+
auto *result = create_function_call(
677678
hook_name, pattern, true, false, true, location_stack);
679+
enable_gc(old_val);
680+
return result;
681+
}
682+
683+
llvm::Value *create_term::disable_gc() {
684+
llvm::Constant *global
685+
= module_->getOrInsertGlobal("gc_enabled", llvm::Type::getInt1Ty(ctx_));
686+
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
687+
auto *old_val = new llvm::LoadInst(
688+
llvm::Type::getInt1Ty(ctx_), global_var, "was_enabled", current_block_);
689+
new llvm::StoreInst(
690+
llvm::ConstantInt::getFalse(ctx_), global_var, current_block_);
691+
return old_val;
692+
}
693+
694+
void create_term::enable_gc(llvm::Value *was_enabled) {
695+
llvm::Constant *global
696+
= module_->getOrInsertGlobal("gc_enabled", llvm::Type::getInt1Ty(ctx_));
697+
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
698+
new llvm::StoreInst(was_enabled, global_var, current_block_);
678699
}
679700

680701
// We use tailcc calling convention for apply_rule_* and eval_* functions to

runtime/alloc/alloc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,19 @@ kore_resize_last_alloc(void *oldptr, size_t newrequest, size_t last_size) {
113113
}
114114

115115
void *kore_alloc_mp(size_t requested) {
116+
bool enabled = gc_enabled;
117+
gc_enabled = false;
116118
auto *new_token = (string *)kore_alloc_token(sizeof(string) + requested);
119+
gc_enabled = enabled;
117120
init_with_len(new_token, requested);
118121
return new_token->data;
119122
}
120123

121124
void *kore_realloc_mp(void *ptr, size_t old_size, size_t new_size) {
125+
bool enabled = gc_enabled;
126+
gc_enabled = false;
122127
auto *new_token = (string *)kore_alloc_token(sizeof(string) + new_size);
128+
gc_enabled = enabled;
123129
size_t min = old_size > new_size ? new_size : old_size;
124130
memcpy(new_token->data, ptr, min);
125131
init_with_len(new_token, new_size);

runtime/alloc/arena.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static void fresh_block(struct arena *arena) {
116116
BLOCK_SIZE - sizeof(memory_block_header));
117117
}
118118

119+
bool gc_enabled;
120+
119121
static __attribute__((noinline)) void *
120122
do_alloc_slow(size_t requested, struct arena *arena) {
121123
MEM_LOG(

runtime/util/ConfigurationParser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct construction {
9898

9999
// NOLINTNEXTLINE(*-cognitive-complexity)
100100
extern "C" void *construct_initial_configuration(kore_pattern const *initial) {
101+
gc_enabled = false;
101102
std::vector<std::variant<kore_pattern const *, construction>> work_list{
102103
initial};
103104
std::vector<void *> output;
@@ -156,13 +157,15 @@ extern "C" void *construct_initial_configuration(kore_pattern const *initial) {
156157
}
157158
}
158159

160+
gc_enabled = true;
159161
return output[0];
160162
}
161163

162164
// NOLINTBEGIN(*-cognitive-complexity)
163165
template <typename It>
164166
static void *
165167
deserialize_initial_configuration(It ptr, It end, binary_version version) {
168+
gc_enabled = false;
166169
using namespace kllvm::detail;
167170
auto begin = ptr;
168171

@@ -253,6 +256,7 @@ deserialize_initial_configuration(It ptr, It end, binary_version version) {
253256
}
254257
}
255258

259+
gc_enabled = true;
256260
assert(output.size() == 1 && "Output stack left in invalid state");
257261
return output.front();
258262
}

unittests/runtime-collections/lists.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ block D1 = {{1}};
6363
block *DUMMY1 = &D1;
6464
}
6565

66+
bool gc_enabled;
67+
6668
BOOST_AUTO_TEST_SUITE(ListTest)
6769

6870
BOOST_AUTO_TEST_CASE(element) {

0 commit comments

Comments
 (0)