Author: Yury Plyakhin
Date: 2026-08-12T14:50:02-07:00
New Revision: 6df891b916749d7dc92924659db9ffba8ac64c66

URL: 
https://github.com/llvm/llvm-project/commit/6df891b916749d7dc92924659db9ffba8ac64c66
DIFF: 
https://github.com/llvm/llvm-project/commit/6df891b916749d7dc92924659db9ffba8ac64c66.diff

LOG: [clang][SYCL] Enable GPURelocatableDeviceCode and predefine __CLANG_RDC__ 
(#215437)

SYCL device compilation defaults to RDC. The driver now passes -fgpu-rdc
to the SYCL device -cc1 invocation by default (disabled with
-fno-gpu-rdc), setting the GPURelocatableDeviceCode LangOpt.

Expose RDC to source code for SYCL through the `__CLANG_RDC__`
predefined macro, similar to CUDA and HIP.

---------

Co-authored-by: Claude <[email protected]>
Co-authored-by: Nick Sarnie <[email protected]>

Added: 
    

Modified: 
    clang/docs/SYCLSupport.md
    clang/lib/Driver/ToolChains/Clang.cpp
    clang/lib/Frontend/InitPreprocessor.cpp
    clang/test/Driver/sycl-offload-jit.cpp
    clang/test/Preprocessor/sycl-macro.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/SYCLSupport.md b/clang/docs/SYCLSupport.md
index 4271c8e12cdfa..f97d1853980b1 100644
--- a/clang/docs/SYCLSupport.md
+++ b/clang/docs/SYCLSupport.md
@@ -11,6 +11,12 @@ library. More details are provided in
 [external 
document](https://github.com/intel/llvm/blob/sycl/sycl/doc/design/CompilerAndRuntimeDesign.md),
 which are going to be added to clang documentation in the future.
 
+## Predefined Macros
+
+| Macro | Description |
+| --- | --- |
+| `__CLANG_RDC__` | Defined when Clang is compiling code in Relocatable Device 
Code (RDC) mode. RDC is necessary for linking device code across translation 
units. It is enabled by default for SYCL and can be disabled with the 
`-fno-gpu-rdc` compiler option. |
+
 ## Address space handling
 
 The SYCL specification represents pointers to disjoint memory regions using C++

diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 2e4bdf7475eef..79127fa5e2db1 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5176,8 +5176,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
                     options::OPT_no_offload_new_driver,
                     C.getActiveOffloadKinds() != Action::OFK_None));
 
-  bool IsRDCMode =
-      Args.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc, false);
+  // SYCL defaults to RDC; CUDA/HIP default to non-RDC.
+  bool IsRDCMode = Args.hasFlag(options::OPT_fgpu_rdc, 
options::OPT_fno_gpu_rdc,
+                                /*Default=*/IsSYCL);
 
   auto LTOMode = TC.getLTOMode(Args, JA.getOffloadingDeviceKind());
   bool IsUsingLTO = LTOMode != LTOK_None;
@@ -7390,9 +7391,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction 
&JA,
                       options::OPT_fno_hip_kernel_arg_name);
   }
 
