Reverted fancy closure organization in List.append

This commit is contained in:
Chad Stearns 2020-07-18 21:50:03 -04:00
parent 36a259b56b
commit cb92b477ea

View file

@ -2242,7 +2242,16 @@ fn list_append<'a, 'ctx, 'env>(
}; };
match second_list_layout { match second_list_layout {
Layout::Builtin(Builtin::EmptyList) => if_second_list_is_empty(), Layout::Builtin(Builtin::EmptyList) => {
let (new_wrapper, _) = clone_nonempty_list(
env,
first_list_len,
load_list_ptr(builder, first_list_wrapper, ptr_type),
elem_layout,
);
BasicValueEnum::StructValue(new_wrapper)
}
Layout::Builtin(Builtin::List(_)) => { Layout::Builtin(Builtin::List(_)) => {
// second_list_len > 0 // second_list_len > 0
// We do this check to avoid allocating memory. If the second input // We do this check to avoid allocating memory. If the second input
@ -2250,7 +2259,7 @@ fn list_append<'a, 'ctx, 'env>(
let second_list_length_comparison = let second_list_length_comparison =
list_is_not_empty(builder, ctx, second_list_len); list_is_not_empty(builder, ctx, second_list_len);
let build_second_list_then = || { let if_second_list_is_not_empty = || {
let combined_list_len = builder.build_int_add( let combined_list_len = builder.build_int_add(
first_list_len, first_list_len,
second_list_len, second_list_len,
@ -2269,36 +2278,36 @@ fn list_append<'a, 'ctx, 'env>(
let index_name = "#index"; let index_name = "#index";
let index_alloca = builder.build_alloca(ctx.i64_type(), index_name); let index_alloca = builder.build_alloca(ctx.i64_type(), index_name);
let first_loop = || { // FIRST LOOP
builder
.build_store(index_alloca, ctx.i64_type().const_int(0, false));
let loop_bb = builder.build_store(index_alloca, ctx.i64_type().const_int(0, false));
let first_loop_bb =
ctx.append_basic_block(parent, "first_list_append_loop"); ctx.append_basic_block(parent, "first_list_append_loop");
builder.build_unconditional_branch(loop_bb); builder.build_unconditional_branch(first_loop_bb);
builder.position_at_end(loop_bb); builder.position_at_end(first_loop_bb);
// #index = #index + 1 // #index = #index + 1
let curr_index = builder let curr_first_loop_index = builder
.build_load(index_alloca, index_name) .build_load(index_alloca, index_name)
.into_int_value(); .into_int_value();
let next_index = builder.build_int_add( let next_first_loop_index = builder.build_int_add(
curr_index, curr_first_loop_index,
ctx.i64_type().const_int(1, false), ctx.i64_type().const_int(1, false),
"nextindex", "nextindex",
); );
builder.build_store(index_alloca, next_index); builder.build_store(index_alloca, next_first_loop_index);
let first_list_ptr = let first_list_ptr =
load_list_ptr(builder, first_list_wrapper, ptr_type); load_list_ptr(builder, first_list_wrapper, ptr_type);
// The pointer to the element in the first list // The pointer to the element in the first list
let elem_ptr = unsafe { let first_list_elem_ptr = unsafe {
builder.build_in_bounds_gep( builder.build_in_bounds_gep(
first_list_ptr, first_list_ptr,
&[curr_index], &[curr_first_loop_index],
"load_index", "load_index",
) )
}; };
@ -2307,56 +2316,55 @@ fn list_append<'a, 'ctx, 'env>(
let combined_list_elem_ptr = unsafe { let combined_list_elem_ptr = unsafe {
builder.build_in_bounds_gep( builder.build_in_bounds_gep(
combined_list_ptr, combined_list_ptr,
&[curr_index], &[curr_first_loop_index],
"load_index_combined_list", "load_index_combined_list",
) )
}; };
let elem = builder.build_load(elem_ptr, "get_elem"); let first_list_elem =
builder.build_load(first_list_elem_ptr, "get_elem");
// Mutate the new array in-place to change the element. // Mutate the new array in-place to change the element.
builder.build_store(combined_list_elem_ptr, elem); builder.build_store(combined_list_elem_ptr, first_list_elem);
// #index < first_list_len // #index < first_list_len
let loop_end_cond = builder.build_int_compare( let first_loop_end_cond = builder.build_int_compare(
IntPredicate::ULT, IntPredicate::ULT,
curr_index, curr_first_loop_index,
first_list_len, first_list_len,
"loopcond", "loopcond",
); );
let after_loop_bb = let after_first_loop_bb =
ctx.append_basic_block(parent, "after_first_loop"); ctx.append_basic_block(parent, "after_first_loop");
builder.build_conditional_branch( builder.build_conditional_branch(
loop_end_cond, first_loop_end_cond,
loop_bb, first_loop_bb,
after_loop_bb, after_first_loop_bb,
); );
builder.position_at_end(after_loop_bb); builder.position_at_end(after_first_loop_bb);
};
let second_loop = || { // SECOND LOOP
builder builder.build_store(index_alloca, ctx.i64_type().const_int(0, false));
.build_store(index_alloca, ctx.i64_type().const_int(0, false));
let loop_bb = let second_loop_bb =
ctx.append_basic_block(parent, "second_list_append_loop"); ctx.append_basic_block(parent, "second_list_append_loop");
builder.build_unconditional_branch(loop_bb); builder.build_unconditional_branch(second_loop_bb);
builder.position_at_end(loop_bb); builder.position_at_end(second_loop_bb);
// #index = #index + 1 // #index = #index + 1
let curr_index = builder let curr_second_index = builder
.build_load(index_alloca, index_name) .build_load(index_alloca, index_name)
.into_int_value(); .into_int_value();
let next_index = builder.build_int_add( let next_second_index = builder.build_int_add(
curr_index, curr_second_index,
ctx.i64_type().const_int(1, false), ctx.i64_type().const_int(1, false),
"nextindex", "nextindex",
); );
builder.build_store(index_alloca, next_index); builder.build_store(index_alloca, next_second_index);
let second_list_ptr = let second_list_ptr =
load_list_ptr(builder, second_list_wrapper, ptr_type); load_list_ptr(builder, second_list_wrapper, ptr_type);
@ -2365,15 +2373,16 @@ fn list_append<'a, 'ctx, 'env>(
let second_list_elem_ptr = unsafe { let second_list_elem_ptr = unsafe {
builder.build_in_bounds_gep( builder.build_in_bounds_gep(
second_list_ptr, second_list_ptr,
&[curr_index], &[curr_second_index],
"load_index", "load_index",
) )
}; };
// The pointer to the element in the combined list. // The pointer to the element in the combined list.
// Note that the pointer does not start at the index // Note that the pointer does not start at the index
// 0, it starts at the index of first_list_len. // 0, it starts at the index of first_list_len. In that
let combined_list_elem_ptr = unsafe { // sense it is "offset".
let offset_combined_list_elem_ptr = unsafe {
builder.build_in_bounds_gep( builder.build_in_bounds_gep(
combined_list_ptr, combined_list_ptr,
&[first_list_len], &[first_list_len],
@ -2385,38 +2394,35 @@ fn list_append<'a, 'ctx, 'env>(
// in the combined list // in the combined list
let combined_list_elem_ptr = unsafe { let combined_list_elem_ptr = unsafe {
builder.build_in_bounds_gep( builder.build_in_bounds_gep(
combined_list_elem_ptr, offset_combined_list_elem_ptr,
&[curr_index], &[curr_second_index],
"load_index_combined_list", "load_index_combined_list",
) )
}; };
let elem = builder.build_load(second_list_elem_ptr, "get_elem"); let second_list_elem =
builder.build_load(second_list_elem_ptr, "get_elem");
// Mutate the new array in-place to change the element. // Mutate the new array in-place to change the element.
builder.build_store(combined_list_elem_ptr, elem); builder.build_store(combined_list_elem_ptr, second_list_elem);
// #index < second_list_len // #index < second_list_len
let loop_end_cond = builder.build_int_compare( let second_loop_end_cond = builder.build_int_compare(
IntPredicate::ULT, IntPredicate::ULT,
curr_index, curr_second_index,
second_list_len, second_list_len,
"loopcond", "loopcond",
); );
let after_loop_bb = let after_second_loop_bb =
ctx.append_basic_block(parent, "after_second_loop"); ctx.append_basic_block(parent, "after_second_loop");
builder.build_conditional_branch( builder.build_conditional_branch(
loop_end_cond, second_loop_end_cond,
loop_bb, second_loop_bb,
after_loop_bb, after_second_loop_bb,
); );
builder.position_at_end(after_loop_bb); builder.position_at_end(after_second_loop_bb);
};
first_loop();
second_loop();
let ptr_bytes = env.ptr_bytes; let ptr_bytes = env.ptr_bytes;
let int_type = ptr_int(ctx, ptr_bytes); let int_type = ptr_int(ctx, ptr_bytes);
@ -2460,7 +2466,7 @@ fn list_append<'a, 'ctx, 'env>(
env, env,
parent, parent,
second_list_length_comparison, second_list_length_comparison,
build_second_list_then, if_second_list_is_not_empty,
if_second_list_is_empty, if_second_list_is_empty,
BasicTypeEnum::StructType(collection(ctx, env.ptr_bytes)), BasicTypeEnum::StructType(collection(ctx, env.ptr_bytes)),
) )