[llvm] [DWARFLinker] Preserve DWARF discriminators while linking (PR #96124)

David Tellenbach via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 16:51:48 PDT 2024


https://github.com/dtellenbach created https://github.com/llvm/llvm-project/pull/96124

DWARF-4 introduced DWARF discriminators but both, the classic and the parallel DWARF linkers discarded them so far.

After applying this patch dsymutil, which uses the DWARF linkers, correctly preserves discriminator information.

>From ce16f6f9d2621ada8b6caf914fbbe9d910a87ee1 Mon Sep 17 00:00:00 2001
From: David Tellenbach <dtellenbach at apple.com>
Date: Wed, 19 Jun 2024 16:45:39 -0700
Subject: [PATCH] [DWARFLinker] Preserve DWARF discriminators while linking

DWARF-4 introduced DWARF discriminators but both, the classic and the
parallel DWARF linkers discarded them so far.

After applying this patch dsymutil, which uses the DWARF linkers, correctly
preserves discriminator information.
---
 .../lib/DWARFLinker/Classic/DWARFStreamer.cpp |  17 ++++++--
 .../Parallel/DebugLineSectionEmitter.h        |  14 +++++--
 .../tools/dsymutil/Inputs/discriminator.arm64 | Bin 0 -> 17232 bytes
 llvm/test/tools/dsymutil/discriminator.test   |  39 ++++++++++++++++++
 4 files changed, 62 insertions(+), 8 deletions(-)
 create mode 100755 llvm/test/tools/dsymutil/Inputs/discriminator.arm64
 create mode 100644 llvm/test/tools/dsymutil/discriminator.test

diff --git a/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp b/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp
index 8b31b5ead29ad..cc4ee2a1b4aec 100644
--- a/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp
+++ b/llvm/lib/DWARFLinker/Classic/DWARFStreamer.cpp
@@ -1060,6 +1060,7 @@ void DwarfStreamer::emitLineTableRows(
   unsigned FileNum = 1;
   unsigned LastLine = 1;
   unsigned Column = 0;
+  unsigned Discriminator = 0;
   unsigned IsStatement = 1;
   unsigned Isa = 0;
   uint64_t Address = -1ULL;
@@ -1098,9 +1099,17 @@ void DwarfStreamer::emitLineTableRows(
       MS->emitULEB128IntValue(Column);
       LineSectionSize += 1 + getULEB128Size(Column);
     }
-
-    // FIXME: We should handle the discriminator here, but dsymutil doesn't
-    // consider it, thus ignore it for now.
+    if (Discriminator != Row.Discriminator &&
+        MS->getContext().getDwarfVersion() >= 4) {
+      Discriminator = Row.Discriminator;
+      unsigned Size = getULEB128Size(Discriminator);
+      MS->emitIntValue(dwarf::DW_LNS_extended_op, 1);
+      MS->emitULEB128IntValue(Size + 1);
+      MS->emitIntValue(dwarf::DW_LNE_set_discriminator, 1);
+      MS->emitULEB128IntValue(Discriminator);
+      LineSectionSize += /* extended op */ 1 + getULEB128Size(Size + 1) +
+                         /* discriminator */ 1 + Size;
+    }
 
     if (Isa != Row.Isa) {
       Isa = Row.Isa;
@@ -1156,7 +1165,7 @@ void DwarfStreamer::emitLineTableRows(
       EncodingBuffer.resize(0);
       Address = -1ULL;
       LastLine = FileNum = IsStatement = 1;
-      RowsSinceLastSequence = Column = Isa = 0;
+      RowsSinceLastSequence = Column = Discriminator = Isa = 0;
     }
   }
 
diff --git a/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h b/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
index 1839164dcec17..0c4395589248a 100644
--- a/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
+++ b/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
@@ -315,6 +315,7 @@ class DebugLineSectionEmitter {
     unsigned FileNum = 1;
     unsigned LastLine = 1;
     unsigned Column = 0;
+    unsigned Discriminator = 0;
     unsigned IsStatement = 1;
     unsigned Isa = 0;
     uint64_t Address = -1ULL;
@@ -350,9 +351,14 @@ class DebugLineSectionEmitter {
         Section.emitIntVal(dwarf::DW_LNS_set_column, 1);
         encodeULEB128(Column, Section.OS);
       }
-
-      // FIXME: We should handle the discriminator here, but dsymutil doesn't
-      // consider it, thus ignore it for now.
+      if (Discriminator != Row.Discriminator && MC->getDwarfVersion() >= 4) {
+        Discriminator = Row.Discriminator;
+        unsigned Size = getULEB128Size(Discriminator);
+        Section.emitIntVal(dwarf::DW_LNS_extended_op, 1);
+        encodeULEB128(Size + 1, Section.OS);
+        Section.emitIntVal(dwarf::DW_LNE_set_discriminator, 1);
+        encodeULEB128(Discriminator, Section.OS);
+      }
 
       if (Isa != Row.Isa) {
         Isa = Row.Isa;
@@ -397,7 +403,7 @@ class DebugLineSectionEmitter {
         EncodingBuffer.resize(0);
         Address = -1ULL;
         LastLine = FileNum = IsStatement = 1;
-        RowsSinceLastSequence = Column = Isa = 0;
+        RowsSinceLastSequence = Column = Discriminator = Isa = 0;
       }
     }
 
diff --git a/llvm/test/tools/dsymutil/Inputs/discriminator.arm64 b/llvm/test/tools/dsymutil/Inputs/discriminator.arm64
new file mode 100755
index 0000000000000000000000000000000000000000..c48979fd8e9ff7bdfe959b6dd98a1b90cde00190
GIT binary patch
literal 17232
zcmeI4TWnNi5XWcR#eyxakz&Q7T at s37bT4JoY+vMTr2>IKMKqiC;d9$PYmeQF*-LB%
zYK<vMFoBXPuk{vzNaF2*^@0r~YN9ErY1Ei#TAsE^c{3VGYW>f-q-WuQ=#vjK$?SYH
z-#7Ek`R#d~bLINAzZ-}=1gU}E39Vo;Mt-b_Hb8elmFkY|?CR+{u%DyVVkTFIZt}QJ
z1*+7+uK2;~Z8Kl1j*;!JLYS3%V^ON84;8Jo<yY5h+-^P0>-G`$om?~IrBQp!G)k4S
zFBsXRGP0>0A~*IoY1ezkPGY~Ro*j403a`Dp_wDK0vD<l8&4vtXify}VEteU%IYx*^
z`M+QeQJ8JEAGLk%;U4G^d_Ff6Bf0=v5ABAsthJS|Fl%w}U&<Xg?}y?Yp<t<y52lU2
zU~)K}gwN6p<vzB~zP)|Z$!P1TZ;$Qy{OEJ1+7R<Yt!MIK<uljj<KEWmeFzbEPY>ex
zxK0{;-NN at qp}c2t--*}3v(gLtd2+Z=)H8w30Qxfetn4i?T>p8EXf^Cs+h<-Z_d$6-
z)QLW=COIr31cZPP5CTF#2nYcoAOwVf5D)@FKnMr{As_^VfDjM at LO=)z0U;m+gn$qb
z0zyCt2mv7=1cZPP5CTF#2nYcoAOwVf5D)@FKnMr{As_^VfDjM at LO=)z0U;m+guwrd
z!160JGuJ}p`DQ9#ZK86eMJt~nieOB*vC^!SD?TcFdEI2K&A1QibvV0DD=*;u>LY8K
z7jTwfQEz;zR{h9zmkxQ(E<Hx$UtMmwFjtQ|YU!`JdaWFz(F1+Z`Q_!)ONU}-og5xb
z-e~i2Tb_mH*>(+<mFLn~H1l`mhv%&uFge`_Wr_3fwBK~_2$uL^Lbh^*?{vf-Pj}oE
z>rvFi9+$19i4puPj$o-W14<v#`%6V#4d{uao~JdbT#nXe5=NHpnixR`5WXYnx at Z&o
zykSc(tlJ-ipF&n$rjX(zoEpH7Gw**l{LL<(_cueW+pP2ZZrJC1o;T;SJjc%Yj?Xd9
z#~i*X>~p?*-`ziB at Oi9>c0SAf_^@#L<NEA7`F?z9;AT>aNhy<5 at Tr2HF9efCJ)PFG
zeTn{oV4%>SPZY6e3$;Z;?coj;Zdc*33Uve`#<SUIsi&tD4pP#<IU{3a6UAIU&`<xl
zQgtEzIs(!4-x{Vs%ug0J7-~cekMF^2=H>T+QKX>`dd%y0hWs_=Cj|KXt;G3sgQxob
zL_X6Vp^mxP4`XjW^82~lH`YBJT>r<F=wjy^^WT5*$!P00)87qmJO9!LAIEcFFQlft
zKh4Yx*6fe3T0Z>g;TOk#`SSR#V-LT7tNkNKCoi3NcX8si at e?Pfug$&_fBF7DKRZ2H
x_fg{4t3O<xytwy#$9wl4zo^fTJ+LJ<*t&klbnEzrOR?TZUsXRp{={4He*mnBI9dPz

literal 0
HcmV?d00001

diff --git a/llvm/test/tools/dsymutil/discriminator.test b/llvm/test/tools/dsymutil/discriminator.test
new file mode 100644
index 0000000000000..9bd1de2083567
--- /dev/null
+++ b/llvm/test/tools/dsymutil/discriminator.test
@@ -0,0 +1,39 @@
+; The DWARF at Inputs/discriminator.arm64 this test is based on was produced by
+; compiling
+;
+;   __attribute__((noinline))
+;   int foo(int a, int b, int c) {
+;     int x = a ? b : c;
+;     return x * a * b;
+;   }
+;
+;   int main(int argc, char** argv) {
+;     return foo(argc, 2, 3);
+;   }
+;
+; with -g -fdebug-info-for-profiling.
+
+RUN: dsymutil --flat --linker=classic -o - --verify-dwarf=none %p/Inputs/discriminator.arm64 | llvm-dwarfdump -debug-line - | FileCheck %s
+RUN: dsymutil --flat --linker=parallel -o - --verify-dwarf=none %p/Inputs/discriminator.arm64 | llvm-dwarfdump -debug-line - | FileCheck %s
+
+CHECK:         Address            Line   Column File   ISA Discriminator OpIndex Flags
+CHECK-NEXT:    ------------------ ------ ------ ------ --- ------------- ------- -------------
+CHECK-NEXT:    0x0000000100003f14      2      0      0   0             0       0  is_stmt
+CHECK-NEXT:    0x0000000100003f24      3     11      0   0             0       0  is_stmt prologue_end
+CHECK-NEXT:    0x0000000100003f30      3     15      0   0             2       0
+CHECK-NEXT:    0x0000000100003f38      3     11      0   0             0       0
+CHECK-NEXT:    0x0000000100003f3c      3     19      0   0             4       0
+CHECK-NEXT:    0x0000000100003f44      3     11      0   0             0       0
+CHECK-NEXT:    0x0000000100003f48      0     11      0   0             0       0
+CHECK-NEXT:    0x0000000100003f4c      3      7      0   0             6       0
+CHECK-NEXT:    0x0000000100003f50      4     10      0   0             0       0  is_stmt
+CHECK-NEXT:    0x0000000100003f54      4     14      0   0             0       0
+CHECK-NEXT:    0x0000000100003f58      4     12      0   0             0       0
+CHECK-NEXT:    0x0000000100003f5c      4     18      0   0             0       0
+CHECK-NEXT:    0x0000000100003f60      4     16      0   0             0       0
+CHECK-NEXT:    0x0000000100003f64      4      3      0   0             0       0  epilogue_begin
+CHECK-NEXT:    0x0000000100003f6c      7      0      0   0             0       0  is_stmt
+CHECK-NEXT:    0x0000000100003f84      8     14      0   0             0       0  is_stmt prologue_end
+CHECK-NEXT:    0x0000000100003f88      8     10      0   0             0       0
+CHECK-NEXT:    0x0000000100003f94      8      3      0   0             0       0  epilogue_begin
+CHECK-NEXT:    0x0000000100003fa0      8      3      0   0             0       0  end_sequence
\ No newline at end of file



More information about the llvm-commits mailing list