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

Arthur O'Dwyer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 30 11:09:42 PDT 2021


Quuxplusone added inline comments.


================
Comment at: libcxx/include/memory:968
+}
+#endif // _LIBCPP_STD_VER >= 20
 
----------------
@ldionne wrote:
```
template<size_t _Np, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI
constexpr _Tp *assume_aligned(_Tp *__ptr) {
    if (is_constant_evaluated()) {
        return __ptr;
    } else {
        return static_cast<_Tp *>(__builtin_assume_aligned(__ptr, _Np));
    }
}
```
AIUI, the non-constexpr-ness problem is due entirely to that static_cast from `void*` back to `_Tp*`. ("Constexpr casts are like the Bay Bridge: free in one direction but not the other.") Did anyone think about this during the design of `__builtin_assume_aligned`? Could we just convince the compiler folks to fix it? The obvious solution is that `__builtin_assume_aligned`, just like `std::assume_aligned`, should return the same type that is passed in. Compiler builtins deal with way harder problems than "return the same type" on a daily basis; this should be a piece of cake, I'd think. And then we could use the builtin (and get the optimization assumption) even in constant-folded contexts.
```
template<size_t _Np, class _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI
constexpr _Tp *assume_aligned(_Tp *__ptr) {
#ifdef HAS_SANE_BUILTIN_SEMANTICS
    return __builtin_assume_aligned(__ptr, _Np);
#else
    if (is_constant_evaluated()) {
        return __ptr;
    } else {
        return static_cast<_Tp *>(__builtin_assume_aligned(__ptr, _Np));
    }
#endif
}
```



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

https://reviews.llvm.org/D108906



More information about the libcxx-commits mailing list