Debian Codegraph @

libgomp/config/gcn/allocator.c

raw
1 /* Copyright (C) 2023-2025 Free Software Foundation, Inc.
2
3 This file is part of the GNU Offloading and Multi Processing Library
4 (libgomp).
5
6 Libgomp is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 /* The low-latency allocators use space reserved in LDS memory when the
26 kernel is launched. The heap is initialized in gomp_gcn_enter_kernel and
27 all allocations are forgotten when the kernel exits. Allocations to other
28 memory spaces all use the system malloc syscall.
29
30 The pointers returned are 64-bit "Flat" addresses indistinguishable from
31 regular pointers, but only compatible with the "flat_load/store"
32 instructions. The compiler has been coded to assign default address
33 spaces accordingly.
34
35 LDS memory is not visible to other teams, and therefore may only be used
36 when the memspace access trait is set accordingly. */
37
38 #include "libgomp.h"
39 #include <stdlib.h>
40
41 #define BASIC_ALLOC_PREFIX __gcn_lowlat
42 #define BASIC_ALLOC_YIELD asm ("s_sleep 1" ::: "memory")
43 #include "../../basic-allocator.c"
44
45 /* The low-latency heap is located in LDS memory, but we need the __flat
46 address space for compatibility reasons. */
47 #define FLAT_HEAP_PTR \
48 ((void *) (uintptr_t) (void __flat *) (void __lds *) GCN_LOWLAT_HEAP)
49
50 static void *
51 gcn_memspace_alloc (omp_memspace_handle_t memspace, size_t size)
52 {
53 if (memspace == omp_low_lat_mem_space)
54 {
55 char *shared_pool = FLAT_HEAP_PTR;
56
57 return __gcn_lowlat_alloc (shared_pool, size);
58 }
59 else
60 return malloc (size);
61 }
62
63 static void *
64 gcn_memspace_calloc (omp_memspace_handle_t memspace, size_t size)
65 {
66 if (memspace == omp_low_lat_mem_space)
67 {
68 char *shared_pool = FLAT_HEAP_PTR;
69
70 return __gcn_lowlat_calloc (shared_pool, size);
71 }
72 else
73 return calloc (1, size);
74 }
75
76 static void
77 gcn_memspace_free (omp_memspace_handle_t memspace, void *addr, size_t size)
78 {
79 if (memspace == omp_low_lat_mem_space)
80 {
81 char *shared_pool = FLAT_HEAP_PTR;
82
83 __gcn_lowlat_free (shared_pool, addr, size);
84 }
85 else
86 free (addr);
87 }
88
89 static void *
90 gcn_memspace_realloc (omp_memspace_handle_t memspace, void *addr,
91 size_t oldsize, size_t size)
92 {
93 if (memspace == omp_low_lat_mem_space)
94 {
95 char *shared_pool = FLAT_HEAP_PTR;
96
97 return __gcn_lowlat_realloc (shared_pool, addr, oldsize, size);
98 }
99 else
100 return realloc (addr, size);
101 }
102
103 static inline int
104 gcn_memspace_validate (omp_memspace_handle_t memspace, unsigned access)
105 {
106 /* Disallow use of low-latency memory when it must be accessible by
107 all threads. */
108 return (memspace != omp_low_lat_mem_space
109 || access != omp_atv_all);
110 }
111
112 #define MEMSPACE_ALLOC(MEMSPACE, SIZE, PIN) \
113 gcn_memspace_alloc (MEMSPACE, ((void)(PIN), (SIZE)))
114 #define MEMSPACE_CALLOC(MEMSPACE, SIZE, PIN) \
115 gcn_memspace_calloc (MEMSPACE, ((void)(PIN), (SIZE)))
116 #define MEMSPACE_REALLOC(MEMSPACE, ADDR, OLDSIZE, SIZE, OLDPIN, PIN) \
117 gcn_memspace_realloc (MEMSPACE, ADDR, OLDSIZE, \
118 ((void)(PIN), (void)(OLDPIN), (SIZE)))
119 #define MEMSPACE_FREE(MEMSPACE, ADDR, SIZE, PIN) \
120 gcn_memspace_free (MEMSPACE, ADDR, ((void)(PIN), (SIZE)))
121 #define MEMSPACE_VALIDATE(MEMSPACE, ACCESS, PIN) \
122 gcn_memspace_validate (MEMSPACE, ((void)(PIN), (ACCESS)))
123
124 /* The default low-latency memspace implies omp_atv_all, which is incompatible
125 with the LDS memory space. */
126 #define OMP_LOW_LAT_MEM_ALLOC_INVALID 1
127
128 #include "../../allocator.c"