[gcc r9-9003] libstdc++: Make std::assume_aligned a constexpr function [PR 97132]

Jonathan Wakely redi@gcc.gnu.org
Mon Oct 19 21:25:56 GMT 2020


https://gcc.gnu.org/g:77923ad01415f6e72af844cbef5227f5b5a9fb4b

commit r9-9003-g77923ad01415f6e72af844cbef5227f5b5a9fb4b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Sep 21 14:28:58 2020 +0100

    libstdc++: Make std::assume_aligned a constexpr function [PR 97132]
    
    The cast from void* to T* in std::assume_aligned is not valid in a
    constexpr function. The optimization hint is redundant during constant
    evaluation anyway (the compiler can see the object and knows its
    alignment). Simply return the original pointer without applying the
    __builtin_assume_aligned hint to it when doing constant evaluation.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/97132
            * include/std/memory (assume_aligned): Do not use
            __builtin_assume_aligned during constant evaluation.
            * testsuite/20_util/assume_aligned/1.cc: Improve test.
            * testsuite/20_util/assume_aligned/97132.cc: New test.
    
    (cherry picked from commit f10ed928e2f8ecc2c859abff8f2f9296b11b8d95)

Diff:
---
 libstdc++-v3/include/std/memory                    | 25 +++++++++++++---
 libstdc++-v3/testsuite/20_util/assume_aligned/1.cc | 23 ++++++++++++++-
 .../testsuite/20_util/assume_aligned/97132.cc      | 34 ++++++++++++++++++++++
 3 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/libstdc++-v3/include/std/memory b/libstdc++-v3/include/std/memory
index cb8be4dc129..995bfa27a17 100644
--- a/libstdc++-v3/include/std/memory
+++ b/libstdc++-v3/include/std/memory
@@ -158,14 +158,31 @@ get_pointer_safety() noexcept { return pointer_safety::relaxed; }
 
 #if __cplusplus > 201703L
 #define __cpp_lib_assume_aligned 201811L
-  /// Inform the compiler that a pointer is aligned.
+  /** @brief Inform the compiler that a pointer is aligned.
+   *
+   *  @tparam _Align An alignment value (i.e. a power of two)
+   *  @tparam _Tp    An object type
+   *  @param  __ptr  A pointer that is aligned to _Align
+   *
+   *  C++20 20.10.6 [ptr.align]
+   *
+   *  @ingroup memory
+   */
   template<size_t _Align, class _Tp>
     [[nodiscard,__gnu__::__always_inline__]]
-    constexpr _Tp* assume_aligned(_Tp* __ptr)
+    constexpr _Tp*
+    assume_aligned(_Tp* __ptr) noexcept
     {
       static_assert(std::ispow2(_Align));
-      _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
-      return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
+      if (std::is_constant_evaluated())
+	return __ptr;
+      else
+	{
+	  // This function is expected to be used in hot code, where
+	  // __glibcxx_assert would add unwanted overhead.
+	  _GLIBCXX_DEBUG_ASSERT((std::uintptr_t)__ptr % _Align == 0);
+	  return static_cast<_Tp*>(__builtin_assume_aligned(__ptr, _Align));
+	}
     }
 #endif // C++2a
 
diff --git a/libstdc++-v3/testsuite/20_util/assume_aligned/1.cc b/libstdc++-v3/testsuite/20_util/assume_aligned/1.cc
index 86208bcdee3..bbd100f18f5 100644
--- a/libstdc++-v3/testsuite/20_util/assume_aligned/1.cc
+++ b/libstdc++-v3/testsuite/20_util/assume_aligned/1.cc
@@ -15,7 +15,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-options "-std=gnu++2a" }
+// { dg-options "-std=gnu++2a -O2" }
 // { dg-do run { target c++2a } }
 
 #include <memory>
@@ -29,7 +29,28 @@ test01()
   VERIFY( p == &i );
 }
 
+[[gnu::noipa,gnu::noinline]]
+int*
+create_aligned(std::size_t alignment, void* p, std::size_t n)
+{
+  return ::new(std::align(alignment, sizeof(int), p, n)) int(42);
+}
+
+extern "C" void undefined(); // call to this should be optimized away
+
+void
+test02()
+{
+  unsigned char buf[sizeof(int) * 128];
+  int* p = create_aligned(64, buf + 1, sizeof(buf) - 1);
+  int* q = std::assume_aligned<64>(p);
+  if ((std::uintptr_t)q % 64)
+    undefined();
+  VERIFY( p == q );
+}
+
 int main()
 {
   test01();
+  test02();
 }
diff --git a/libstdc++-v3/testsuite/20_util/assume_aligned/97132.cc b/libstdc++-v3/testsuite/20_util/assume_aligned/97132.cc
new file mode 100644
index 00000000000..887e3470651
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/assume_aligned/97132.cc
@@ -0,0 +1,34 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include <memory>
+
+// PR libstdc++/97132 - assume_aligned is not constexpr
+
+constexpr bool test01()
+{
+  struct alignas(32) S { int i; };
+  S s{42};
+  int* p = std::assume_aligned<32>(&s.i);
+  *p = 48;
+  return s.i == 48;
+}
+
+static_assert( test01() );


More information about the Libstdc++-cvs mailing list