update rust code to llvm-16 and fix build

also update:
 - llvm frame address
 - erased function type
 - dec passing to zig
 - gen dev storage size
This commit is contained in:
Brendan Hansknecht 2023-09-21 13:12:48 -07:00
parent 2e2e609547
commit 398bf2f96c
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
20 changed files with 1830 additions and 1432 deletions

View file

@ -37,7 +37,7 @@ impl<'ctx> SharedMemoryPointer<'ctx> {
let call_result = env
.builder
.build_call(func, &[], "call_expect_start_failed");
.new_build_call(func, &[], "call_expect_start_failed");
let ptr = call_result
.try_as_basic_value()
@ -99,7 +99,7 @@ fn read_state<'ctx>(
ptr: PointerValue<'ctx>,
) -> (IntValue<'ctx>, IntValue<'ctx>) {
let ptr_type = env.ptr_int().ptr_type(AddressSpace::default());
let ptr = env.builder.build_pointer_cast(ptr, ptr_type, "");
let ptr = env.builder.new_build_pointer_cast(ptr, ptr_type, "");
let one = env.ptr_int().const_int(1, false);
let offset_ptr = pointer_at_offset(env.builder, env.ptr_int(), ptr, one);
@ -119,13 +119,13 @@ fn write_state<'ctx>(
offset: IntValue<'ctx>,
) {
let ptr_type = env.ptr_int().ptr_type(AddressSpace::default());
let ptr = env.builder.build_pointer_cast(ptr, ptr_type, "");
let ptr = env.builder.new_build_pointer_cast(ptr, ptr_type, "");
let one = env.ptr_int().const_int(1, false);
let offset_ptr = pointer_at_offset(env.builder, env.ptr_int(), ptr, one);
env.builder.build_store(ptr, count);
env.builder.build_store(offset_ptr, offset);
env.builder.new_build_store(ptr, count);
env.builder.new_build_store(offset_ptr, offset);
}
fn offset_add<'ctx>(
@ -134,7 +134,7 @@ fn offset_add<'ctx>(
extra: u32,
) -> IntValue<'ctx> {
let intval = current.get_type().const_int(extra as _, false);
builder.build_int_add(current, intval, "offset_add")
builder.new_build_int_add(current, intval, "offset_add")
}
pub(crate) fn notify_parent_expect(env: &Env, shared_memory: &SharedMemoryPointer) {
@ -143,7 +143,7 @@ pub(crate) fn notify_parent_expect(env: &Env, shared_memory: &SharedMemoryPointe
.get_function(bitcode::NOTIFY_PARENT_EXPECT)
.unwrap();
env.builder.build_call(
env.builder.new_build_call(
func,
&[shared_memory.0.into()],
"call_expect_failed_finalize",
@ -153,7 +153,7 @@ pub(crate) fn notify_parent_expect(env: &Env, shared_memory: &SharedMemoryPointe
pub(crate) fn notify_parent_dbg(env: &Env, shared_memory: &SharedMemoryPointer) {
let func = env.module.get_function(bitcode::NOTIFY_PARENT_DBG).unwrap();
env.builder.build_call(
env.builder.new_build_call(
func,
&[shared_memory.0.into()],
"call_expect_failed_finalize",
@ -203,7 +203,7 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx>(
offset = env
.builder
.build_int_add(offset, space_for_offsets, "offset");
.new_build_int_add(offset, space_for_offsets, "offset");
for lookup in lookups.iter() {
lookup_starts.push(offset);
@ -214,7 +214,7 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx>(
.ptr_int()
.const_int(layout_interner.stack_size(layout) as u64, false);
let mut extra_offset = env.builder.build_int_add(offset, stack_size, "offset");
let mut extra_offset = env.builder.new_build_int_add(offset, stack_size, "offset");
let cursors = Cursors {
offset,
@ -246,7 +246,7 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx>(
.ptr_int()
.const_int(env.target_info.ptr_size() as _, false);
offset = env.builder.build_int_add(offset, ptr_width, "offset");
offset = env.builder.new_build_int_add(offset, ptr_width, "offset");
}
// Store the specialized variable of the value
@ -263,26 +263,26 @@ pub(crate) fn clone_to_shared_memory<'a, 'ctx>(
let u32_ptr = env.context.i32_type().ptr_type(AddressSpace::default());
let ptr = env
.builder
.build_pointer_cast(ptr, u32_ptr, "cast_ptr_type");
.new_build_pointer_cast(ptr, u32_ptr, "cast_ptr_type");
let var_value = env
.context
.i32_type()
.const_int(lookup_var.index() as _, false);
env.builder.build_store(ptr, var_value);
env.builder.new_build_store(ptr, var_value);
let var_size = env
.ptr_int()
.const_int(std::mem::size_of::<u32>() as _, false);
offset = env.builder.build_int_add(offset, var_size, "offset");
offset = env.builder.new_build_int_add(offset, var_size, "offset");
}
}
}
let one = env.ptr_int().const_int(1, false);
let new_count = env.builder.build_int_add(count, one, "inc");
let new_count = env.builder.new_build_int_add(count, one, "inc");
write_state(env, original_ptr, new_count, offset)
}
@ -335,7 +335,7 @@ fn build_clone<'a, 'ctx>(
let ptr_type = value.get_type().ptr_type(AddressSpace::default());
let ptr = env
.builder
.build_pointer_cast(ptr, ptr_type, "cast_ptr_type");
.new_build_pointer_cast(ptr, ptr_type, "cast_ptr_type");
store_roc_value(env, layout_interner, layout, ptr, value);
@ -363,7 +363,7 @@ fn build_clone<'a, 'ctx>(
let bt = basic_type_from_layout(env, layout_interner, layout_interner.get_repr(layout));
// cast the i64 pointer to a pointer to block of memory
let field1_cast = env.builder.build_pointer_cast(
let field1_cast = env.builder.new_build_pointer_cast(
value.into_pointer_value(),
bt.into_pointer_type(),
"i64_to_opaque",
@ -429,7 +429,7 @@ fn build_clone_struct<'a, 'ctx>(
cursors.extra_offset = new_extra;
cursors.offset = env
.builder
.build_int_add(cursors.offset, field_width, "offset");
.new_build_int_add(cursors.offset, field_width, "offset");
}
cursors.extra_offset
@ -496,7 +496,7 @@ fn build_clone_tag<'a, 'ctx>(
}
};
let call = env.builder.build_call(
let call = env.builder.new_build_call(
function,
&[
ptr.into(),
@ -523,17 +523,14 @@ fn load_tag_data<'a, 'ctx>(
) -> BasicValueEnum<'ctx> {
let union_struct_type = struct_type_from_union_layout(env, layout_interner, &union_layout);
let raw_data_ptr = env
.builder
.new_build_struct_gep(
union_struct_type,
tag_value,
RocUnion::TAG_DATA_INDEX,
"tag_data",
)
.unwrap();
let raw_data_ptr = env.builder.new_build_struct_gep(
union_struct_type,
tag_value,
RocUnion::TAG_DATA_INDEX,
"tag_data",
);
let data_ptr = env.builder.build_pointer_cast(
let data_ptr = env.builder.new_build_pointer_cast(
raw_data_ptr,
tag_type.ptr_type(AddressSpace::default()),
"data_ptr",
@ -555,7 +552,7 @@ fn clone_tag_payload_and_id<'a, 'ctx>(
) -> IntValue<'ctx> {
let payload_type = basic_type_from_layout(env, layout_interner, payload_layout);
let payload_ptr = env.builder.build_pointer_cast(
let payload_ptr = env.builder.new_build_pointer_cast(
opaque_payload_ptr,
payload_type.ptr_type(AddressSpace::default()),
"cast_payload_ptr",
@ -636,13 +633,13 @@ fn build_clone_tag_help<'a, 'ctx>(
match union_layout {
NonRecursive(&[]) => {
// we're comparing empty tag unions; this code is effectively unreachable
env.builder.build_unreachable();
env.builder.new_build_unreachable();
}
NonRecursive(tags) => {
let id = get_tag_id(env, layout_interner, parent, &union_layout, tag_value);
let switch_block = env.context.append_basic_block(parent, "switch_block");
env.builder.build_unconditional_branch(switch_block);
env.builder.new_build_unconditional_branch(switch_block);
let mut cases = Vec::with_capacity_in(tags.len(), env.arena);
@ -655,15 +652,12 @@ fn build_clone_tag_help<'a, 'ctx>(
// load the tag payload (if any)
let payload_layout = LayoutRepr::struct_(field_layouts);
let opaque_payload_ptr = env
.builder
.new_build_struct_gep(
roc_union.struct_type(),
tag_value.into_pointer_value(),
RocUnion::TAG_DATA_INDEX,
"data_buffer",
)
.unwrap();
let opaque_payload_ptr = env.builder.new_build_struct_gep(
roc_union.struct_type(),
tag_value.into_pointer_value(),
RocUnion::TAG_DATA_INDEX,
"data_buffer",
);
let answer = clone_tag_payload_and_id(
env,
@ -677,7 +671,7 @@ fn build_clone_tag_help<'a, 'ctx>(
opaque_payload_ptr,
);
env.builder.build_return(Some(&answer));
env.builder.new_build_return(Some(&answer));
cases.push((id.get_type().const_int(tag_id as u64, false), block));
}
@ -686,11 +680,11 @@ fn build_clone_tag_help<'a, 'ctx>(
match cases.pop() {
Some((_, default)) => {
env.builder.build_switch(id, default, &cases);
env.builder.new_build_switch(id, default, &cases);
}
None => {
// we're serializing an empty tag union; this code is effectively unreachable
env.builder.build_unreachable();
env.builder.new_build_unreachable();
}
}
}
@ -698,7 +692,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let id = get_tag_id(env, layout_interner, parent, &union_layout, tag_value);
let switch_block = env.context.append_basic_block(parent, "switch_block");
env.builder.build_unconditional_branch(switch_block);
env.builder.new_build_unconditional_branch(switch_block);
let mut cases = Vec::with_capacity_in(tags.len(), env.arena);
@ -728,7 +722,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let cursors = Cursors {
offset: extra_offset,
extra_offset: env.builder.build_int_add(
extra_offset: env.builder.new_build_int_add(
extra_offset,
env.ptr_int().const_int(width as _, false),
"new_offset",
@ -738,7 +732,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let answer =
build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
env.builder.new_build_return(Some(&answer));
cases.push((id.get_type().const_int(tag_id as u64, false), block));
}
@ -747,11 +741,11 @@ fn build_clone_tag_help<'a, 'ctx>(
match cases.pop() {
Some((_, default)) => {
env.builder.build_switch(id, default, &cases);
env.builder.new_build_switch(id, default, &cases);
}
None => {
// we're serializing an empty tag union; this code is effectively unreachable
env.builder.build_unreachable();
env.builder.new_build_unreachable();
}
}
}
@ -767,7 +761,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let cursors = Cursors {
offset: extra_offset,
extra_offset: env.builder.build_int_add(
extra_offset: env.builder.new_build_int_add(
extra_offset,
env.ptr_int().const_int(width as _, false),
"new_offset",
@ -778,7 +772,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let answer = build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
env.builder.new_build_return(Some(&answer));
}
NullableWrapped {
nullable_id,
@ -791,10 +785,10 @@ fn build_clone_tag_help<'a, 'ctx>(
let comparison = env
.builder
.build_is_null(tag_value.into_pointer_value(), "is_null");
.new_build_is_null(tag_value.into_pointer_value(), "is_null");
env.builder
.build_conditional_branch(comparison, null_block, switch_block);
.new_build_conditional_branch(comparison, null_block, switch_block);
{
let mut cases = Vec::with_capacity_in(other_tags.len(), env.arena);
@ -823,7 +817,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let cursors = Cursors {
offset: extra_offset,
extra_offset: env.builder.build_int_add(
extra_offset: env.builder.new_build_int_add(
extra_offset,
env.ptr_int().const_int(width as _, false),
"new_offset",
@ -837,7 +831,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let answer =
build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
env.builder.new_build_return(Some(&answer));
cases.push((id.get_type().const_int(i as u64, false), block));
}
@ -846,11 +840,11 @@ fn build_clone_tag_help<'a, 'ctx>(
match cases.pop() {
Some((_, default)) => {
env.builder.build_switch(id, default, &cases);
env.builder.new_build_switch(id, default, &cases);
}
None => {
// we're serializing an empty tag union; this code is effectively unreachable
env.builder.build_unreachable();
env.builder.new_build_unreachable();
}
}
}
@ -861,7 +855,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let value = env.ptr_int().const_zero();
build_copy(env, ptr, offset, value.into());
env.builder.build_return(Some(&extra_offset));
env.builder.new_build_return(Some(&extra_offset));
}
}
NullableUnwrapped { other_fields, .. } => {
@ -870,10 +864,10 @@ fn build_clone_tag_help<'a, 'ctx>(
let comparison = env
.builder
.build_is_null(tag_value.into_pointer_value(), "is_null");
.new_build_is_null(tag_value.into_pointer_value(), "is_null");
env.builder
.build_conditional_branch(comparison, null_block, other_block);
.new_build_conditional_branch(comparison, null_block, other_block);
{
env.builder.position_at_end(null_block);
@ -881,7 +875,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let value = env.ptr_int().const_zero();
build_copy(env, ptr, offset, value.into());
env.builder.build_return(Some(&extra_offset));
env.builder.new_build_return(Some(&extra_offset));
}
{
@ -895,7 +889,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let cursors = Cursors {
offset: extra_offset,
extra_offset: env.builder.build_int_add(
extra_offset: env.builder.new_build_int_add(
extra_offset,
env.ptr_int()
.const_int(layout.stack_size(layout_interner) as _, false),
@ -914,7 +908,7 @@ fn build_clone_tag_help<'a, 'ctx>(
let answer =
build_clone(env, layout_interner, layout_ids, ptr, cursors, data, layout);
env.builder.build_return(Some(&answer));
env.builder.new_build_return(Some(&answer));
}
}
}
@ -935,12 +929,12 @@ fn write_pointer_with_tag_id<'a, 'ctx>(
// increment offset by 4
let four = env.ptr_int().const_int(4, false);
let offset = env.builder.build_int_add(offset, four, "");
let offset = env.builder.new_build_int_add(offset, four, "");
// cast to u32
let extra_offset = env
.builder
.build_int_cast(extra_offset, env.context.i32_type(), "");
.new_build_int_cast(extra_offset, env.context.i32_type(), "");
build_copy(env, ptr, offset, extra_offset.into());
} else {
@ -966,12 +960,12 @@ fn build_copy<'ctx>(
let ptr_type = value.get_type().ptr_type(AddressSpace::default());
let ptr = env
.builder
.build_pointer_cast(ptr, ptr_type, "cast_ptr_type");
.new_build_pointer_cast(ptr, ptr_type, "cast_ptr_type");
env.builder.build_store(ptr, value);
env.builder.new_build_store(ptr, value);
let width = value.get_type().size_of().unwrap();
env.builder.build_int_add(offset, width, "new_offset")
env.builder.new_build_int_add(offset, width, "new_offset")
}
fn build_clone_builtin<'a, 'ctx>(
@ -1024,7 +1018,7 @@ fn build_clone_builtin<'a, 'ctx>(
let (element_width, _element_align) = layout_interner.stack_size_and_alignment(elem);
let element_width = env.ptr_int().const_int(element_width as _, false);
let elements_width = bd.build_int_mul(element_width, len, "elements_width");
let elements_width = bd.new_build_int_mul(element_width, len, "elements_width");
// We clone the elements into the extra_offset address.
let _ = offset;
@ -1033,41 +1027,44 @@ fn build_clone_builtin<'a, 'ctx>(
if layout_interner.safe_to_memcpy(elem) {
// NOTE we are not actually sure the dest is properly aligned
let dest = pointer_at_offset(bd, env.context.i8_type(), ptr, elements_start_offset);
let src = bd.build_pointer_cast(
let src = bd.new_build_pointer_cast(
elements,
env.context.i8_type().ptr_type(AddressSpace::default()),
"to_bytes_pointer",
);
bd.build_memcpy(dest, 1, src, 1, elements_width).unwrap();
bd.build_int_add(elements_start_offset, elements_width, "new_offset")
bd.new_build_int_add(elements_start_offset, elements_width, "new_offset")
} else {
let element_type =
basic_type_from_layout(env, layout_interner, layout_interner.get_repr(elem));
let elements = bd.build_pointer_cast(
let elements = bd.new_build_pointer_cast(
elements,
element_type.ptr_type(AddressSpace::default()),
"elements",
);
// if the element has any pointers, we clone them to this offset
let rest_offset = bd.build_alloca(env.ptr_int(), "rest_offset");
let rest_offset = bd.new_build_alloca(env.ptr_int(), "rest_offset");
let element_stack_size = env
.ptr_int()
.const_int(layout_interner.stack_size(elem) as u64, false);
let rest_start_offset = bd.build_int_add(
let rest_start_offset = bd.new_build_int_add(
cursors.extra_offset,
bd.build_int_mul(len, element_stack_size, "elements_width"),
bd.new_build_int_mul(len, element_stack_size, "elements_width"),
"rest_start_offset",
);
bd.build_store(rest_offset, rest_start_offset);
bd.new_build_store(rest_offset, rest_start_offset);
let body = |layout_interner: &STLayoutInterner<'a>, index, element| {
let current_offset =
bd.build_int_mul(element_stack_size, index, "current_offset");
let current_offset =
bd.build_int_add(elements_start_offset, current_offset, "current_offset");
bd.new_build_int_mul(element_stack_size, index, "current_offset");
let current_offset = bd.new_build_int_add(
elements_start_offset,
current_offset,
"current_offset",
);
let current_extra_offset =
bd.new_build_load(env.ptr_int(), rest_offset, "element_offset");
@ -1090,7 +1087,7 @@ fn build_clone_builtin<'a, 'ctx>(
elem_layout,
);
bd.build_store(rest_offset, new_offset);
bd.new_build_store(rest_offset, new_offset);
};
let parent = env