Lazarus

Programming => General => Topic started by: julkas on September 11, 2019, 08:22:38 pm

Title: [CLOSED] Inline function as argument
Post by: julkas on September 11, 2019, 08:22:38 pm
Would be inlined function declared as inline and passed as argument?
Title: Re: Inline function as argument
Post by: marcov on September 11, 2019, 08:27:04 pm
Probably, but the result wouldn't be inline.
Title: Re: Inline function as argument
Post by: PascalDragon 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.
Title: Re: Inline function as argument
Post by: julkas 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.  
Title: Re: Inline function as argument
Post by: PascalDragon 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).
Title: Re: Inline function as argument
Post by: Mr.Madguy 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.
Title: Re: Inline function as argument
Post by: julkas 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.
Title: Re: Inline function as argument
Post by: PascalDragon 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).
Title: Re: [CLOSED] Inline function as argument
Post by: JernejL 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  )
 
Title: Re: [CLOSED] Inline function as argument
Post by: PascalDragon 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.
Title: Re: [CLOSED] Inline function as argument
Post by: Akira1364 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.
TinyPortal © 2005-2018