WIP - [artiq] Test

This commit is contained in:
David Mak 2024-08-15 04:08:46 +08:00
parent b3d86f7187
commit 27aada4d59
1 changed files with 181 additions and 151 deletions

View File

@ -568,28 +568,58 @@ fn format_rpc_ret<'ctx>(
let llvm_ret_ty = ctx.get_llvm_abi_type(generator, ret_ty);
let result = match &*ctx.unifier.get_ty_immutable(ret_ty) {
// TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
// let llvm_i1 = ctx.ctx.bool_type();
// let llvm_usize = generator.get_size_type(ctx.ctx);
//
// let slot = ctx.builder.build_alloca(llvm_ret_ty, "rpc.ret.slot").unwrap();
// let (elem_ty, ndims) = unpack_ndarray_var_tys(&mut ctx.unifier, ret_ty);
// let llvm_ret_ty =
// NDArrayType::new(generator, ctx.ctx, ctx.get_llvm_type(generator, elem_ty));
// let ndarray = llvm_ret_ty.new_value(generator, ctx, None);
// ctx.builder.build_store(slot, ndarray.as_base_value()).unwrap();
//
// let ndims =
// if let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) {
// assert_eq!(values.len(), 1);
//
// u64::try_from(values[0].clone()).unwrap()
// } else {
// unreachable!();
// };
// ndarray.store_ndims(ctx, generator, llvm_usize.const_int(ndims, false));
// ndarray.create_dim_sizes(ctx, llvm_usize, ndarray.load_ndims(ctx));
//
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
let llvm_i1 = ctx.ctx.bool_type();
let llvm_usize = generator.get_size_type(ctx.ctx);
let slot = ctx.builder.build_alloca(llvm_ret_ty, "rpc.ret.slot").unwrap();
let (elem_ty, ndims) = unpack_ndarray_var_tys(&mut ctx.unifier, ret_ty);
let llvm_ret_ty =
NDArrayType::new(generator, ctx.ctx, ctx.get_llvm_type(generator, elem_ty));
let ndarray = llvm_ret_ty.new_value(generator, ctx, None);
let ndims =
if let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) {
assert_eq!(values.len(), 1);
u64::try_from(values[0].clone()).unwrap()
} else {
unreachable!();
};
ndarray.store_ndims(ctx, generator, llvm_usize.const_int(ndims, false));
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray.load_ndims(ctx));
let buffer = ctx.builder.build_alloca(llvm_ret_ty.as_base_type(), "rpc.ret.slot").unwrap();
let slotgen = ctx.builder.build_bitcast(buffer, llvm_pi8, "rpc.ret.ptr").unwrap();
ctx.builder.build_unconditional_branch(head_bb).unwrap();
ctx.builder.position_at_end(head_bb);
let phi = ctx.builder.build_phi(llvm_pi8, "rpc.ptr").unwrap();
phi.add_incoming(&[(&slotgen, prehead_bb)]);
let alloc_size = ctx
.build_call_or_invoke(rpc_recv, &[phi.as_basic_value()], "rpc.size.next")
.unwrap()
.into_int_value();
let is_done = ctx
.builder
.build_int_compare(IntPredicate::EQ, llvm_i32.const_zero(), alloc_size, "rpc.done")
.unwrap();
ctx.builder.build_conditional_branch(is_done, tail_bb, alloc_bb).unwrap();
ctx.builder.position_at_end(alloc_bb);
let alloc_ptr =
ctx.builder.build_array_alloca(llvm_pi8, alloc_size, "rpc.alloc").unwrap();
let alloc_ptr =
ctx.builder.build_bitcast(alloc_ptr, llvm_pi8, "rpc.alloc.ptr").unwrap();
phi.add_incoming(&[(&alloc_ptr, alloc_bb)]);
ctx.builder.build_unconditional_branch(head_bb).unwrap();
ctx.builder.position_at_end(tail_bb);
ctx.builder.build_store(slot, ndarray.as_base_value()).unwrap();
ctx.builder.build_load(slot, "rpc.result").unwrap()
// let llvm_usize_sizeof = ctx
// .builder
// .build_int_truncate_or_bit_cast(llvm_ret_ty.size_type().size_of(), llvm_usize, "")
@ -718,7 +748,7 @@ fn format_rpc_ret<'ctx>(
//
// ctx.builder.position_at_end(tail_bb);
// ctx.builder.build_load(slot, "rpc.result").unwrap()
// }
}
_ => {
let slot = ctx.builder.build_alloca(llvm_ret_ty, "rpc.ret.slot").unwrap();