From patchwork Tue Feb 20 10:02:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 86022 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 92D893858D3C for ; Tue, 20 Feb 2024 10:03:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 2F13E3858D28 for ; Tue, 20 Feb 2024 10:02:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F13E3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2F13E3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708423365; cv=none; b=m7wJ6lbs+xhzMYaQe7BWbFXufcxQtSYANqM5e5lz5G+ANyM3yZoXRrH5w5ChCsn0iAEra9GXHsegvu4ewL2Q+1II5R6e2XXDe08D/REyUbEuI2ganqtVYn8aacurd6EEnVilbryta7qhALjimI2KeeeDtjodtk9UMJILFLO/m1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708423365; c=relaxed/simple; bh=pQjJFXpcqUEQp8Er3Gj+D2j60tOLEPRCqMK5jIopUeU=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=nf+qpYWsafpAYTUS6gbJuSYoymyHoWtBoSPogz4D/B0EoMnMWEoamlyFbIdc71hA1Qp68xZbWZYO3oIcRYnmSV6/P7ct6xS5YO6fE8Nf62+rJUqtgbnHD05iFN0K9u7kNBKhXb6BFY0Kxx+IYb3Qo1j2B+wtyi+5Det1aA3J9Co= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708423358; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=m9NoGC8oCjt4iQXKdMOwA/LD/TZN/gJi5++7SIAKXT0=; b=cb3FcuIqBs/u04ouygHWfvsyWUIbVaD8srkWJefr9U8Y+4vRW9Wcz/HnBynFmhzS4zzYgU DQ8KjMQXZfVLHqLJop9LAqIviMaFmhkYFMigpHChxj08hTJ0c9HA13f6HCV3hrAYAwjKK8 42CjJw693tQpr7xA5KA7OPRZtKsjFIY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-643-dpT2CbDWMcajd8pyBQZOxg-1; Tue, 20 Feb 2024 05:02:34 -0500 X-MC-Unique: dpT2CbDWMcajd8pyBQZOxg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5C4783812013; Tue, 20 Feb 2024 10:02:33 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1F4FE492BDA; Tue, 20 Feb 2024 10:02:33 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 41KA2UCD436647 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 20 Feb 2024 11:02:30 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 41KA2T9R436646; Tue, 20 Feb 2024 11:02:29 +0100 Date: Tue, 20 Feb 2024 11:02:29 +0100 From: Jakub Jelinek To: Richard Biener , Jason Merrill Cc: gcc-patches@gcc.gnu.org, Jonathan Wakely Subject: [PATCH] c-family, c++, v2: Fix up handling of types which may have padding in __atomic_{compare_}exchange Message-ID: References: <5988812a-3f21-4ef4-9205-fe267c356d58@redhat.com> <39q9r137-3884-67oq-2334-70q42so2sp22@fhfr.qr> MIME-Version: 1.0 In-Reply-To: <39q9r137-3884-67oq-2334-70q42so2sp22@fhfr.qr> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org On Tue, Feb 20, 2024 at 09:01:10AM +0100, Richard Biener wrote: > I'm not sure those would be really equivalent (MEM_REF vs. V_C_E > as well as combined vs. split). It really depends how RTL expansion > handles this (as you can see padding can be fun here). > > So I'd be nervous for a match.pd rule here (also we can't match > memory defs). Ok. Perhaps forwprop then; anyway, that would be an optimization. > As for your patch I'd go with a MEM_REF unconditionally, I don't > think we want different behavior whether there's padding or not ... I've made it conditional so that the MEM_REFs don't appear that often in the FE trees, but maybe that is fine. The unconditional patch would then be: 2024-02-20 Jakub Jelinek gcc/c-family/ * c-common.cc (resolve_overloaded_atomic_exchange): Instead of setting p1 to VIEW_CONVERT_EXPR (*p1), set it to MEM_REF with p1 and (typeof (p1)) 0 operands and I_type type. (resolve_overloaded_atomic_compare_exchange): Similarly for p2. gcc/cp/ * pt.cc (tsubst_expr): Handle MEM_REF. gcc/testsuite/ * g++.dg/ext/atomic-5.C: New test. Jakub --- gcc/c-family/c-common.cc.jj 2024-02-17 16:40:42.831571693 +0100 +++ gcc/c-family/c-common.cc 2024-02-20 10:58:56.599865656 +0100 @@ -7793,9 +7793,14 @@ resolve_overloaded_atomic_exchange (loca /* Convert object pointer to required type. */ p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0); (*params)[0] = p0; - /* Convert new value to required type, and dereference it. */ - p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR); - p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1); + /* Convert new value to required type, and dereference it. + If *p1 type can have padding or may involve floating point which + could e.g. be promoted to wider precision and demoted afterwards, + state of padding bits might not be preserved. */ + build_indirect_ref (loc, p1, RO_UNARY_STAR); + p1 = build2_loc (loc, MEM_REF, I_type, + build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1), + build_zero_cst (TREE_TYPE (p1))); (*params)[1] = p1; /* Move memory model to the 3rd position, and end param list. */ @@ -7873,9 +7878,14 @@ resolve_overloaded_atomic_compare_exchan p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1); (*params)[1] = p1; - /* Convert desired value to required type, and dereference it. */ - p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR); - p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2); + /* Convert desired value to required type, and dereference it. + If *p2 type can have padding or may involve floating point which + could e.g. be promoted to wider precision and demoted afterwards, + state of padding bits might not be preserved. */ + build_indirect_ref (loc, p2, RO_UNARY_STAR); + p2 = build2_loc (loc, MEM_REF, I_type, + build1 (VIEW_CONVERT_EXPR, I_type_ptr, p2), + build_zero_cst (TREE_TYPE (p2))); (*params)[2] = p2; /* The rest of the parameters are fine. NULL means no special return value --- gcc/cp/pt.cc.jj 2024-02-17 16:40:42.868571182 +0100 +++ gcc/cp/pt.cc 2024-02-20 10:57:36.646973603 +0100 @@ -20088,6 +20088,14 @@ tsubst_expr (tree t, tree args, tsubst_f RETURN (r); } + case MEM_REF: + { + tree op0 = RECUR (TREE_OPERAND (t, 0)); + tree op1 = RECUR (TREE_OPERAND (t, 0)); + tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); + RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1)); + } + case NOP_EXPR: { tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); --- gcc/testsuite/g++.dg/ext/atomic-5.C.jj 2024-02-20 10:57:36.647973589 +0100 +++ gcc/testsuite/g++.dg/ext/atomic-5.C 2024-02-20 10:57:36.647973589 +0100 @@ -0,0 +1,42 @@ +// { dg-do compile { target c++14 } } + +template +void +foo (long double *ptr, long double *val, long double *ret) +{ + __atomic_exchange (ptr, val, ret, __ATOMIC_RELAXED); +} + +template +bool +bar (long double *ptr, long double *exp, long double *des) +{ + return __atomic_compare_exchange (ptr, exp, des, false, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); +} + +bool +baz (long double *p, long double *q, long double *r) +{ + foo<0> (p, q, r); + foo<1> (p + 1, q + 1, r + 1); + return bar<0> (p + 2, q + 2, r + 2) || bar<1> (p + 3, q + 3, r + 3); +} + +constexpr int +qux (long double *ptr, long double *val, long double *ret) +{ + __atomic_exchange (ptr, val, ret, __ATOMIC_RELAXED); + return 0; +} + +constexpr bool +corge (long double *ptr, long double *exp, long double *des) +{ + return __atomic_compare_exchange (ptr, exp, des, false, + __ATOMIC_RELAXED, __ATOMIC_RELAXED); +} + +long double a[6]; +const int b = qux (a, a + 1, a + 2); +const bool c = corge (a + 3, a + 4, a + 5);