Poplar and PopLibs
Alignment.hpp
1// Copyright (c) 2019 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_Alignment_hpp
4#define poplar_Alignment_hpp
5
6#include <cstddef>
7
8// This is a replacement for __attribute__((assume_aligned(N))) as it can more
9// reliably generate alignment assumptions at the IR level after the following
10// clang patches:
11// * https://reviews.llvm.org/D73005
12// * https://reviews.llvm.org/D73006
13//
14// Also see T17070.
15//
16// This function has the same interface as std::assume_aligned (C++20), making
17// a future transition for the standard function easier.
18//
19// Based on proposed implementation here:
20// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1007r1.pdf
21//
22// Extend this for other platforms and compilers as needed (see link above).
23template <std::size_t N, typename T>
24#if defined(__clang__) || defined(__GNUC__)
25__attribute__((always_inline))
26#endif
27constexpr T *
28poplar_assume_aligned(T *ptr) noexcept {
29#if defined(__clang__) || defined(__GNUC__)
30 return reinterpret_cast<T *>(__builtin_assume_aligned(ptr, N));
31#else
32 return ptr;
33#endif
34}
35
36#endif // poplar_Alignment_hpp