From patchwork Tue Feb 21 12:01:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 60009 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1835546wrn; Tue, 21 Feb 2023 04:34:02 -0800 (PST) X-Google-Smtp-Source: AK7set8lXtQf48PI8SJVHUoomLi1FwjBFGMHcd0IhI4La36GgHCgLrn/sFfXWXQDUpRiXm8q2H/l X-Received: by 2002:a17:907:3201:b0:888:a32d:b50 with SMTP id xg1-20020a170907320100b00888a32d0b50mr18189318ejb.40.1676982842172; Tue, 21 Feb 2023 04:34:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676982842; cv=none; d=google.com; s=arc-20160816; b=OIAdso0TuqM3QmDxSBE9dJayT+zDfyIZ2mBZR8RnQelbDGC03B33OPYxWh8PyiDZZa PBmqIawGM7n4xulRyWH7Bc9XpyQ6B2IirkCfOnAcP2Q4NPMeEoe59YBcgWOyXBrj7Gqv egcUXNbbQzDd6MmNPLr57ByNRfESKFMOFHiPEVd3WBx70DzlIM/w9QJH9H1/WYx0FVm1 8GhYDeALBLpWO1kP978Vk5PyoU3E03UBYv/59hL8qrq3MGRb5Vg8kt/H80vEZtMYWvLu IZN7hcI7W3CesO0WzBV9UgHNEkDj8kpuYs/1fVtN6ijpeYpc5zrdlx9wwQAy1uF2PLEn 6P5Q== 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=u7X/OfhZtknKt9FIFWe3Qg0++vyap5e/JC7aYw+rSVA=; b=Jik7hV0Svmc8KtbQX+gq2IUJ9Xa1hGkszxBcWFWBT2BQQSHBTbrWDvhiU+th3vHSSe 4328de3/fU8MSJrWybMjKx06M9VTJaujKKKaWYC5I7oR8QLK4U5ZOymv98cjtrD9Q+8w dXJJbKeWR313+O4M4/vIWm+7Z5DBxly/Uc7LqmHv1i8RIg/l2omXe8qoJZLhmnDNygRJ SlzayP7iiSTIENjEBG6erBpJWg7Ek2DhP1vpnDdBK6lgnL7XeexTcCfXz9a+xv5HxFF6 NuC4dYgF4A9YiBjWHBk15ADPph3UeObhW4wYp8NidWzo6Z/62PAwTCHdFABWd1ljGuhM ogMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=IWugtZc2; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id f25-20020a170906739900b008b17cc2e38asi18939677ejl.582.2023.02.21.04.34.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:34:02 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=IWugtZc2; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 5AA0B383FB88 for ; Tue, 21 Feb 2023 12:12:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id E69743838152 for ; Tue, 21 Feb 2023 12:04:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E69743838152 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-x430.google.com with SMTP id z8so3884509wrm.8 for ; Tue, 21 Feb 2023 04:04:13 -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=u7X/OfhZtknKt9FIFWe3Qg0++vyap5e/JC7aYw+rSVA=; b=IWugtZc2IJHJ72JgYFHqtdAAtZiOhZkQUqufRQIMStepnxV6aO2Ub9yUUMiwLJsiMf A5Qpg9CBXQAcqdjC//wnAmjYRTh1RTA4050l8w13vMOz1h3lXC49XTYtQwvLbwgDsita jjjgOILZrKX0fYulrfbK+i+PNBsiuG0mSoD2AmE9HGGJ6VkOjGwUBXsBtQ8yV2DsHb8x cfJ26119OQBCGs19mARepx0cjRsZ8zxh1LtvnwQ++iaDD+phuRt4WfNas8YElQMOAVfS 17nmSvqB4fvBq3yNVvAz1ARigmA4nX5UsdQJH8q+N/sF2whWHfa95CJh9eqYetRRbYXM JmAQ== 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=u7X/OfhZtknKt9FIFWe3Qg0++vyap5e/JC7aYw+rSVA=; b=CzLoxVRQSsIbZYu92wI0WaOak6JOgMyInBQWmNs2owopr6ZrvT35/3z1X3WVX19tPD i4bRRv1fLkd7W8MFbpjq3A3aNoun6HpEQQnXSrRbHWzQXOXWK0Yf0kmKSPcWrv7vqq4C ZUAH5eyQwNNucz08DcBsuW2bgrZNpbnib8BqXOKkkX+TRNR7CGLbEIZ3zJRmh0BuHgEO rAZv/DbHe80b918kY6TgMDkuRjwhgPPTUVmddfk91HwA8DG7UCgHZVrLc9ra2/0r2fWD 6mleTV9UJG554vnI3RaVlm3T3cBLXtZ9CyLzpDZMZREMXZnsOIeLyxzXNDC2rhm47xoS WkWg== X-Gm-Message-State: AO0yUKUUK1lU7nvYssdhL79EBGMwfCEL0KNSN0B3GrXlCh6vSBPALeCq aE1qTeJMbZBq0y08s14TTWBxUQSdnLnxPCxeIw== X-Received: by 2002:a5d:5945:0:b0:2c5:5c00:b917 with SMTP id e5-20020a5d5945000000b002c55c00b917mr2323189wri.0.1676981052561; Tue, 21 Feb 2023 04:04:12 -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.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:04:12 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 061/103] gccrs: intrinsics: Add unchecked operation intrinsics Date: Tue, 21 Feb 2023 13:01:51 +0100 Message-Id: <20230221120230.596966-62-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?1758443960626760221?= X-GMAIL-MSGID: =?utf-8?q?1758443960626760221?= From: Arthur Cohen gcc/rust/ChangeLog: * backend/rust-compile-intrinsic.cc (is_basic_integer_type): New function. (unchecked_op_inner): New handler. (unchecked_op_handler): New handler. gcc/testsuite/ChangeLog: * rust/compile/torture/intrinsics-6.rs: New test. * rust/compile/torture/intrinsics-7.rs: New test. --- gcc/rust/backend/rust-compile-intrinsic.cc | 80 +++++++++++++++++++ .../rust/compile/torture/intrinsics-6.rs | 21 +++++ .../rust/compile/torture/intrinsics-7.rs | 10 +++ 3 files changed, 111 insertions(+) create mode 100644 gcc/testsuite/rust/compile/torture/intrinsics-6.rs create mode 100644 gcc/testsuite/rust/compile/torture/intrinsics-7.rs diff --git a/gcc/rust/backend/rust-compile-intrinsic.cc b/gcc/rust/backend/rust-compile-intrinsic.cc index 7c592dabb38..46ea5b3f795 100644 --- a/gcc/rust/backend/rust-compile-intrinsic.cc +++ b/gcc/rust/backend/rust-compile-intrinsic.cc @@ -34,6 +34,22 @@ namespace Rust { namespace Compile { +static bool +is_basic_integer_type (TyTy::BaseType *type) +{ + switch (type->get_kind ()) + { + case TyTy::INT: + case TyTy::UINT: + case TyTy::USIZE: + case TyTy::ISIZE: + return true; + default: + return false; + break; + } +} + static tree offset_handler (Context *ctx, TyTy::FnType *fntype); static tree @@ -104,6 +120,17 @@ atomic_store_handler (int ordering) }; } +static inline tree +unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op); + +const static std::function +unchecked_op_handler (tree_code op) +{ + return [op] (Context *ctx, TyTy::FnType *fntype) { + return unchecked_op_inner (ctx, fntype, op); + }; +} + static inline tree sorry_handler (Context *ctx, TyTy::FnType *fntype) { @@ -132,6 +159,13 @@ static const std::mapadd_statement (store_call); + finalize_intrinsic_block (ctx, fndecl); + + return fndecl; +} + +static inline tree +unchecked_op_inner (Context *ctx, TyTy::FnType *fntype, tree_code op) +{ + rust_assert (fntype->get_params ().size () == 2); + 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 unchecked_ BODY BEGIN + + auto x = ctx->get_backend ()->var_expression (param_vars[0], Location ()); + auto y = ctx->get_backend ()->var_expression (param_vars[1], Location ()); + + auto *monomorphized_type + = fntype->get_substs ().at (0).get_param_ty ()->resolve (); + if (!is_basic_integer_type (monomorphized_type)) + rust_error_at (fntype->get_locus (), + "unchecked operation intrinsics can only be used with " + "basic integer types (got %qs)", + monomorphized_type->get_name ().c_str ()); + + auto expr = build2 (op, TREE_TYPE (x), x, y); + auto return_statement + = ctx->get_backend ()->return_statement (fndecl, {expr}, Location ()); + + ctx->add_statement (return_statement); + + // BUILTIN unchecked_ BODY END finalize_intrinsic_block (ctx, fndecl); diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-6.rs b/gcc/testsuite/rust/compile/torture/intrinsics-6.rs new file mode 100644 index 00000000000..143b62a2b27 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/intrinsics-6.rs @@ -0,0 +1,21 @@ +extern "rust-intrinsic" { + pub fn unchecked_add(x: T, y: T) -> T; + pub fn unchecked_sub(x: T, y: T) -> T; + pub fn unchecked_mul(x: T, y: T) -> T; + pub fn unchecked_div(x: T, y: T) -> T; + pub fn unchecked_rem(x: T, y: T) -> T; + pub fn unchecked_shl(x: T, y: T) -> T; + pub fn unchecked_shr(x: T, y: T) -> T; +} + +fn main() -> i32 { + let zero0 = unsafe { (1 + 5) - unchecked_add(1, 5) }; + let zero1 = unsafe { (1 - 5) - unchecked_sub(1, 5) }; + let zero2 = unsafe { (1 * 5) - unchecked_mul(1, 5) }; + let zero3 = unsafe { (1 / 5) - unchecked_div(1, 5) }; + let zero4 = unsafe { (1 % 5) - unchecked_rem(1, 5) }; + let zero5 = unsafe { (1 << 5) - unchecked_shl(1, 5) }; + let zero6 = unsafe { (1 >> 5) - unchecked_shr(1, 5) }; + + zero0 + zero1 + zero2 + zero3 + zero4 + zero5 + zero6 +} diff --git a/gcc/testsuite/rust/compile/torture/intrinsics-7.rs b/gcc/testsuite/rust/compile/torture/intrinsics-7.rs new file mode 100644 index 00000000000..8e8c5fe0cdf --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/intrinsics-7.rs @@ -0,0 +1,10 @@ +extern "rust-intrinsic" { + pub fn unchecked_add(x: T, y: T) -> T; + // { dg-error "unchecked operation intrinsics can only be used with basic integer types .got .NotAdd.." "" { target *-*-* } .-1 } +} + +fn main() { + struct NotAdd; + + unsafe { unchecked_add(NotAdd, NotAdd) }; +}