From ecbe27442a553e79de644a2938737d0e83904ce0 Mon Sep 17 00:00:00 2001 From: pchintalapudi <34727397+pchintalapudi@users.noreply.github.com> Date: Sat, 8 Jan 2022 12:32:39 -0800 Subject: [PATCH] Generate memset and memmove intrinsics from ccalls (#43580) --- src/ccall.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/ccall.cpp b/src/ccall.cpp index c51385e6477906..2ed1b614aa201d 100644 --- a/src/ccall.cpp +++ b/src/ccall.cpp @@ -1714,6 +1714,42 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs) return rt == (jl_value_t*)jl_nothing_type ? ghostValue(jl_nothing_type) : mark_or_box_ccall_result(ctx, destp, retboxed, rt, unionall, static_rt); } + else if (is_libjulia_func(memset) && (rt == (jl_value_t*)jl_nothing_type || jl_is_cpointer_type(rt))) { + const jl_cgval_t &dst = argv[0]; + const jl_cgval_t &val = argv[1]; + const jl_cgval_t &n = argv[2]; + Value *destp = emit_unbox(ctx, T_size, dst, (jl_value_t*)jl_voidpointer_type); + Value *val32 = emit_unbox(ctx, T_int32, val, (jl_value_t*)jl_uint32_type); + Value *val8 = ctx.builder.CreateTrunc(val32, T_int8, "memset_val"); + ctx.builder.CreateMemSet( + emit_inttoptr(ctx, destp, T_pint8), + val8, + emit_unbox(ctx, T_size, n, (jl_value_t*)jl_ulong_type), + MaybeAlign(1) + ); + JL_GC_POP(); + return rt == (jl_value_t*)jl_nothing_type ? ghostValue(jl_nothing_type) : + mark_or_box_ccall_result(ctx, destp, retboxed, rt, unionall, static_rt); + } + else if (is_libjulia_func(memmove) && (rt == (jl_value_t*)jl_nothing_type || jl_is_cpointer_type(rt))) { + const jl_cgval_t &dst = argv[0]; + const jl_cgval_t &src = argv[1]; + const jl_cgval_t &n = argv[2]; + Value *destp = emit_unbox(ctx, T_size, dst, (jl_value_t*)jl_voidpointer_type); + + ctx.builder.CreateMemMove( + emit_inttoptr(ctx, destp, T_pint8), + MaybeAlign(0), + emit_inttoptr(ctx, + emit_unbox(ctx, T_size, src, (jl_value_t*)jl_voidpointer_type), + T_pint8), + MaybeAlign(0), + emit_unbox(ctx, T_size, n, (jl_value_t*)jl_ulong_type), + false); + JL_GC_POP(); + return rt == (jl_value_t*)jl_nothing_type ? ghostValue(jl_nothing_type) : + mark_or_box_ccall_result(ctx, destp, retboxed, rt, unionall, static_rt); + } else if (is_libjulia_func(jl_object_id) && nccallargs == 1 && rt == (jl_value_t*)jl_ulong_type) { jl_cgval_t val = argv[0];