Skip to content

Commit 74d23f1

Browse files
authored
[OpenMP] Implement 'omp_alloc' on the device (#102526)
Summary: The 'omp_alloc' function should be callable from a target region. This patch implemets it by simply calling `malloc` for every non-default trait value allocator. All the special access modifiers are unimplemented and return null. The null allocator returns null as the spec states it should not be usable from the target.
1 parent 86db215 commit 74d23f1

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

offload/DeviceRTL/include/Allocator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ void free(void *Ptr);
3939

4040
} // namespace ompx
4141

42+
extern "C" {
43+
[[gnu::weak]] void *malloc(size_t Size);
44+
[[gnu::weak]] void free(void *Ptr);
45+
}
46+
4247
#pragma omp end declare target
4348

4449
#endif

offload/DeviceRTL/include/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ typedef enum omp_allocator_handle_t {
188188
omp_cgroup_mem_alloc = 6,
189189
omp_pteam_mem_alloc = 7,
190190
omp_thread_mem_alloc = 8,
191-
KMP_ALLOCATOR_MAX_HANDLE = ~(0U)
191+
KMP_ALLOCATOR_MAX_HANDLE = ~(0LU)
192192
} omp_allocator_handle_t;
193193

194194
#define __PRAGMA(STR) _Pragma(#STR)

offload/DeviceRTL/src/Misc.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
#include "Allocator.h"
1213
#include "Configuration.h"
1314
#include "Types.h"
1415

@@ -128,6 +129,33 @@ double omp_get_wtime(void) { return ompx::impl::getWTime(); }
128129
void *__llvm_omp_indirect_call_lookup(void *HstPtr) {
129130
return ompx::impl::indirectCallLookup(HstPtr);
130131
}
132+
133+
void *omp_alloc(size_t size, omp_allocator_handle_t allocator) {
134+
switch (allocator) {
135+
case omp_default_mem_alloc:
136+
case omp_large_cap_mem_alloc:
137+
case omp_const_mem_alloc:
138+
case omp_high_bw_mem_alloc:
139+
case omp_low_lat_mem_alloc:
140+
return malloc(size);
141+
default:
142+
return nullptr;
143+
}
144+
}
145+
146+
void omp_free(void *ptr, omp_allocator_handle_t allocator) {
147+
switch (allocator) {
148+
case omp_default_mem_alloc:
149+
case omp_large_cap_mem_alloc:
150+
case omp_const_mem_alloc:
151+
case omp_high_bw_mem_alloc:
152+
case omp_low_lat_mem_alloc:
153+
free(ptr);
154+
case omp_null_allocator:
155+
default:
156+
return;
157+
}
158+
}
131159
}
132160

133161
///}

offload/DeviceRTL/src/State.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ namespace {
5353
extern "C" {
5454
#ifdef __AMDGPU__
5555

56-
[[gnu::weak]] void *malloc(uint64_t Size) { return allocator::alloc(Size); }
56+
[[gnu::weak]] void *malloc(size_t Size) { return allocator::alloc(Size); }
5757
[[gnu::weak]] void free(void *Ptr) { allocator::free(Ptr); }
5858

5959
#else
6060

61-
[[gnu::weak, gnu::leaf]] void *malloc(uint64_t Size);
61+
[[gnu::weak, gnu::leaf]] void *malloc(size_t Size);
6262
[[gnu::weak, gnu::leaf]] void free(void *Ptr);
6363

6464
#endif

offload/test/api/omp_device_alloc.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %libomptarget-compile-run-and-check-generic
2+
3+
#include <assert.h>
4+
#include <omp.h>
5+
#include <stdio.h>
6+
7+
int main() {
8+
#pragma omp target teams num_teams(4)
9+
#pragma omp parallel
10+
{
11+
int *ptr = (int *)omp_alloc(sizeof(int), omp_default_mem_alloc);
12+
assert(ptr && "Ptr is (null)!");
13+
*ptr = 1;
14+
assert(*ptr == 1 && "Ptr is not 1");
15+
omp_free(ptr, omp_default_mem_alloc);
16+
}
17+
18+
#pragma omp target
19+
{
20+
assert(!omp_alloc(sizeof(int), omp_null_allocator) && "Ptr is not (null)!");
21+
}
22+
23+
// CHECK: PASS
24+
printf("PASS\n");
25+
}

openmp/docs/design/Runtimes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,14 @@ clause. Examples for both are given below.
14961496
$ clang++ -fopenmp --offload-arch=gfx90a -O3 shared.c
14971497
$ env ./shared
14981498
1499+
.. _libomptarget_device_allocator:
1500+
1501+
Device Allocation
1502+
^^^^^^^^^^^^^^^^^
1503+
1504+
The device runtime supports basic runtime allocation via the ``omp_alloc``
1505+
function. Currently, this allocates global memory for all default traits. Access
1506+
modifiers are currently not supported and return a null pointer.
14991507

15001508
.. _libomptarget_device_debugging:
15011509

0 commit comments

Comments
 (0)