[OpenMP] Make `-Xarch_host` and `-Xarch_device` work for OpenMP offloading
authorJoseph Huber <jhuber6@vols.utk.edu>
Tue, 17 Jan 2023 15:34:49 +0000 (09:34 -0600)
committerJoseph Huber <jhuber6@vols.utk.edu>
Tue, 17 Jan 2023 18:38:56 +0000 (12:38 -0600)
Clang currently supports the `-Xarch_host` and `-Xarch_device` variants
to handle passing arguments to only one part of the offloading
toolchain. This was previously only supported fully for HIP / CUDA This
patch simple updates the logic to make it work for any offloading kind.

Fixes #59799

Reviewed By: tianshilei1992

Differential Revision: https://reviews.llvm.org/D141935

clang/lib/Driver/ToolChain.cpp
clang/test/Driver/openmp-offload-gpu.c

index 7708d1b..bc70205 100644 (file)
@@ -1333,17 +1333,17 @@ llvm::opt::DerivedArgList *ToolChain::TranslateXarchArgs(
   DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
   bool Modified = false;
 
-  bool IsGPU = OFK == Action::OFK_Cuda || OFK == Action::OFK_HIP;
+  bool IsDevice = OFK != Action::OFK_None && OFK != Action::OFK_Host;
   for (Arg *A : Args) {
     bool NeedTrans = false;
     bool Skip = false;
     if (A->getOption().matches(options::OPT_Xarch_device)) {
-      NeedTrans = IsGPU;
-      Skip = !IsGPU;
+      NeedTrans = IsDevice;
+      Skip = !IsDevice;
     } else if (A->getOption().matches(options::OPT_Xarch_host)) {
-      NeedTrans = !IsGPU;
-      Skip = IsGPU;
-    } else if (A->getOption().matches(options::OPT_Xarch__) && IsGPU) {
+      NeedTrans = !IsDevice;
+      Skip = IsDevice;
+    } else if (A->getOption().matches(options::OPT_Xarch__) && IsDevice) {
       // Do not translate -Xarch_ options for non CUDA/HIP toolchain since
       // they may need special translation.
       // Skip this argument unless the architecture matches BoundArch
index 3dc2830..9a05daf 100644 (file)
 // RUN:    | FileCheck --check-prefix=CHECK-SET-FEATURES %s
 
 // CHECK-SET-FEATURES: clang-offload-packager{{.*}}--image={{.*}}feature=+ptx64
+
+//
+// Check that `-Xarch_host` works for OpenMP offloading.
+//
+// RUN:   %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
+// RUN:     -fopenmp-targets=nvptx64-nvidia-cuda -Xarch_host -O3 %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=XARCH-HOST %s
+// XARCH-HOST: "-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-O3"
+// XARCH-HOST-NOT: "-cc1" "-triple" "nvptx64-nvidia-cuda"{{.*}}"-O3"
+
+//
+// Check that `-Xarch_device` works for OpenMP offloading.
+//
+// RUN:   %clang -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp \
+// RUN:     -fopenmp-targets=nvptx64-nvidia-cuda -Xarch_device -O3 %s 2>&1 \
+// RUN:   | FileCheck --check-prefix=XARCH-DEVICE %s
+// XARCH-DEVICE: "-cc1" "-triple" "nvptx64-nvidia-cuda"{{.*}}"-O3"
+// XARCH-DEVICE-NOT: "-cc1" "-triple" "x86_64-unknown-linux-gnu"{{.*}}"-O3"