From patchwork Sun Jul 2 01:44:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1802278 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=vdeNwJm/; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QtsLf6nzzz20Pg for ; Sun, 2 Jul 2023 11:44:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B2D0A3858CDB for ; Sun, 2 Jul 2023 01:44:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B2D0A3858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688262294; bh=ajIUX0OKqpSeVGUPfAUuQxgmWW6YGc/8eGstkiQR39w=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=vdeNwJm/yG3b8+ldOtsgzEXwHxToOjk96ObcFxR2TQ5+xn4RfYPqzjrqIQswj2tey uOpW+8AMP7vga5w92My55NRFkpeRHVISp381KUoAJ5eGDERoo8e3Ic+ORRgrKuyXJG YUfD0+5l/Ux53yDvgg2zFt4gTN3QcXkl7sb+7qQo= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by sourceware.org (Postfix) with ESMTPS id 844513858D39 for ; Sun, 2 Jul 2023 01:44:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 844513858D39 Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4QtsKg38wcz9sRp; Sun, 2 Jul 2023 03:44:07 +0200 (CEST) To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Fix core.volatile.volatileLoad discarded if result is unused Date: Sun, 2 Jul 2023 03:44:04 +0200 Message-Id: <20230702014404.3398624-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_BLACK 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: , X-Patchwork-Original-From: Iain Buclaw via Gcc-patches From: Iain Buclaw Reply-To: Iain Buclaw Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi, The first pass of code generation in the D front-end splits up all compound expressions and discards expressions that have no side effects. This included calls to the `volatileLoad' intrinsic if its result was not used, causing such calls to be eliminated from the program. We already set TREE_THIS_VOLATILE on the expression, however the tree documentation says if this bit is set in an expression, so is TREE_SIDE_EFFECTS. So set TREE_SIDE_EFFECTS on the expression too. This prevents any early discarding from occuring. Bootstrapped and regression tested on x86_64-linux-gnu/-m32, committed to mainline, and backported to releases/gcc-13, gcc-12, and gcc-11. Regards, Iain. --- PR d/110516 gcc/d/ChangeLog: * intrinsics.cc (expand_volatile_load): Set TREE_SIDE_EFFECTS on the expanded expression. (expand_volatile_store): Likewise. gcc/testsuite/ChangeLog: * gdc.dg/torture/pr110516a.d: New test. * gdc.dg/torture/pr110516b.d: New test. --- gcc/d/intrinsics.cc | 2 ++ gcc/testsuite/gdc.dg/torture/pr110516a.d | 12 ++++++++++++ gcc/testsuite/gdc.dg/torture/pr110516b.d | 12 ++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 gcc/testsuite/gdc.dg/torture/pr110516a.d create mode 100644 gcc/testsuite/gdc.dg/torture/pr110516b.d diff --git a/gcc/d/intrinsics.cc b/gcc/d/intrinsics.cc index 0121d81eb14..aaf04e50baa 100644 --- a/gcc/d/intrinsics.cc +++ b/gcc/d/intrinsics.cc @@ -1007,6 +1007,7 @@ expand_volatile_load (tree callexp) tree type = build_qualified_type (TREE_TYPE (ptrtype), TYPE_QUAL_VOLATILE); tree result = indirect_ref (type, ptr); TREE_THIS_VOLATILE (result) = 1; + TREE_SIDE_EFFECTS (result) = 1; return result; } @@ -1034,6 +1035,7 @@ expand_volatile_store (tree callexp) tree type = build_qualified_type (TREE_TYPE (ptrtype), TYPE_QUAL_VOLATILE); tree result = indirect_ref (type, ptr); TREE_THIS_VOLATILE (result) = 1; + TREE_SIDE_EFFECTS (result) = 1; /* (*(volatile T *) ptr) = value; */ tree value = CALL_EXPR_ARG (callexp, 1); diff --git a/gcc/testsuite/gdc.dg/torture/pr110516a.d b/gcc/testsuite/gdc.dg/torture/pr110516a.d new file mode 100644 index 00000000000..276455ae408 --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/pr110516a.d @@ -0,0 +1,12 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110516 +// { dg-do compile } +// { dg-options "-fno-moduleinfo -fdump-tree-optimized" } +void fn110516(ubyte* ptr) +{ + import core.volatile : volatileLoad; + volatileLoad(ptr); + volatileLoad(ptr); + volatileLoad(ptr); + volatileLoad(ptr); +} +// { dg-final { scan-tree-dump-times " ={v} " 4 "optimized" } } diff --git a/gcc/testsuite/gdc.dg/torture/pr110516b.d b/gcc/testsuite/gdc.dg/torture/pr110516b.d new file mode 100644 index 00000000000..b7a67e716a5 --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/pr110516b.d @@ -0,0 +1,12 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110516 +// { dg-do compile } +// { dg-options "-fno-moduleinfo -fdump-tree-optimized" } +void fn110516(ubyte* ptr) +{ + import core.volatile : volatileStore; + volatileStore(ptr, 0); + volatileStore(ptr, 0); + volatileStore(ptr, 0); + volatileStore(ptr, 0); +} +// { dg-final { scan-tree-dump-times " ={v} " 4 "optimized" } }