Skip to content

Commit 2cf8118

Browse files
authored
[Libomptarget] Add RPC-based printf implementation for OpenMP (llvm#85638)
Summary: This patch adds an implementation of `printf` that's provided by the GPU C library runtime. This `pritnf` currently implemented using the same wrapper handling that OpenMP sets up. This will be removed once we have proper varargs support. This `printf` differs from the one CUDA offers in that it is synchronous and uses a finite size. Additionally we support pretty much every format specifier except the `%n` option. Depends on llvm#85331
1 parent 5029949 commit 2cf8118

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

openmp/libomptarget/DeviceRTL/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ set(clang_opt_flags -O3 -mllvm -openmp-opt-disable -DSHARED_SCRATCHPAD_SIZE=512
122122
set(link_opt_flags -O3 -openmp-opt-disable -attributor-enable=module -vectorize-slp=false )
123123
set(link_export_flag -passes=internalize -internalize-public-api-file=${source_directory}/exports)
124124

125+
# If the user built with the GPU C library enabled we will use that instead.
126+
if(${LIBOMPTARGET_GPU_LIBC_SUPPORT})
127+
list(APPEND clang_opt_flags -DOMPTARGET_HAS_LIBC)
128+
endif()
129+
125130
# Prepend -I to each list element
126131
set (LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL "${LIBOMPTARGET_LLVM_INCLUDE_DIRS}")
127132
list(TRANSFORM LIBOMPTARGET_LLVM_INCLUDE_DIRS_DEVICERTL PREPEND "-I")

openmp/libomptarget/DeviceRTL/src/LibC.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,23 @@ void memset(void *dst, int C, size_t count) {
5353
dstc[I] = C;
5454
}
5555

56+
// If the user built with the GPU C library enabled we will assume that we can
57+
// call it.
58+
#ifdef OMPTARGET_HAS_LIBC
59+
60+
// TODO: Remove this handling once we have varargs support.
61+
extern struct FILE *stdout;
62+
int32_t rpc_fprintf(FILE *, const char *, void *, uint64_t);
63+
64+
int32_t __llvm_omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
65+
return rpc_fprintf(stdout, Format, Arguments, Size);
66+
}
67+
#else
5668
/// printf() calls are rewritten by CGGPUBuiltin to __llvm_omp_vprintf
5769
int32_t __llvm_omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
5870
return impl::omp_vprintf(Format, Arguments, Size);
5971
}
72+
#endif
6073
}
6174

6275
#pragma omp end declare target
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %libomptarget-compile-run-and-check-generic
2+
3+
// REQUIRES: libc
4+
5+
#include <stdio.h>
6+
7+
int main() {
8+
// CHECK: PASS
9+
#pragma omp target
10+
{ printf("PASS\n"); }
11+
12+
// CHECK: PASS
13+
#pragma omp target
14+
{ printf("%s\n", "PASS"); }
15+
16+
// CHECK: PASS
17+
// CHECK: PASS
18+
// CHECK: PASS
19+
// CHECK: PASS
20+
// CHECK: PASS
21+
// CHECK: PASS
22+
// CHECK: PASS
23+
// CHECK: PASS
24+
#pragma omp target teams num_teams(4)
25+
#pragma omp parallel num_threads(2)
26+
{ printf("PASS\n"); }
27+
28+
// CHECK: PASS
29+
char str[] = {'P', 'A', 'S', 'S', '\0'};
30+
#pragma omp target map(to : str)
31+
{ printf("%s\n", str); }
32+
33+
// CHECK: 11111111111
34+
#pragma omp target
35+
{ printf("%s%-.0f%4b%c%ld\n", "1111", 1.0, 0xf, '1', 1lu); }
36+
}

0 commit comments

Comments
 (0)