From 2bbffdb6c89b3e37e86fcd3e252eeeaf25ca216f Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Wed, 10 Aug 2022 16:08:38 +0200 Subject: [PATCH] intrinsics: Add copy_nonoverlapping This intrinsic is similar to C's memcpy (or in our case, GCC's __builtin_memcpy) with the order of arguments swapped and knowledge about the type of the operands. So we can desugar the following calls: `copy_nonoverlapping::(src, dst, count)` can be converted to `__builtin_memcpy(dst, src, count * size_of::())` --- gcc/rust/backend/rust-builtins.h | 8 +++ gcc/rust/backend/rust-compile-intrinsic.cc | 71 +++++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/gcc/rust/backend/rust-builtins.h b/gcc/rust/backend/rust-builtins.h index 13c16d15723f..2bfa6c6cdf79 100644 --- a/gcc/rust/backend/rust-builtins.h +++ b/gcc/rust/backend/rust-builtins.h @@ -122,6 +122,14 @@ class BuiltinsContext define_builtin ("breakpoint", BUILT_IN_TRAP, "__builtin_trap", "breakpoint", build_function_type (void_type_node, void_list_node), builtin_const | builtin_noreturn); + + define_builtin ( + "memcpy", BUILT_IN_MEMCPY, "__builtin_memcpy", "memcpy", + build_function_type_list (build_pointer_type (void_type_node), + build_pointer_type (void_type_node), + build_pointer_type (void_type_node), + size_type_node, NULL_TREE), + 0); } // Define a builtin function. BCODE is the builtin function code diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index 67e38c3261c1..b51206d95e26 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -25,6 +25,7 @@ #include "rust-location.h" #include "rust-tree.h" #include "tree-core.h" +#include "print-tree.h" namespace Rust { namespace Compile { @@ -37,6 +38,8 @@ static tree transmute_handler (Context *ctx, TyTy::FnType *fntype); static tree rotate_handler (Context *ctx, TyTy::FnType *fntype, tree_code op); +static tree +copy_nonoverlapping_handler (Context *ctx, TyTy::FnType *fntype); static inline tree rotate_left_handler (Context *ctx, TyTy::FnType *fntype) @@ -55,7 +58,8 @@ static const std::mappop_block (); gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR); + DECL_SAVED_TREE (fndecl) = bind_tree; + ctx->push_function (fndecl); } @@ -373,5 +379,68 @@ rotate_handler (Context *ctx, TyTy::FnType *fntype, tree_code op) return fndecl; } +/** + * fn copy_nonoverlapping(src: *const T, dst: *mut T, count: usize); + */ +static tree +copy_nonoverlapping_handler (Context *ctx, TyTy::FnType *fntype) +{ + rust_assert (fntype->get_params ().size () == 3); + rust_assert (fntype->get_num_substitutions () == 1); + + tree lookup = NULL_TREE; + if (check_for_cached_intrinsic (ctx, fntype, &lookup)) + return lookup; + + auto fndecl = compile_intrinsic_function (ctx, fntype); + + // setup the params + std::vector param_vars; + compile_fn_params (ctx, fntype, fndecl, ¶m_vars); + + if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars)) + return error_mark_node; + + enter_intrinsic_block (ctx, fndecl); + + // BUILTIN copy_nonoverlapping BODY BEGIN + + auto src = ctx->get_backend ()->var_expression (param_vars[0], Location ()); + auto dst = ctx->get_backend ()->var_expression (param_vars[1], Location ()); + auto count = ctx->get_backend ()->var_expression (param_vars[2], Location ()); + + // We want to create the following statement + // memcpy(dst, src, size_of::()); + // so + // memcpy(dst, src, size_expr); + + auto *resolved_ty = fntype->get_substs ().at (0).get_param_ty ()->resolve (); + auto param_type = TyTyResolveCompile::compile (ctx, resolved_ty); + + tree size_expr + = build2 (MULT_EXPR, size_type_node, TYPE_SIZE_UNIT (param_type), count); + + tree memcpy_raw; + BuiltinsContext::get ().lookup_simple_builtin ("memcpy", &memcpy_raw); + rust_assert (memcpy_raw); + auto memcpy + = build_fold_addr_expr_loc (Location ().gcc_location (), memcpy_raw); + + auto copy_call + = ctx->get_backend ()->call_expression (memcpy, {dst, src, size_expr}, + nullptr, Location ()); + + ctx->add_statement (copy_call); + + // BUILTIN copy_nonoverlapping BODY END + + finalize_intrinsic_block (ctx, fndecl); + + // `memcpy` always has side effects, it is far from a pure function + TREE_SIDE_EFFECTS (fndecl) = 1; + + return fndecl; +} + } // namespace Compile } // namespace Rust