Skip to content

Unnecessary copying of structured variables data when they passed as const parameters to procedures (64bit compiler)

Original Reporter info from Mantis: Filuta Vitaliy
  • Reporter name: FVI

Description:

Look at asm comments in code. We have difference between 32bit and 64bit compilers when calling WriteConst(S) or Write_(S)

32bit compiler pass pointer of S into procedures;

64bit compiler make copy of S in stack twice, and pass one of that copy into procedure (WriteConst or Write_) - that is an error, compiler have to pass pointer of S, as it does 32bit compiler (or delphi compiler)

program test_project;

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

type

  TStruct = record
    V1: Integer;
    V2: Integer;
    V3: Integer;
  end;
  PStruct = ^TStruct;

procedure WritePtr(Ptr: PStruct);
begin
  write(Ptr^.V1);
end;

procedure WriteVar(var S: TStruct);
begin
  write(S.V1);
end;

procedure WriteConst(const S: TStruct);
begin
  write(S.V1);
end;

procedure Write_(S: TStruct);
begin
  S.V2 := 1;
  write(S.V1);
end;

procedure Execute;
var
  S: TStruct;
begin
  {
    64 bit compiler                          32 bit compiler
  }
  S.V1 := 1;
  S.V2 := 2;
  S.V3 := 3;
  {
    S located at -48(%rbp)                   S located at -12(%ebp)
  }
  WritePtr(@S);
  {
    leaq  -48(%rbp),%rdi                     leal  -12(%ebp),%eax
    call  P$TEST_PROJECT_WRITEPTR$PSTRUCT    call  P$TEST_PROJECT_WRITEPTR$PSTRUCT
  }
  WriteVar(S);
  {
    leaq  -48(%rbp),%rdi                     leal  -12(%ebp),%eax
    call  P$TEST_PROJECT_WRITEVAR$TSTRUCT    call  P$TEST_PROJECT_WRITEVAR$TSTRUCT
  }
  WriteConst(S);
  {
    movq  -48(%rbp),%rax
    movq  %rax,-72(%rbp)
    movq  -40(%rbp),%rax
    movq  %rax,-64(%rbp)
    movq  -32(%rbp),%rax
    movq  %rax,-56(%rbp)
    movq  -72(%rbp),%rax
    movq  %rax,(%rsp)
    movq  -64(%rbp),%rax
    movq  %rax,8(%rsp)
    movq  -56(%rbp),%rax
    movq  %rax,16(%rsp)                      leal  -12(%ebp),%eax
    call  P$TEST_PROJECT_WRITECONST$TSTRUCT  call  P$TEST_PROJECT_WRITECONST$TSTRUCT
  }
  Write_(S);
  {
    movq  -48(%rbp),%rax
    movq  %rax,-72(%rbp)
    movq  -40(%rbp),%rax
    movq  %rax,-64(%rbp)
    movq  -32(%rbp),%rax
    movq  %rax,-56(%rbp)
    movq  -72(%rbp),%rax
    movq  %rax,(%rsp)
    movq  -64(%rbp),%rax
    movq  %rax,8(%rsp)
    movq  -56(%rbp),%rax
    movq  %rax,16(%rsp)                      leal  -12(%ebp),%eax
    call  P$TEST_PROJECT_WRITE_$TSTRUCT      call  P$TEST_PROJECT_WRITE_$TSTRUCT
  }
end;

begin
  Execute;
end.

Mantis conversion info:

  • Mantis ID: 17442
  • OS: ubuntu
  • OS Build: 9
  • Build: ?
  • Platform: 64bit
  • Version: 2.4.0
  • Fixed in version: 2.6.0
  • Fixed in revision: 16620 (#067536f8)
  • Monitored by: » Adriaan van Os (Adriaan van Os), » luizamerico (Luiz Americo)
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information