[clang] [NVPTX] Add a warning that device debug info does not work with optimizations (PR #65327)

Georgi Mirazchiyski via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 5 06:39:32 PDT 2023


https://github.com/GeorgeWeb created https://github.com/llvm/llvm-project/pull/65327:

Passing `-g` to `ptxas` with any optimizations enabled is not allowed because, `ptxas` does not support optimized debugging.

>From 0efb5b6f3042224cf89978f607d5ff9cd1665351 Mon Sep 17 00:00:00 2001
From: Georgi Mirazchiyski <georgi.mirazchiyski at codeplay.com>
Date: Tue, 5 Sep 2023 14:21:24 +0100
Subject: [PATCH] [NVPTX] Add a warning that device debug info does not work
 with optimizations

Passing -g to ptxas with any optimizations enabled is not allowed because,
ptxas does not support optimized debugging.
---
 clang/lib/Driver/ToolChains/Cuda.cpp     | 14 +++++++++++++-
 clang/test/Driver/cuda-external-tools.cu | 15 +++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 97472a302715bb..3f76aa49822c26 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -413,13 +413,25 @@ void NVPTX::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
     // TODO: Perhaps we should map host -O2 to ptxas -O3. -O3 is ptxas's
     // default, so it may correspond more closely to the spirit of clang -O2.
 
+    bool noOptimization = A->getOption().matches(options::OPT_O0);
+    // Emit a driver diagnostic as warning if any -O option different from -O0,
+    // is passed since ptxas does not support optimized debugging.
+    if (!noOptimization) {
+      auto &Diags = TC.getDriver().getDiags();
+      unsigned DiagID = Diags.getCustomDiagID(
+          DiagnosticsEngine::Warning,
+          "ptxas cannot emit debug info with optimization "
+          "level ('%0') different than O0.");
+      Diags.Report(DiagID) << A->getAsString(Args);
+    }
+
     // -O3 seems like the least-bad option when -Osomething is specified to
     // clang but it isn't handled below.
     StringRef OOpt = "3";
     if (A->getOption().matches(options::OPT_O4) ||
         A->getOption().matches(options::OPT_Ofast))
       OOpt = "3";
-    else if (A->getOption().matches(options::OPT_O0))
+    else if (noOptimization)
       OOpt = "0";
     else if (A->getOption().matches(options::OPT_O)) {
       // -Os, -Oz, and -O(anything else) map to -O2, for lack of better options.
diff --git a/clang/test/Driver/cuda-external-tools.cu b/clang/test/Driver/cuda-external-tools.cu
index 1aa87cc09982c6..c49ba51b660ef5 100644
--- a/clang/test/Driver/cuda-external-tools.cu
+++ b/clang/test/Driver/cuda-external-tools.cu
@@ -28,6 +28,17 @@
 // RUN:   --offload-arch=sm_35 --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
 // RUN: | FileCheck -check-prefixes=CHECK,ARCH64,SM35,RDC %s
 
+// Compiling -O{1,2,3,4,fast,s,z} with -g does not pass -g debug info to ptxas.
+// NOTE: This is because ptxas does not support optimized debugging.
+// RUN: %clang -### --target=x86_64-linux-gnu -O3 -g -c %s 2>&1 \
+// RUN:   --offload-arch=sm_35 --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
+// RUN: | FileCheck -check-prefixes=CHECK,ARCH64,SM35,OPT3-DBG %s
+
+// Compiling -O0 with -g passes -g debug info to ptxas.
+// RUN: %clang -### --target=x86_64-linux-gnu -O0 -g -c %s 2>&1 \
+// RUN:   --offload-arch=sm_35 --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
+// RUN: | FileCheck -check-prefixes=CHECK,ARCH64,SM35,OPT0-DBG %s
+
 // With debugging enabled, ptxas should be run with with no ptxas optimizations.
 // RUN: %clang -### --target=x86_64-linux-gnu --cuda-noopt-device-debug -O2 -g -c %s 2>&1 \
 // RUN:   --offload-arch=sm_35 --cuda-path=%S/Inputs/CUDA/usr/local/cuda \
@@ -134,6 +145,10 @@
 // OPT2-NOT: "-g"
 // OPT3-SAME: "-O3"
 // OPT3-NOT: "-g"
+// OPT3-DBG-SAME: "-O3" "-lineinfo"
+// OPT3-DBG-NOT: "-g"
+// OPT0-DBG-SAME: "-g" "--dont-merge-basicblocks" "--return-at-end"
+// OPT0-DBG-NOT: "-O0"
 // DBG-SAME: "-g" "--dont-merge-basicblocks" "--return-at-end"
 // SM35-SAME: "--gpu-name" "sm_35"
 // SM35-SAME: "--output-file" "[[CUBINFILE:[^"]*]]"



More information about the cfe-commits mailing list