List repeat works for all test cases except one with a negative number repeats param, and avoids malloc in the case that the number of repeats is not more than 0

This commit is contained in:
Chad Stearns 2020-06-21 00:55:53 -04:00
parent 6485b039c7
commit e4eeff5bd0
2 changed files with 104 additions and 51 deletions

View file

@ -1471,77 +1471,130 @@ fn call_with_args<'a, 'ctx, 'env>(
) )
} }
Symbol::LIST_REPEAT => { Symbol::LIST_REPEAT => {
// List.repeat : Int, a -> List a // List.repeat : Int, elem -> List elem
debug_assert!(args.len() == 2); debug_assert!(args.len() == 2);
let ptr_bytes = env.ptr_bytes;
let (elem, elem_layout) = args[1];
// Number of repeats // Number of repeats
let list_len = args[0].0.into_int_value(); let list_len = args[0].0.into_int_value();
let builder = env.builder; let builder = env.builder;
let ctx = env.context; let ctx = env.context;
// Allocate space for the new array that we'll copy into. let (elem, elem_layout) = args[1];
let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes); let elem_type = basic_type_from_layout(env.arena, ctx, elem_layout, env.ptr_bytes);
let list_ptr = { let comparison = builder.build_int_compare(
let bytes_len = elem_bytes; IntPredicate::UGT,
let len_type = env.ptr_int(); list_len,
let len = len_type.const_int(bytes_len, false); ctx.i64_type().const_int(0, false),
"atleastzero",
);
env.builder let build_then = || {
.build_array_malloc(elem_type, len, "create_list_ptr") // Allocate space for the new array that we'll copy into.
.unwrap() let elem_bytes = elem_layout.stack_size(env.ptr_bytes) as u64;
// TODO check if malloc returned null; if so, runtime error for OOM! let list_ptr = {
}; let bytes_len = elem_bytes;
let len_type = env.ptr_int();
let len = len_type.const_int(bytes_len, false);
// dbg!(elem_type); env.builder
.build_array_malloc(elem_type, len, "create_list_ptr")
.unwrap()
for (dummy_i) in (0..5) { // TODO check if malloc returned null; if so, runtime error for OOM!
let elem_ptr = unsafe {
builder.build_in_bounds_gep(
list_ptr,
&[ctx.i64_type().const_int(dummy_i, false)],
"load_index",
)
}; };
let index_name = "#index";
let start_alloca = builder.build_alloca(ctx.i64_type(), index_name);
builder.build_store(start_alloca, list_len);
let loop_bb = ctx.append_basic_block(parent, "loop");
builder.build_unconditional_branch(loop_bb);
builder.position_at_end(loop_bb);
// #index = #index - 1
let curr_index = builder
.build_load(start_alloca, index_name)
.into_int_value();
let next_index = builder.build_int_sub(
curr_index,
ctx.i64_type().const_int(1, false),
"nextindex",
);
builder.build_store(start_alloca, next_index);
let elem_ptr =
unsafe { builder.build_in_bounds_gep(list_ptr, &[curr_index], "load_index") };
// Mutate the new array in-place to change the element. // Mutate the new array in-place to change the element.
builder.build_store(elem_ptr, elem); builder.build_store(elem_ptr, elem);
}
let ptr_bytes = env.ptr_bytes; // #index != 0
let int_type = ptr_int(ctx, ptr_bytes); let end_cond = builder.build_int_compare(
let ptr_as_int = builder.build_ptr_to_int(list_ptr, int_type, "list_cast_ptr"); IntPredicate::NE,
let struct_type = collection(ctx, ptr_bytes); ctx.i64_type().const_int(0, false),
curr_index,
"loopcond",
);
let mut struct_val; let after_bb = ctx.append_basic_block(parent, "afterloop");
// Store the pointer builder.build_conditional_branch(end_cond, loop_bb, after_bb);
struct_val = builder builder.position_at_end(after_bb);
.build_insert_value(
struct_type.get_undef(), let ptr_bytes = env.ptr_bytes;
ptr_as_int, let int_type = ptr_int(ctx, ptr_bytes);
Builtin::WRAPPER_PTR, let ptr_as_int = builder.build_ptr_to_int(list_ptr, int_type, "list_cast_ptr");
"insert_ptr", let struct_type = collection(ctx, ptr_bytes);
let mut struct_val;
// Store the pointer
struct_val = builder
.build_insert_value(
struct_type.get_undef(),
ptr_as_int,
Builtin::WRAPPER_PTR,
"insert_ptr",
)
.unwrap();
// Store the length
struct_val = builder
.build_insert_value(struct_val, list_len, Builtin::WRAPPER_LEN, "insert_len")
.unwrap();
builder.build_bitcast(
struct_val.into_struct_value(),
collection(ctx, ptr_bytes),
"cast_collection",
) )
.unwrap(); };
// Store the length // If the number of repeats is 0 or lower, dont even
struct_val = builder // bother allocating memory, since that it a costly
.build_insert_value(struct_val, list_len, Builtin::WRAPPER_LEN, "insert_len") // operation. Just return an empty list.
.unwrap(); let build_else = || {
let struct_type = collection(ctx, env.ptr_bytes);
// // The pointer should be null (aka zero) and the length should be zero,
builder.build_bitcast( // so the whole struct should be a const_zero
struct_val.into_struct_value(), BasicValueEnum::StructValue(struct_type.const_zero())
collection(ctx, ptr_bytes), };
"cast_collection",
let struct_type = collection(ctx, env.ptr_bytes);
build_basic_phi2(
env,
parent,
comparison,
build_then,
build_else,
BasicTypeEnum::StructType(struct_type),
) )
} }
Symbol::INT_DIV_UNSAFE => { Symbol::INT_DIV_UNSAFE => {

View file

@ -503,11 +503,11 @@ mod gen_builtins {
#[test] #[test]
fn list_repeat() { fn list_repeat() {
assert_evals_to!("List.repeat 5 1", &[1, 1, 1, 1, 1], &'static [i64]); assert_evals_to!("List.repeat 5 1", &[1, 1, 1, 1, 1], &'static [i64]);
// assert_evals_to!("List.repeat -1 1", &[], &'static [i64]); assert_evals_to!("List.repeat -1 1", &[], &'static [i64]);
// assert_evals_to!("List.repeat 4 5", &[5, 5, 5, 5], &'static [i64]); assert_evals_to!("List.repeat 4 2", &[2, 2, 2, 2], &'static [i64]);
// assert_evals_to!("List.repeat 0 []", &[], &'static [i64]); assert_evals_to!("List.repeat 0 []", &[], &'static [i64]);
// assert_evals_to!("List.repeat 2 []", &[&[], &[]], &'static [&'static [i64]]); assert_evals_to!("List.repeat 2 []", &[&[], &[]], &'static [&'static [i64]]);
} }
#[test] #[test]