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,33 @@ // 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 primary input file, but adds other input to the 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 +354,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/clang/test/Driver/flang/flang-omp.f90 b/clang/test/Driver/flang/flang-omp.f90 new file mode 100644 --- /dev/null +++ b/clang/test/Driver/flang/flang-omp.f90 @@ -0,0 +1,20 @@ +! Check that flang -fc1 is invoked when in --driver-mode=flang +! and the relevant openmp and openmp offload flags are utilised +! and passed down correctly + +! Test regular -fopenmp with no offload +! RUN: %clang --driver-mode=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: %clang --driver-mode=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" + +! Test -fembed-object, -fopenmp-is-device and -fopenmp carry through when offload-arch is provided +! RUN: %clang --driver-mode=flang -### -S --target=aarch64-unknown-linux-gnu -fopenmp --offload-arch=gfx90a %s 2>&1 | FileCheck --check-prefixes=CHECK-EMBED-OBJ-WITH-OARCH %s +! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90" +! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90" +! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90" +! CHECK-EMBED-OBJ-WITH-OARCH-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc" +! CHECK-EMBED-OBJ-WITH-OARCH: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc" 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,17 @@ +! REQUIRES: amdgpu-registered-target + +! Test that flang-new OpenMP and OpenMP offload related +! commands forward or expand to the appropriate commands +! for flang-new -fc1 as expected. + +! 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: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}}.f90" +! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90" +! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "amdgcn-amd-amdhsa" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.f90" +! CHECK: "{{[^"]*}}clang-offload-packager" {{.*}} "--image=file={{.*}}gfx90a.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp" +! CHECK-NOT: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fopenmp-is-device" {{.*}}.bc" +! CHECK: "{{[^"]*}}flang-new" "-fc1" "-triple" "aarch64-unknown-linux-gnu" {{.*}} "-fopenmp" {{.*}} "-fembed-offload-object={{.*}}.out" {{.*}}.bc"