[libcxx-commits] [PATCH] D108906: Add std::assume_aligned

Gonzalo Brito Gadeschi via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 30 03:11:01 PDT 2021


gonzalobg updated this revision to Diff 369407.
gonzalobg added a comment.

Add missing maybe_unused attributes.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108906/new/

https://reviews.llvm.org/D108906

Files:
  libcxx/include/memory
  libcxx/test/std/utilities/memory/ptr.align/assume_aligned.fail.cpp
  libcxx/test/std/utilities/memory/ptr.align/assume_aligned.pass.cpp


Index: libcxx/test/std/utilities/memory/ptr.align/assume_aligned.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/utilities/memory/ptr.align/assume_aligned.pass.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// #include <memory>
+
+// template<size_t N, class T>
+//     [[nodiscard]] constexpr T* assume_aligned(T* ptr);
+
+#include <memory>
+
+constexpr int test() {
+  int a;
+  [[maybe_unused]] int* b = std::assume_aligned<4>(&a);
+  return sizeof(int);
+}
+
+int main(int, char**)
+{
+  [[maybe_unused]] constexpr int N = test();
+  int* a = nullptr;
+  [[maybe_unused]] int* b = std::assume_aligned<4>(a);
+
+  return 0;
+}
Index: libcxx/test/std/utilities/memory/ptr.align/assume_aligned.fail.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/utilities/memory/ptr.align/assume_aligned.fail.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+
+// #include <memory>
+
+// template<size_t N, class T>
+//     [[nodiscard]] constexpr T* assume_aligned(T* ptr);
+
+#include <memory>
+
+int main ()
+{
+  [[maybe_unused]] int *p = nullptr;
+  std::assume_aligned<4>(p);  // expected-error {{ignoring return value of function declared with 'nodiscard' attribute}}
+  return 0;
+}
Index: libcxx/include/memory
===================================================================
--- libcxx/include/memory
+++ libcxx/include/memory
@@ -953,6 +953,19 @@
   }
 };
 
+#if _LIBCPP_STD_VER >= 20
+template <size_t N, typename T>
+[[nodiscard]]
+_LIBCPP_CONSTEXPR
+_LIBCPP_ALWAYS_INLINE
+T* assume_aligned(T* __ptr)
+#if defined(__clang__)
+  __attribute__((assume_aligned(N)))
+#endif
+{
+    return __ptr;
+}
+#endif // _LIBCPP_STD_VER >= 20
 
 _LIBCPP_END_NAMESPACE_STD
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108906.369407.patch
Type: text/x-patch
Size: 2568 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210830/01f263bc/attachment.bin>


More information about the libcxx-commits mailing list