Skip to content

Commit d4dffe4

Browse files
committed
[SYCL] Add support for NVPTX device printf
Use `::printf` when not compiling for `__SPIR__`, this allows the use of `EmitNVPTXDevicePrintfCallExpr` which packs the var args and dispatches to CUDA's `vprintf`. Fixes #1154
1 parent 98ebbb7 commit d4dffe4

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

clang/lib/CodeGen/CGGPUBuiltin.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,19 @@ CodeGenFunction::EmitNVPTXDevicePrintfCallExpr(const CallExpr *E,
118118

119119
// Invoke vprintf and return.
120120
llvm::Function* VprintfFunc = GetVprintfDeclaration(CGM.getModule());
121-
return RValue::get(Builder.CreateCall(
122-
VprintfFunc, {Args[0].getRValue(*this).getScalarVal(), BufferPtr}));
121+
auto FormatSpecifier = Args[0].getRValue(*this).getScalarVal();
122+
// Check if the format specifier is in the constant address space, vprintf is
123+
// oblivious to address spaces, so it would have to be casted away.
124+
if (Args[0]
125+
.getRValue(*this)
126+
.getScalarVal()
127+
->getType()
128+
->getPointerAddressSpace() == 4)
129+
FormatSpecifier = Builder.CreateAddrSpaceCast(
130+
FormatSpecifier, llvm::Type::getInt8PtrTy(Ctx));
131+
132+
return RValue::get(
133+
Builder.CreateCall(VprintfFunc, {FormatSpecifier, BufferPtr}));
123134
}
124135

125136
RValue

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ static bool IsSyclMathFunc(unsigned BuiltinID) {
420420
bool Sema::isKnownGoodSYCLDecl(const Decl *D) {
421421
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
422422
const IdentifierInfo *II = FD->getIdentifier();
423+
if (FD->getBuiltinID() == Builtin::BIprintf)
424+
return true;
423425
const DeclContext *DC = FD->getDeclContext();
424426
if (II && II->isStr("__spirv_ocl_printf") &&
425427
!FD->isDefined() &&

sycl/include/sycl/ext/oneapi/experimental/builtins.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ namespace experimental {
6161
//
6262
template <typename... Args>
6363
int printf(const __SYCL_CONSTANT_AS char *__format, Args... args) {
64-
#ifdef __SYCL_DEVICE_ONLY__
64+
#if defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
6565
return __spirv_ocl_printf(__format, args...);
6666
#else
6767
return ::printf(__format, args...);
68-
#endif
68+
#endif // defined(__SYCL_DEVICE_ONLY__) && defined(__SPIR__)
6969
}
7070

7171
} // namespace experimental

0 commit comments

Comments
 (0)