[clang] f5f746f - [OpenMP] Introduce '-f[no-]openmp-target-jit' flag to control JIT for offloading
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 6 18:01:48 PST 2023
Author: Joseph Huber
Date: 2023-01-06T20:01:41-06:00
New Revision: f5f746f1efd45a6e9ed8ca9be26e8c01c5aa00b1
URL: https://github.com/llvm/llvm-project/commit/f5f746f1efd45a6e9ed8ca9be26e8c01c5aa00b1
DIFF: https://github.com/llvm/llvm-project/commit/f5f746f1efd45a6e9ed8ca9be26e8c01c5aa00b1.diff
LOG: [OpenMP] Introduce '-f[no-]openmp-target-jit' flag to control JIT for offloading
JIT support for OpenMP offloading was introduced in D139287. This patch
adds a simple flag that enables this mode. It simply requires enabling
`-foffload-lto` mode and `--embed-bitcode` in the linker wrapper. This
option implies LTO if it is not enabled.
Reviewed By: jdoerfert
Differential Revision: https://reviews.llvm.org/D141158
Added:
clang/test/Driver/openmp-offload-jit.c
Modified:
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/include/clang/Driver/Options.td
clang/lib/Driver/Driver.cpp
clang/lib/Driver/ToolChains/Clang.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 4e86a5ec46b99..ed4f7b8affeb3 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -126,6 +126,8 @@ def err_drv_invalid_unwindlib_name : Error<
"invalid unwind library name in argument '%0'">;
def err_drv_incompatible_unwindlib : Error<
"--rtlib=libgcc requires --unwindlib=libgcc">;
+def err_drv_incompatible_options : Error<
+ "The combination of '%0' and '%1' is incompatible">;
def err_drv_invalid_stdlib_name : Error<
"invalid library name in argument '%0'">;
def err_drv_invalid_output_with_multiple_archs : Error<
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index b2f74add94ee4..d4afa4d98f5b5 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2652,6 +2652,11 @@ def fopenmp_offload_mandatory : Flag<["-"], "fopenmp-offload-mandatory">, Group<
Flags<[CC1Option, NoArgumentUnused]>,
HelpText<"Do not create a host fallback if offloading to the device fails.">,
MarshallingInfoFlag<LangOpts<"OpenMPOffloadMandatory">>;
+def fopenmp_target_jit : Flag<["-"], "fopenmp-target-jit">, Group<f_Group>,
+ Flags<[CoreOption, NoArgumentUnused]>,
+ HelpText<"Emit code that can be JIT compiled for OpenMP offloading. Implies -foffload-lto=full">;
+def fno_openmp_target_jit : Flag<["-"], "fno-openmp-target-jit">, Group<f_Group>,
+ Flags<[CoreOption, NoArgumentUnused, HelpHidden]>;
def fopenmp_target_new_runtime : Flag<["-"], "fopenmp-target-new-runtime">,
Group<f_Group>, Flags<[CC1Option, HelpHidden]>;
def fno_openmp_target_new_runtime : Flag<["-"], "fno-openmp-target-new-runtime">,
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 61f835054c977..1a4fceb80a9ad 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -721,6 +721,17 @@ void Driver::setLTOMode(const llvm::opt::ArgList &Args) {
OffloadLTOMode = parseLTOMode(*this, Args, options::OPT_foffload_lto_EQ,
options::OPT_fno_offload_lto);
+
+ // Try to enable `-foffload-lto=full` if `-fopenmp-target-jit` is on.
+ if (Args.hasFlag(options::OPT_fopenmp_target_jit,
+ options::OPT_fno_openmp_target_jit, false)) {
+ if (Arg *A = Args.getLastArg(options::OPT_foffload_lto_EQ,
+ options::OPT_fno_offload_lto))
+ if (OffloadLTOMode != LTOK_Full)
+ Diag(diag::err_drv_incompatible_options)
+ << A->getSpelling() << "-fopenmp-target-jit";
+ OffloadLTOMode = LTOK_Full;
+ }
}
/// Compute the desired OpenMP runtime from the flags provided.
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index f16c799887995..e407d65f678db 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -8428,6 +8428,10 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
}
Args.ClaimAllArgs(options::OPT_Xoffload_linker);
+ // Embed bitcode instead of an object in JIT mode.
+ if (const Arg *A = Args.getLastArg(options::OPT_fopenmp_target_jit))
+ CmdArgs.push_back("--embed-bitcode");
+
// Forward `-mllvm` arguments to the LLVM invocations if present.
for (Arg *A : Args.filtered(options::OPT_mllvm)) {
CmdArgs.push_back("-mllvm");
diff --git a/clang/test/Driver/openmp-offload-jit.c b/clang/test/Driver/openmp-offload-jit.c
new file mode 100644
index 0000000000000..93c38393025db
--- /dev/null
+++ b/clang/test/Driver/openmp-offload-jit.c
@@ -0,0 +1,54 @@
+// REQUIRES: x86-registered-target
+// REQUIRES: nvptx-registered-target
+// REQUIRES: amdgpu-registered-target
+
+// Check that we enable LTO-mode properly with '-fopenmp-target-jit' and that it
+// still enabled LTO-mode if `-fno-offload-lto` is on.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-phases -fopenmp \
+// RUN: -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=PHASES-JIT %s
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-phases -fopenmp \
+// RUN: -fopenmp-targets=nvptx64-nvidia-cuda -foffload-lto -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=PHASES-JIT %s
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-phases -fopenmp \
+// RUN: -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=PHASES-JIT %s
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-phases -fopenmp \
+// RUN: -fopenmp-targets=amdgcn-amd-amdhsa -foffload-lto -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=PHASES-JIT %s
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -ccc-print-phases -fopenmp \
+// RUN: -fopenmp-targets=amdgcn-amd-amdhsa -fno-offload-lto -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=PHASES-JIT %s
+//
+// PHASES-JIT: 0: input, "[[INPUT:.+]]", c, (host-openmp)
+// PHASES-JIT-NEXT: 1: preprocessor, {0}, cpp-output, (host-openmp)
+// PHASES-JIT-NEXT: 2: compiler, {1}, ir, (host-openmp)
+// PHASES-JIT-NEXT: 3: input, "[[INPUT]]", c, (device-openmp)
+// PHASES-JIT-NEXT: 4: preprocessor, {3}, cpp-output, (device-openmp)
+// PHASES-JIT-NEXT: 5: compiler, {4}, ir, (device-openmp)
+// PHASES-JIT-NEXT: 6: offload, "host-openmp (x86_64-unknown-linux-gnu)" {2}, "device-openmp ([[TARGET:.+]])" {5}, ir
+// PHASES-JIT-NEXT: 7: backend, {6}, lto-bc, (device-openmp)
+// PHASES-JIT-NEXT: 8: offload, "device-openmp ([[TARGET]])" {7}, lto-bc
+// PHASES-JIT-NEXT: 9: clang-offload-packager, {8}, image, (device-openmp)
+// PHASES-JIT-NEXT: 10: offload, "host-openmp (x86_64-unknown-linux-gnu)" {2}, "device-openmp (x86_64-unknown-linux-gnu)" {9}, ir
+// PHASES-JIT-NEXT: 11: backend, {10}, assembler, (host-openmp)
+// PHASES-JIT-NEXT: 12: assembler, {11}, object, (host-openmp)
+// PHASES-JIT-NEXT: 13: clang-linker-wrapper, {12}, image, (host-openmp)
+
+// Check that we add the `--embed-bitcode` flag to the linker wrapper.
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp \
+// RUN: -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=LINKER %s
+// LINKER: clang-linker-wrapper"{{.*}}"--embed-bitcode"
+
+// Check for incompatible combinations
+
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp -fno-offload-lto \
+// RUN: -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=NO-LTO %s
+// NO-LTO: error: The combination of '-fno-offload-lto' and '-fopenmp-target-jit' is incompatible
+
+// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fopenmp -foffload-lto=thin \
+// RUN: -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-target-jit %s 2>&1 \
+// RUN: | FileCheck -check-prefix=THIN-LTO %s
+// THIN-LTO: error: The combination of '-foffload-lto=' and '-fopenmp-target-jit' is incompatible
More information about the cfe-commits
mailing list