Skip to content

Commit 9fd767d

Browse files
authored
[SYCL][Driver] Fix regression that enabled Cuda-mode in cc1 and defined __CUDA_ARCH__ (#15441)
The `CudaToolChain` set `-fcuda-is-device` unconditionally which made `InitializePredefinedMacros` (called from `clang::InitializePreprocessor`) to define `__CUDA_ARCH__` (default-init to 1). As such, the driver assumed Cuda mode while in also SYCL mode, but we don't properly support Cuda device-code compatibility and we want to avoid having the `__CUDA_ARCH__` macro defined altogether for SYCL offload.
1 parent ea03f46 commit 9fd767d

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

clang/lib/Driver/ToolChains/Cuda.cpp

+14-13
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,17 @@ void CudaToolChain::addClangTargetOptions(
938938
DeviceOffloadingKind == Action::OFK_Cuda) &&
939939
"Only OpenMP, SYCL or CUDA offloading kinds are supported for NVIDIA GPUs.");
940940

941-
CC1Args.append(
942-
{"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"});
941+
// If we are compiling SYCL kernels for Nvidia GPUs, we do not support Cuda
942+
// device code compatability, hence we do not set Cuda mode in that instance.
943+
if (DeviceOffloadingKind == Action::OFK_SYCL) {
944+
toolchains::SYCLToolChain::AddSYCLIncludeArgs(getDriver(), DriverArgs,
945+
CC1Args);
946+
947+
if (DriverArgs.hasArg(options::OPT_fsycl_fp32_prec_sqrt))
948+
CC1Args.push_back("-fcuda-prec-sqrt");
949+
} else {
950+
CC1Args.append(
951+
{"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"});
943952

944953
// Unsized function arguments used for variadics were introduced in CUDA-9.0
945954
// We still do not support generating code that actually uses variadic
@@ -948,18 +957,10 @@ void CudaToolChain::addClangTargetOptions(
948957
if (CudaInstallation.version() >= CudaVersion::CUDA_90)
949958
CC1Args.push_back("-fcuda-allow-variadic-functions");
950959

951-
if (DriverArgs.hasArg(options::OPT_fsycl)) {
952-
// Add these flags for .cu SYCL compilation.
960+
// Add these flags for .cu SYCL compilation.
961+
if (DeviceOffloadingKind == Action::OFK_Cuda &&
962+
DriverArgs.hasArg(options::OPT_fsycl))
953963
CC1Args.append({"-std=c++17", "-fsycl-is-host"});
954-
}
955-
956-
if (DeviceOffloadingKind == Action::OFK_SYCL) {
957-
toolchains::SYCLToolChain::AddSYCLIncludeArgs(getDriver(), DriverArgs,
958-
CC1Args);
959-
960-
if (DriverArgs.hasArg(options::OPT_fsycl_fp32_prec_sqrt)) {
961-
CC1Args.push_back("-fcuda-prec-sqrt");
962-
}
963964
}
964965

965966
auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv) ||

clang/test/Preprocessor/sycl-macro.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
// RUNx: %clang_cc1 %s -fsycl-id-queries-fit-in-int -fsycl-is-device -E -dM -fms-compatibility | FileCheck --check-prefix=CHECK-MSVC %s
77
// RUN: %clang_cc1 -fno-sycl-id-queries-fit-in-int %s -E -dM | FileCheck \
88
// RUN: --check-prefix=CHECK-NO-SYCL_FIT_IN_INT %s
9-
// RUN: %clang_cc1 %s -triple nvptx64-nvidia-cuda -target-cpu sm_80 -fsycl-is-device -E -dM | FileCheck --check-prefix=CHECK-CUDA %s
9+
// RUN: %clang_cc1 %s -triple nvptx64-nvidia-cuda -target-cpu sm_80 -fsycl-is-device -E -dM | FileCheck \
10+
// RUN: --check-prefix=CHECK-CUDA %s -DARCH_CODE=800
11+
// RUN: %clangxx %s -fsycl -nocudalib -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --offload-arch=sm_80 -E -dM | FileCheck \
12+
// RUN: --check-prefix=CHECK-CUDA-SYCL-DRIVER %s
1013
// RUN: %clang_cc1 %s -triple amdgcn-amd-amdhsa -target-cpu gfx906 -fsycl-is-device -E -dM | FileCheck --check-prefix=CHECK-HIP %s
1114

1215
// RUN: %clang_cc1 %s -triple nvptx64-nvidia-cuda -target-cpu sm_90a -fsycl-is-device -E -dM | FileCheck --check-prefix=CHECK-CUDA-FEATURE %s
@@ -32,8 +35,10 @@
3235
// CHECK-NO-SYCL_FIT_IN_INT-NOT:#define __SYCL_ID_QUERIES_FIT_IN_INT__ 1
3336
// CHECK-SYCL-ID:#define __SYCL_ID_QUERIES_FIT_IN_INT__ 1
3437

35-
// CHECK-CUDA:#define __SYCL_CUDA_ARCH__ 800
36-
// CHECK-CUDA-NOT:#define __CUDA_ARCH__ 800
38+
// CHECK-CUDA:#define __SYCL_CUDA_ARCH__ [[ARCH_CODE]]
39+
// CHECK-CUDA-NOT:#define __CUDA_ARCH__ {{[0-9]+}}
40+
41+
// CHECK-CUDA-SYCL-DRIVER-NOT: #define __CUDA_ARCH__ {{[0-9]+}}
3742

3843
// CHECK-HIP:#define __CUDA_ARCH__ 0
3944

0 commit comments

Comments
 (0)