[libcxx-commits] [libcxx] [libc++] Add assumption for align of begin and end pointers of vector. (PR #108961)
Dana Jansens via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Sep 17 11:08:34 PDT 2024
================
@@ -1027,6 +1027,10 @@ private:
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(vector&, false_type) _NOEXCEPT {}
+
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __add_alignment_assumption(pointer __p) const _NOEXCEPT {
+ return static_cast<pointer>(__builtin_assume_aligned(__p, alignof(decltype(*__p))));
----------------
danakj wrote:
It seems that `__builtin_assume_aligned` can't be used in this way in a constexpr context since it returns `void*`:
Example:
```
../../base/containers/checked_iterators_unittest.cc:52:49: error: constexpr variable 'begin' must be initialized by a constant expression
52 | constexpr CheckedContiguousConstIterator<int> begin = MakeConstIter(arr, 0u);
| ^ ~~~~~~~~~~~~~~~~~~~~~~
../../base/containers/checked_iterators.h:35:12: note: cast from 'void *' is not allowed in a constant expression in C++ standards before C++2c
35 | return static_cast<T*>(__builtin_assume_aligned(t, alignof(T)));
| ^
```
This works around the compile issue for me in Chromium with clang, though I hope it still optimizes the same.
```cc
if (std::is_constant_evaluated()) {
return t;
} else {
return static_cast<T*>(__builtin_assume_aligned(t, alignof(T)));
}
```
I think we'd need to maintain constexpr for these methods in libc++ too, hopefully there are tests that use the iterators from a constexpr context?
https://github.com/llvm/llvm-project/pull/108961
More information about the libcxx-commits
mailing list