[llvm] 3e8ab54 - [MC] Upgrade DWARF version to 5 upon .file 0

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 2 09:41:11 PST 2021


Author: Fangrui Song
Date: 2021-02-02T09:41:05-08:00
New Revision: 3e8ab54ba072cc50e41510ec8c56f49b06cac256

URL: https://github.com/llvm/llvm-project/commit/3e8ab54ba072cc50e41510ec8c56f49b06cac256
DIFF: https://github.com/llvm/llvm-project/commit/3e8ab54ba072cc50e41510ec8c56f49b06cac256.diff

LOG: [MC] Upgrade DWARF version to 5 upon .file 0

Without `-dwarf-version`, llvm-mc uses the default `MCContext::DwarfVersion` 4.

Without `-gdwarf-N`, Clang cc1as uses `clang::driver::ToolChain::GetDefaultDwarfVersion`
which is 4 on many toolchains. Note: `clang -c` can synthesize .debug_info without -g.

There is currently a MCParser warning upon `.file 0` and MCParser errors upon
`.loc 0` if the DWARF version is less than 5. This causes friction to the
following usage:

```
clang -S -g -gdwarf-5 a.c

// MC warning due to .file 0, MC error due to .loc 0
clang -c a.s
llvm-mc -filetype=obj a.s
```

My idea is that we can just upgrade `MCContext::DwarfVersion` to 5 upon
`.file 0` to make the above commands work.

The downside is that for an explicit version `clang -c -gdwarf-4 a.s`, it can be
argued that the new behavior drops the probably intended diagnostic. I think the
downside is small because in most cases DWARF version for an assembly action
should either match the original compile action or be omitted.

Ongoing discussion taking a similar action for GNU as: https://sourceware.org/pipermail/binutils/2021-January/114980.html

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

Added: 
    

Modified: 
    llvm/lib/MC/MCParser/AsmParser.cpp
    llvm/test/MC/ELF/dwarf-file0.s
    llvm/test/MC/ELF/dwarf-loc0.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index c5ff241ead74..2989041e64b2 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -3485,8 +3485,9 @@ bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
       Source = StringRef(SourceBuf, SourceString.size());
     }
     if (FileNumber == 0) {
+      // Upgrade to Version 5 for assembly actions like clang -c a.s.
       if (Ctx.getDwarfVersion() < 5)
-        return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5");
+        Ctx.setDwarfVersion(5);
       getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source);
     } else {
       Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective(

diff  --git a/llvm/test/MC/ELF/dwarf-file0.s b/llvm/test/MC/ELF/dwarf-file0.s
index f98fdcc2b40d..acf1fab0935c 100644
--- a/llvm/test/MC/ELF/dwarf-file0.s
+++ b/llvm/test/MC/ELF/dwarf-file0.s
@@ -1,34 +1,22 @@
 # REQUIRES: default_triple
-# RUN: llvm-mc -dwarf-version 4 %s -filetype=obj -o - | llvm-dwarfdump -debug-line - | FileCheck %s --check-prefixes=CHECK,CHECK-4
-# RUN: llvm-mc -dwarf-version 4 %s -filetype=asm -o - | FileCheck %s --check-prefixes=ASM,ASM-4
-# RUN: llvm-mc -dwarf-version 4 %s -filetype=asm -o - 2>&1 | FileCheck %s --check-prefix=WARN
-# RUN: llvm-mc -dwarf-version 5 %s -filetype=obj -o - | llvm-dwarfdump -debug-line - | FileCheck %s --check-prefixes=CHECK,CHECK-5
-# RUN: llvm-mc -dwarf-version 5 %s -filetype=asm -o - | FileCheck %s --check-prefixes=ASM,ASM-5
+# RUN: llvm-mc -dwarf-version 4 %s -filetype=obj -o - | llvm-dwarfdump -debug-line - | FileCheck %s
+# RUN: llvm-mc -dwarf-version 4 %s --fatal-warnings -o - | FileCheck %s --check-prefix=ASM
+# RUN: llvm-mc -dwarf-version 5 %s -filetype=obj -o - | llvm-dwarfdump -debug-line - | FileCheck %s
+# RUN: llvm-mc -dwarf-version 5 %s -o - | FileCheck %s --check-prefix=ASM
+
+## If the DWARF version is less than 5, .file 0 upgrades the version to 5.
         .file 0 "/test" "root.cpp"
         .file 1 "/include" "header.h"
         .file 2 "/test" "root.cpp"
-# CHECK-5:     include_directories[ 0] = "/test"
-# CHECK-4-NOT: include_directories[ 0]
-# CHECK:       include_directories[ 1] = "/include"
-# CHECK-4:     include_directories[ 2] = "/test"
-# CHECK-NOT:   include_directories
-# CHECK-4-NOT: file_names[ 0]
-# CHECK-5:     file_names[ 0]:
-# CHECK-5-NEXT: name: "root.cpp"
-# CHECK-5-NEXT: dir_index: 0
-# CHECK:       file_names[ 1]:
-# CHECK-NEXT:  name: "header.h"
-# CHECK-NEXT:  dir_index: 1
-# CHECK-4:     file_names[ 2]:
-# CHECK-4-NEXT: name: "root.cpp"
-# CHECK-4-NEXT: dir_index: 2
+# CHECK:       include_directories[  0] = "/test"
+# CHECK-NEXT:  include_directories[  1] = "/include"
+# CHECK:       file_names[  0]:
+# CHECK-NEXT:             name: "root.cpp"
+# CHECK-NEXT:        dir_index: 0
+# CHECK-NEXT:  file_names[  1]:
+# CHECK-NEXT:             name: "header.h"
+# CHECK-NEXT:        dir_index: 1
 
-# ASM-NOT: .file
-# ASM-5:   .file 0 "/test" "root.cpp"
+# ASM:     .file 0 "/test" "root.cpp"
 # ASM:     .file 1 "/include" "header.h"
-# ASM-4:   .file 2 "/test" "root.cpp"
 # ASM-NOT: .file
-
-# WARN:      file 0 not supported prior to DWARF-5
-# WARN-NEXT: .file 0
-# WARN-NEXT: ^

diff  --git a/llvm/test/MC/ELF/dwarf-loc0.s b/llvm/test/MC/ELF/dwarf-loc0.s
index b3b11c457b0a..2ae025e1dbd5 100644
--- a/llvm/test/MC/ELF/dwarf-loc0.s
+++ b/llvm/test/MC/ELF/dwarf-loc0.s
@@ -1,8 +1,10 @@
 # REQUIRES: default_triple
-# RUN: llvm-mc -dwarf-version 5 %s -filetype=obj -o - | llvm-dwarfdump -debug-line - | FileCheck %s
+# RUN: llvm-mc -dwarf-version 5 --defsym FILE0=1 %s -filetype=obj -o - | llvm-dwarfdump -debug-line - | FileCheck %s
 # RUN: not llvm-mc -dwarf-version 4 %s -filetype=asm -o - 2>&1 | FileCheck %s -check-prefix=ERR
 # Show that ".loc 0" works in DWARF v5, gets an error for earlier versions.
+.ifdef FILE0
         .file 0 "root.cpp"
+.endif
         .file 1 "header.h"
 	.loc  0 10 0
         .byte 0


        


More information about the llvm-commits mailing list