Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-11

How does inline work?

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Thierry Coq

Hello,

I'm trying to use inline to remove unnecessary debugging code, such as the following code, with FPC 2.3.1 on Windows, when compiling for production.
However when I compile the code, at O1 optimisation level (my default), the call to DebugInfo is still present in the generated assembler.

Is there a way to completely remove a call to an empty non-virtual operation without ifdefing it?

Best regards,
Thierry

>>> Start of demo code... ><<<
{$Inline on}
type
  TForm1 = class(TForm)
    private
      procedure DebugInfo; inline;
    public
      procedure DoSomething;
  end;

...

Implementation
procedure TForm1.DebugInfo;
begin
  {$ifdef debug}
     DebugInfo
  {$endif}
end;
procedure TForm1.DoSomething;
begin
  DebugInfo;
end;

Reply by email

Jonas Maebe
to Thierry Coq
Thierry Coq wrote
I'm trying to use inline to remove unnecessary debugging code, such as the following code, with FPC 2.3.1 on Windows, when compiling for production. However when I compile the code, at O1 optimisation level (my default), the call to DebugInfo is still present in the generated assembler.

Always provide a compilable sample with the full compiler invocation when reporting problems. After guessing how your the full source code looks and compiling it, the call is removed here:

$ cat tt.pp
unit tt;
interface
{$Inline on}
type
TForm1 = class
  private
    procedure DebugInfo; inline;
  public
    procedure DoSomething;
end;
Implementation
procedure TForm1.DebugInfo;
begin
{$ifdef debug}
   DebugInfo
{$endif}
end;
procedure TForm1.DoSomething;
begin
DebugInfo;
end;
end.

$ fpc -O1 -al tt -Sd
Free Pascal Compiler version 2.4.0rc1 [2009/10/24] for i386 Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Darwin for i386
Compiling tt.pp
Assembling tt
28 lines compiled, 0.0 sec

$ cat tt.s
...

.text
.align 4
.globl _TT_TFORM1_$__DOSOMETHING
_TT_TFORM1_$__DOSOMETHING:
# Temps allocated between ebp-4 and ebp-4
# [24] begin
pushl %ebp
movl %esp,%ebp
subl $8,%esp
# Var $self located at ebp-4
movl %eax,-4(%ebp)
# [26] end;
leave
ret
...

I guess the real case in which you observe that behaviour is more complicated, with circular dependencies between implementation units, or whereby the implementation of the "inline" routine appears only after it is called. In the former case you have to recompile several times before all inline statements can take effect, since the compiler can only methods for which it knows the implementation at the point when these methods are called. In the latter case, the compiler will not be able to inline the routine in the unit before its implementation is parsed.

Jonas

Reply by email

JoshyFun
to Jonas Maebe

Hello FPC-Pascal,

Sunday, November 22, 2009, 12:25:32 PM, you wrote:

JM> I guess the real case in which you observe that behaviour is JM> more complicated, with circular dependencies between JM> implementation units, or whereby the implementation of the JM> "inline" routine appears only after it is called. In the former

So inlined methods should appear just after implementation keyword to get inlined all along the code ?

Best regards, JoshyFun

Reply by email

Roland Schaefer
to Jonas Maebe: Always provide a compilable sample with the full compiler invocation when reporting…
Jonas Maebe wrote 28 lines
unit tt; interface {$Inline on} type TForm1 = class private procedure DebugInfo; inline; public procedure DoSomething; end; Implementation procedure TForm1.DebugInfo; begin {$ifdef debug} DebugInfo {$endif} end; procedure TForm1.DoSomething; begin DebugInfo; end; end.

While on the subject of inline: I always wondered (currently using fpc 2.2.4 on W32 and GNU/Linux) why one can have inline hints either just in the interface (as in the ex. above), or the implementation, or both. This seems to be the case both for class methods and single functions/procedures. I haven't checked under which conditions the compiler actually inlines the code, though. Is that an intended behavior? If so, why?

Regards
Roland

Reply by email

Thierry Coq
to Jonas Maebe: Always provide a compilable sample with the full compiler invocation when reporting…
Jonas Maebe wrote 9 lines
On 22 Nov 2009, at 10:10, Thierry Coq wrote: > I'm trying to use inline to remove unnecessary debugging code, such as the following code, with FPC 2.3.1 on Windows, when compiling for production. > However when I compile the code, at O1 optimisation level (my default), the call to DebugInfo is still present in the generated assembler. > Always provide a compilable sample with the full compiler invocation when reporting problems. After guessing how your the full source code looks and compiling it, the call is removed here:

Thank you very much. I guessed I was doing something wrong!

Best regards,
Thierry

Reply by email

Thierry Coq
to Thierry Coq

Dear All,

Wouldn't it be interesting to document somewhere the requirement that inlined procedures need to be implemented BEFORE where they're called if it's in the same unit?

Would it be a good idea if I changed the FPC online doc, but where would that be, and also Lazarus' as well?

Best regards,
Thierry

Thierry Coq wrote 22 lines
Jonas Maebe wrote: > On 22 Nov 2009, at 10:10, Thierry Coq wrote: > >> I'm trying to use inline to remove unnecessary debugging code, such >> as the following code, with FPC 2.3.1 on Windows, when compiling for >> production. >> However when I compile the code, at O1 optimisation level (my >> default), the call to DebugInfo is still present in the generated >> assembler. > > Always provide a compilable sample with the full compiler invocation > when reporting problems. After guessing how your the full source code > looks and compiling it, the call is removed here: Thank you very much. I guessed I was doing something wrong! Best regards, Thierry _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email