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 "Allocator.h"
12 #include "Configuration.h"
13 #include "Environment.h"
15 #include "Synchronization.h"
21 #pragma omp begin declare target device_type(nohost)
23 [[gnu::used
, gnu::retain
, gnu::weak
,
25 "protected")]] DeviceMemoryPoolTy __omp_rtl_device_memory_pool
;
26 [[gnu::used
, gnu::retain
, gnu::weak
,
27 gnu::visibility("protected")]] DeviceMemoryPoolTrackingTy
28 __omp_rtl_device_memory_pool_tracker
;
30 /// Stateless bump allocator that uses the __omp_rtl_device_memory_pool
32 struct BumpAllocatorTy final
{
34 void *alloc(uint64_t Size
) {
35 Size
= utils::roundUp(Size
, uint64_t(allocator::ALIGNMENT
));
37 if (config::isDebugMode(DeviceDebugKind::AllocationTracker
)) {
38 atomic::add(&__omp_rtl_device_memory_pool_tracker
.NumAllocations
, 1,
40 atomic::add(&__omp_rtl_device_memory_pool_tracker
.AllocationTotal
, Size
,
42 atomic::min(&__omp_rtl_device_memory_pool_tracker
.AllocationMin
, Size
,
44 atomic::max(&__omp_rtl_device_memory_pool_tracker
.AllocationMax
, Size
,
49 reinterpret_cast<uint64_t *>(&__omp_rtl_device_memory_pool
.Ptr
);
51 reinterpret_cast<uint64_t>(Data
) + __omp_rtl_device_memory_pool
.Size
;
53 uint64_t OldData
= atomic::add(Data
, Size
, atomic::seq_cst
);
54 if (OldData
+ Size
> End
)
57 return reinterpret_cast<void *>(OldData
);
63 BumpAllocatorTy BumpAllocator
;
65 /// allocator namespace implementation
69 void allocator::init(bool IsSPMD
, KernelEnvironmentTy
&KernelEnvironment
) {
70 // TODO: Check KernelEnvironment for an allocator choice as soon as we have
74 void *allocator::alloc(uint64_t Size
) { return BumpAllocator
.alloc(Size
); }
76 void allocator::free(void *Ptr
) { BumpAllocator
.free(Ptr
); }
80 #pragma omp end declare target