diff --git a/clang/lib/Driver/ToolChains/Flang.h b/clang/lib/Driver/ToolChains/Flang.h --- a/clang/lib/Driver/ToolChains/Flang.h +++ b/clang/lib/Driver/ToolChains/Flang.h @@ -56,6 +56,17 @@ void addTargetOptions(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const; + /// Extract offload options from the driver arguments and add them to + /// the command arguments. + /// \param [in] C The current compilation for the driver invocation + /// \param [in] Inputs The input infomration on the current file inputs + /// \param [in] JA The job action + /// \param [in] Args The list of input driver arguments + /// \param [out] CmdArgs The list of output command arguments + void addOffloadOptions(Compilation &C, const InputInfoList &Inputs, + const JobAction &JA, const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) const; + /// Extract other compilation options from the driver arguments and add them /// to the command arguments. /// diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -114,6 +114,35 @@ // TODO: Add target specific flags, ABI, mtune option etc. } +void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs, + const JobAction &JA, const ArgList &Args, + ArgStringList &CmdArgs) const { + bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP); + bool IsHostOffloadingAction = JA.isHostOffloading(Action::OFK_OpenMP) || + JA.isHostOffloading(C.getActiveOffloadKinds()); + + // Skips the primary input file, which is the input file that the compilation + // proccess will be executed upon (e.g. the host bitcode file) and + // adds the other secondary input (e.g. device bitcode files for embedding) + // to the embed offload object. This is condensed logic from the Clang driver + // for embedding offload objects during HostOffloading. + if (IsHostOffloadingAction) { + for (size_t i = 1; i < Inputs.size(); ++i) { + if (Inputs[i].getType() != types::TY_Nothing) + CmdArgs.push_back( + Args.MakeArgString("-fembed-offload-object=" + + getToolChain().getInputFilename(Inputs[i]))); + } + } + + if (IsOpenMPDevice) { + // -fopenmp-is-device is passed along to tell the frontend that it is + // generating code for a device, so that only the relevant code is + // emitted. + CmdArgs.push_back("-fopenmp-is-device"); + } +} + static void addFloatingPointOptions(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) { StringRef FPContract; @@ -327,6 +356,9 @@ // Add other compile options addOtherOptions(Args, CmdArgs); + // Offloading related options + addOffloadOptions(C, Inputs, JA, Args, CmdArgs); + // Forward -Xflang arguments to -fc1 Args.AddAllArgValues(CmdArgs, options::OPT_Xflang); diff --git a/flang/test/Driver/omp-frontend-forwarding.f90 b/flang/test/Driver/omp-frontend-forwarding.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Driver/omp-frontend-forwarding.f90 @@ -0,0 +1,22 @@ +! Test that flang-new OpenMP and OpenMP offload related +! commands forward or expand to the appropriate commands +! for flang-new -fc1 as expected. + +! Test regular -fopenmp with no offload +! RUN: %flang -### -fopenmp %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP %s +! CHECK-OPENMP: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}}.f90" +! CHECK-OPENMP-NOT: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90" + +! Test regular -fopenmp with offload for basic fopenmp-is-device flag addition and correct fopenmp +! RUN: %flang -### -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefixes=CHECK-OPENMP-IS-DEVICE %s +! CHECK-OPENMP-IS-DEVICE: "{{[^"]*}}flang-new" "-fc1" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90" + +! Testing fembed-offload-object and fopenmp-is-device +! RUN: %flang -S -### %s -o %t 2>&1 \ +! RUN: -fopenmp --offload-arch=gfx90a \ +! RUN: --target=aarch64-unknown-linux-gnu \ +! RUN: | FileCheck %s --check-prefixes=CHECK-OPENMP-EMBED +! CHECK-OPENMP-EMBED: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90" +! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90" +! CHECK-OPENMP-EMBED: "{{[^"]*}}clang-offload-packager" {{.*}} "--image=file={{.*}}.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp" +! CHECK-OPENMP-EMBED-NEXT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"