From b29dae56c3d650dd07f92f6ae1d5ef6e0c632d6e Mon Sep 17 00:00:00 2001 From: Dwight Guth Date: Tue, 4 Jun 2024 14:02:50 -0500 Subject: [PATCH] Add MapIter and SetIter sort categories (#1080) This is the first 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 SetIter and MapIter sort category. These sort categories are needed because the stack may contain references to live sets and maps through these iterators that need to be relocated by the GC. However, no K terms will ever be created with these sort categories, so a lot of the code cases currently in the code deliberately do not handle these categories. Future PRs will add GC code to handle fixing up the pointer to a collection contained in these iterators. --------- Co-authored-by: rv-jenkins --- bindings/python/ast.cpp | 2 ++ cmake/RuntimeConfig.cmake | 4 +++- config/macros.h | 2 ++ include/kllvm/ast/AST.h | 6 ++++-- lib/ast/AST.cpp | 9 ++++++--- lib/codegen/CreateStaticTerm.cpp | 2 ++ lib/codegen/CreateTerm.cpp | 2 ++ lib/codegen/Debug.cpp | 11 +++++++++++ lib/codegen/Decision.cpp | 8 +++++++- lib/codegen/EmitConfigParser.cpp | 8 ++++++++ 10 files changed, 47 insertions(+), 7 deletions(-) diff --git a/bindings/python/ast.cpp b/bindings/python/ast.cpp index e20c194e8..63cd265c6 100644 --- a/bindings/python/ast.cpp +++ b/bindings/python/ast.cpp @@ -231,9 +231,11 @@ void bind_ast(py::module_ &m) { py::enum_(ast, "SortCategory") .value("Uncomputed", sort_category::Uncomputed) .value("Map", sort_category::Map) + .value("MapIter", sort_category::MapIter) .value("RangeMap", sort_category::RangeMap) .value("List", sort_category::List) .value("Set", sort_category::Set) + .value("SetIter", sort_category::SetIter) .value("Int", sort_category::Int) .value("Float", sort_category::Float) .value("StringBuffer", sort_category::StringBuffer) diff --git a/cmake/RuntimeConfig.cmake b/cmake/RuntimeConfig.cmake index b84a6e4d9..3b3373ed1 100644 --- a/cmake/RuntimeConfig.cmake +++ b/cmake/RuntimeConfig.cmake @@ -34,7 +34,9 @@ set(STRINGBUFFER_LAYOUT 6) set(BOOL_LAYOUT 7) set(SYMBOL_LAYOUT 8) set(VARIABLE_LAYOUT 9) -set(RANGEMAP_LAYOUT 11) +set(RANGEMAP_LAYOUT 10) +set(SETITER_LAYOUT 11) +set(MAPITER_LAYOUT 12) get_filename_component(INSTALL_DIR_ABS_PATH "${CMAKE_INSTALL_PREFIX}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") diff --git a/config/macros.h b/config/macros.h index 42496905a..d3c248533 100644 --- a/config/macros.h +++ b/config/macros.h @@ -25,6 +25,8 @@ #define SYMBOL_LAYOUT @SYMBOL_LAYOUT@ #define VARIABLE_LAYOUT @VARIABLE_LAYOUT@ #define RANGEMAP_LAYOUT @RANGEMAP_LAYOUT@ +#define SETITER_LAYOUT @SETITER_LAYOUT@ +#define MAPITER_LAYOUT @MAPITER_LAYOUT@ #define STRINGIFY(x) #x #define TOSTRING(X) STRINGIFY(X) diff --git a/include/kllvm/ast/AST.h b/include/kllvm/ast/AST.h index 091721dab..0e2518c72 100644 --- a/include/kllvm/ast/AST.h +++ b/include/kllvm/ast/AST.h @@ -132,8 +132,10 @@ enum class sort_category { Bool, Symbol, Variable, - MInt, - RangeMap + RangeMap, + SetIter, + MapIter, + MInt }; // represents the syntactic category of an LLVM backend term at runtime diff --git a/lib/ast/AST.cpp b/lib/ast/AST.cpp index 778d9695a..ee08703f2 100644 --- a/lib/ast/AST.cpp +++ b/lib/ast/AST.cpp @@ -255,17 +255,20 @@ std::string kore_symbol::layout_string(kore_definition *definition) const { value_type cat = sort->get_category(definition); switch (cat.cat) { case sort_category::Map: result.push_back('1'); break; - case sort_category::RangeMap: result.push_back('b'); break; case sort_category::List: result.push_back('2'); break; case sort_category::Set: result.push_back('3'); break; case sort_category::Int: result.push_back('4'); break; case sort_category::Float: result.push_back('5'); break; case sort_category::StringBuffer: result.push_back('6'); break; case sort_category::Bool: result.push_back('7'); break; - case sort_category::Variable: result.push_back('8'); break; + case sort_category::Symbol: result.push_back('8'); break; + case sort_category::Variable: result.push_back('9'); break; + case sort_category::RangeMap: result.push_back('a'); break; + case sort_category::SetIter: result.push_back('b'); break; + case sort_category::MapIter: result.push_back('c'); break; case sort_category::MInt: result.append("_" + std::to_string(cat.bits) + "_"); - case sort_category::Symbol: result.push_back('0'); break; + break; case sort_category::Uncomputed: abort(); } } diff --git a/lib/codegen/CreateStaticTerm.cpp b/lib/codegen/CreateStaticTerm.cpp index 60193f027..0f1f6ed99 100644 --- a/lib/codegen/CreateStaticTerm.cpp +++ b/lib/codegen/CreateStaticTerm.cpp @@ -340,6 +340,8 @@ create_static_term::create_token(value_type sort, std::string contents) { global, llvm::PointerType::getUnqual(llvm::StructType::getTypeByName( module_->getContext(), block_struct))); } + case sort_category::SetIter: + case sort_category::MapIter: case sort_category::Uncomputed: abort(); } } diff --git a/lib/codegen/CreateTerm.cpp b/lib/codegen/CreateTerm.cpp index d2210fa12..60213224f 100644 --- a/lib/codegen/CreateTerm.cpp +++ b/lib/codegen/CreateTerm.cpp @@ -184,6 +184,8 @@ llvm::Type *getvalue_type(value_type sort, llvm::Module *module) { case sort_category::Variable: return llvm::PointerType::getUnqual( llvm::StructType::getTypeByName(module->getContext(), block_struct)); + case sort_category::MapIter: + case sort_category::SetIter: case sort_category::Uncomputed: abort(); } } diff --git a/lib/codegen/Debug.cpp b/lib/codegen/Debug.cpp index a18b681fc..d3e1dc94b 100644 --- a/lib/codegen/Debug.cpp +++ b/lib/codegen/Debug.cpp @@ -152,6 +152,7 @@ llvm::DIType *get_forward_decl(std::string const &name) { } static std::string map_struct = "map"; +static std::string iter_struct = "iter"; static std::string rangemap_struct = "rangemap"; static std::string list_struct = "list"; static std::string set_struct = "set"; @@ -166,9 +167,11 @@ llvm::DIType *get_debug_type(value_type type, std::string const &type_name) { } static std::map types; llvm::DIType *map = nullptr; + llvm::DIType *mapiter = nullptr; llvm::DIType *rangemap = nullptr; llvm::DIType *list = nullptr; llvm::DIType *set = nullptr; + llvm::DIType *setiter = nullptr; llvm::DIType *integer = nullptr; llvm::DIType *floating = nullptr; llvm::DIType *buffer = nullptr; @@ -183,6 +186,10 @@ llvm::DIType *get_debug_type(value_type type, std::string const &type_name) { map = get_pointer_debug_type(get_forward_decl(map_struct), type_name); types[type_name] = map; return map; + case sort_category::MapIter: + mapiter = get_pointer_debug_type(get_forward_decl(iter_struct), type_name); + types[type_name] = mapiter; + return mapiter; case sort_category::RangeMap: rangemap = get_pointer_debug_type(get_forward_decl(rangemap_struct), type_name); @@ -196,6 +203,10 @@ llvm::DIType *get_debug_type(value_type type, std::string const &type_name) { set = get_pointer_debug_type(get_forward_decl(set_struct), type_name); types[type_name] = set; return set; + case sort_category::SetIter: + setiter = get_pointer_debug_type(get_forward_decl(iter_struct), type_name); + types[type_name] = setiter; + return setiter; case sort_category::Int: integer = get_pointer_debug_type(get_forward_decl(int_struct), type_name); types[type_name] = integer; diff --git a/lib/codegen/Decision.cpp b/lib/codegen/Decision.cpp index b1dfb45ff..1381d7092 100644 --- a/lib/codegen/Decision.cpp +++ b/lib/codegen/Decision.cpp @@ -385,7 +385,7 @@ void make_pattern_node::codegen(decision *d) { * the sort that they originated from (int, bool, symbol, ...). In LLVM versions * < 16, we could encode this information in the LLVM type safely. However, * after the LLVM opaque pointer migration, we can no longer do so (as the - * legacy types %mpz* and %block* would both be %ptr, for example). We + * legacy types %mpz* and %block* would both be ptr, for example). We * therefore define a compatibility translation between sort categories and what * their corresponding LLVM type _would have been_ before opaque pointers. */ @@ -400,9 +400,11 @@ static std::string legacy_value_type_to_string(value_type sort) { // Cases below are deliberately not implemented; the return values are // placeholders to help with debugging only. case sort_category::Map: return ""; + case sort_category::MapIter: return ""; case sort_category::RangeMap: return ""; case sort_category::List: return ""; case sort_category::Set: return ""; + case sort_category::SetIter: return ""; case sort_category::StringBuffer: return ""; case sort_category::MInt: return ""; case sort_category::Uncomputed: abort(); @@ -1084,6 +1086,8 @@ std::pair, llvm::BasicBlock *> step_function_header( break; case sort_category::Bool: case sort_category::MInt: break; + case sort_category::MapIter: + case sort_category::SetIter: case sort_category::Uncomputed: abort(); } i++; @@ -1120,6 +1124,8 @@ std::pair, llvm::BasicBlock *> step_function_header( break; case sort_category::Bool: case sort_category::MInt: break; + case sort_category::MapIter: + case sort_category::SetIter: case sort_category::Uncomputed: abort(); } } diff --git a/lib/codegen/EmitConfigParser.cpp b/lib/codegen/EmitConfigParser.cpp index 20ab9165a..e8f770db8 100644 --- a/lib/codegen/EmitConfigParser.cpp +++ b/lib/codegen/EmitConfigParser.cpp @@ -346,6 +346,8 @@ static llvm::Value *get_arg_value( case sort_category::Variable: arg = new llvm::BitCastInst(arg, getvalue_type(cat, mod), "", case_block); break; + case sort_category::MapIter: + case sort_category::SetIter: case sort_category::Uncomputed: abort(); } return arg; @@ -402,6 +404,8 @@ static std::pair get_eval( creator.get_current_block()); break; } + case sort_category::MapIter: + case sort_category::SetIter: case sort_category::Uncomputed: abort(); } inst->insertAfter(&creator.get_current_block()->back()); @@ -628,6 +632,8 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) { } case sort_category::Variable: case sort_category::Symbol: break; + case sort_category::MapIter: + case sort_category::SetIter: case sort_category::Uncomputed: abort(); } current_block = false_block; @@ -1171,6 +1177,8 @@ static void get_visitor( callbacks.at(2), state_ptr, use_sort_name); break; } + case sort_category::MapIter: + case sort_category::SetIter: case sort_category::Uncomputed: abort(); } if (i != symbol->get_arguments().size() - 1 && use_sort_name) {