From f6083d1c8af3d7b1b659bdc7ed710b2cbf574df2 Mon Sep 17 00:00:00 2001 From: Dwight Guth Date: Mon, 3 Jun 2024 12:03:50 -0500 Subject: [PATCH] add flag to control whether gc should be enabled or not --- include/kllvm/codegen/CreateTerm.h | 2 ++ include/runtime/header.h | 5 +++++ lib/codegen/CreateTerm.cpp | 23 ++++++++++++++++++++++- runtime/alloc/alloc.cpp | 6 ++++++ runtime/alloc/arena.cpp | 2 ++ runtime/util/ConfigurationParser.cpp | 4 ++++ unittests/runtime-collections/lists.cpp | 2 ++ 7 files changed, 43 insertions(+), 1 deletion(-) diff --git a/include/kllvm/codegen/CreateTerm.h b/include/kllvm/codegen/CreateTerm.h index 16ca886b3..a9d41aade 100644 --- a/include/kllvm/codegen/CreateTerm.h +++ b/include/kllvm/codegen/CreateTerm.h @@ -35,6 +35,8 @@ class create_term { bool populate_static_set(kore_pattern *pattern); std::pair create_allocation( kore_pattern *pattern, std::string const &location_stack = ""); + llvm::Value *disable_gc(); + void enable_gc(llvm::Value *was_enabled); public: create_term( diff --git a/include/runtime/header.h b/include/runtime/header.h index c8d4c75be..0c9ab7849 100644 --- a/include/runtime/header.h +++ b/include/runtime/header.h @@ -111,6 +111,8 @@ size_t hash_k(block *); void k_hash(block *, void *); bool hash_enter(void); void hash_exit(void); + +extern bool gc_enabled; } __attribute__((always_inline)) constexpr uint64_t len_hdr(uint64_t hdr) { @@ -225,7 +227,10 @@ struct kore_alloc_heap { if (during_gc()) { return ::operator new(size); } + bool enabled = gc_enabled; + gc_enabled = false; auto *result = (string *)kore_alloc_token(size + sizeof(blockheader)); + gc_enabled = enabled; init_with_len(result, size); return result->data; } diff --git a/lib/codegen/CreateTerm.cpp b/lib/codegen/CreateTerm.cpp index 77410eca5..f0c24b1c4 100644 --- a/lib/codegen/CreateTerm.cpp +++ b/lib/codegen/CreateTerm.cpp @@ -673,8 +673,29 @@ llvm::Value *create_term::create_hook( } std::string hook_name = "hook_" + name.substr(0, name.find('.')) + "_" + name.substr(name.find('.') + 1); - return create_function_call( + auto *old_val = disable_gc(); + auto *result = create_function_call( hook_name, pattern, true, false, true, location_stack); + enable_gc(old_val); + return result; +} + +llvm::Value *create_term::disable_gc() { + llvm::Constant *global + = module_->getOrInsertGlobal("gc_enabled", llvm::Type::getInt1Ty(ctx_)); + auto *global_var = llvm::cast(global); + auto old_val = new llvm::LoadInst( + llvm::Type::getInt1Ty(ctx_), global_var, "was_enabled", current_block_); + new llvm::StoreInst( + llvm::ConstantInt::getFalse(ctx_), global_var, current_block_); + return old_val; +} + +void create_term::enable_gc(llvm::Value *was_enabled) { + llvm::Constant *global + = module_->getOrInsertGlobal("gc_enabled", llvm::Type::getInt1Ty(ctx_)); + auto *global_var = llvm::cast(global); + new llvm::StoreInst(was_enabled, global_var, current_block_); } // We use tailcc calling convention for apply_rule_* and eval_* functions to diff --git a/runtime/alloc/alloc.cpp b/runtime/alloc/alloc.cpp index 51afdab73..d1787bc15 100644 --- a/runtime/alloc/alloc.cpp +++ b/runtime/alloc/alloc.cpp @@ -113,13 +113,19 @@ kore_resize_last_alloc(void *oldptr, size_t newrequest, size_t last_size) { } void *kore_alloc_mp(size_t requested) { + bool enabled = gc_enabled; + gc_enabled = false; auto *new_token = (string *)kore_alloc_token(sizeof(string) + requested); + gc_enabled = enabled; init_with_len(new_token, requested); return new_token->data; } void *kore_realloc_mp(void *ptr, size_t old_size, size_t new_size) { + bool enabled = gc_enabled; + gc_enabled = false; auto *new_token = (string *)kore_alloc_token(sizeof(string) + new_size); + gc_enabled = enabled; size_t min = old_size > new_size ? new_size : old_size; memcpy(new_token->data, ptr, min); init_with_len(new_token, new_size); diff --git a/runtime/alloc/arena.cpp b/runtime/alloc/arena.cpp index 284c8fd38..fbe1046fd 100644 --- a/runtime/alloc/arena.cpp +++ b/runtime/alloc/arena.cpp @@ -116,6 +116,8 @@ static void fresh_block(struct arena *arena) { BLOCK_SIZE - sizeof(memory_block_header)); } +bool gc_enabled; + static __attribute__((noinline)) void * do_alloc_slow(size_t requested, struct arena *arena) { MEM_LOG( diff --git a/runtime/util/ConfigurationParser.cpp b/runtime/util/ConfigurationParser.cpp index 2f74372e2..01656fccf 100644 --- a/runtime/util/ConfigurationParser.cpp +++ b/runtime/util/ConfigurationParser.cpp @@ -98,6 +98,7 @@ struct construction { // NOLINTNEXTLINE(*-cognitive-complexity) extern "C" void *construct_initial_configuration(kore_pattern const *initial) { + gc_enabled = false; std::vector> work_list{ initial}; std::vector output; @@ -156,6 +157,7 @@ extern "C" void *construct_initial_configuration(kore_pattern const *initial) { } } + gc_enabled = true; return output[0]; } @@ -163,6 +165,7 @@ extern "C" void *construct_initial_configuration(kore_pattern const *initial) { template static void * deserialize_initial_configuration(It ptr, It end, binary_version version) { + gc_enabled = false; using namespace kllvm::detail; auto begin = ptr; @@ -253,6 +256,7 @@ deserialize_initial_configuration(It ptr, It end, binary_version version) { } } + gc_enabled = true; assert(output.size() == 1 && "Output stack left in invalid state"); return output.front(); } diff --git a/unittests/runtime-collections/lists.cpp b/unittests/runtime-collections/lists.cpp index ab9ea550e..96dbdafcc 100644 --- a/unittests/runtime-collections/lists.cpp +++ b/unittests/runtime-collections/lists.cpp @@ -63,6 +63,8 @@ block D1 = {{1}}; block *DUMMY1 = &D1; } +bool gc_enabled; + BOOST_AUTO_TEST_SUITE(ListTest) BOOST_AUTO_TEST_CASE(element) {