diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2527,6 +2527,10 @@ PosFlag, NegFlag, BothFlags<[NoArgumentUnused, HelpHidden]>>; def static_openmp: Flag<["-"], "static-openmp">, HelpText<"Use the static host OpenMP runtime while linking.">; +def foffload_device_only : Flag<["-"], "foffload-device-only">, + HelpText<"Only compile for the offloading device.">; +def foffload_host_only : Flag<["-"], "foffload-host-only">, + HelpText<"Only compile for the offloading host.">; def fopenmp_new_driver : Flag<["-"], "fopenmp-new-driver">, Flags<[CC1Option]>, Group, HelpText<"Use the new driver for OpenMP offloading.">; def fno_openmp_new_driver : Flag<["-"], "fno-openmp-new-driver">, Flags<[CC1Option]>, Group, diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -3983,10 +3983,10 @@ break; } - // Try to build the offloading actions and add the result as a dependency - // to the host. - if (UseNewOffloadingDriver) - Current = BuildOffloadingActions(C, Args, I, Current); + // We should stop if we only want to compile for the device. + if (Args.hasArg(options::OPT_foffload_device_only) && + isa(Current)) + break; // FIXME: Should we include any prior module file outputs as inputs of // later actions in the same command line? @@ -4011,6 +4011,11 @@ if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) break; + // Try to build the offloading actions and add the result as a dependency + // to the host. + if (UseNewOffloadingDriver) + Current = BuildOffloadingActions(C, Args, I, Current); + if (Current->getType() == types::TY_Nothing) break; } @@ -4143,14 +4148,19 @@ llvm::opt::DerivedArgList &Args, const InputTy &Input, Action *HostAction) const { - if (!isa(HostAction)) + if (Args.hasArg(options::OPT_foffload_host_only)) return HostAction; - OffloadAction::DeviceDependences DDeps; - types::ID InputType = Input.first; const Arg *InputArg = Input.second; + auto PL = types::getCompilationPhases(*this, Args, InputType); + + if (!isa(HostAction) && PL.back() != phases::Preprocess) + return HostAction; + + OffloadAction::DeviceDependences DDeps; + const Action::OffloadKind OffloadKinds[] = {Action::OFK_OpenMP}; for (Action::OffloadKind Kind : OffloadKinds) { @@ -4170,8 +4180,6 @@ if (DeviceActions.empty()) return HostAction; - auto PL = types::getCompilationPhases(*this, Args, InputType); - for (phases::ID Phase : PL) { if (Phase == phases::Link) { assert(Phase == PL.back() && "linking must be final compilation step."); @@ -4182,7 +4190,8 @@ for (Action *&A : DeviceActions) { A = ConstructPhaseAction(C, Args, Phase, A, Kind); - if (isa(A) && Kind == Action::OFK_OpenMP) { + if (isa(A) && isa(HostAction) && + Kind == Action::OFK_OpenMP) { HostAction->setCannotBeCollapsedWithNextDependentAction(); OffloadAction::HostDependence HDep( *HostAction, *C.getSingleOffloadToolChain(), @@ -4202,6 +4211,10 @@ } } + if (Args.hasArg(options::OPT_foffload_device_only)) + return C.MakeAction(DDeps, + DDeps.getActions().front()->getType()); + OffloadAction::HostDependence HDep( *HostAction, *C.getSingleOffloadToolChain(), /*BoundArch=*/nullptr, DDeps); diff --git a/clang/test/Driver/openmp-offload-gpu-new.c b/clang/test/Driver/openmp-offload-gpu-new.c --- a/clang/test/Driver/openmp-offload-gpu-new.c +++ b/clang/test/Driver/openmp-offload-gpu-new.c @@ -3,7 +3,6 @@ /// // REQUIRES: x86-registered-target -// REQUIRES: powerpc-registered-target // REQUIRES: nvptx-registered-target // REQUIRES: amdgpu-registered-target @@ -50,3 +49,18 @@ // RUN: | FileCheck -check-prefix=DRIVER_EMBEDDING %s // DRIVER_EMBEDDING: -fembed-offload-object=[[CUBIN:.*\.cubin]],openmp,nvptx64-nvidia-cuda,sm_70 + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-bindings -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda \ +// RUN: -foffload-host-only -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-HOST-ONLY +// CHECK-HOST-ONLY: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT:.*]]"], output: "[[OUTPUT:.*]]" +// CHECK-HOST-ONLY: "x86_64-unknown-linux-gnu" - "Offload::Linker", inputs: ["[[OUTPUT]]"], output: "a.out" + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-bindings -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda \ +// RUN: -foffload-device-only -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-DEVICE-ONLY +// CHECK-DEVICE-ONLY: "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT:.*]]"], output: "[[HOST_BC:.*]]" +// CHECK-DEVICE-ONLY: "nvptx64-nvidia-cuda" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[DEVICE_ASM:.*]]" +// CHECK-DEVICE-ONLY: "nvptx64-nvidia-cuda" - "NVPTX::Assembler", inputs: ["[[DEVICE_ASM]]"], output: "{{.*}}-openmp-nvptx64-nvidia-cuda.o" + +// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-bindings -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda \ +// RUN: -foffload-device-only -E -nogpulib %s 2>&1 | FileCheck %s --check-prefix=CHECK-DEVICE-ONLY-PP +// CHECK-DEVICE-ONLY-PP: "nvptx64-nvidia-cuda" - "clang", inputs: ["[[INPUT:.*]]"], output: "-"