1//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the AlignedCharArray and AlignedCharArrayUnion classes.
11//
12//===----------------------------------------------------------------------===//
13
14// ATen: modified from llvm::AlignOf
15// replaced LLVM_ALIGNAS with alignas
16
17#pragma once
18
19#include <cstddef>
20
21namespace c10 {
22
23/// \struct AlignedCharArray
24/// \brief Helper for building an aligned character array type.
25///
26/// This template is used to explicitly build up a collection of aligned
27/// character array types. We have to build these up using a macro and explicit
28/// specialization to cope with MSVC (at least till 2015) where only an
29/// integer literal can be used to specify an alignment constraint. Once built
30/// up here, we can then begin to indirect between these using normal C++
31/// template parameters.
32
33// MSVC requires special handling here.
34#ifndef _MSC_VER
35
36template <size_t Alignment, size_t Size>
37struct AlignedCharArray {
38 alignas(Alignment) char buffer[Size];
39};
40
41#else // _MSC_VER
42
43/// \brief Create a type with an aligned char buffer.
44template <size_t Alignment, size_t Size>
45struct AlignedCharArray;
46
47// We provide special variations of this template for the most common
48// alignments because __declspec(align(...)) doesn't actually work when it is
49// a member of a by-value function argument in MSVC, even if the alignment
50// request is something reasonably like 8-byte or 16-byte. Note that we can't
51// even include the declspec with the union that forces the alignment because
52// MSVC warns on the existence of the declspec despite the union member forcing
53// proper alignment.
54
55template <size_t Size>
56struct AlignedCharArray<1, Size> {
57 union {
58 char aligned;
59 char buffer[Size];
60 };
61};
62
63template <size_t Size>
64struct AlignedCharArray<2, Size> {
65 union {
66 short aligned;
67 char buffer[Size];
68 };
69};
70
71template <size_t Size>
72struct AlignedCharArray<4, Size> {
73 union {
74 int aligned;
75 char buffer[Size];
76 };
77};
78
79template <size_t Size>
80struct AlignedCharArray<8, Size> {
81 union {
82 double aligned;
83 char buffer[Size];
84 };
85};
86
87// The rest of these are provided with a __declspec(align(...)) and we simply
88// can't pass them by-value as function arguments on MSVC.
89
90#define AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
91 template <size_t Size> \
92 struct AlignedCharArray<x, Size> { \
93 __declspec(align(x)) char buffer[Size]; \
94 };
95
96AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
97AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
98AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
99AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
100
101#undef AT_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
102
103#endif // _MSC_VER
104
105namespace detail {
106template <
107 typename T1,
108 typename T2 = char,
109 typename T3 = char,
110 typename T4 = char,
111 typename T5 = char,
112 typename T6 = char,
113 typename T7 = char,
114 typename T8 = char,
115 typename T9 = char,
116 typename T10 = char>
117class AlignerImpl {
118 T1 t1;
119 T2 t2;
120 T3 t3;
121 T4 t4;
122 T5 t5;
123 T6 t6;
124 T7 t7;
125 T8 t8;
126 T9 t9;
127 T10 t10;
128
129 public:
130 AlignerImpl() = delete;
131};
132
133template <
134 typename T1,
135 typename T2 = char,
136 typename T3 = char,
137 typename T4 = char,
138 typename T5 = char,
139 typename T6 = char,
140 typename T7 = char,
141 typename T8 = char,
142 typename T9 = char,
143 typename T10 = char>
144union SizerImpl {
145 char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
146 arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
147 arr9[sizeof(T9)], arr10[sizeof(T10)];
148};
149} // end namespace detail
150
151/// \brief This union template exposes a suitably aligned and sized character
152/// array member which can hold elements of any of up to ten types.
153///
154/// These types may be arrays, structs, or any other types. The goal is to
155/// expose a char array buffer member which can be used as suitable storage for
156/// a placement new of any of these types. Support for more than ten types can
157/// be added at the cost of more boilerplate.
158template <
159 typename T1,
160 typename T2 = char,
161 typename T3 = char,
162 typename T4 = char,
163 typename T5 = char,
164 typename T6 = char,
165 typename T7 = char,
166 typename T8 = char,
167 typename T9 = char,
168 typename T10 = char>
169struct AlignedCharArrayUnion
170 : AlignedCharArray<
171 alignof(detail::AlignerImpl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>),
172 sizeof(::c10::detail::
173 SizerImpl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>)> {};
174} // end namespace c10
175