| 1 | //===------ State.cpp - OpenMP State & ICV interface ------------- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "Shared/Environment.h" |
| 12 | |
| 13 | #include "Allocator.h" |
| 14 | #include "Configuration.h" |
| 15 | #include "DeviceTypes.h" |
| 16 | #include "DeviceUtils.h" |
| 17 | #include "Mapping.h" |
| 18 | #include "Synchronization.h" |
| 19 | |
| 20 | using namespace ompx; |
| 21 | |
| 22 | [[gnu::used, gnu::retain, gnu::weak, |
| 23 | gnu::visibility( |
| 24 | "protected" )]] DeviceMemoryPoolTy __omp_rtl_device_memory_pool; |
| 25 | [[gnu::used, gnu::retain, gnu::weak, |
| 26 | gnu::visibility("protected" )]] DeviceMemoryPoolTrackingTy |
| 27 | __omp_rtl_device_memory_pool_tracker; |
| 28 | |
| 29 | /// Stateless bump allocator that uses the __omp_rtl_device_memory_pool |
| 30 | /// directly. |
| 31 | struct BumpAllocatorTy final { |
| 32 | |
| 33 | void *alloc(uint64_t Size) { |
| 34 | Size = utils::roundUp(Size, uint64_t(allocator::ALIGNMENT)); |
| 35 | |
| 36 | if (config::isDebugMode(DeviceDebugKind::AllocationTracker)) { |
| 37 | atomic::add(&__omp_rtl_device_memory_pool_tracker.NumAllocations, 1, |
| 38 | atomic::seq_cst); |
| 39 | atomic::add(&__omp_rtl_device_memory_pool_tracker.AllocationTotal, Size, |
| 40 | atomic::seq_cst); |
| 41 | atomic::min(&__omp_rtl_device_memory_pool_tracker.AllocationMin, Size, |
| 42 | atomic::seq_cst); |
| 43 | atomic::max(&__omp_rtl_device_memory_pool_tracker.AllocationMax, Size, |
| 44 | atomic::seq_cst); |
| 45 | } |
| 46 | |
| 47 | uint64_t *Data = |
| 48 | reinterpret_cast<uint64_t *>(&__omp_rtl_device_memory_pool.Ptr); |
| 49 | uint64_t End = |
| 50 | reinterpret_cast<uint64_t>(Data) + __omp_rtl_device_memory_pool.Size; |
| 51 | |
| 52 | uint64_t OldData = atomic::add(Data, Size, atomic::seq_cst); |
| 53 | if (OldData + Size > End) |
| 54 | __builtin_trap(); |
| 55 | |
| 56 | return reinterpret_cast<void *>(OldData); |
| 57 | } |
| 58 | |
| 59 | void free(void *) {} |
| 60 | }; |
| 61 | |
| 62 | BumpAllocatorTy BumpAllocator; |
| 63 | |
| 64 | /// allocator namespace implementation |
| 65 | /// |
| 66 | ///{ |
| 67 | |
| 68 | void allocator::init(bool IsSPMD, KernelEnvironmentTy &KernelEnvironment) { |
| 69 | // TODO: Check KernelEnvironment for an allocator choice as soon as we have |
| 70 | // more than one. |
| 71 | } |
| 72 | |
| 73 | void *allocator::alloc(uint64_t Size) { return BumpAllocator.alloc(Size); } |
| 74 | |
| 75 | void allocator::free(void *Ptr) { BumpAllocator.free(Ptr); } |
| 76 | |
| 77 | ///} |
| 78 | |