46 #ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H 47 #define LLVM_SUPPORT_TRAILINGOBJECTS_H 55 #include <type_traits> 59 namespace trailing_objects_internal {
65 FirstAlignment =
alignof(First),
71 Alignment = FirstAlignment > RestAlignment ? FirstAlignment : RestAlignment
77 enum { Alignment =
alignof(First) };
123 template <
int Align,
typename BaseTy,
typename TopTrailingObj,
typename PrevTy,
130 template <
int Align,
typename BaseTy,
typename TopTrailingObj,
typename PrevTy,
131 typename NextTy,
typename... MoreTys>
140 struct RequiresRealignment {
141 static const bool value =
alignof(PrevTy) <
alignof(NextTy);
144 static constexpr
bool requiresRealignment() {
145 return RequiresRealignment::value;
150 using ParentType::getTrailingObjectsImpl;
161 static const NextTy *
164 auto *Ptr = TopTrailingObj::getTrailingObjectsImpl(
166 TopTrailingObj::callNumTrailingObjects(
169 if (requiresRealignment())
170 return reinterpret_cast<const NextTy *
>(
173 return reinterpret_cast<const NextTy *
>(Ptr);
179 auto *Ptr = TopTrailingObj::getTrailingObjectsImpl(
181 TopTrailingObj::callNumTrailingObjects(
184 if (requiresRealignment())
185 return reinterpret_cast<NextTy *
>(
alignAddr(Ptr, Align::Of<NextTy>()));
187 return reinterpret_cast<NextTy *
>(Ptr);
194 size_t SizeSoFar,
size_t Count1,
196 return ParentType::additionalSizeToAllocImpl(
197 (requiresRealignment() ?
llvm::alignTo<
alignof(NextTy)>(SizeSoFar)
199 sizeof(NextTy) * Count1,
206 template <
int Align,
typename BaseTy,
typename TopTrailingObj,
typename PrevTy>
213 static void getTrailingObjectsImpl();
228 template <
typename BaseTy,
typename... TrailingTys>
230 trailing_objects_internal::AlignmentCalcHelper<
231 TrailingTys...>::Alignment,
232 BaseTy, TrailingObjects<BaseTy, TrailingTys...>,
233 BaseTy, TrailingTys...> {
235 template <
int A,
typename B,
typename T,
typename P,
typename... M>
238 template <
typename... Tys>
class Foo {};
242 BaseTy,
TrailingObjects<BaseTy, TrailingTys...>, BaseTy, TrailingTys...>
246 using ParentType::getTrailingObjectsImpl;
252 static void verifyTrailingObjectsAssertions() {
253 static_assert(std::is_final<BaseTy>(),
"BaseTy must be final.");
257 static const BaseTy *
258 getTrailingObjectsImpl(
const BaseTy *Obj,
264 getTrailingObjectsImpl(BaseTy *Obj,
277 callNumTrailingObjects(
const BaseTy *Obj,
282 template <
typename T>
283 static size_t callNumTrailingObjects(
const BaseTy *Obj,
291 using ParentType::OverloadToken;
295 template <
typename T>
296 using OverloadToken =
typename ParentType::template OverloadToken<T>;
303 verifyTrailingObjectsAssertions();
306 return this->getTrailingObjectsImpl(
307 static_cast<const BaseTy *>(
this),
315 verifyTrailingObjectsAssertions();
318 return this->getTrailingObjectsImpl(
328 template <
typename... Tys>
329 static constexpr
typename std::enable_if<
330 std::is_same<Foo<TrailingTys...>, Foo<Tys...>>::value,
size_t>
::type 332 TrailingTys,
size_t>::
type... Counts) {
333 return ParentType::additionalSizeToAllocImpl(0, Counts...);
340 template <
typename... Tys>
341 static constexpr
typename std::enable_if<
342 std::is_same<Foo<TrailingTys...>, Foo<Tys...>>::value,
size_t>
::type 344 TrailingTys,
size_t>::
type... Counts) {
345 return sizeof(BaseTy) + ParentType::additionalSizeToAllocImpl(0, Counts...);
365 enum {
Size = totalSizeToAlloc<Tys...>(Counts...) };
367 alignas(BaseTy)
char buffer[
Size];
377 assert(p &&
"FixedSizeStorageOwner owns null?");
381 BaseTy *
get() {
return p; }
382 const BaseTy *
get()
const {
return p; }
A type that acts as the owner for an object placed into fixed storage.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This class represents lattice values for constants.
static void verifyTrailingObjectsAlignment()
A type where its ::with_counts template member has a ::type member suitable for use as uninitialized ...
static constexpr std::enable_if< std::is_same< Foo< TrailingTys... >, Foo< Tys... > >::value, size_t >::type additionalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType< TrailingTys, size_t >::type... Counts)
Returns the size of the trailing data, if an object were allocated with the given counts (The counts ...
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static NextTy * getTrailingObjectsImpl(BaseTy *Obj, TrailingObjectsBase::OverloadToken< NextTy >)
T * getTrailingObjects()
Returns a pointer to the trailing object array of the given type (which must be one of those specifie...
static constexpr size_t additionalSizeToAllocImpl(size_t SizeSoFar, size_t Count1, typename ExtractSecondType< MoreTys, size_t >::type... MoreCounts)
const T * getTrailingObjects() const
Returns a pointer to the trailing object array of the given type (which must be one of those specifie...
See the file comment for details on the usage of the TrailingObjects type.
static constexpr std::enable_if< std::is_same< Foo< TrailingTys... >, Foo< Tys... > >::value, size_t >::type totalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType< TrailingTys, size_t >::type... Counts)
Returns the total size of an object if it were allocated with the given trailing object counts...
The base class for TrailingObjects* classes.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
static constexpr size_t additionalSizeToAllocImpl(size_t SizeSoFar)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const NextTy * getTrailingObjectsImpl(const BaseTy *Obj, TrailingObjectsBase::OverloadToken< NextTy >)
Helper template to calculate the max alignment requirement for a set of objects.
FixedSizeStorageOwner(BaseTy *p)
OverloadToken's purpose is to allow specifying function overloads for different types, without actually taking the types as parameters.