Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -296,56 +296,43 @@ !O.hasFlag(options::DriverOption) && !O.hasFlag(options::LinkerInput); } -/// Add the C++ include args of other offloading toolchains. If this is a host -/// job, the device toolchains are added. If this is a device job, the host -/// toolchains will be added. -static void addExtraOffloadCXXStdlibIncludeArgs(Compilation &C, - const JobAction &JA, - const ArgList &Args, - ArgStringList &CmdArgs) { - - if (JA.isHostOffloading(Action::OFK_Cuda)) - C.getSingleOffloadToolChain() - ->AddClangCXXStdlibIncludeArgs(Args, CmdArgs); - else if (JA.isDeviceOffloading(Action::OFK_Cuda)) - C.getSingleOffloadToolChain() - ->AddClangCXXStdlibIncludeArgs(Args, CmdArgs); - - // TODO: Add support for other programming models here. -} - -/// Add the C include args of other offloading toolchains. If this is a host -/// job, the device toolchains are added. If this is a device job, the host -/// toolchains will be added. -static void addExtraOffloadClangSystemIncludeArgs(Compilation &C, - const JobAction &JA, - const ArgList &Args, - ArgStringList &CmdArgs) { - - if (JA.isHostOffloading(Action::OFK_Cuda)) - C.getSingleOffloadToolChain()->AddClangSystemIncludeArgs( - Args, CmdArgs); - else if (JA.isDeviceOffloading(Action::OFK_Cuda)) - C.getSingleOffloadToolChain()->AddClangSystemIncludeArgs( - Args, CmdArgs); - - // TODO: Add support for other programming models here. -} - -/// Add the include args that are specific of each offloading programming model. -static void addExtraOffloadSpecificIncludeArgs(Compilation &C, - const JobAction &JA, - const ArgList &Args, - ArgStringList &CmdArgs) { - - if (JA.isHostOffloading(Action::OFK_Cuda)) - C.getSingleOffloadToolChain()->AddCudaIncludeArgs( - Args, CmdArgs); - else if (JA.isDeviceOffloading(Action::OFK_Cuda)) - C.getSingleOffloadToolChain()->AddCudaIncludeArgs( - Args, CmdArgs); - - // TODO: Add support for other programming models here. +/// Apply \a Work on the current tool chain \a RegularToolChain and any other +/// offloading tool chain that is associated with the current action \a JA. If +/// \a RegularToolChain is null, \a Work is only applied to the offloading +/// toolchains. If an offloading kind is provided, only offloading actions of +/// that kind are considered. +static void forAllAssociatedToolChains( + Compilation &C, const JobAction &JA, const ToolChain *RegularToolChain, + llvm::function_ref Work, + Action::OffloadKind ActiveOffloadingKind = Action::OFK_None) { + SmallVector RelevantToolChains; + // Add the current tool chain to the relevant tool chain list if it is + // defined. + if (RegularToolChain) + RelevantToolChains.push_back(RegularToolChain); + + // Add all the offloading tool chains associated with the current action to + // the relevant tool chain list. If we don't have a specific active offload + // kind, consider all available, otherwise consider only the active kind. + if (ActiveOffloadingKind == Action::OFK_None || + ActiveOffloadingKind == Action::OFK_Cuda) { + if (JA.isHostOffloading(Action::OFK_Cuda)) + RelevantToolChains.push_back( + C.getSingleOffloadToolChain()); + else if (JA.isDeviceOffloading(Action::OFK_Cuda)) + RelevantToolChains.push_back( + C.getSingleOffloadToolChain()); + } + + // + // TODO: Add support for other offloading programming models here. + // + + // Apply Work on all the relevant tool chains. + for (const auto *TC : RelevantToolChains) { + assert(TC && "Undefined tool chain??"); + Work(TC); + } } void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, @@ -622,22 +609,30 @@ // of an offloading programming model. // Add C++ include arguments, if needed. - if (types::isCXX(Inputs[0].getType())) { - getToolChain().AddClangCXXStdlibIncludeArgs(Args, CmdArgs); - addExtraOffloadCXXStdlibIncludeArgs(C, JA, Args, CmdArgs); - } + if (types::isCXX(Inputs[0].getType())) + forAllAssociatedToolChains( + C, JA, &getToolChain(), [&Args, &CmdArgs](const ToolChain *TC) { + TC->AddClangCXXStdlibIncludeArgs(Args, CmdArgs); + }); // Add system include arguments for all targets but IAMCU. - if (!IsIAMCU) { - getToolChain().AddClangSystemIncludeArgs(Args, CmdArgs); - addExtraOffloadClangSystemIncludeArgs(C, JA, Args, CmdArgs); - } else { + if (!IsIAMCU) + forAllAssociatedToolChains(C, JA, &getToolChain(), + [&Args, &CmdArgs](const ToolChain *TC) { + TC->AddClangSystemIncludeArgs(Args, CmdArgs); + }); + else { // For IAMCU add special include arguments. getToolChain().AddIAMCUIncludeArgs(Args, CmdArgs); } - // Add offload include arguments, if needed. - addExtraOffloadSpecificIncludeArgs(C, JA, Args, CmdArgs); + // Add offload include arguments specific for CUDA if that is required. We + // don't have to do that for the regular tool chain. + forAllAssociatedToolChains(C, JA, /*RegularToolChain=*/nullptr, + [&Args, &CmdArgs](const ToolChain *TC) { + TC->AddCudaIncludeArgs(Args, CmdArgs); + }, + /*ActiveOffloadingKind=*/Action::OFK_Cuda); } // FIXME: Move to target hook.