Skip to content

Commit ac9cff8

Browse files
Dwight Guthrv-jenkins
andauthored
Finalize removal of typed pointers (#1103)
Previously we still had some references to typed pointers in .ll files and in API calls that still created typed pointers when working with older versions of LLVM. This led to some invalid bitcode being generated in ways that wasn't necessarily a problem, but which caused problems when I was making changes to that code. This PR is a preliminary to further changes which removes all remaining references to the API constructor for typed pointers and removes all typed pointers from flat bitcode files. --------- Co-authored-by: rv-jenkins <[email protected]>
1 parent 67376bc commit ac9cff8

File tree

15 files changed

+241
-429
lines changed

15 files changed

+241
-429
lines changed

include/kllvm/codegen/Util.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ llvm::Constant *get_offset_of_member(llvm::Module *, llvm::StructType *, int);
2222
llvm::Function *kore_heap_alloc(std::string const &name, llvm::Module *module);
2323

2424
llvm::Instruction *create_malloc(
25-
llvm::BasicBlock *block, llvm::Type *int_ptr_ty, llvm::Type *alloc_ty,
26-
llvm::Value *alloc_size, llvm::Value *array_size, llvm::Function *malloc_f,
27-
std::string const &name = "");
25+
llvm::BasicBlock *block, llvm::Value *alloc_size, llvm::Function *malloc_f);
2826

2927
// getOrInsertFunction on module, aborting on failure
3028
template <class... Ts>

lib/codegen/CreateStaticTerm.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ llvm::Constant *create_static_term::not_injection_case(
8686
return llvm::ConstantExpr::getBitCast(
8787
llvm::ConstantExpr::getInBoundsGetElementPtr(
8888
block_type, global_var, idxs),
89-
llvm::PointerType::getUnqual(llvm::StructType::getTypeByName(
90-
module_->getContext(), block_struct)));
89+
llvm::PointerType::getUnqual(module_->getContext()));
9190
}
9291

9392
std::pair<llvm::Constant *, bool>
@@ -106,13 +105,11 @@ create_static_term::operator()(kore_pattern *pattern) {
106105
false);
107106
}
108107
if (symbol->get_arguments().empty()) {
109-
llvm::StructType *block_type = llvm::StructType::getTypeByName(
110-
module_->getContext(), block_struct);
111108
llvm::Constant *cast = llvm::ConstantExpr::getIntToPtr(
112109
llvm::ConstantInt::get(
113110
llvm::Type::getInt64Ty(ctx_),
114111
(((uint64_t)symbol->get_tag()) << 32) | 1),
115-
llvm::PointerType::getUnqual(block_type));
112+
llvm::PointerType::getUnqual(module_->getContext()));
116113
return std::make_pair(cast, false);
117114
}
118115
kore_symbol_declaration *symbol_decl
@@ -144,6 +141,7 @@ create_static_term::operator()(kore_pattern *pattern) {
144141
// NOLINTBEGIN(*-cognitive-complexity)
145142
llvm::Constant *
146143
create_static_term::create_token(value_type sort, std::string contents) {
144+
auto *ptr_ty = llvm::PointerType::getUnqual(ctx_);
147145
switch (sort.cat) {
148146
case sort_category::Map:
149147
case sort_category::RangeMap:
@@ -193,8 +191,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
193191
llvm::StructType::getTypeByName(
194192
module_->getContext(), int_struct),
195193
num_limbs, mp_size,
196-
llvm::ConstantExpr::getPointerCast(
197-
limbs_var, llvm::PointerType::getUnqual(ctx_)))));
194+
llvm::ConstantExpr::getPointerCast(limbs_var, ptr_ty))));
198195
mpz_clear(value);
199196
}
200197
std::vector<llvm::Constant *> idxs
@@ -289,8 +286,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
289286
expbits,
290287
llvm::ConstantStruct::getAnon(
291288
{mpfr_prec, mpfr_sign, mpfr_exp,
292-
llvm::ConstantExpr::getPointerCast(
293-
limbs_var, llvm::PointerType::getUnqual(ctx_))}))));
289+
llvm::ConstantExpr::getPointerCast(limbs_var, ptr_ty)}))));
294290
mpfr_clear(value);
295291
}
296292
std::vector<llvm::Constant *> idxs
@@ -336,9 +332,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
336332
string_type, block_header,
337333
llvm::ConstantDataArray::getString(ctx_, contents, false)));
338334
}
339-
return llvm::ConstantExpr::getPointerCast(
340-
global, llvm::PointerType::getUnqual(llvm::StructType::getTypeByName(
341-
module_->getContext(), block_struct)));
335+
return llvm::ConstantExpr::getPointerCast(global, ptr_ty);
342336
}
343337
case sort_category::SetIter:
344338
case sort_category::MapIter:

