Recent

Author Topic: [CLOSED] Inline function as argument  (Read 626 times)

julkas

  • Sr. Member
  • ****
  • Posts: 434
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
[CLOSED] Inline function as argument
« on: September 11, 2019, 08:22:38 pm »
Would be inlined function declared as inline and passed as argument?
« Last Edit: September 14, 2019, 10:58:44 am by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7613
Re: Inline function as argument
« Reply #1 on: September 11, 2019, 08:27:04 pm »
Probably, but the result wouldn't be inline.

PascalDragon

  • Hero Member
  • *****
  • Posts: 716
  • Compiler Developer
Re: Inline function as argument
« Reply #2 on: September 12, 2019, 09:18:25 am »
Would be inlined function declared as inline and passed as argument?
Only the result would be passed. The execution of the inline function's code would always be on the caller side (one exception is if the called function is also inline, then the compiler might optimize this a bit more). If you want to pass around functions you need to declare and use function pointers.

julkas

  • Sr. Member
  • ****
  • Posts: 434
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: Inline function as argument
« Reply #3 on: September 13, 2019, 09:26:25 am »
Code example -
Code: Pascal  [Select]
  1. unit emath;
  2. {$mode delphi}
  3. {$inline on}
  4. interface
  5. function qube(v: integer): integer; inline;
  6. implementation
  7. function qube(v: integer): integer;
  8. begin
  9.   Result := v*v*v;
  10. end;
  11. end.
  12.  
Code: Pascal  [Select]
  1. program ipv;
  2. {$mode delphi}
  3. {$inline on}
  4. uses emath;
  5. type
  6.   TFun = function (v: integer): integer;
  7. function mt(v: integer; f: TFun): integer;
  8. begin
  9.   Result := f(v) // What's going here?
  10. end;
  11. begin
  12.   WriteLn(mt(100, qube));
  13.   ReadLn();
  14. end.
  15.  
« Last Edit: September 13, 2019, 09:34:11 am by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 716
  • Compiler Developer
Re: Inline function as argument
« Reply #4 on: September 13, 2019, 09:44:34 am »
You're passing a pointer to the qube function which is then called inside mt. The inline directive won't make a difference there (even if mt would be declared as inline as well).

Mr.Madguy

  • Sr. Member
  • ****
  • Posts: 457
Re: Inline function as argument
« Reply #5 on: September 13, 2019, 10:11:52 am »
What's the purpose of using pointer to inline function? Optimization or ability to make function instances with different parameters? In second case you should look at anonymous functions (closures). Problem is: I'm not sure about trunk, but official versions of FPC/Lazarus still don't support them.
DynamicData 3.0 is released!
Since now development is frozen - only optimization passes will be made at some point.
Lack of multiple inheritance turns it into abomination.

julkas

  • Sr. Member
  • ****
  • Posts: 434
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: Inline function as argument
« Reply #6 on: September 13, 2019, 10:19:42 am »
What's the purpose of using pointer to inline function? Optimization or ability to make function instances with different parameters?
The first one - Optimization.
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 716
  • Compiler Developer
Re: Inline function as argument
« Reply #7 on: September 14, 2019, 11:12:29 am »
What's the purpose of using pointer to inline function? Optimization or ability to make function instances with different parameters? In second case you should look at anonymous functions (closures). Problem is: I'm not sure about trunk, but official versions of FPC/Lazarus still don't support them.
It could be that the function is both directly called and passed along as a function variable. In the former case the compiler will then inline it (if nothing else prevents that). If the function is only passed on as a function variable then it's currently useless.
However it could be that in the future the compiler might optimize the above mentioned example as well (if mt is declared as inline as well).

JernejL

  • Jr. Member
  • **
  • Posts: 64
Re: [CLOSED] Inline function as argument
« Reply #8 on: September 17, 2019, 02:14:10 pm »
It does seem like the function qube is correctly inlined (if i am correctly interpreting assembly):
 
https://godbolt.org/z/WYV8w5  ( x86-64 fpc 3.0.4  )
 

PascalDragon

  • Hero Member
  • *****
  • Posts: 716
  • Compiler Developer
Re: [CLOSED] Inline function as argument
« Reply #9 on: September 18, 2019, 09:12:06 am »
No, it is not. In line 40 of the assembly output you linked you can see that the pointer to cube is stored into %rsi and then mt is called 2 lines later.

Akira1364

  • Hero Member
  • *****
  • Posts: 539
Re: [CLOSED] Inline function as argument
« Reply #10 on: September 18, 2019, 04:18:30 pm »
Would be inlined function declared as inline and passed as argument?

As other people have said, no, not currently.

Note though that this is not because it's impossible to inline a function pointer (many other compilers do inline them basically by default), it's just because (sadly) no one has implemented that functionality in FPC yet.