Skip to content

[OpenMP] Adjust 'printf' handling in the OpenMP runtime #123670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions offload/DeviceRTL/include/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,10 @@ void __assert_fail_internal(const char *expr, const char *msg, const char *file,
__assert_assume(expr); \
}
#define UNREACHABLE(msg) \
PRINT(msg); \
printf(msg); \
__builtin_trap(); \
__builtin_unreachable();

///}

#define PRINTF(fmt, ...) (void)printf(fmt, ##__VA_ARGS__);
#define PRINT(str) PRINTF("%s", str)

///}

#endif
9 changes: 4 additions & 5 deletions offload/DeviceRTL/include/LibC.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

#include "DeviceTypes.h"

extern "C" {
namespace ompx {

int memcmp(const void *lhs, const void *rhs, size_t count);
void memset(void *dst, int C, size_t count);
int printf(const char *format, ...);
}
int printf(const char *Format, ...);

} // namespace ompx

#endif
4 changes: 2 additions & 2 deletions offload/DeviceRTL/src/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ void __assert_assume(bool condition) { __builtin_assume(condition); }
void __assert_fail_internal(const char *expr, const char *msg, const char *file,
unsigned line, const char *function) {
if (msg) {
PRINTF("%s:%u: %s: Assertion %s (`%s`) failed.\n", file, line, function,
printf("%s:%u: %s: Assertion %s (`%s`) failed.\n", file, line, function,
msg, expr);
} else {
PRINTF("%s:%u: %s: Assertion `%s` failed.\n", file, line, function, expr);
printf("%s:%u: %s: Assertion `%s` failed.\n", file, line, function, expr);
}
__builtin_trap();
}
Expand Down
45 changes: 19 additions & 26 deletions offload/DeviceRTL/src/LibC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,11 @@

#pragma omp begin declare target device_type(nohost)

namespace impl {
int32_t omp_vprintf(const char *Format, __builtin_va_list vlist);
}

#ifndef OMPTARGET_HAS_LIBC
namespace impl {
#pragma omp begin declare variant match( \
device = {arch(nvptx, nvptx64)}, \
implementation = {extension(match_any)})
extern "C" int vprintf(const char *format, ...);
int omp_vprintf(const char *Format, __builtin_va_list vlist) {
return vprintf(Format, vlist);
}
#pragma omp end declare variant

#pragma omp begin declare variant match(device = {arch(amdgcn)})
int omp_vprintf(const char *Format, __builtin_va_list) { return -1; }
#pragma omp end declare variant
} // namespace impl

extern "C" int printf(const char *Format, ...) {
__builtin_va_list vlist;
__builtin_va_start(vlist, Format);
return impl::omp_vprintf(Format, vlist);
}
#endif // OMPTARGET_HAS_LIBC
#if defined(__AMDGPU__) && !defined(OMPTARGET_HAS_LIBC)
extern "C" int vprintf(const char *format, __builtin_va_list) { return -1; }
#else
extern "C" int vprintf(const char *format, __builtin_va_list);
#endif

extern "C" {
[[gnu::weak]] int memcmp(const void *lhs, const void *rhs, size_t count) {
Expand All @@ -54,6 +33,20 @@ extern "C" {
for (size_t I = 0; I < count; ++I)
dstc[I] = C;
}

[[gnu::weak]] int printf(const char *Format, ...) {
__builtin_va_list vlist;
__builtin_va_start(vlist, Format);
return ::vprintf(Format, vlist);
}
}

namespace ompx {
[[clang::no_builtin("printf")]] int printf(const char *Format, ...) {
__builtin_va_list vlist;
__builtin_va_start(vlist, Format);
return ::vprintf(Format, vlist);
}
} // namespace ompx

#pragma omp end declare target
3 changes: 2 additions & 1 deletion offload/DeviceRTL/src/Parallelism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "DeviceTypes.h"
#include "DeviceUtils.h"
#include "Interface.h"
#include "LibC.h"
#include "Mapping.h"
#include "State.h"
#include "Synchronization.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ uint32_t determineNumberOfThreads(int32_t NumThreadsClause) {
switch (nargs) {
#include "generated_microtask_cases.gen"
default:
PRINT("Too many arguments in kmp_invoke_microtask, aborting execution.\n");
printf("Too many arguments in kmp_invoke_microtask, aborting execution.\n");
__builtin_trap();
}
}
Expand Down
8 changes: 4 additions & 4 deletions offload/DeviceRTL/src/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ void *SharedMemorySmartStackTy::push(uint64_t Bytes) {
}

if (config::isDebugMode(DeviceDebugKind::CommonIssues))
PRINT("Shared memory stack full, fallback to dynamic allocation of global "
"memory will negatively impact performance.\n");
printf("Shared memory stack full, fallback to dynamic allocation of global "
"memory will negatively impact performance.\n");
void *GlobalMemory = memory::allocGlobal(
AlignedBytes, "Slow path shared memory allocation, insufficient "
"shared memory stack memory!");
Expand Down Expand Up @@ -173,7 +173,7 @@ void memory::freeShared(void *Ptr, uint64_t Bytes, const char *Reason) {
void *memory::allocGlobal(uint64_t Bytes, const char *Reason) {
void *Ptr = malloc(Bytes);
if (config::isDebugMode(DeviceDebugKind::CommonIssues) && Ptr == nullptr)
PRINT("nullptr returned by malloc!\n");
printf("nullptr returned by malloc!\n");
return Ptr;
}

Expand Down Expand Up @@ -277,7 +277,7 @@ void state::enterDataEnvironment(IdentTy *Ident) {
sizeof(ThreadStates[0]) * mapping::getNumberOfThreadsInBlock();
void *ThreadStatesPtr =
memory::allocGlobal(Bytes, "Thread state array allocation");
memset(ThreadStatesPtr, 0, Bytes);
__builtin_memset(ThreadStatesPtr, 0, Bytes);
if (!atomic::cas(ThreadStatesBitsPtr, uintptr_t(0),
reinterpret_cast<uintptr_t>(ThreadStatesPtr),
atomic::seq_cst, atomic::seq_cst))
Expand Down
Loading