public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-10890] d: Fix core.volatile.volatileLoad discarded if result is unused
@ 2023-07-02  1:42 Iain Buclaw
  0 siblings, 0 replies; only message in thread
From: Iain Buclaw @ 2023-07-02  1:42 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:da7a10ea9245d7461253495698873891ffc20666

commit r11-10890-gda7a10ea9245d7461253495698873891ffc20666
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Sun Jul 2 03:24:53 2023 +0200

    d: Fix core.volatile.volatileLoad discarded if result is unused
    
    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.
    
            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.
    
    (cherry picked from commit 80ae426a195a0d035640a6301da833564deade52)

Diff:
---
 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(+)

diff --git a/gcc/d/intrinsics.cc b/gcc/d/intrinsics.cc
index 539dc0c1f37..5788f5cb4ad 100644
--- a/gcc/d/intrinsics.cc
+++ b/gcc/d/intrinsics.cc
@@ -721,6 +721,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;
 }
@@ -748,6 +749,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" } }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-02  1:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-02  1:42 [gcc r11-10890] d: Fix core.volatile.volatileLoad discarded if result is unused Iain Buclaw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).