Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further cleanup GC code for map/list/set #1086

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/kllvm/codegen/Util.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef KLLVM_UTIL_H
#define KLLVM_UTIL_H

#include <kllvm/ast/AST.h>

#include <llvm/Config/llvm-config.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/DerivedTypes.h>
Expand Down Expand Up @@ -38,6 +40,8 @@ llvm::Function *get_or_insert_function(llvm::Module *module, Ts &&...args) {
return func;
}

char const *get_collection_alloc_fn(sort_category cat);

} // namespace kllvm

#endif // KLLVM_UTIL_H
8 changes: 0 additions & 8 deletions include/runtime/collect.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ void migrate_set(void *s);
void migrate_collection_node(void **node_ptr);
void set_kore_memory_functions_for_gmp(void);
void kore_collect(void **, uint8_t, layoutitem *);
void store_map_for_gc(void **, map *);
void store_set_for_gc(void **, set *);
void store_list_for_gc(void **, list *);
void store_rangemap_for_gc(void **, rangemap *);
map *load_map_for_gc(void **);
set *load_set_for_gc(void **);
list *load_list_for_gc(void **);
rangemap *load_rangemap_for_gc(void **);
}

#ifdef GC_DBG
Expand Down
6 changes: 3 additions & 3 deletions lib/codegen/CreateTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,8 @@ llvm::Value *create_term::create_function_call(
if (sret) {
// we don't use alloca here because the tail call optimization pass for llvm
// doesn't handle correctly functions with alloca
alloc_sret
= allocate_term(return_type, current_block_, "kore_alloc_always_gc");
alloc_sret = allocate_term(
return_type, current_block_, get_collection_alloc_fn(return_cat.cat));
sret_type = return_type;
real_args.insert(real_args.begin(), alloc_sret);
types.insert(types.begin(), alloc_sret->getType());
Expand Down Expand Up @@ -1244,7 +1244,7 @@ std::string make_apply_rule_function(
if (!arg->getType()->isPointerTy()) {
auto *ptr = allocate_term(
arg->getType(), creator.get_current_block(),
"kore_alloc_always_gc");
get_collection_alloc_fn(cat.cat));
new llvm::StoreInst(arg, ptr, creator.get_current_block());
arg = ptr;
}
Expand Down
88 changes: 7 additions & 81 deletions lib/codegen/Decision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,8 @@ void add_owise(
->get_category(d);
if (is_collection_sort(return_sort)) {
auto *temp_alloc = allocate_term(
retval->getType(), creator.get_current_block(), "kore_alloc_always_gc");
retval->getType(), creator.get_current_block(),
get_collection_alloc_fn(return_sort.cat));
new llvm::StoreInst(retval, temp_alloc, creator.get_current_block());
retval = temp_alloc;
}
Expand All @@ -929,58 +930,16 @@ static void store_ptrs_for_gc(
std::vector<std::pair<llvm::Value *, llvm::Type *>> &root_ptrs) {
auto *zero
= llvm::ConstantInt::get(llvm::Type::getInt64Ty(module->getContext()), 0);
llvm::Type *voidptrptr = llvm::PointerType::getUnqual(
llvm::PointerType::getUnqual(module->getContext()));
for (unsigned i = 0; i < nroots; i++) {
auto *ptr = llvm::GetElementPtrInst::CreateInBounds(
root_ty, arr,
{zero, llvm::ConstantInt::get(
llvm::Type::getInt64Ty(module->getContext()), i)},
"", collect);
switch (types[i].cat) {
case sort_category::Map:
llvm::CallInst::Create(
get_or_insert_function(
module, "store_map_for_gc",
llvm::Type::getInt1Ty(module->getContext()), voidptrptr,
ptr_types[i]),
{ptr, roots[i]}, "", collect);
root_ptrs.emplace_back(ptr, ptr_types[i]);
break;
case sort_category::RangeMap:
llvm::CallInst::Create(
get_or_insert_function(
module, "store_rangemap_for_gc",
llvm::Type::getInt1Ty(module->getContext()), voidptrptr,
ptr_types[i]),
{ptr, roots[i]}, "", collect);
root_ptrs.emplace_back(ptr, ptr_types[i]);
break;
case sort_category::List:
llvm::CallInst::Create(
get_or_insert_function(
module, "store_list_for_gc",
llvm::Type::getInt1Ty(module->getContext()), voidptrptr,
ptr_types[i]),
{ptr, roots[i]}, "", collect);
root_ptrs.emplace_back(ptr, ptr_types[i]);
break;
case sort_category::Set:
llvm::CallInst::Create(
get_or_insert_function(
module, "store_set_for_gc",
llvm::Type::getInt1Ty(module->getContext()), voidptrptr,
ptr_types[i]),
{ptr, roots[i]}, "", collect);
root_ptrs.emplace_back(ptr, ptr_types[i]);
break;
default:
auto *casted = new llvm::BitCastInst(
ptr, llvm::PointerType::getUnqual(ptr_types[i]), "", collect);
new llvm::StoreInst(roots[i], casted, collect);
root_ptrs.emplace_back(casted, ptr_types[i]);
break;
}
auto *casted = new llvm::BitCastInst(
ptr, llvm::PointerType::getUnqual(ptr_types[i]), "", collect);
new llvm::StoreInst(roots[i], casted, collect);
root_ptrs.emplace_back(casted, ptr_types[i]);
}
}

Expand All @@ -992,42 +951,9 @@ static void load_ptrs_for_gc(
std::vector<llvm::Value *> &phis, std::vector<llvm::Value *> const &roots,
std::vector<std::pair<llvm::Value *, llvm::Type *>> const &root_ptrs,
std::vector<value_type> const &types) {
llvm::Type *voidptrptr = llvm::PointerType::getUnqual(
llvm::PointerType::getUnqual(module->getContext()));
unsigned i = 0;
for (auto [ptr, pointee_ty] : root_ptrs) {
llvm::Value *loaded = nullptr;
switch (types[i].cat) {
case sort_category::Map:
loaded = llvm::CallInst::Create(
get_or_insert_function(
module, "load_map_for_gc", pointee_ty, voidptrptr,
llvm::Type::getInt1Ty(module->getContext())),
{ptr}, "", collect);
break;
case sort_category::RangeMap:
loaded = llvm::CallInst::Create(
get_or_insert_function(
module, "load_rangemap_for_gc", pointee_ty, voidptrptr,
llvm::Type::getInt1Ty(module->getContext())),
{ptr}, "", collect);
break;
case sort_category::List:
loaded = llvm::CallInst::Create(
get_or_insert_function(
module, "load_list_for_gc", pointee_ty, voidptrptr,
llvm::Type::getInt1Ty(module->getContext())),
{ptr}, "", collect);
break;
case sort_category::Set:
loaded = llvm::CallInst::Create(
get_or_insert_function(
module, "load_set_for_gc", pointee_ty, voidptrptr,
llvm::Type::getInt1Ty(module->getContext())),
{ptr}, "", collect);
break;
default: loaded = new llvm::LoadInst(pointee_ty, ptr, "", collect);
}
llvm::Value *loaded = new llvm::LoadInst(pointee_ty, ptr, "", collect);
auto *phi = llvm::PHINode::Create(loaded->getType(), 2, "phi", merge);
phi->addIncoming(loaded, collect);
phi->addIncoming(roots[i++], check_collect);
Expand Down
10 changes: 10 additions & 0 deletions lib/codegen/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ llvm::Constant *get_offset_of_member(
#endif
}

char const *get_collection_alloc_fn(sort_category cat) {
switch (cat) {
case sort_category::Map: return "kore_alloc_map";
case sort_category::Set: return "kore_alloc_set";
case sort_category::List: return "kore_alloc_list";
case sort_category::RangeMap: return "kore_alloc_rangemap";
default: abort();
}
}

} // namespace kllvm
33 changes: 33 additions & 0 deletions runtime/alloc/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,37 @@ __attribute__((always_inline)) void *kore_alloc_floating_old(size_t requested) {
init_with_len(result, sizeof(floating_hdr) - sizeof(blockheader));
return &result->f;
}

extern "C++" {
template <typename collection>
static inline void *kore_alloc_collection(kllvm::sort_category cat) {
void *mem
= kore_alloc(sizeof(blockheader) + sizeof(collection) + sizeof(uint64_t));
auto *hdr = (blockheader *)mem;
static std::string name = get_raw_symbol_name(cat) + "{}";
static blockheader hdr_val
= get_block_header_for_symbol(get_tag_for_symbol_name(name.c_str()));
*hdr = hdr_val;
auto *offset = (uint64_t *)(hdr + 1);
*offset = 16;
auto *child = hdr + 2;
return child;
}
}

void *kore_alloc_map(size_t requested) {
return kore_alloc_collection<map>(kllvm::sort_category::Map);
}

void *kore_alloc_set(size_t requested) {
return kore_alloc_collection<set>(kllvm::sort_category::Set);
}

void *kore_alloc_list(size_t requested) {
return kore_alloc_collection<list>(kllvm::sort_category::List);
}

void *kore_alloc_rangemap(size_t requested) {
return kore_alloc_collection<rangemap>(kllvm::sort_category::RangeMap);
}
}
85 changes: 0 additions & 85 deletions runtime/collect/collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,89 +360,4 @@ bool is_collection() {
size_t threshold = get_gc_threshold();
return youngspace_almost_full(threshold);
}

void store_map_for_gc(void **roots, map *ptr) {
if (get_arena_semispace_id_of_object(ptr) != ALWAYSGCSPACE_ID) {
*roots = ptr;
return;
}
void *mem = kore_alloc(sizeof(blockheader) + sizeof(map) + sizeof(uint64_t));
auto *hdr = (blockheader *)mem;
std::string name = get_raw_symbol_name(kllvm::sort_category::Map) + "{}";
*hdr = get_block_header_for_symbol(get_tag_for_symbol_name(name.c_str()));
auto *offset = (uint64_t *)(hdr + 1);
*offset = 16;
auto *child = (map *)(hdr + 2);
*child = std::move(*ptr);
*roots = child;
}

void store_set_for_gc(void **roots, set *ptr) {
if (get_arena_semispace_id_of_object(ptr) != ALWAYSGCSPACE_ID) {
*roots = ptr;
return;
}
void *mem = kore_alloc(sizeof(blockheader) + sizeof(set) + sizeof(uint64_t));
auto *hdr = (blockheader *)mem;
std::string name = get_raw_symbol_name(kllvm::sort_category::Set) + "{}";
*hdr = get_block_header_for_symbol(get_tag_for_symbol_name(name.c_str()));
auto *offset = (uint64_t *)(hdr + 1);
*offset = 16;
auto *child = (set *)(hdr + 2);
*child = std::move(*ptr);
*roots = child;
}

void store_list_for_gc(void **roots, list *ptr) {
if (get_arena_semispace_id_of_object(ptr) != ALWAYSGCSPACE_ID) {
*roots = ptr;
return;
}
void *mem = kore_alloc(sizeof(blockheader) + sizeof(list) + sizeof(uint64_t));
auto *hdr = (blockheader *)mem;
std::string name = get_raw_symbol_name(kllvm::sort_category::List) + "{}";
*hdr = get_block_header_for_symbol(get_tag_for_symbol_name(name.c_str()));
auto *offset = (uint64_t *)(hdr + 1);
*offset = 16;
auto *child = (list *)(hdr + 2);
*child = std::move(*ptr);
*roots = child;
}

void store_rangemap_for_gc(void **roots, rangemap *ptr) {
if (get_arena_semispace_id_of_object(ptr) != ALWAYSGCSPACE_ID) {
*roots = ptr;
return;
}
void *mem
= kore_alloc(sizeof(blockheader) + sizeof(rangemap) + sizeof(uint64_t));
auto *hdr = (blockheader *)mem;
std::string name = get_raw_symbol_name(kllvm::sort_category::RangeMap) + "{}";
*hdr = get_block_header_for_symbol(get_tag_for_symbol_name(name.c_str()));
auto *offset = (uint64_t *)(hdr + 1);
*offset = 16;
auto *child = (rangemap *)(hdr + 2);
*child = std::move(*ptr);
*roots = child;
}

map *load_map_for_gc(void **roots) {
void *mem = *roots;
return (map *)mem;
}

set *load_set_for_gc(void **roots) {
void *mem = *roots;
return (set *)mem;
}

list *load_list_for_gc(void **roots) {
void *mem = *roots;
return (list *)mem;
}

rangemap *load_rangemap_for_gc(void **roots) {
void *mem = *roots;
return (rangemap *)mem;
}
}
4 changes: 4 additions & 0 deletions unittests/runtime-strings/stringtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ floating *move_float(floating *i) {
}

void add_hash64(void *, uint64_t) { }

struct blockheader get_block_header_for_symbol(uint32_t tag) {
return blockheader{tag};
}
}

BOOST_AUTO_TEST_SUITE(StringTest)
Expand Down
Loading