[OpenMP][DeviceRTL] Add the support for printf in a freestanding way
authorShilei Tian <tianshilei1992@gmail.com>
Fri, 8 Oct 2021 02:15:23 +0000 (22:15 -0400)
committerShilei Tian <tianshilei1992@gmail.com>
Fri, 8 Oct 2021 02:15:37 +0000 (22:15 -0400)
For NVPTX, `printf` can be used just with a function declaration. For AMDGCN, an
function definition is added, but it simply returns.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D109728

openmp/libomptarget/DeviceRTL/include/Debug.h

index 48ee3e9..e8e9078 100644 (file)
@@ -25,9 +25,24 @@ void __assert_fail(const char *assertion, const char *file, unsigned line,
 
 ///}
 
-// TODO: We need to allow actual printf.
-#define PRINTF(fmt, ...) (void)fmt;
+/// Print
+/// TODO: For now we have to use macros to guard the code because Clang lowers
+/// `printf` to different function calls on NVPTX and AMDGCN platforms, and it
+/// doesn't work for AMDGCN. After it can work on AMDGCN, we will remove the
+/// macro.
+/// {
+
+#ifndef __AMDGCN__
+extern "C" {
+int printf(const char *format, ...);
+}
+
+#define PRINTF(fmt, ...) (void)printf(fmt, __VA_ARGS__);
 #define PRINT(str) PRINTF("%s", str)
+#else
+#define PRINTF(fmt, ...)
+#define PRINT(str)
+#endif
 
 ///}