From 6a8df7d870c901c4e9aa7c8e8fe2b06f5e156e46 Mon Sep 17 00:00:00 2001 From: Harald van Dijk Date: Thu, 24 Apr 2025 11:21:10 +0100 Subject: [PATCH 1/2] [SYCL] Hide inline definitions of stdio functions 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. --- clang/lib/Frontend/InitPreprocessor.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 288effba7a3d9..7259522e88c52 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -1547,6 +1547,13 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__ENABLE_USM_ADDR_SPACE__"); Builder.defineMacro("SYCL_DISABLE_FALLBACK_ASSERT"); } + + // Despite the name, DeviceTriple is not necessarily the device triple. + if (DeviceTriple.isWindowsMSVCEnvironment()) { + // MSVC inline definitions of stdio functions should not be used for SYCL + // device code. + Builder.defineMacro("_NO_CRT_STDIO_INLINE"); + } } else if (LangOpts.SYCLIsHost && LangOpts.SYCLESIMDBuildHostCode) { Builder.defineMacro("__ESIMD_BUILD_HOST_CODE"); } From 2aaad1d2f20caa29272d46258569eb34fb234147 Mon Sep 17 00:00:00 2001 From: Harald van Dijk Date: Mon, 28 Apr 2025 11:24:55 +0100 Subject: [PATCH 2/2] [NFC] Rename DeviceTriple, move comment. --- clang/lib/Frontend/InitPreprocessor.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index 7259522e88c52..7665c85a40d2a 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -1535,21 +1535,21 @@ static void InitializePredefinedMacros(const TargetInfo &TI, if (LangOpts.GPURelocatableDeviceCode) Builder.defineMacro("SYCL_EXTERNAL", "__attribute__((sycl_device))"); - const llvm::Triple &DeviceTriple = TI.getTriple(); - const llvm::Triple::SubArchType DeviceSubArch = DeviceTriple.getSubArch(); - if (DeviceTriple.isNVPTX() || DeviceTriple.isAMDGPU() || - (DeviceTriple.isSPIR() && - DeviceSubArch != llvm::Triple::SPIRSubArch_fpga) || + // This gets called twice, once with TI set to the host TargetInfo, once + // with TI set to the device TargetInfo. + const llvm::Triple &Triple = TI.getTriple(); + const llvm::Triple::SubArchType SubArch = Triple.getSubArch(); + if (Triple.isNVPTX() || Triple.isAMDGPU() || + (Triple.isSPIR() && SubArch != llvm::Triple::SPIRSubArch_fpga) || LangOpts.SYCLIsNativeCPU) Builder.defineMacro("SYCL_USE_NATIVE_FP_ATOMICS"); // Enable generation of USM address spaces for FPGA. - if (DeviceSubArch == llvm::Triple::SPIRSubArch_fpga) { + if (SubArch == llvm::Triple::SPIRSubArch_fpga) { Builder.defineMacro("__ENABLE_USM_ADDR_SPACE__"); Builder.defineMacro("SYCL_DISABLE_FALLBACK_ASSERT"); } - // Despite the name, DeviceTriple is not necessarily the device triple. - if (DeviceTriple.isWindowsMSVCEnvironment()) { + if (Triple.isWindowsMSVCEnvironment()) { // MSVC inline definitions of stdio functions should not be used for SYCL // device code. Builder.defineMacro("_NO_CRT_STDIO_INLINE");