https://github.com/oraluben updated 
https://github.com/llvm/llvm-project/pull/85222

>From 9d6fe5f8522ddedde66525e93f4b66e547ddadc6 Mon Sep 17 00:00:00 2001
From: Yichen Yan <wenji....@alibaba-inc.com>
Date: Thu, 14 Mar 2024 19:43:49 +0800
Subject: [PATCH 1/6] [NVPTX] Add `-march=general` option to mirror default
 configuration

---
 clang/lib/Driver/ToolChains/Cuda.cpp     | 4 ++--
 clang/test/Driver/cuda-cross-compiling.c | 9 +++++++--
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp 
b/clang/lib/Driver/ToolChains/Cuda.cpp
index c6007d3cfab864..4cb98f9f28963c 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -750,8 +750,8 @@ NVPTXToolChain::TranslateArgs(const 
llvm::opt::DerivedArgList &Args,
     if (!llvm::is_contained(*DAL, A))
       DAL->append(A);
 
-  // TODO: We should accept 'generic' as a valid architecture.
-  if (!DAL->hasArg(options::OPT_march_EQ) && OffloadKind != Action::OFK_None) {
+  if ((!DAL->hasArg(options::OPT_march_EQ) && OffloadKind != Action::OFK_None) 
||
+      (DAL->getLastArgValue(options::OPT_march_EQ) == "generic")) {
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
                       CudaArchToString(CudaArch::CudaDefault));
   } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "native") {
diff --git a/clang/test/Driver/cuda-cross-compiling.c 
b/clang/test/Driver/cuda-cross-compiling.c
index 086840accebe7f..e5aeca8300f85c 100644
--- a/clang/test/Driver/cuda-cross-compiling.c
+++ b/clang/test/Driver/cuda-cross-compiling.c
@@ -32,10 +32,15 @@
 //
 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -### %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=ARGS %s
+// RUN: %clang -target nvptx64-nvidia-cuda -march=generic -### %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=GENERIC %s
 
 //      ARGS: -cc1" "-triple" "nvptx64-nvidia-cuda" "-S" {{.*}} "-target-cpu" 
"sm_61" "-target-feature" "+ptx{{[0-9]+}}" {{.*}} "-o" "[[PTX:.+]].s"
 // ARGS-NEXT: ptxas{{.*}}"-m64" "-O0" "--gpu-name" "sm_61" "--output-file" 
"[[CUBIN:.+]].cubin" "[[PTX]].s" "-c"
 // ARGS-NEXT: nvlink{{.*}}"-o" "a.out" "-arch" "sm_61" {{.*}} "[[CUBIN]].cubin"
+//      GENERIC: -cc1" "-triple" "nvptx64-nvidia-cuda" "-S" {{.*}} 
"-target-cpu" "sm_52" "-target-feature" "+ptx{{[0-9]+}}" {{.*}} "-o" 
"[[PTX:.+]].s"
+// GENERIC-NEXT: ptxas{{.*}}"-m64" "-O0" "--gpu-name" "sm_52" "--output-file" 
"[[CUBIN:.+]].cubin" "[[PTX]].s" "-c"
+// GENERIC-NEXT: nvlink{{.*}}"-o" "a.out" "-arch" "sm_52" {{.*}} 
"[[CUBIN]].cubin"
 
 //
 // Test the generated arguments to the CUDA binary utils when targeting NVPTX. 
@@ -85,6 +90,6 @@
 // MISSING: error: Must pass in an explicit nvptx64 gpu architecture to 
'nvlink'
 
 // RUN: %clang -target nvptx64-nvidia-cuda -flto -c %s -### 2>&1 \
-// RUN:   | FileCheck -check-prefix=GENERIC %s
+// RUN:   | FileCheck -check-prefix=COMPILE %s
 
-// GENERIC-NOT: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-target-cpu"
+// COMPILE-NOT: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-target-cpu"

>From ecea39a30bf3b3fb6a11744bf4888b49ccc66179 Mon Sep 17 00:00:00 2001
From: Yichen Yan <wenji....@alibaba-inc.com>
Date: Thu, 14 Mar 2024 21:13:02 +0800
Subject: [PATCH 2/6] fmt

---
 clang/lib/Driver/ToolChains/Cuda.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp 
b/clang/lib/Driver/ToolChains/Cuda.cpp
index 4cb98f9f28963c..6114e4f015ab35 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -750,7 +750,8 @@ NVPTXToolChain::TranslateArgs(const 
llvm::opt::DerivedArgList &Args,
     if (!llvm::is_contained(*DAL, A))
       DAL->append(A);
 
-  if ((!DAL->hasArg(options::OPT_march_EQ) && OffloadKind != Action::OFK_None) 
||
+  if ((!DAL->hasArg(options::OPT_march_EQ) &&
+       OffloadKind != Action::OFK_None) ||
       (DAL->getLastArgValue(options::OPT_march_EQ) == "generic")) {
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
                       CudaArchToString(CudaArch::CudaDefault));

>From b0ae86c6a50981ae4a5514763c97fec7ac57db68 Mon Sep 17 00:00:00 2001
From: Yichen Yan <oralu...@outlook.com>
Date: Thu, 14 Mar 2024 21:33:07 +0800
Subject: [PATCH 3/6] Correct `-march=generic`'s semantic in test

Co-authored-by: Joseph Huber <hube...@outlook.com>
---
 clang/test/Driver/cuda-cross-compiling.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/clang/test/Driver/cuda-cross-compiling.c 
b/clang/test/Driver/cuda-cross-compiling.c
index e5aeca8300f85c..d72eeace88c88f 100644
--- a/clang/test/Driver/cuda-cross-compiling.c
+++ b/clang/test/Driver/cuda-cross-compiling.c
@@ -32,15 +32,10 @@
 //
 // RUN: %clang -target nvptx64-nvidia-cuda -march=sm_61 -### %s 2>&1 \
 // RUN:   | FileCheck -check-prefix=ARGS %s
-// RUN: %clang -target nvptx64-nvidia-cuda -march=generic -### %s 2>&1 \
-// RUN:   | FileCheck -check-prefix=GENERIC %s
 
 //      ARGS: -cc1" "-triple" "nvptx64-nvidia-cuda" "-S" {{.*}} "-target-cpu" 
"sm_61" "-target-feature" "+ptx{{[0-9]+}}" {{.*}} "-o" "[[PTX:.+]].s"
 // ARGS-NEXT: ptxas{{.*}}"-m64" "-O0" "--gpu-name" "sm_61" "--output-file" 
"[[CUBIN:.+]].cubin" "[[PTX]].s" "-c"
 // ARGS-NEXT: nvlink{{.*}}"-o" "a.out" "-arch" "sm_61" {{.*}} "[[CUBIN]].cubin"
-//      GENERIC: -cc1" "-triple" "nvptx64-nvidia-cuda" "-S" {{.*}} 
"-target-cpu" "sm_52" "-target-feature" "+ptx{{[0-9]+}}" {{.*}} "-o" 
"[[PTX:.+]].s"
-// GENERIC-NEXT: ptxas{{.*}}"-m64" "-O0" "--gpu-name" "sm_52" "--output-file" 
"[[CUBIN:.+]].cubin" "[[PTX]].s" "-c"
-// GENERIC-NEXT: nvlink{{.*}}"-o" "a.out" "-arch" "sm_52" {{.*}} 
"[[CUBIN]].cubin"
 
 //
 // Test the generated arguments to the CUDA binary utils when targeting NVPTX. 
@@ -90,6 +85,8 @@
 // MISSING: error: Must pass in an explicit nvptx64 gpu architecture to 
'nvlink'
 
 // RUN: %clang -target nvptx64-nvidia-cuda -flto -c %s -### 2>&1 \
-// RUN:   | FileCheck -check-prefix=COMPILE %s
+// RUN:   | FileCheck -check-prefix=GENERIC %s
+// RUN: %clang -target nvptx64-nvidia-cuda -march=sm_52 -march=generic -flto 
-c %s -### 2>&1 \
+// RUN:   | FileCheck -check-prefix=GENERIC %s
 
-// COMPILE-NOT: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-target-cpu"
+// GENERIC-NOT: -cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}} "-target-cpu"

>From 60a8c035ca7bbf74b307bc44d11ee240284983fa Mon Sep 17 00:00:00 2001
From: Yichen Yan <wenji....@alibaba-inc.com>
Date: Fri, 15 Mar 2024 12:26:46 +0800
Subject: [PATCH 4/6] Clear -march when `-march=generic`

---
 clang/lib/Driver/ToolChains/Cuda.cpp     | 6 +++---
 clang/test/Driver/cuda-cross-compiling.c | 2 ++
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp 
b/clang/lib/Driver/ToolChains/Cuda.cpp
index 6114e4f015ab35..2a449381b4d617 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -750,11 +750,11 @@ NVPTXToolChain::TranslateArgs(const 
llvm::opt::DerivedArgList &Args,
     if (!llvm::is_contained(*DAL, A))
       DAL->append(A);
 
-  if ((!DAL->hasArg(options::OPT_march_EQ) &&
-       OffloadKind != Action::OFK_None) ||
-      (DAL->getLastArgValue(options::OPT_march_EQ) == "generic")) {
+  if (!DAL->hasArg(options::OPT_march_EQ) && OffloadKind != Action::OFK_None) {
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
                       CudaArchToString(CudaArch::CudaDefault));
+  } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "generic") {
+    DAL->eraseArg(options::OPT_march_EQ);
   } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "native") {
     auto GPUsOrErr = getSystemGPUArchs(Args);
     if (!GPUsOrErr) {
diff --git a/clang/test/Driver/cuda-cross-compiling.c 
b/clang/test/Driver/cuda-cross-compiling.c
index d72eeace88c88f..a1719a6fbe042b 100644
--- a/clang/test/Driver/cuda-cross-compiling.c
+++ b/clang/test/Driver/cuda-cross-compiling.c
@@ -80,6 +80,8 @@
 //
 // RUN: not %clang -target nvptx64-nvidia-cuda %s -### 2>&1 \
 // RUN:   | FileCheck -check-prefix=MISSING %s
+// RUN: not %clang -target nvptx64-nvidia-cuda -march=generic %s -### 2>&1 \
+// RUN:   | FileCheck -check-prefix=MISSING %s
 
 // MISSING: error: Must pass in an explicit nvptx64 gpu architecture to 'ptxas'
 // MISSING: error: Must pass in an explicit nvptx64 gpu architecture to 
'nvlink'

>From 28eeaa31b60fda958b02fb5a90127c650f10af07 Mon Sep 17 00:00:00 2001
From: Yichen Yan <oralu...@outlook.com>
Date: Fri, 15 Mar 2024 22:28:54 +0800
Subject: [PATCH 5/6] Update Cuda.cpp

Co-authored-by: Joseph Huber <hube...@outlook.com>
---
 clang/lib/Driver/ToolChains/Cuda.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp 
b/clang/lib/Driver/ToolChains/Cuda.cpp
index 2a449381b4d617..99928b565429d6 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -753,7 +753,8 @@ NVPTXToolChain::TranslateArgs(const 
llvm::opt::DerivedArgList &Args,
   if (!DAL->hasArg(options::OPT_march_EQ) && OffloadKind != Action::OFK_None) {
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
                       CudaArchToString(CudaArch::CudaDefault));
-  } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "generic") {
+  } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "generic"
+             && OffloadKind == Action::OFK_None) {
     DAL->eraseArg(options::OPT_march_EQ);
   } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "native") {
     auto GPUsOrErr = getSystemGPUArchs(Args);

>From 27f97b1e18bc1939cc46bb3d9a89c7e211e2c23c Mon Sep 17 00:00:00 2001
From: Yichen Yan <oralu...@outlook.com>
Date: Fri, 15 Mar 2024 22:36:34 +0800
Subject: [PATCH 6/6] Update Cuda.cpp

---
 clang/lib/Driver/ToolChains/Cuda.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp 
b/clang/lib/Driver/ToolChains/Cuda.cpp
index 99928b565429d6..5f0b516e1a1a08 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -753,8 +753,8 @@ NVPTXToolChain::TranslateArgs(const 
llvm::opt::DerivedArgList &Args,
   if (!DAL->hasArg(options::OPT_march_EQ) && OffloadKind != Action::OFK_None) {
     DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_march_EQ),
                       CudaArchToString(CudaArch::CudaDefault));
-  } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "generic"
-             && OffloadKind == Action::OFK_None) {
+  } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "generic" &&
+             OffloadKind == Action::OFK_None) {
     DAL->eraseArg(options::OPT_march_EQ);
   } else if (DAL->getLastArgValue(options::OPT_march_EQ) == "native") {
     auto GPUsOrErr = getSystemGPUArchs(Args);

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to