View Issue Details

IDProjectCategoryView StatusLast Update
0038980FPCCompilerpublic2021-06-10 20:26
ReporterJ. Gareth Moreton Assigned ToFlorian  
PrioritylowSeverityfeatureReproducibilityN/A
Status resolvedResolutionfixed 
PlatformCross-platformOSMicrosoft Windows 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0038980: [Patch] [Debug] New label debugging feature
DescriptionThis patch introduces a new debugging feature that can be enabled by defining DEBUG_LABEL. When active, comments are inserted before each label in the assembly dumps that indicate their final reference count. This will be a useful feature for developing and debugging features that work on labels, especially if their reference counts appear to be erroneously high, thus blocking some optimisation potential.
Steps To ReproduceApply patch and confiirm correct compilation with no change to generated binaries, and no change to assembly dumps when DEBUG_LABEL is not defined.
Additional InformationI first developed this for aarch64 when I noticed that many label reference counts were higher than they should be (most notably when the only jump was removed, but the destination label was still live), but testing on x86_64-win64 also revealed some labels whose reference counts were too high. For example, in system.s when compiled under -O4:

...
    testb $1,%dl
    je .Lj60
    movb (%rcx,%rdx,1),%al
    decq %r8
    movb %al,(%rdx)
    addq $1,%rdx
# References = 3
.Lj60:
    testb $2,%dl
...

Other than "je .Lj60", there is no other reference to .Lj60, not even in the metadata, yet its reference count is revealed to be 3. In the same subroutine, label .Lj59 is revealed to have a reference count of 6, but there are only 3 actual references (two conditional jumps and one unconditional jump).
Tagscompiler, debug, patch
Fixed in Revision49494
FPCOldBugId
FPCTarget-
Attached Files

Activities

J. Gareth Moreton

2021-06-10 01:39

developer  

debug-label.patch (1,306 bytes)   
Index: compiler/aggas.pas
===================================================================
--- compiler/aggas.pas	(revision 49493)
+++ compiler/aggas.pas	(working copy)
@@ -1323,13 +1323,17 @@
              begin
                if (tai_label(hp).labsym.is_used) then
                 begin
-                  if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
-                    begin
-                      writer.AsmWrite(#9'.private_extern ');
-                      writer.AsmWriteln(tai_label(hp).labsym.name);
-                    end;
+{$ifdef DEBUG_LABEL}
+                  writer.AsmWrite(asminfo^.comment);
+                  writer.AsmWriteLn('References = ' + tostr(tai_label(hp).labsym.getrefs));
+{$endif DEBUG_LABEL}
                   if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
                    begin
+                     if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
+                       begin
+                         writer.AsmWrite(#9'.private_extern ');
+                         writer.AsmWriteln(tai_label(hp).labsym.name);
+                       end;
 {$ifdef arm}
                      { do no change arm mode accidently, .globl seems to reset the mode }
                      if GenerateThumbCode or GenerateThumb2Code then
debug-label.patch (1,306 bytes)   

J. Gareth Moreton

2021-06-10 01:43

developer   ~0131224

The patch also moves an if-block to make the writer slightly more efficient, so "bind = AB_PRIVATE_EXTERN" isn't checked twice if it's false.

J. Gareth Moreton

2021-06-10 01:44

developer   ~0131225

If there's any additional debug information for labels that others think would be beneficial to print out when DEBUG_LABEL is defined, feel free to extend the feature.

Florian

2021-06-10 20:26

administrator   ~0131229

Thanks, applied.

Issue History

Date Modified Username Field Change
2021-06-10 01:39 J. Gareth Moreton New Issue
2021-06-10 01:39 J. Gareth Moreton File Added: debug-label.patch
2021-06-10 01:40 J. Gareth Moreton Tag Attached: patch
2021-06-10 01:40 J. Gareth Moreton Tag Attached: compiler
2021-06-10 01:40 J. Gareth Moreton Tag Attached: debug
2021-06-10 01:40 J. Gareth Moreton Priority normal => low
2021-06-10 01:40 J. Gareth Moreton Severity minor => feature
2021-06-10 01:40 J. Gareth Moreton FPCTarget => -
2021-06-10 01:41 J. Gareth Moreton Additional Information Updated View Revisions
2021-06-10 01:43 J. Gareth Moreton Note Added: 0131224
2021-06-10 01:44 J. Gareth Moreton Note Added: 0131225
2021-06-10 20:26 Florian Assigned To => Florian
2021-06-10 20:26 Florian Status new => resolved
2021-06-10 20:26 Florian Resolution open => fixed
2021-06-10 20:26 Florian Fixed in Version => 3.3.1
2021-06-10 20:26 Florian Fixed in Revision => 49494
2021-06-10 20:26 Florian Note Added: 0131229