[fpc-devel] Questionable compiler side-effect with label names

J. Gareth Moreton gareth at moreton-family.com
Sat May 7 00:20:10 CEST 2022


Hi everyone,

My laptop is in the repair shop again unfortunately due to a jammed cooling fan, so this e-mail may have indentation problems in the mailing list.

That aside, I'm taking the opportunity to look at some optimisations for AArch64, and in the process I rediscovered the problem with labels having incorrect reference counts, which ultimately means I cannot reliably 
detect if a label only has a single reference.

After some investigation, I found out this is due to how AArch64 creates jumps in the code generator:

----

procedure tcgaarch64.a_jmp_always(list: TAsmList; l: TAsmLabel);
  var
    ai: taicpu;
  begin
    ai:=TAiCpu.op_sym(A_B,current_asmdata.RefAsmSymbol(l.name,AT_FUNCTION));
    ai.is_jmp:=true;
    list.Concat(ai);
  end;

----

Nothing too out of the ordinary.  However, the problem comes from reading the "name" property, since this calls the virtual method GetName, which is overridden in TAsmLabel:

----

function TAsmLabel.getname:TSymStr;
  begin
    getname:=inherited getname;
    increfs;
  end;

----

Every time you want to get the name of the label, its reference count is incremented.  Coupled with tai_cpu_abstract.loadref incrementing the reference count for a symbol, this has the result for increasing the 
reference count by 2 for each jump.  This is very undesirable because it has the side-effect of increasing the reference count whenever you want to find out the label's name, which also has the side-effect of 
increasing the reference count when writing the assembly dumps (as evidenced when DEBUG_LABEL Is defined).

At the moment I can't just simply remove the overridden method because other parts of the compiler depend on the reference count being incremented, as evidenced when I try to build for aarch64-linux:

----

as  -o /home/pi/Documents/source/fpc/rtl/units/aarch64-linux/gprt0.o aarch64/gprt0.as
/home/pi/Documents/source/fpc/compiler/ppc1 -Ur -Ur -Xs -O2 -n -Fi../inc -Fi../aarch64 -Fi../unix -Fiaarch64 -FE. -FU/home/pi/Documents/source/fpc/rtl/units/aarch64-linux -Fl/usr/lib/gcc/aarch64-linux-gnu/8 -a -O4 -
DD2022/05/07 -daarch64 -dRELEASE  -Us -Sg system.pp
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Assembler messages:
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Error: .size expression for .Ld17$strlab does not evaluate to a constant
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Error: .size expression for .Ld41$strlab does not evaluate to a constant
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Error: .size expression for .Ld42$strlab does not evaluate to a constant
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Error: .size expression for .Ld43$strlab does not evaluate to a constant
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Error: .size expression for .Ld44$strlab does not evaluate to a constant
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Error: .size expression for .Ld45$strlab does not evaluate to a constant
/home/pi/Documents/source/fpc/rtl/units/aarch64-linux/system.s: Error: .size expression for .Ld46$strlab does not evaluate to a constant
system.pp(737) Error: Error while assembling exitcode 1
system.pp(737) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted

----

In conclusion, I would like to refactor the compiler so simply reading the label name does not automatically increase its reference count, and correct the parts of the compiler that depend on this behaviour.  
Without accurate reference counts, it is very difficult to make reliable optimisations for AArch64 related to branches, and probably other platforms too.

Gareth aka. Kit


More information about the fpc-devel mailing list