[PATCH] D145726: Fix assembler error when -g and -gdwarf-* is passed with -fno-integrated-as.

garvit gupta via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 9 13:17:07 PST 2023


garvitgupta08 created this revision.
garvitgupta08 added reviewers: apazos, efriedma, MaskRay, nickdesaulniers.
Herald added a project: All.
garvitgupta08 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

D136309 <https://reviews.llvm.org/D136309> and D136707 <https://reviews.llvm.org/D136707> are 2 differentials that pass -g and -gdwarf-* to
assembler when -fno-integrated-as is used. Due to this we started seeing
assembler errors with certain .c and .cpp files -

"Error: file number 1 already allocated"

This is because the debug info generated at the source code level is conflicting
with the debug info generated by assembler as mentioned in the gcc bug report -
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=35925

This patch solves the above failure by passing -g and -gdwarf-* flags to
assembler only when the source code is assembly, otherwise just generate the
debug info at the source code level.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145726

Files:
  clang/lib/Driver/ToolChains/Gnu.cpp
  clang/test/Driver/as-options.c
  clang/test/Driver/as-options.cpp


Index: clang/test/Driver/as-options.cpp
===================================================================
--- /dev/null
+++ clang/test/Driver/as-options.cpp
@@ -0,0 +1,11 @@
+// Test that -g and -gdwarf-* are not passed through to GAS.
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -g -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g \
+// RUN:   -fdebug-default-version=4 -c %s -### 2>&1 | FileCheck --check-prefix=DEBUG %s
+// DEBUG-NOT: "-g"
+// DEBUG-NOT: "-gdwarf-4"
Index: clang/test/Driver/as-options.c
===================================================================
--- /dev/null
+++ clang/test/Driver/as-options.c
@@ -0,0 +1,11 @@
+// Test that -g and -gdwarf-* are not passed through to GAS.
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -gdwarf-4 -g -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=DEBUG %s
+// RUN: %clang --target=arm-linux-gnueabi -fno-integrated-as -g \
+// RUN:   -fdebug-default-version=4 -c %s -### 2>&1 | FileCheck --check-prefix=DEBUG %s
+// DEBUG-NOT: "-g"
+// DEBUG-NOT: "-gdwarf-4"
Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -969,19 +969,23 @@
   CmdArgs.push_back("-o");
   CmdArgs.push_back(Output.getFilename());
 
-  for (const auto &II : Inputs)
+  for (const auto &II : Inputs) {
     CmdArgs.push_back(II.getFilename());
-
-  if (Arg *A = Args.getLastArg(options::OPT_g_Flag, options::OPT_gN_Group,
-                               options::OPT_gdwarf_2, options::OPT_gdwarf_3,
-                               options::OPT_gdwarf_4, options::OPT_gdwarf_5,
-                               options::OPT_gdwarf))
-    if (!A->getOption().matches(options::OPT_g0)) {
-      Args.AddLastArg(CmdArgs, options::OPT_g_Flag);
-
-      unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args);
-      CmdArgs.push_back(Args.MakeArgString("-gdwarf-" + Twine(DwarfVersion)));
-    }
+    StringRef BaseInput = StringRef(II.getBaseInput());
+    types::ID InputType = types::lookupTypeForExtension(
+        llvm::sys::path::extension(BaseInput).drop_front());
+    if (InputType == types::TY_Asm || InputType == types::TY_PP_Asm)
+      if (Arg *A = Args.getLastArg(options::OPT_g_Flag, options::OPT_gN_Group,
+                                   options::OPT_gdwarf_2, options::OPT_gdwarf_3,
+                                   options::OPT_gdwarf_4, options::OPT_gdwarf_5,
+                                   options::OPT_gdwarf))
+        if (!A->getOption().matches(options::OPT_g0)) {
+          Args.AddLastArg(CmdArgs, options::OPT_g_Flag);
+          unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args);
+          CmdArgs.push_back(
+              Args.MakeArgString("-gdwarf-" + Twine(DwarfVersion)));
+        }
+  }
 
   const char *Exec =
       Args.MakeArgString(getToolChain().GetProgramPath(DefaultAssembler));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145726.503909.patch
Type: text/x-patch
Size: 3597 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230309/994cd123/attachment.bin>


More information about the cfe-commits mailing list