lib/codegen/CreateTerm.cpp

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,20 @@ target triple = "{triple}"
7676
; We also define the following LLVM structure types:
7777
7878
%string = type { %blockheader, [0 x i8] } ; 10-bit layout, 4-bit gc flags, 10 unused bits, 40-bit length (or buffer capacity for string pointed by stringbuffers), bytes
79-
%stringbuffer = type { i64, i64, %string* } ; 10-bit layout, 4-bit gc flags, 10 unused bits, 40-bit length, string length, current contents
80-
%map = type { { i8 *, i64 } } ; immer::map
81-
%rangemap = type { { { { { i32 (...)**, i32, i64 }*, { { i32 (...)**, i32, i32 }* } } } } } ; rng_map::RangeMap
82-
%set = type { { i8 *, i64 } } ; immer::set
83-
%iter = type { { i8 *, i8 *, i32, [14 x i8**] }, { { i8 *, i64 } } } ; immer::map_iter / immer::set_iter
84-
%list = type { { i64, i32, i8 *, i8 * } } ; immer::flex_vector
85-
%mpz = type { i32, i32, i64 * } ; mpz_t
79+
%stringbuffer = type { i64, i64, ptr } ; 10-bit layout, 4-bit gc flags, 10 unused bits, 40-bit length, string length, current contents
80+
%map = type { { ptr, i64 } } ; immer::map
81+
%rangemap = type { { { { ptr, { ptr } } } } } ; rng_map::RangeMap
82+
%set = type { { ptr, i64 } } ; immer::set
83+
%iter = type { { ptr, ptr, i32, [14 x ptr] }, { { ptr, i64 } } } ; immer::map_iter / immer::set_iter
84+
%list = type { { i64, i32, ptr, ptr } } ; immer::flex_vector
85+
%mpz = type { i32, i32, ptr } ; mpz_t
8686
%mpz_hdr = type { %blockheader, %mpz } ; 10-bit layout, 4-bit gc flags, 10 unused bits, 40-bit length, mpz_t
87-
%floating = type { i64, { i64, i32, i64, i64 * } } ; exp, mpfr_t
87+
%floating = type { i64, { i64, i32, i64, ptr } } ; exp, mpfr_t
8888
%floating_hdr = type { %blockheader, %floating } ; 10-bit layout, 4-bit gc flags, 10 unused bits, 40-bit length, floating
8989
%blockheader = type { i64 }
90-
%block = type { %blockheader, [0 x i64 *] } ; 16-bit layout, 8-bit length, 32-bit tag, children
90+
%block = type { %blockheader, [0 x ptr] } ; 16-bit layout, 8-bit length, 32-bit tag, children
9191
92-
%layout = type { i8, %layoutitem* } ; number of children, array of children
92+
%layout = type { i8, ptr } ; number of children, array of children
9393
%layoutitem = type { i64, i16 } ; offset, category
9494
9595
; The layout of a block uniquely identifies the categories of its children as
@@ -111,8 +111,8 @@ target triple = "{triple}"
111111
; %layoutN = type { %blockheader, [0 x i64 *], %map, %mpz *, %block * }
112112
113113
; Interface to the configuration parser
114-
declare %block* @parse_configuration(i8*)
115-
declare void @print_configuration(i8 *, %block *)
114+
declare ptr @parse_configuration(ptr)
115+
declare void @print_configuration(ptr, ptr)
116116
)LLVM";
117117
return target_dependent + rest;
118118
}
@@ -147,7 +147,9 @@ llvm::Type *get_param_type(value_type sort, llvm::Module *module) {
147147
case sort_category::Map:
148148
case sort_category::RangeMap:
149149
case sort_category::List:
150-
case sort_category::Set: type = llvm::PointerType::getUnqual(type); break;
150+
case sort_category::Set:
151+
type = llvm::PointerType::getUnqual(module->getContext());
152+
break;
151153
default: break;
152154
}
153155
return type;
@@ -168,22 +170,15 @@ llvm::Type *getvalue_type(value_type sort, llvm::Module *module) {
168170
return llvm::StructType::getTypeByName(module->getContext(), list_struct);
169171
case sort_category::Set:
170172
return llvm::StructType::getTypeByName(module->getContext(), set_struct);
171-
case sort_category::Int:
172-
return llvm::PointerType::getUnqual(
173-
llvm::StructType::getTypeByName(module->getContext(), int_struct));
174-
case sort_category::Float:
175-
return llvm::PointerType::getUnqual(
176-
llvm::StructType::getTypeByName(module->getContext(), float_struct));
177-
case sort_category::StringBuffer:
178-
return llvm::PointerType::getUnqual(
179-
llvm::StructType::getTypeByName(module->getContext(), buffer_struct));
180173
case sort_category::Bool: return llvm::Type::getInt1Ty(module->getContext());
181174
case sort_category::MInt:
182175
return llvm::IntegerType::get(module->getContext(), sort.bits);
176+
case sort_category::Int:
177+
case sort_category::Float:
178+
case sort_category::StringBuffer:
183179
case sort_category::Symbol:
184180
case sort_category::Variable:
185-
return llvm::PointerType::getUnqual(
186-
llvm::StructType::getTypeByName(module->getContext(), block_struct));
181+
return llvm::PointerType::getUnqual(module->getContext());
187182
case sort_category::MapIter:
188183
case sort_category::SetIter:
189184
case sort_category::Uncomputed: abort();
@@ -268,8 +263,7 @@ llvm::Value *allocate_term(
268263
llvm::Type *alloc_type, llvm::Value *len, llvm::BasicBlock *block,
269264
char const *alloc_fn) {
270265
auto *malloc = create_malloc(
271-
block, llvm::Type::getInt64Ty(block->getContext()), alloc_type, len,
272-
nullptr, kore_heap_alloc(alloc_fn, block->getModule()));
266+
block, len, kore_heap_alloc(alloc_fn, block->getModule()));
273267

274268
set_debug_loc(malloc);
275269
return malloc;
@@ -340,6 +334,7 @@ std::string escape(std::string const &str) {
340334
llvm::Value *create_term::create_hook(
341335
kore_composite_pattern *hook_att, kore_composite_pattern *pattern,
342336
std::string const &location_stack) {
337+
auto *ptr_ty = llvm::PointerType::getUnqual(ctx_);
343338
assert(hook_att->get_arguments().size() == 1);
344339
auto *str_pattern
345340
= dynamic_cast<kore_string_pattern *>(hook_att->get_arguments()[0].get());
@@ -513,9 +508,8 @@ llvm::Value *create_term::create_hook(
513508
auto *result = llvm::CallInst::Create(
514509
get_or_insert_function(
515510
module_, "hook_MINT_import",
516-
getvalue_type({sort_category::Int, 0}, module_),
517-
llvm::PointerType::getUnqual(ctx_), llvm::Type::getInt64Ty(ctx_),
518-
llvm::Type::getInt1Ty(ctx_)),
511+
getvalue_type({sort_category::Int, 0}, module_), ptr_ty,
512+
llvm::Type::getInt64Ty(ctx_), llvm::Type::getInt1Ty(ctx_)),
519513
{ptr, llvm::ConstantInt::get(llvm::Type::getInt64Ty(ctx_), cat.bits),
520514
llvm::ConstantInt::getFalse(ctx_)},
521515
"hook_MINT_uvalue", current_block_);
@@ -565,9 +559,8 @@ llvm::Value *create_term::create_hook(
565559
auto *result = llvm::CallInst::Create(
566560
get_or_insert_function(
567561
module_, "hook_MINT_import",
568-
getvalue_type({sort_category::Int, 0}, module_),
569-
llvm::PointerType::getUnqual(ctx_), llvm::Type::getInt64Ty(ctx_),
570-
llvm::Type::getInt1Ty(ctx_)),
562+
getvalue_type({sort_category::Int, 0}, module_), ptr_ty,
563+
llvm::Type::getInt64Ty(ctx_), llvm::Type::getInt1Ty(ctx_)),
571564
{ptr, llvm::ConstantInt::get(llvm::Type::getInt64Ty(ctx_), cat.bits),
572565
llvm::ConstantInt::getTrue(ctx_)},
573566
"hook_MINT_svalue", current_block_);
@@ -582,7 +575,7 @@ llvm::Value *create_term::create_hook(
582575
auto *type = getvalue_type(cat, module_);
583576
llvm::Instruction *ptr = llvm::CallInst::Create(
584577
get_or_insert_function(
585-
module_, "hook_MINT_export", llvm::PointerType::getUnqual(ctx_),
578+
module_, "hook_MINT_export", ptr_ty,
586579
getvalue_type({sort_category::Int, 0}, module_),
587580
llvm::Type::getInt64Ty(ctx_)),
588581
{mpz, llvm::ConstantInt::get(llvm::Type::getInt64Ty(ctx_), cat.bits)},
@@ -784,7 +777,7 @@ llvm::Value *create_term::create_function_call(
784777
types.insert(types.begin(), alloc_sret->getType());
785778
return_type = llvm::Type::getVoidTy(ctx_);
786779
} else if (collection) {
787-
return_type = llvm::PointerType::getUnqual(return_type);
780+
return_type = llvm::PointerType::getUnqual(ctx_);
788781
}
789782

790783
llvm::FunctionType *func_type
@@ -868,8 +861,7 @@ llvm::Value *create_term::not_injection_case(
868861
new llvm::StoreInst(child_value, child_ptr, current_block_);
869862
}
870863

871-
auto *block_ptr = llvm::PointerType::getUnqual(
872-
llvm::StructType::getTypeByName(module_->getContext(), block_struct));
864+
auto *block_ptr = llvm::PointerType::getUnqual(module_->getContext());
873865
auto *bitcast = new llvm::BitCastInst(block, block_ptr, "", current_block_);
874866
if (symbol_decl->attributes().contains(attribute_set::key::Binder)) {
875867
auto *call = llvm::CallInst::Create(
@@ -1056,6 +1048,7 @@ bool make_function(
10561048
std::vector<llvm::Type *> param_types;
10571049
std::vector<std::string> param_names;
10581050
std::vector<llvm::Metadata *> debug_args;
1051+
auto *ptr_ty = llvm::PointerType::getUnqual(module->getContext());
10591052
for (auto &entry : vars) {
10601053
auto *sort
10611054
= dynamic_cast<kore_composite_sort *>(entry.second->get_sort().get());
@@ -1070,9 +1063,7 @@ bool make_function(
10701063
case sort_category::Map:
10711064
case sort_category::RangeMap:
10721065
case sort_category::List:
1073-
case sort_category::Set:
1074-
param_type = llvm::PointerType::getUnqual(param_type);
1075-
break;
1066+
case sort_category::Set: param_type = ptr_ty; break;
10761067
default: break;
10771068
}
10781069

@@ -1086,9 +1077,7 @@ bool make_function(
10861077
case sort_category::Map:
10871078
case sort_category::RangeMap:
10881079
case sort_category::List:
1089-
case sort_category::Set:
1090-
return_type = llvm::PointerType::getUnqual(return_type);
1091-
break;
1080+
case sort_category::Set: return_type = ptr_ty; break;
10921081
default: break;
10931082
}
10941083
llvm::FunctionType *func_type
@@ -1203,7 +1192,7 @@ std::string make_apply_rule_function(
12031192
case sort_category::RangeMap:
12041193
case sort_category::List:
12051194
case sort_category::Set:
1206-
param_type = llvm::PointerType::getUnqual(param_type);
1195+
param_type = llvm::PointerType::getUnqual(module->getContext());
12071196
break;
12081197
default: break;
12091198
}

0 commit comments

Comments
 (0)