1 //===------ State.cpp - OpenMP State & ICV interface ------------- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 #include "Shared/Environment.h"
13 #include "Allocator.h"
14 #include "Configuration.h"
15 #include "DeviceTypes.h"
16 #include "DeviceUtils.h"
18 #include "Synchronization.h"
22 #pragma omp begin declare target device_type(nohost)
24 [[gnu::used
, gnu::retain
, gnu::weak
,
26 "protected")]] DeviceMemoryPoolTy __omp_rtl_device_memory_pool
;
27 [[gnu::used
, gnu::retain
, gnu::weak
,
28 gnu::visibility("protected")]] DeviceMemoryPoolTrackingTy
29 __omp_rtl_device_memory_pool_tracker
;
31 /// Stateless bump allocator that uses the __omp_rtl_device_memory_pool
33 struct BumpAllocatorTy final
{
35 void *alloc(uint64_t Size
) {
36 Size
= utils::roundUp(Size
, uint64_t(allocator::ALIGNMENT
));
38 if (config::isDebugMode(DeviceDebugKind::AllocationTracker
)) {
39 atomic::add(&__omp_rtl_device_memory_pool_tracker
.NumAllocations
, 1,
41 atomic::add(&__omp_rtl_device_memory_pool_tracker
.AllocationTotal
, Size
,
43 atomic::min(&__omp_rtl_device_memory_pool_tracker
.AllocationMin
, Size
,
45 atomic::max(&__omp_rtl_device_memory_pool_tracker
.AllocationMax
, Size
,
50 reinterpret_cast<uint64_t *>(&__omp_rtl_device_memory_pool
.Ptr
);
52 reinterpret_cast<uint64_t>(Data
) + __omp_rtl_device_memory_pool
.Size
;
54 uint64_t OldData
= atomic::add(Data
, Size
, atomic::seq_cst
);
55 if (OldData
+ Size
> End
)
58 return reinterpret_cast<void *>(OldData
);
64 BumpAllocatorTy BumpAllocator
;
66 /// allocator namespace implementation
70 void allocator::init(bool IsSPMD
, KernelEnvironmentTy
&KernelEnvironment
) {
71 // TODO: Check KernelEnvironment for an allocator choice as soon as we have
75 void *allocator::alloc(uint64_t Size
) { return BumpAllocator
.alloc(Size
); }
77 void allocator::free(void *Ptr
) { BumpAllocator
.free(Ptr
); }
81 #pragma omp end declare target