@@ -9138,81 +9138,64 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
91389138 const InputInfoList &Inputs,
91399139 const ArgList &Args,
91409140 const char *LinkingOutput) const {
9141- const Driver &D = getToolChain ().getDriver ();
9142- const llvm::Triple TheTriple = getToolChain ().getTriple ();
9143- ArgStringList CmdArgs;
9141+ using namespace options ;
9142+
9143+ // A list of permitted options that will be forwarded to the embedded device
9144+ // compilation job.
9145+ const llvm::DenseSet<unsigned > CompilerOptions{OPT_v,
9146+ OPT_cuda_path_EQ,
9147+ OPT_rocm_path_EQ,
9148+ OPT_O_Group,
9149+ OPT_g_Group,
9150+ OPT_g_flags_Group,
9151+ OPT_R_value_Group,
9152+ OPT_R_Group,
9153+ OPT_Xcuda_ptxas,
9154+ OPT_ftime_report,
9155+ OPT_save_temps,
9156+ OPT_mcode_object_version_EQ,
9157+ OPT_fno_lto,
9158+ OPT_flto,
9159+ OPT_flto_EQ};
9160+ const llvm::DenseSet<unsigned > LinkerOptions{OPT_mllvm};
9161+ auto ShouldForward = [&](const llvm::DenseSet<unsigned > &Set, Arg *A) {
9162+ return Set.contains (A->getOption ().getID ()) ||
9163+ (A->getOption ().getGroup ().isValid () &&
9164+ Set.contains (A->getOption ().getGroup ().getID ()));
9165+ };
91449166
9145- // Pass the CUDA path to the linker wrapper tool.
9167+ ArgStringList CmdArgs;
91469168 for (Action::OffloadKind Kind : {Action::OFK_Cuda, Action::OFK_OpenMP}) {
91479169 auto TCRange = C.getOffloadToolChains (Kind);
91489170 for (auto &I : llvm::make_range (TCRange)) {
91499171 const ToolChain *TC = I.second ;
9150- if (TC->getTriple ().isNVPTX ()) {
9151- CudaInstallationDetector CudaInstallation (D, TheTriple, Args);
9152- if (CudaInstallation.isValid ())
9153- CmdArgs.push_back (Args.MakeArgString (
9154- " --cuda-path=" + CudaInstallation.getInstallPath ()));
9155- break ;
9172+
9173+ // We do not use a bound architecture here so options passed only to a
9174+ // specific architecture via -Xarch_<cpu> will not be forwarded.
9175+ ArgStringList CompilerArgs;
9176+ ArgStringList LinkerArgs;
9177+ for (Arg *A : C.getArgsForToolChain (TC, /* BoundArch=*/ " " , Kind)) {
9178+ if (ShouldForward (CompilerOptions, A))
9179+ A->render (Args, CompilerArgs);
9180+ else if (ShouldForward (LinkerOptions, A))
9181+ A->render (Args, LinkerArgs);
91569182 }
9157- }
9158- }
91599183
9160- // Pass in the optimization level to use for LTO.
9161- if (const Arg *A = Args.getLastArg (options::OPT_O_Group)) {
9162- StringRef OOpt;
9163- if (A->getOption ().matches (options::OPT_O4) ||
9164- A->getOption ().matches (options::OPT_Ofast))
9165- OOpt = " 3" ;
9166- else if (A->getOption ().matches (options::OPT_O)) {
9167- OOpt = A->getValue ();
9168- if (OOpt == " g" )
9169- OOpt = " 1" ;
9170- else if (OOpt == " s" || OOpt == " z" )
9171- OOpt = " 2" ;
9172- } else if (A->getOption ().matches (options::OPT_O0))
9173- OOpt = " 0" ;
9174- if (!OOpt.empty ())
9175- CmdArgs.push_back (Args.MakeArgString (Twine (" --opt-level=O" ) + OOpt));
9184+ // Forward all of these to the appropriate toolchain.
9185+ for (StringRef Arg : CompilerArgs)
9186+ CmdArgs.push_back (Args.MakeArgString (
9187+ " --device-compiler=" + TC->getTripleString () + " =" + Arg));
9188+ for (StringRef Arg : LinkerArgs)
9189+ CmdArgs.push_back (Args.MakeArgString (
9190+ " --device-linker=" + TC->getTripleString () + " =" + Arg));
9191+ }
91769192 }
91779193
91789194 CmdArgs.push_back (
9179- Args.MakeArgString (" --host-triple=" + TheTriple. getTriple ()));
9195+ Args.MakeArgString (" --host-triple=" + getToolChain (). getTripleString ()));
91809196 if (Args.hasArg (options::OPT_v))
91819197 CmdArgs.push_back (" --wrapper-verbose" );
91829198
9183- if (const Arg *A = Args.getLastArg (options::OPT_g_Group)) {
9184- if (!A->getOption ().matches (options::OPT_g0))
9185- CmdArgs.push_back (" --device-debug" );
9186- }
9187-
9188- // code-object-version=X needs to be passed to clang-linker-wrapper to ensure
9189- // that it is used by lld.
9190- if (const Arg *A = Args.getLastArg (options::OPT_mcode_object_version_EQ)) {
9191- CmdArgs.push_back (Args.MakeArgString (" -mllvm" ));
9192- CmdArgs.push_back (Args.MakeArgString (
9193- Twine (" --amdhsa-code-object-version=" ) + A->getValue ()));
9194- }
9195-
9196- for (const auto &A : Args.getAllArgValues (options::OPT_Xcuda_ptxas))
9197- CmdArgs.push_back (Args.MakeArgString (" --ptxas-arg=" + A));
9198-
9199- // Forward remarks passes to the LLVM backend in the wrapper.
9200- if (const Arg *A = Args.getLastArg (options::OPT_Rpass_EQ))
9201- CmdArgs.push_back (Args.MakeArgString (Twine (" --offload-opt=-pass-remarks=" ) +
9202- A->getValue ()));
9203- if (const Arg *A = Args.getLastArg (options::OPT_Rpass_missed_EQ))
9204- CmdArgs.push_back (Args.MakeArgString (
9205- Twine (" --offload-opt=-pass-remarks-missed=" ) + A->getValue ()));
9206- if (const Arg *A = Args.getLastArg (options::OPT_Rpass_analysis_EQ))
9207- CmdArgs.push_back (Args.MakeArgString (
9208- Twine (" --offload-opt=-pass-remarks-analysis=" ) + A->getValue ()));
9209-
9210- if (Args.getLastArg (options::OPT_ftime_report))
9211- CmdArgs.push_back (" --device-compiler=-ftime-report" );
9212-
9213- if (Args.getLastArg (options::OPT_save_temps_EQ))
9214- CmdArgs.push_back (" --save-temps" );
9215-
92169199 // Construct the link job so we can wrap around it.
92179200 Linker->ConstructJob (C, JA, Output, Inputs, Args, LinkingOutput);
92189201 const auto &LinkCommand = C.getJobs ().getJobs ().back ();
@@ -9236,13 +9219,6 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
92369219 options::OPT_fno_openmp_target_jit, false ))
92379220 CmdArgs.push_back (" --embed-bitcode" );
92389221
9239- // Forward `-mllvm` arguments to the LLVM invocations if present.
9240- for (Arg *A : Args.filtered (options::OPT_mllvm)) {
9241- CmdArgs.push_back (" -mllvm" );
9242- CmdArgs.push_back (A->getValue ());
9243- A->claim ();
9244- }
9245-
92469222 // Pass in the C library for GPUs if present and not disabled.
92479223 if (!Args.hasArg (options::OPT_nostdlib, options::OPT_r, options::OPT_nogpulib,
92489224 options::OPT_nodefaultlibs, options::OPT_nolibc,
@@ -9268,11 +9244,6 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
92689244 });
92699245 }
92709246
9271- // If we disable the GPU C library support it needs to be forwarded to the
9272- // link job.
9273- if (!Args.hasFlag (options::OPT_gpulibc, options::OPT_nogpulibc, true ))
9274- CmdArgs.push_back (" --device-compiler=-nolibc" );
9275-
92769247 // Add the linker arguments to be forwarded by the wrapper.
92779248 CmdArgs.push_back (Args.MakeArgString (Twine (" --linker-path=" ) +
92789249 LinkCommand->getExecutable ()));
0 commit comments