[clang] [Driver][SYCL][Windows] Add SYCL windows runtime library linking support for SYCL JIT compilations (PR #194744)
Michael Toguchi via cfe-commits
cfe-commits at lists.llvm.org
Fri May 1 16:45:53 PDT 2026
================
@@ -85,6 +85,104 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back(Args.MakeArgString("--dependent-lib=amath"));
}
+ // ==========================================================================
+ // SYCL: Auto-add /MD if no C++ runtime specified
+ // SYCL requires dynamic C++ runtime because STL objects cross DLL boundaries
+ // This must happen BEFORE other CRT handling logic
+ // ==========================================================================
+ if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false) &&
+ !Args.hasArg(options::OPT_nolibsycl) &&
+ !Args.hasArg(options::OPT_nostdlib) &&
+ !Args.hasArg(options::OPT_nostartfiles)) {
+
+ bool HasExplicitCRT = false;
+
+ // Check if user already specified a CRT flag
+ if (Args.hasArg(options::OPT__SLASH_MT) ||
+ Args.hasArg(options::OPT__SLASH_MTd) ||
+ Args.hasArg(options::OPT__SLASH_MD) ||
+ Args.hasArg(options::OPT__SLASH_MDd) ||
+ Args.hasArg(options::OPT_fms_runtime_lib_EQ))
+ HasExplicitCRT = true;
+
+ // Auto-add /MD if no CRT specified (makes dynamic CRT the default for SYCL)
+ if (!HasExplicitCRT) {
+ CmdArgs.push_back("/MD");
+ }
+ }
+
+ // ==========================================================================
+ // SYCL: Validate CRT compatibility and add SYCL runtime libraries
+ // This needs to run for both CL mode and regular mode
+ // ==========================================================================
+ if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false) &&
+ !Args.hasArg(options::OPT_nolibsycl) &&
+ !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
+
+ // ------------------------------------------------------------------------
+ // STEP 1: Validate that static CRT is not being used
+ // ------------------------------------------------------------------------
+ bool HasStaticCRT = false;
+
+ // Check for explicit /MT or /MTd flags
+ if (Args.hasArg(options::OPT__SLASH_MT) ||
+ Args.hasArg(options::OPT__SLASH_MTd)) {
+ HasStaticCRT = true;
+ }
+
+ // Check -fms-runtime-lib flag
+ if (const Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) {
+ StringRef RuntimeLib = A->getValue();
+ if (RuntimeLib == "static" || RuntimeLib == "static_dbg") {
+ HasStaticCRT = true;
+ }
+ }
+
+ // Reject static CRT with SYCL
+ if (HasStaticCRT) {
+ TC.getDriver().Diag(diag::err_drv_sycl_requires_dynamic_crt);
+ // Continue to show other potential errors, but skip SYCL library linking
+ } else {
+ // ----------------------------------------------------------------------
+ // STEP 2: Add SYCL library search path
+ // ----------------------------------------------------------------------
+ CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
+ TC.getDriver().Dir + "/../lib"));
+
+ // ----------------------------------------------------------------------
+ // STEP 3: Determine if this is a debug build
+ // Only check DYNAMIC CRT flags (/MDd), never static (/MTd)
+ // ----------------------------------------------------------------------
+ bool IsDebugBuild = false;
+
+ // Method 1: Check -fms-runtime-lib=dll_dbg
+ if (const Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) {
+ StringRef RuntimeVal = A->getValue();
+ if (RuntimeVal == "dll_dbg")
+ IsDebugBuild = true;
+ }
+
+ // Method 2: Check for /MDd flag (dynamic debug CRT)
+ if (Args.hasArg(options::OPT__SLASH_MDd))
+ IsDebugBuild = true;
+
+ // ----------------------------------------------------------------------
+ // STEP 4: Add appropriate SYCL runtime library
+ // Using LLVMSYCL for LLVM naming convention
+ // ----------------------------------------------------------------------
----------------
mdtoguchi wrote:
all the callouts of the 'steps' and the additional `----` borders on the comments are a bit out of place. Just general comments of what is going on should be sufficent.
https://github.com/llvm/llvm-project/pull/194744
More information about the cfe-commits
mailing list