Skip to content

Commit 1dee646

Browse files
authored
[SYCL] Hide inline definitions of stdio functions (#18174)
MSVC's inline definitions of stdio functions such as printf() are only meant for host code; device code cannot call them. Device code can, however, call ext::oneapi::experimental::printf() which depending on the target is implemented as a call to the global printf(). The availability of a definition of the global host printf() makes it more difficult to handle in device code, so hide it.
1 parent 0caa83c commit 1dee646

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

clang/lib/Frontend/InitPreprocessor.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -1535,18 +1535,25 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
15351535
if (LangOpts.GPURelocatableDeviceCode)
15361536
Builder.defineMacro("SYCL_EXTERNAL", "__attribute__((sycl_device))");
15371537

1538-
const llvm::Triple &DeviceTriple = TI.getTriple();
1539-
const llvm::Triple::SubArchType DeviceSubArch = DeviceTriple.getSubArch();
1540-
if (DeviceTriple.isNVPTX() || DeviceTriple.isAMDGPU() ||
1541-
(DeviceTriple.isSPIR() &&
1542-
DeviceSubArch != llvm::Triple::SPIRSubArch_fpga) ||
1538+
// This gets called twice, once with TI set to the host TargetInfo, once
1539+
// with TI set to the device TargetInfo.
1540+
const llvm::Triple &Triple = TI.getTriple();
1541+
const llvm::Triple::SubArchType SubArch = Triple.getSubArch();
1542+
if (Triple.isNVPTX() || Triple.isAMDGPU() ||
1543+
(Triple.isSPIR() && SubArch != llvm::Triple::SPIRSubArch_fpga) ||
15431544
LangOpts.SYCLIsNativeCPU)
15441545
Builder.defineMacro("SYCL_USE_NATIVE_FP_ATOMICS");
15451546
// Enable generation of USM address spaces for FPGA.
1546-
if (DeviceSubArch == llvm::Triple::SPIRSubArch_fpga) {
1547+
if (SubArch == llvm::Triple::SPIRSubArch_fpga) {
15471548
Builder.defineMacro("__ENABLE_USM_ADDR_SPACE__");
15481549
Builder.defineMacro("SYCL_DISABLE_FALLBACK_ASSERT");
15491550
}
1551+
1552+
if (Triple.isWindowsMSVCEnvironment()) {
1553+
// MSVC inline definitions of stdio functions should not be used for SYCL
1554+
// device code.
1555+
Builder.defineMacro("_NO_CRT_STDIO_INLINE");
1556+
}
15501557
} else if (LangOpts.SYCLIsHost && LangOpts.SYCLESIMDBuildHostCode) {
15511558
Builder.defineMacro("__ESIMD_BUILD_HOST_CODE");
15521559
}

0 commit comments

Comments
 (0)