Debian Codegraph @

libgomp/config/accel/target-indirect.c

raw
1 /* Copyright (C) 2023-2025 Free Software Foundation, Inc.
2
3 Contributed by Siemens.
4
5 This file is part of the GNU Offloading and Multi Processing Library
6 (libgomp).
7
8 Libgomp is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 #include <assert.h>
28 #include <string.h>
29 #include "libgomp.h"
30
31 struct indirect_map_t
32 {
33 void *host_addr;
34 void *target_addr;
35 };
36
37 typedef struct indirect_map_t *hash_entry_type;
38
39 static inline void * htab_alloc (size_t size) { return gomp_malloc (size); }
40 static inline void htab_free (void *ptr) { free (ptr); }
41
42 #include "hashtab.h"
43
44 static inline hashval_t
45 htab_hash (hash_entry_type element)
46 {
47 return hash_pointer (element->host_addr);
48 }
49
50 static inline bool
51 htab_eq (hash_entry_type x, hash_entry_type y)
52 {
53 return x->host_addr == y->host_addr;
54 }
55
56 void **GOMP_INDIRECT_ADDR_MAP = NULL;
57
58 /* Use a hashtab to lookup the target address instead of using a linear
59 search. */
60 #define USE_HASHTAB_LOOKUP
61
62 #ifdef USE_HASHTAB_LOOKUP
63
64 static htab_t indirect_htab = NULL;
65
66 /* Build the hashtab used for host->target address lookups. */
67
68 void
69 build_indirect_map (void)
70 {
71 size_t num_ind_funcs = 0;
72 void **map_entry;
73
74 if (!GOMP_INDIRECT_ADDR_MAP)
75 return;
76
77 if (!indirect_htab)
78 {
79 /* Count the number of entries in the NULL-terminated address map. */
80 for (map_entry = GOMP_INDIRECT_ADDR_MAP; *map_entry;
81 map_entry += 2, num_ind_funcs++);
82
83 /* Build hashtab for address lookup. */
84 indirect_htab = htab_create (num_ind_funcs);
85 map_entry = GOMP_INDIRECT_ADDR_MAP;
86
87 for (int i = 0; i < num_ind_funcs; i++, map_entry += 2)
88 {
89 struct indirect_map_t element = { *map_entry, NULL };
90 hash_entry_type *slot = htab_find_slot (&indirect_htab, &element,
91 INSERT);
92 *slot = (hash_entry_type) map_entry;
93 }
94 }
95 }
96
97 void *
98 GOMP_target_map_indirect_ptr (void *ptr)
99 {
100 /* NULL pointers always resolve to NULL. */
101 if (!ptr)
102 return ptr;
103
104 assert (indirect_htab);
105
106 struct indirect_map_t element = { ptr, NULL };
107 hash_entry_type entry = htab_find (indirect_htab, &element);
108 return entry ? entry->target_addr : ptr;
109 }
110
111 #else
112
113 void
114 build_indirect_map (void)
115 {
116 }
117
118 void *
119 GOMP_target_map_indirect_ptr (void *ptr)
120 {
121 /* NULL pointers always resolve to NULL. */
122 if (!ptr)
123 return ptr;
124
125 assert (GOMP_INDIRECT_ADDR_MAP);
126
127 for (void **map_entry = GOMP_INDIRECT_ADDR_MAP; *map_entry;
128 map_entry += 2)
129 if (*map_entry == ptr)
130 return (void *) *(map_entry + 1);
131
132 return ptr;
133 }
134
135 #endif