Skip to content

Commit

Permalink
[SYCL][Driver] Fix regression that enabled Cuda-mode in cc1 and defin…
Browse files Browse the repository at this point in the history
…ed __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.
  • Loading branch information
GeorgeWeb committed Sep 23, 2024
1 parent ea03f46 commit 9fd767d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
27 changes: 14 additions & 13 deletions clang/lib/Driver/ToolChains/Cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,17 @@ void CudaToolChain::addClangTargetOptions(
DeviceOffloadingKind == Action::OFK_Cuda) &&
"Only OpenMP, SYCL or CUDA offloading kinds are supported for NVIDIA GPUs.");

CC1Args.append(
{"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"});
// If we are compiling SYCL kernels for Nvidia GPUs, we do not support Cuda
// device code compatability, hence we do not set Cuda mode in that instance.
if (DeviceOffloadingKind == Action::OFK_SYCL) {
toolchains::SYCLToolChain::AddSYCLIncludeArgs(getDriver(), DriverArgs,
CC1Args);

if (DriverArgs.hasArg(options::OPT_fsycl_fp32_prec_sqrt))
CC1Args.push_back("-fcuda-prec-sqrt");
} else {
CC1Args.append(
{"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"});

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

if (DriverArgs.hasArg(options::OPT_fsycl)) {
// Add these flags for .cu SYCL compilation.
// Add these flags for .cu SYCL compilation.
if (DeviceOffloadingKind == Action::OFK_Cuda &&
DriverArgs.hasArg(options::OPT_fsycl))
CC1Args.append({"-std=c++17", "-fsycl-is-host"});
}

if (DeviceOffloadingKind == Action::OFK_SYCL) {
toolchains::SYCLToolChain::AddSYCLIncludeArgs(getDriver(), DriverArgs,
CC1Args);

if (DriverArgs.hasArg(options::OPT_fsycl_fp32_prec_sqrt)) {
CC1Args.push_back("-fcuda-prec-sqrt");
}
}

auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv) ||
Expand Down
11 changes: 8 additions & 3 deletions clang/test/Preprocessor/sycl-macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// RUNx: %clang_cc1 %s -fsycl-id-queries-fit-in-int -fsycl-is-device -E -dM -fms-compatibility | FileCheck --check-prefix=CHECK-MSVC %s
// RUN: %clang_cc1 -fno-sycl-id-queries-fit-in-int %s -E -dM | FileCheck \
// RUN: --check-prefix=CHECK-NO-SYCL_FIT_IN_INT %s
// RUN: %clang_cc1 %s -triple nvptx64-nvidia-cuda -target-cpu sm_80 -fsycl-is-device -E -dM | FileCheck --check-prefix=CHECK-CUDA %s
// RUN: %clang_cc1 %s -triple nvptx64-nvidia-cuda -target-cpu sm_80 -fsycl-is-device -E -dM | FileCheck \
// RUN: --check-prefix=CHECK-CUDA %s -DARCH_CODE=800
// RUN: %clangxx %s -fsycl -nocudalib -fsycl-targets=nvptx64-nvidia-cuda -Xsycl-target-backend --offload-arch=sm_80 -E -dM | FileCheck \
// RUN: --check-prefix=CHECK-CUDA-SYCL-DRIVER %s
// RUN: %clang_cc1 %s -triple amdgcn-amd-amdhsa -target-cpu gfx906 -fsycl-is-device -E -dM | FileCheck --check-prefix=CHECK-HIP %s

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

// CHECK-CUDA:#define __SYCL_CUDA_ARCH__ 800
// CHECK-CUDA-NOT:#define __CUDA_ARCH__ 800
// CHECK-CUDA:#define __SYCL_CUDA_ARCH__ [[ARCH_CODE]]
// CHECK-CUDA-NOT:#define __CUDA_ARCH__ {{[0-9]+}}

// CHECK-CUDA-SYCL-DRIVER-NOT: #define __CUDA_ARCH__ {{[0-9]+}}

// CHECK-HIP:#define __CUDA_ARCH__ 0

Expand Down

0 comments on commit 9fd767d

Please sign in to comment.