Skip to content

Commit 069e8bc

Browse files
authored
[OpenMP][libc] Remove special handling for OpenMP printf (#98940)
Summary: Currently there are several layers to handle `printf`. Since we now have varargs and an implementation of `printf` this can be heavily simplified. 1. The frontend renames `printf` into `omp_vprintf` and gives it an argument buffer. Removing 1. triggered some code in the AMDGPU backend menat for HIP / OpenCL, so I hadded an exception to it. 2. Forward this to CUDA vprintf or ignore it. We no longer need special handling for it since we have varargs. So now we just forward this to CUDA vprintf if we have libc, otherwise just leave `printf` as an external function and expect that `libc` will be linked in.
1 parent 26d9282 commit 069e8bc

File tree

11 files changed

+16
-183
lines changed

11 files changed

+16
-183
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5986,8 +5986,6 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
59865986
getTarget().getTriple().isAMDGCN() ||
59875987
(getTarget().getTriple().isSPIRV() &&
59885988
getTarget().getTriple().getVendor() == Triple::VendorType::AMD)) {
5989-
if (getLangOpts().OpenMPIsTargetDevice)
5990-
return EmitOpenMPDevicePrintfCallExpr(E);
59915989
if (getTarget().getTriple().isNVPTX())
59925990
return EmitNVPTXDevicePrintfCallExpr(E);
59935991
if ((getTarget().getTriple().isAMDGCN() ||

clang/lib/CodeGen/CGGPUBuiltin.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,6 @@ llvm::Function *GetVprintfDeclaration(llvm::Module &M) {
4242
VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, "vprintf", &M);
4343
}
4444

45-
llvm::Function *GetOpenMPVprintfDeclaration(CodeGenModule &CGM) {
46-
const char *Name = "__llvm_omp_vprintf";
47-
llvm::Module &M = CGM.getModule();
48-
llvm::Type *ArgTypes[] = {llvm::PointerType::getUnqual(M.getContext()),
49-
llvm::PointerType::getUnqual(M.getContext()),
50-
llvm::Type::getInt32Ty(M.getContext())};
51-
llvm::FunctionType *VprintfFuncType = llvm::FunctionType::get(
52-
llvm::Type::getInt32Ty(M.getContext()), ArgTypes, false);
53-
54-
if (auto *F = M.getFunction(Name)) {
55-
if (F->getFunctionType() != VprintfFuncType) {
56-
CGM.Error(SourceLocation(),
57-
"Invalid type declaration for __llvm_omp_vprintf");
58-
return nullptr;
59-
}
60-
return F;
61-
}
62-
63-
return llvm::Function::Create(
64-
VprintfFuncType, llvm::GlobalVariable::ExternalLinkage, Name, &M);
65-
}
66-
6745
// Transforms a call to printf into a call to the NVPTX vprintf syscall (which
6846
// isn't particularly special; it's invoked just like a regular function).
6947
// vprintf takes two args: A format string, and a pointer to a buffer containing
@@ -213,10 +191,3 @@ RValue CodeGenFunction::EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E) {
213191
Builder.SetInsertPoint(IRB.GetInsertBlock(), IRB.GetInsertPoint());
214192
return RValue::get(Printf);
215193
}
216-
217-
RValue CodeGenFunction::EmitOpenMPDevicePrintfCallExpr(const CallExpr *E) {
218-
assert(getTarget().getTriple().isNVPTX() ||
219-
getTarget().getTriple().isAMDGCN());
220-
return EmitDevicePrintfCallExpr(E, this, GetOpenMPVprintfDeclaration(CGM),
221-
true);
222-
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4536,7 +4536,6 @@ class CodeGenFunction : public CodeGenTypeCache {
45364536

45374537
RValue EmitNVPTXDevicePrintfCallExpr(const CallExpr *E);
45384538
RValue EmitAMDGPUDevicePrintfCallExpr(const CallExpr *E);
4539-
RValue EmitOpenMPDevicePrintfCallExpr(const CallExpr *E);
45404539

45414540
RValue EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
45424541
const CallExpr *E, ReturnValueSlot ReturnValue);

libc/config/gpu/entrypoints.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ set(TARGET_LIBC_ENTRYPOINTS
226226

227227
# gpu/rpc.h entrypoints
228228
libc.src.gpu.rpc_host_call
229-
libc.src.gpu.rpc_fprintf
230229
)
231230

232231
set(TARGET_LIBM_ENTRYPOINTS

libc/spec/gpu_ext.td

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ def GPUExtensions : StandardSpec<"GPUExtensions"> {
1010
RetValSpec<VoidType>,
1111
[ArgSpec<VoidPtr>, ArgSpec<VoidPtr>, ArgSpec<SizeTType>]
1212
>,
13-
FunctionSpec<
14-
"rpc_fprintf",
15-
RetValSpec<IntType>,
16-
[ArgSpec<FILERestrictedPtr>,
17-
ArgSpec<ConstCharRestrictedPtr>,
18-
ArgSpec<VoidPtr>,
19-
ArgSpec<SizeTType>]
20-
>,
2113
]
2214
>;
2315
let Headers = [

libc/src/gpu/CMakeLists.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,3 @@ add_entrypoint_object(
88
libc.src.__support.RPC.rpc_client
99
libc.src.__support.GPU.utils
1010
)
11-
12-
add_entrypoint_object(
13-
rpc_fprintf
14-
SRCS
15-
rpc_fprintf.cpp
16-
HDRS
17-
rpc_fprintf.h
18-
DEPENDS
19-
libc.src.stdio.gpu.gpu_file
20-
libc.src.__support.RPC.rpc_client
21-
libc.src.__support.GPU.utils
22-
)

libc/src/gpu/rpc_fprintf.cpp

Lines changed: 0 additions & 75 deletions
This file was deleted.

libc/src/gpu/rpc_fprintf.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ bool AMDGPUPrintfRuntimeBindingImpl::run(Module &M) {
437437
return false;
438438

439439
auto PrintfFunction = M.getFunction("printf");
440-
if (!PrintfFunction || !PrintfFunction->isDeclaration())
440+
if (!PrintfFunction || !PrintfFunction->isDeclaration() ||
441+
M.getModuleFlag("openmp"))
441442
return false;
442443

443444
for (auto &U : PrintfFunction->uses()) {

offload/DeviceRTL/include/LibC.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extern "C" {
1818

1919
int memcmp(const void *lhs, const void *rhs, size_t count);
2020
void memset(void *dst, int C, size_t count);
21-
2221
int printf(const char *format, ...);
2322
}
2423

offload/DeviceRTL/src/LibC.cpp

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,33 @@
1111
#pragma omp begin declare target device_type(nohost)
1212

1313
namespace impl {
14-
int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t);
14+
int32_t omp_vprintf(const char *Format, __builtin_va_list vlist);
1515
}
1616

17+
#ifndef OMPTARGET_HAS_LIBC
18+
namespace impl {
1719
#pragma omp begin declare variant match( \
1820
device = {arch(nvptx, nvptx64)}, \
1921
implementation = {extension(match_any)})
20-
extern "C" int32_t vprintf(const char *, void *);
21-
namespace impl {
22-
int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
23-
return vprintf(Format, Arguments);
22+
extern "C" int vprintf(const char *format, ...);
23+
int omp_vprintf(const char *Format, __builtin_va_list vlist) {
24+
return vprintf(Format, vlist);
2425
}
25-
} // namespace impl
2626
#pragma omp end declare variant
2727

2828
#pragma omp begin declare variant match(device = {arch(amdgcn)})
29-
30-
#ifdef OMPTARGET_HAS_LIBC
31-
// TODO: Remove this handling once we have varargs support.
32-
extern "C" struct FILE *stdout;
33-
extern "C" int32_t rpc_fprintf(FILE *, const char *, void *, uint64_t);
34-
35-
namespace impl {
36-
int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
37-
return rpc_fprintf(stdout, Format, Arguments, Size);
38-
}
29+
int omp_vprintf(const char *Format, __builtin_va_list) { return -1; }
30+
#pragma omp end declare variant
3931
} // namespace impl
40-
#else
41-
// We do not have a vprintf implementation for AMD GPU so we use a stub.
42-
namespace impl {
43-
int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
44-
return -1;
32+
33+
extern "C" int printf(const char *Format, ...) {
34+
__builtin_va_list vlist;
35+
__builtin_va_start(vlist, Format);
36+
return impl::omp_vprintf(Format, vlist);
4537
}
46-
} // namespace impl
47-
#endif
48-
#pragma omp end declare variant
38+
#endif // OMPTARGET_HAS_LIBC
4939

5040
extern "C" {
51-
5241
[[gnu::weak]] int memcmp(const void *lhs, const void *rhs, size_t count) {
5342
auto *L = reinterpret_cast<const unsigned char *>(lhs);
5443
auto *R = reinterpret_cast<const unsigned char *>(rhs);
@@ -65,11 +54,6 @@ extern "C" {
6554
for (size_t I = 0; I < count; ++I)
6655
dstc[I] = C;
6756
}
68-
69-
/// printf() calls are rewritten by CGGPUBuiltin to __llvm_omp_vprintf
70-
int32_t __llvm_omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
71-
return impl::omp_vprintf(Format, Arguments, Size);
72-
}
7357
}
7458

7559
#pragma omp end declare target

0 commit comments

Comments
 (0)