+  if ((IsCuda || IsHIP || IsSYCL) && IsRDCMode)
+    CmdArgs.push_back("-fgpu-rdc");
+
   if (IsCuda || IsHIP) {
-    if (IsRDCMode)
-      CmdArgs.push_back("-fgpu-rdc");
     Args.addOptInFlag(CmdArgs, options::OPT_fgpu_defer_diag,
                       options::OPT_fno_gpu_defer_diag);
     if (Args.hasFlag(options::OPT_fgpu_exclude_wrong_side_overloads,

diff  --git a/clang/lib/Frontend/InitPreprocessor.cpp 
b/clang/lib/Frontend/InitPreprocessor.cpp
index 349628c3439bf..7f32f70fe52a3 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -585,9 +585,9 @@ static void InitializeStandardPredefinedMacros(const 
TargetInfo &TI,
   // Not "standard" per se, but available even with the -undef flag.
   if (LangOpts.AsmPreprocessor)
     Builder.defineMacro("__ASSEMBLER__");
+  if ((LangOpts.CUDA || LangOpts.isSYCL()) && 
LangOpts.GPURelocatableDeviceCode)
+    Builder.defineMacro("__CLANG_RDC__");
   if (LangOpts.CUDA) {
-    if (LangOpts.GPURelocatableDeviceCode)
-      Builder.defineMacro("__CLANG_RDC__");
     if (!LangOpts.HIP)
       Builder.defineMacro("__CUDA__");
     if (LangOpts.GPUDefaultStream ==

diff  --git a/clang/test/Driver/sycl-offload-jit.cpp 
b/clang/test/Driver/sycl-offload-jit.cpp
index c8eff1a4dd28f..6720e8d250381 100644
--- a/clang/test/Driver/sycl-offload-jit.cpp
+++ b/clang/test/Driver/sycl-offload-jit.cpp
@@ -47,6 +47,19 @@
 // CHK-FSYCL-IS-DEVICE: "-cc1"{{.*}} "-fsycl-is-device" {{.*}} "-emit-llvm-bc"
 // CHK-FSYCL-IS-HOST: "-cc1"{{.*}} "-fsycl-is-host"
 
+/// Check that SYCL compilation defaults to relocatable device code (-fgpu-rdc
+/// is passed to both the device and the host -cc1 invocation) and that
+/// -fno-gpu-rdc disables it.
+// RUN: %clang -### -fsycl -c %s 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHK-SYCL-RDC,CHK-SYCL-RDC-HOST %s
+// RUN: %clang -### -fsycl -fgpu-rdc -c %s 2>&1 \
+// RUN:   | FileCheck -check-prefixes=CHK-SYCL-RDC,CHK-SYCL-RDC-HOST %s
+// RUN: %clang -### -fsycl -fno-gpu-rdc -c %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHK-SYCL-NORDC %s
+// CHK-SYCL-RDC: "-cc1"{{.*}} "-fsycl-is-device" {{.*}} "-fgpu-rdc"
+// CHK-SYCL-RDC-HOST: "-cc1"{{.*}} "-fsycl-is-host" {{.*}} "-fgpu-rdc"
+// CHK-SYCL-NORDC-NOT: "-fgpu-rdc"
+
 // Check that --allow-partial-linkage and --create-library are not passed to
 // clang-linker-wrapper for SYCL (they are spirv-link flags, not 
clang-sycl-linker flags).
 // RUN: %clang -### --target=x86_64-unknown-linux-gnu -fsycl %s 2>&1 \

diff  --git a/clang/test/Preprocessor/sycl-macro.cpp 
b/clang/test/Preprocessor/sycl-macro.cpp
index a509086f99e41..9537237f0d8de 100644
--- a/clang/test/Preprocessor/sycl-macro.cpp
+++ b/clang/test/Preprocessor/sycl-macro.cpp
@@ -7,8 +7,18 @@
 // RUN: %clang_cc1 %s -fsycl-is-device -sycl-std=2020 -E -dM | FileCheck 
--check-prefix=CHECK-SYCL-STD-2020 %s
 // RUN: %clang_cc1 %s -fsycl-is-device -E -dM | FileCheck 
--check-prefixes=CHECK-SYCL %s
 
+/// __CLANG_RDC__ tells a SYCL implementation whether device symbols may be
+/// referenced across translation units. It is predefined in both the device 
and
+/// the host pass, and only when relocatable device code is enabled.
+// RUN: %clang_cc1 %s -fsycl-is-device -fgpu-rdc -E -dM | FileCheck 
--check-prefix=CHECK-RDC %s
+// RUN: %clang_cc1 %s -fsycl-is-host -fgpu-rdc -E -dM | FileCheck 
--check-prefix=CHECK-RDC %s
+// RUN: %clang_cc1 %s -fsycl-is-device -E -dM | FileCheck 
--check-prefix=CHECK-NO-RDC %s
+// RUN: %clang_cc1 %s -fsycl-is-host -E -dM | FileCheck 
--check-prefix=CHECK-NO-RDC %s
+
 // CHECK-NOT:#define __SYCL_DEVICE_ONLY__ 1
 // CHECK-NOT:#define CL_SYCL_LANGUAGE_VERSION 121
 // CHECK-SYCL-STD:#define CL_SYCL_LANGUAGE_VERSION 121
 // CHECK-SYCL-STD-2020:#define SYCL_LANGUAGE_VERSION 202012
 // CHECK-SYCL:#define __SYCL_DEVICE_ONLY__ 1
+// CHECK-RDC:#define __CLANG_RDC__ 1
+// CHECK-NO-RDC-NOT:#define __CLANG_RDC__


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to