[libcxx-commits] [libcxx] [libc++] Diagnoses insufficiently aligned pointers for std::assume_aligned during constant evaluation (PR #73775)
Rajveer Singh Bharadwaj via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 30 23:33:42 PST 2023
https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/73775
>From 75f71ac36176a578e298ece15dc17fbd3b0173d5 Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Wed, 29 Nov 2023 15:55:25 +0530
Subject: [PATCH] [libc++] Diagnoses insufficiently aligned pointers for
std::assume_aligned during constant evaluation
Resolves Issue #64078
This is a libc++ enhancement when violating alignment assumption for __builtin_assume_aligned.
---
libcxx/include/__memory/assume_aligned.h | 1 +
.../assume_aligned.const_eval.verify.cpp | 32 +++++++++++++++++++
2 files changed, 33 insertions(+)
create mode 100644 libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp
diff --git a/libcxx/include/__memory/assume_aligned.h b/libcxx/include/__memory/assume_aligned.h
index bce00bf02a718c0..c87c3d9db16dffe 100644
--- a/libcxx/include/__memory/assume_aligned.h
+++ b/libcxx/include/__memory/assume_aligned.h
@@ -28,6 +28,7 @@ _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __ass
"std::assume_aligned<N>(p) requires N to be a power of two");
if (__libcpp_is_constant_evaluated()) {
+ (void)__builtin_assume_aligned(__ptr, _Np);
return __ptr;
} else {
_LIBCPP_ASSERT_UNCATEGORIZED(reinterpret_cast<uintptr_t>(__ptr) % _Np == 0, "Alignment assumption is violated");
diff --git a/libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp b/libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp
new file mode 100644
index 000000000000000..a5777451c99271c
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/memory/ptr.align/assume_aligned.const_eval.verify.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++03, c++11, c++14, c++17
+
+// #include <memory>
+
+// template<size_t N, class T>
+// [[nodiscard]] constexpr T* assume_aligned(T* ptr);
+
+// Diagnose insufficiently aligned pointers during constant evaluation.
+// ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=error -Xclang -verify-ignore-unexpected=warning
+
+#include <memory>
+
+template <size_t Size>
+constexpr bool test() {
+ auto data = new char[1];
+ auto data2 = std::assume_aligned<2>(data); // expected-note {{}}
+ delete[] data2;
+
+ return true;
+}
+
+static_assert(test<1>()); // expected-note {{}}
+static_assert(test<2>()); // expected-note {{}}
More information about the libcxx-commits
mailing list