From patchwork Tue Feb 21 12:01:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 60035 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1840286wrn; Tue, 21 Feb 2023 04:45:13 -0800 (PST) X-Google-Smtp-Source: AK7set9GcJVTuWBrwPPbpkIBdJkbvZhFhWTCeMfDhl67a4JxNJb7xzXqNebDjdj2WCxwtoGcAnNS X-Received: by 2002:a17:907:8c89:b0:8b1:779c:a8ac with SMTP id td9-20020a1709078c8900b008b1779ca8acmr17210477ejc.13.1676983513750; Tue, 21 Feb 2023 04:45:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676983513; cv=none; d=google.com; s=arc-20160816; b=jYSbh0yX5a2Ietv4FNzosPwvj/EWaNXOH4VNfXs0APQegQ5pZGGgJZ8OwiKM7Ms7MB jmCMtCEt4p6sMTWTFrol61L/u0lmN1vDabptvqpE/D0ReThTj5Sz4gJwLi2wnV4LoWZS q6jtWp/6/Yl79GGDWoowPZaEBUC5L+EFRCKJYhO0YsC0a/n4mZg40Gq9+XDS9Bj3mwMv R5971C2Cpo9Yi5Z75MjhgB/KP8QUjDEXYyOJKczMSaYUst7ekhs3I8NFlhGyWXiFVBq9 aC18AAydZfX9ynXfcsGQ4F0MQfUF9IoPOXpqPUm4X11t4mxaLEeA4Q0TySOYVvcds87o 1Y4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=nYzQRgOHmXTgQjDWfcMLGmNVMNqbdUIYDJHuTupsqS0=; b=h/grDEWEi1U0gUMEwd9Wa/ms2YQHXCHZmzmfzkbtONx2pqQAtKO3mOIRoLDiHYls/r RLY8s3AQek9rr1Ut6qVN7XYabT70XAjD/v3TVhWTFqprJXWKD1sl9RwWmlZ2gqPDjE58 hVpqcVHkPtjC11FY4ONn/lHfH5zyGJX42AUHquTgOHxEtdmVRXerCfYPtlFU80+Kv+6c GViTjpKIL6pN0pSfCF/IiyTM0aHlFVeYkBJhVmUt8ersD39k2W4TytMJNmKjiStarCG9 GD0UPGkHyxBqPVL/bLMm1fLOIzNV9qG8jAAUPZpT2d0crG39F1bRBRsBTumUZwf/ySMq YIdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=FccjDFOk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id u17-20020a170906109100b008b17f6b8c98si18887744eju.994.2023.02.21.04.45.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:45:13 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=FccjDFOk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AA3413A20B96 for ; Tue, 21 Feb 2023 12:15:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 9781238323CE for ; Tue, 21 Feb 2023 12:04:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9781238323CE Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x436.google.com with SMTP id c12so4164082wrw.1 for ; Tue, 21 Feb 2023 04:04:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=nYzQRgOHmXTgQjDWfcMLGmNVMNqbdUIYDJHuTupsqS0=; b=FccjDFOksXoFL0JDMz56Rfx/cJ7AgK+J1hFZebOCpWQK71CDGzad1or8qjeuhFR8w/ E7vY8JtDN6zhLT1j4rTgxuGbQhidMP4zmyUrtBja/9VwWCCy1YSWh1Gifg+1S+DglRmD z9nQYS7tz1FmyJgTh6l0QI8AwwvM/wNnjp1d4EJ35j1LFgugma4jjJ69luoeD1PPOvbI UDokzF7sOzdSXtRbULmQeiw+9ZOt4yuM26Ta2F+yH0hd4UhXgDgvmy75XDHDiP5i147p DzBnU4fWnwlpsEDoIirT2MlvbLsVCG4nB0I271O+6OtEE1FS7DhL65mGnSQZLuaZwwDZ 5Xhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=nYzQRgOHmXTgQjDWfcMLGmNVMNqbdUIYDJHuTupsqS0=; b=qiA07Zk6XtrjK3dBsH11ELWqbimytfjyxTWG9OAHSzfz/IAC5e4HdXzzPllkFyrem/ EjKBnapQzugo6gN7xOSEWh59oCck/NugRbcL/D4MVlrtKFLhePRtjhzH/seR+k+vLxLS fXhh6MGxQfyxE21NZx7EfnPPcqw3xTxrXUDn0BPsneigl8ilnu2/TlTGo1WMj1s8nrZW f81dwnwXi56H/R3sVs/U8gFBEqZCaCjXSOwgvPg5EsjCovwIW4FTHYYWJnDosxrFOkM7 of+/U/iwulcwoNQvJ6FlL+THMPU33nzUPebFwZjUTB6x/vBmT1TMR68rbhpUIAHljY8H IL4w== X-Gm-Message-State: AO0yUKWXzv9FYz/aDTRTKaRFm9jI1/GXo12/9puspW5kO0FS8JamLNja gGDxZm9mYt0dnu+Sv6ueai7w+qe5ivQBmUnFPw== X-Received: by 2002:a5d:4805:0:b0:2c5:4e3c:d390 with SMTP id l5-20020a5d4805000000b002c54e3cd390mr4166390wrq.62.1676981054887; Tue, 21 Feb 2023 04:04:14 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id c15-20020adffb4f000000b002c55b0e6ef1sm5013811wrs.4.2023.02.21.04.04.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:04:14 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 064/103] gccrs: intrinsics: Implement atomic_load intrinsics Date: Tue, 21 Feb 2023 13:01:54 +0100 Message-Id: <20230221120230.596966-65-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230221120230.596966-1-arthur.cohen@embecosm.com> References: <20230221120230.596966-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758444665280763054?= X-GMAIL-MSGID: =?utf-8?q?1758444665280763054?= From: Arthur Cohen gcc/rust/ChangeLog: * backend/rust-builtins.cc (BuiltinsContext::setup_atomic_fns): Declare atomic load intrinsics. * backend/rust-compile-intrinsic.cc (atomic_load_handler_inner): New handler. (atomic_load_handler): Likewise. (unchecked_op_handler): Remove `static` function qualifier. (build_atomic_builtin_name): Handle load intrinsics. (atomic_store_handler_inner): New handler. gcc/testsuite/ChangeLog: * rust/compile/torture/intrinsics-3.rs: * rust/execute/torture/atomic_load.rs: New test. --- gcc/rust/backend/rust-builtins.cc | 56 ++++++------ gcc/rust/backend/rust-compile-intrinsic.cc | 88 +++++++++++++++++-- .../rust/compile/torture/intrinsics-3.rs | 2 - .../rust/execute/torture/atomic_load.rs | 31 +++++++ 4 files changed, 144 insertions(+), 33 deletions(-) create mode 100644 gcc/testsuite/rust/execute/torture/atomic_load.rs diff --git a/gcc/rust/backend/rust-builtins.cc b/gcc/rust/backend/rust-builtins.cc index 64e06e1a240..66b3becc47a 100644 --- a/gcc/rust/backend/rust-builtins.cc +++ b/gcc/rust/backend/rust-builtins.cc @@ -71,36 +71,42 @@ BuiltinsContext::setup_math_fns () void BuiltinsContext::setup_atomic_fns () { - define_builtin ("atomic_store", BUILT_IN_ATOMIC_STORE, "__atomic_store", NULL, - build_function_type_list (void_type_node, size_type_node, - build_pointer_type (void_type_node), - const_ptr_type_node, - integer_type_node, NULL_TREE), - 0); - define_builtin ("atomic_store_n", BUILT_IN_ATOMIC_STORE_N, "__atomic_store_n", - NULL, - build_varargs_function_type_list (void_type_node, NULL_TREE), - 0); + auto atomic_store_type + = build_varargs_function_type_list (void_type_node, NULL_TREE); + auto atomic_load_type = [] (tree ret_type_node) { + return build_function_type_list (ret_type_node, + ptr_type_node, // const_ptr_type_node? + integer_type_node, NULL_TREE); + }; + + // FIXME: These should be the definition for the generic version of the + // atomic_store builtins, but I cannot get them to work properly. Revisit + // later. define_builtin ("atomic_store", BUILT_IN_ATOMIC_STORE, + // "__atomic_store", NULL, + // atomic_store_type, 0); + // define_builtin ("atomic_store_n", BUILT_IN_ATOMIC_STORE_N, + // "__atomic_store_n", + // NULL, atomic_store_type, 0); + define_builtin ("atomic_store_1", BUILT_IN_ATOMIC_STORE_1, "__atomic_store_1", - NULL, - build_varargs_function_type_list (void_type_node, NULL_TREE), - 0); + NULL, atomic_store_type, 0); define_builtin ("atomic_store_2", BUILT_IN_ATOMIC_STORE_2, "__atomic_store_2", - NULL, - build_varargs_function_type_list (void_type_node, NULL_TREE), - 0); + NULL, atomic_store_type, 0); define_builtin ("atomic_store_4", BUILT_IN_ATOMIC_STORE_4, "__atomic_store_4", - NULL, - build_varargs_function_type_list (void_type_node, NULL_TREE), - 0); + NULL, atomic_store_type, 0); define_builtin ("atomic_store_8", BUILT_IN_ATOMIC_STORE_8, "__atomic_store_8", - NULL, - build_varargs_function_type_list (void_type_node, NULL_TREE), - 0); + NULL, atomic_store_type, 0); define_builtin ("atomic_store_16", BUILT_IN_ATOMIC_STORE_16, - "__atomic_store_16", NULL, - build_varargs_function_type_list (void_type_node, NULL_TREE), - 0); + "__atomic_store_16", NULL, atomic_store_type, 0); + + define_builtin ("atomic_load_1", BUILT_IN_ATOMIC_LOAD_1, "__atomic_load_1", + NULL, atomic_load_type (integer_type_node), 0); + define_builtin ("atomic_load_2", BUILT_IN_ATOMIC_LOAD_2, "__atomic_load_2", + NULL, atomic_load_type (integer_type_node), 0); + define_builtin ("atomic_load_4", BUILT_IN_ATOMIC_LOAD_4, "__atomic_load_4", + NULL, atomic_load_type (integer_type_node), 0); + define_builtin ("atomic_load_8", BUILT_IN_ATOMIC_LOAD_8, "__atomic_load_8", + NULL, atomic_load_type (integer_type_node), 0); } void diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index b0c6015bee2..55222116366 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -120,6 +120,8 @@ prefetch_write_data (Context *ctx, TyTy::FnType *fntype) static tree atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering); +static tree +atomic_load_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering); static inline std::function atomic_store_handler (int ordering) @@ -129,6 +131,14 @@ atomic_store_handler (int ordering) }; } +static inline std::function +atomic_load_handler (int ordering) +{ + return [ordering] (Context *ctx, TyTy::FnType *fntype) { + return atomic_load_handler_inner (ctx, fntype, ordering); + }; +} + static inline tree unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op); @@ -140,7 +150,7 @@ unchecked_op_handler (tree_code op) }; } -static inline tree +inline tree sorry_handler (Context *ctx, TyTy::FnType *fntype) { rust_sorry_at (fntype->get_locus (), "intrinsic %qs is not yet implemented", @@ -163,11 +173,14 @@ static const std::map allowed_types = { {"i8", "1"}, {"i16", "2"}, {"i32", "4"}, {"i64", "8"}, @@ -679,7 +693,7 @@ build_atomic_builtin_name (Location locus, TyTy::BaseType *operand_type) // TODO: Can we maybe get the generic version (atomic_store_n) to work... This // would be so much better - std::string result = "atomic_store_"; + std::string result = prefix; auto type_name = operand_type->get_name (); if (type_name == "usize" || type_name == "isize") @@ -735,7 +749,8 @@ atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering) = fntype->get_substs ()[0].get_param_ty ()->resolve (); auto builtin_name - = build_atomic_builtin_name (fntype->get_locus (), monomorphized_type); + = build_atomic_builtin_name ("atomic_store_", fntype->get_locus (), + monomorphized_type); if (builtin_name.empty ()) return error_mark_node; @@ -751,7 +766,6 @@ atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering) = ctx->get_backend ()->call_expression (atomic_store, {dst, value, memorder}, nullptr, Location ()); - TREE_READONLY (store_call) = 0; TREE_SIDE_EFFECTS (store_call) = 1; @@ -761,6 +775,68 @@ atomic_store_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering) return fndecl; } +static tree +atomic_load_handler_inner (Context *ctx, TyTy::FnType *fntype, int ordering) +{ + rust_assert (fntype->get_params ().size () == 1); + 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); + + // Most intrinsic functions are pure but not the atomic ones + // FIXME: Is atomic_load_* pure? Feels like it shouldn't so + TREE_READONLY (fndecl) = 0; + TREE_SIDE_EFFECTS (fndecl) = 1; + + // setup the params + std::vector param_vars; + std::vector types; + compile_fn_params (ctx, fntype, fndecl, ¶m_vars, &types); + + auto ok = ctx->get_backend ()->function_set_parameters (fndecl, param_vars); + rust_assert (ok); + + enter_intrinsic_block (ctx, fndecl); + + auto src = ctx->get_backend ()->var_expression (param_vars[0], Location ()); + auto memorder = make_unsigned_long_tree (ctx, ordering); + + auto monomorphized_type + = fntype->get_substs ()[0].get_param_ty ()->resolve (); + + auto builtin_name + = build_atomic_builtin_name ("atomic_load_", fntype->get_locus (), + monomorphized_type); + if (builtin_name.empty ()) + return error_mark_node; + + tree atomic_load_raw = nullptr; + BuiltinsContext::get ().lookup_simple_builtin (builtin_name, + &atomic_load_raw); + rust_assert (atomic_load_raw); + + auto atomic_load + = build_fold_addr_expr_loc (Location ().gcc_location (), atomic_load_raw); + + auto load_call + = ctx->get_backend ()->call_expression (atomic_load, {src, memorder}, + nullptr, Location ()); + auto return_statement + = ctx->get_backend ()->return_statement (fndecl, {load_call}, Location ()); + + TREE_READONLY (load_call) = 0; + TREE_SIDE_EFFECTS (load_call) = 1; + + ctx->add_statement (return_statement); + finalize_intrinsic_block (ctx, fndecl); + + return fndecl; +} + static inline tree unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op) { diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-3.rs b/gcc/testsuite/rust/compile/torture/intrinsics-3.rs index 1acb3533c08..5c131bd8aa2 100644 --- a/gcc/testsuite/rust/compile/torture/intrinsics-3.rs +++ b/gcc/testsuite/rust/compile/torture/intrinsics-3.rs @@ -1,9 +1,7 @@ extern "rust-intrinsic" { fn not_an_intrinsic(); - fn atomic_load(); // { dg-message "sorry, unimplemented: intrinsic .atomic_load. is not yet implemented" } } fn main() { unsafe { not_an_intrinsic() }; // { dg-error "unknown builtin intrinsic: not_an_intrinsic" } - unsafe { atomic_load() }; } diff --git a/gcc/testsuite/rust/execute/torture/atomic_load.rs b/gcc/testsuite/rust/execute/torture/atomic_load.rs new file mode 100644 index 00000000000..28ed8ae78f1 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/atomic_load.rs @@ -0,0 +1,31 @@ +trait Copy {} + +extern "rust-intrinsic" { + pub fn atomic_load_seqcst(src: *const T) -> T; + pub fn atomic_load_acquire(src: *const T) -> T; + pub fn atomic_load_relaxed(src: *const T) -> T; + pub fn atomic_load_unordered(src: *const T) -> T; +} + +fn main() -> i32 { + let one; + let two; + let three; + let four; + + unsafe { + let mut src = 1; + one = atomic_load_seqcst(&src); + + src = 2; + two = atomic_load_acquire(&src); + + src = 3; + three = atomic_load_relaxed(&src); + + src = 4; + four = atomic_load_unordered(&src); + } + + (four + three + two + one) - 10 +}