[llvm-dev] How to inline function from other file in IR

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] How to inline function from other file in IR

Jonathan Wakely via llvm-dev
Hello,

How can I inline a function mentioned in other file?

I have an inline function foo in C source file (a.c) which is not referenced in this file. I compile this file to a.ll (I notice that the compiled a.ll doesn't have foo's definition, probably because it was inlined but not called anywhere)
I have another C source file b.c with function bar; I compile this to b.ll
I link these two files with llvm-link
I have written a pass which inserts foo function calls to the bar function at some points. I want foo function calls to be inlined. However, I get  compilation error because foo is not found (a.ll doesn't have foo)
If I remove the inline attribute of foo in a.c, I get the correctly compilation, with calls to foo inserted in bar from b.c
How do I achieve this kind of inline-ing?

Regards,
Soham Sinha
PhD Student, Department of Computer Science
Boston University

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to inline function from other file in IR

Jonathan Wakely via llvm-dev

It sounds like you want to compile with LTO (-flto) after removing the 'inline' from 'foo'.  That should enable the cross-compilation-unit inlining you want.

--paulr

 

From: llvm-dev [mailto:[hidden email]] On Behalf Of Soham Sinha via llvm-dev
Sent: Tuesday, May 15, 2018 7:33 PM
To: via llvm-dev
Subject: [llvm-dev] How to inline function from other file in IR

 

Hello,

 

How can I inline a function mentioned in other file?

 

I have an inline function foo in C source file (a.c) which is not referenced in this file. I compile this file to a.ll (I notice that the compiled a.ll doesn't have foo's definition, probably because it was inlined but not called anywhere)

I have another C source file b.c with function bar; I compile this to b.ll

I link these two files with llvm-link

I have written a pass which inserts foo function calls to the bar function at some points. I want foo function calls to be inlined. However, I get  compilation error because foo is not found (a.ll doesn't have foo)

If I remove the inline attribute of foo in a.c, I get the correctly compilation, with calls to foo inserted in bar from b.c

How do I achieve this kind of inline-ing?


Regards,

Soham Sinha

PhD Student, Department of Computer Science

Boston University


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to inline function from other file in IR

Jonathan Wakely via llvm-dev
In reply to this post by Jonathan Wakely via llvm-dev
Hi Soham,

Are you intending to use the inline keyword in order to control the inlining optimization?

The inline keyword is not doing this, it is changing the linkage type of the symbol and instruct the compiler to discard it if there is no call left, and also will instruct the linker to deduplicate. For instance if multiple file include a header that defines an inline function, this function could end up in multiple objects. Without the inline keyword you would get linker errors for duplicate symbols.

So, the question become: why do you want foo to be inline in bar? If it is for performance then you don't need the keyword and you should rely on the optimizer to "do the right thing": running `opt -O2` on the resulting .ll file you get after llvm-link and after running your pass that adds the call should be enough.

Best,

-- 
Mehdi




Le mar. 15 mai 2018 à 16:34, Soham Sinha via llvm-dev <[hidden email]> a écrit :
Hello,

How can I inline a function mentioned in other file?

I have an inline function foo in C source file (a.c) which is not referenced in this file. I compile this file to a.ll (I notice that the compiled a.ll doesn't have foo's definition, probably because it was inlined but not called anywhere)
I have another C source file b.c with function bar; I compile this to b.ll
I link these two files with llvm-link
I have written a pass which inserts foo function calls to the bar function at some points. I want foo function calls to be inlined. However, I get  compilation error because foo is not found (a.ll doesn't have foo)
If I remove the inline attribute of foo in a.c, I get the correctly compilation, with calls to foo inserted in bar from b.c
How do I achieve this kind of inline-ing?

Regards,
Soham Sinha
PhD Student, Department of Computer Science
Boston University
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to inline function from other file in IR

Jonathan Wakely via llvm-dev

Hi Soham,

 

“extern inline” keyword will help in this case, with this keyword compiler is forced to keeps the definition of the function and make it available for the external usage.

Also it retains the “inlinehint” attribute on the function, with that lto inliner may make it inline.

 

Best,

Ashutosh

 

From: llvm-dev [mailto:[hidden email]] On Behalf Of Mehdi AMINI via llvm-dev
Sent: Thursday, May 17, 2018 9:42 AM
To: [hidden email]
Cc: llvm-dev <[hidden email]>
Subject: Re: [llvm-dev] How to inline function from other file in IR

 

Hi Soham,

 

Are you intending to use the inline keyword in order to control the inlining optimization?

 

The inline keyword is not doing this, it is changing the linkage type of the symbol and instruct the compiler to discard it if there is no call left, and also will instruct the linker to deduplicate. For instance if multiple file include a header that defines an inline function, this function could end up in multiple objects. Without the inline keyword you would get linker errors for duplicate symbols.

 

So, the question become: why do you want foo to be inline in bar? If it is for performance then you don't need the keyword and you should rely on the optimizer to "do the right thing": running `opt -O2` on the resulting .ll file you get after llvm-link and after running your pass that adds the call should be enough.

 

Best,

 

-- 

Mehdi

 

 

 

Le mar. 15 mai 2018 à 16:34, Soham Sinha via llvm-dev <[hidden email]> a écrit :

Hello,

 

How can I inline a function mentioned in other file?

 

I have an inline function foo in C source file (a.c) which is not referenced in this file. I compile this file to a.ll (I notice that the compiled a.ll doesn't have foo's definition, probably because it was inlined but not called anywhere)

I have another C source file b.c with function bar; I compile this to b.ll

I link these two files with llvm-link

I have written a pass which inserts foo function calls to the bar function at some points. I want foo function calls to be inlined. However, I get  compilation error because foo is not found (a.ll doesn't have foo)

If I remove the inline attribute of foo in a.c, I get the correctly compilation, with calls to foo inserted in bar from b.c

How do I achieve this kind of inline-ing?


Regards,

Soham Sinha

PhD Student, Department of Computer Science

Boston University

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to inline function from other file in IR

Jonathan Wakely via llvm-dev
Hi all,

I solved it. I just added the keyword "__attribute__((always_inline))" before the function signature foo. I didn't add any extern inline or static inline or inline keyword. LLVM marks this function as an alwaysinline function. So when I add that synthetically with my transform pass the function is called from bar. However, the function foo is inlined when it is turned into machine code ultimately. That's what I wanted. Thanks for all your help.

Regards,
Soham Sinha
CS PhD Student
Boston University

On Thu, May 17, 2018, 12:47 AM Nema, Ashutosh <[hidden email]> wrote:

Hi Soham,

 

“extern inline” keyword will help in this case, with this keyword compiler is forced to keeps the definition of the function and make it available for the external usage.

Also it retains the “inlinehint” attribute on the function, with that lto inliner may make it inline.

 

Best,

Ashutosh

 

From: llvm-dev [mailto:[hidden email]] On Behalf Of Mehdi AMINI via llvm-dev
Sent: Thursday, May 17, 2018 9:42 AM
To: [hidden email]
Cc: llvm-dev <[hidden email]>
Subject: Re: [llvm-dev] How to inline function from other file in IR

 

Hi Soham,

 

Are you intending to use the inline keyword in order to control the inlining optimization?

 

The inline keyword is not doing this, it is changing the linkage type of the symbol and instruct the compiler to discard it if there is no call left, and also will instruct the linker to deduplicate. For instance if multiple file include a header that defines an inline function, this function could end up in multiple objects. Without the inline keyword you would get linker errors for duplicate symbols.

 

So, the question become: why do you want foo to be inline in bar? If it is for performance then you don't need the keyword and you should rely on the optimizer to "do the right thing": running `opt -O2` on the resulting .ll file you get after llvm-link and after running your pass that adds the call should be enough.

 

Best,

 

-- 

Mehdi

 

 

 

Le mar. 15 mai 2018 à 16:34, Soham Sinha via llvm-dev <[hidden email]> a écrit :

Hello,

 

How can I inline a function mentioned in other file?

 

I have an inline function foo in C source file (a.c) which is not referenced in this file. I compile this file to a.ll (I notice that the compiled a.ll doesn't have foo's definition, probably because it was inlined but not called anywhere)

I have another C source file b.c with function bar; I compile this to b.ll

I link these two files with llvm-link

I have written a pass which inserts foo function calls to the bar function at some points. I want foo function calls to be inlined. However, I get  compilation error because foo is not found (a.ll doesn't have foo)

If I remove the inline attribute of foo in a.c, I get the correctly compilation, with calls to foo inserted in bar from b.c

How do I achieve this kind of inline-ing?


Regards,

Soham Sinha

PhD Student, Department of Computer Science

Boston University

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev