[llvm-dev] How to force an unused function declaration in clang

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

[llvm-dev] How to force an unused function declaration in clang

Robin Eklind via llvm-dev
clang doesn't seem to respect __attribute__((used)) in C functions. Even if I declare a function like the following:   __attribute__((used)) void function(), then also it doesn't declare the function in its IR file if I don't use the function. Is there any other way to force the declaration of "unused" function declarations with clang.

I have hacked in clang 6.0.0 in tools/clang/lib/CodeGen/CodeGenModule.cpp file. I changed in EmitGlobal function to detect if the function has "used" attribute and then if it has then use "GetOrCreateLLVMFunction" this to create the function declaration. However, this hack fails in some other cases. Therefore I am looking for a proper way to do this.

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 force an unused function declaration in clang

Robin Eklind via llvm-dev
Out of curiosity, how does an unused declaration affect the emitted object file

Zhang

> 在 2018年6月26日,08:48,Soham Sinha via llvm-dev <[hidden email]> 写道:
>
> clang doesn't seem to respect __attribute__((used)) in C functions. Even if I declare a function like the following:   __attribute__((used)) void function(), then also it doesn't declare the function in its IR file if I don't use the function. Is there any other way to force the declaration of "unused" function declarations with clang.
>
> I have hacked in clang 6.0.0 in tools/clang/lib/CodeGen/CodeGenModule.cpp file. I changed in EmitGlobal function to detect if the function has "used" attribute and then if it has then use "GetOrCreateLLVMFunction" this to create the function declaration. However, this hack fails in some other cases. Therefore I am looking for a proper way to do this.
>
> 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 force an unused function declaration in clang

Robin Eklind via llvm-dev
For the same reason GCC allowed the attribute. Even if I want to use/inline a function later on in the pipeline.

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


On Tue, Jun 26, 2018 at 8:30 AM mayuyu.io <[hidden email]> wrote:
Out of curiosity, how does an unused declaration affect the emitted object file

Zhang

> 在 2018年6月26日,08:48,Soham Sinha via llvm-dev <[hidden email]> 写道:
>
> clang doesn't seem to respect __attribute__((used)) in C functions. Even if I declare a function like the following:   __attribute__((used)) void function(), then also it doesn't declare the function in its IR file if I don't use the function. Is there any other way to force the declaration of "unused" function declarations with clang.
>
> I have hacked in clang 6.0.0 in tools/clang/lib/CodeGen/CodeGenModule.cpp file. I changed in EmitGlobal function to detect if the function has "used" attribute and then if it has then use "GetOrCreateLLVMFunction" this to create the function declaration. However, this hack fails in some other cases. Therefore I am looking for a proper way to do this.
>
> 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 force an unused function declaration in clang

Robin Eklind via llvm-dev
It works for me:

https://godbolt.org/g/3QzqAH

Can you share an example?

On Tue, Jun 26, 2018 at 5:21 PM, Soham Sinha via llvm-dev
<[hidden email]> wrote:

> For the same reason GCC allowed the attribute. Even if I want to use/inline
> a function later on in the pipeline.
>
> Regards,
> Soham Sinha
> PhD Student, Department of Computer Science
> Boston University
>
>
> On Tue, Jun 26, 2018 at 8:30 AM mayuyu.io <[hidden email]> wrote:
>>
>> Out of curiosity, how does an unused declaration affect the emitted object
>> file
>>
>> Zhang
>>
>> > 在 2018年6月26日,08:48,Soham Sinha via llvm-dev <[hidden email]>
>> > 写道:
>> >
>> > clang doesn't seem to respect __attribute__((used)) in C functions. Even
>> > if I declare a function like the following:   __attribute__((used)) void
>> > function(), then also it doesn't declare the function in its IR file if I
>> > don't use the function. Is there any other way to force the declaration of
>> > "unused" function declarations with clang.
>> >
>> > I have hacked in clang 6.0.0 in
>> > tools/clang/lib/CodeGen/CodeGenModule.cpp file. I changed in EmitGlobal
>> > function to detect if the function has "used" attribute and then if it has
>> > then use "GetOrCreateLLVMFunction" this to create the function declaration.
>> > However, this hack fails in some other cases. Therefore I am looking for a
>> > proper way to do this.
>> >
>> > 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
>
_______________________________________________
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 force an unused function declaration in clang

Robin Eklind via llvm-dev
It does, when the function has a body. When it doesn't, it ignores. The body might be provided later on in the toolchain via linking a library.

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


On Tue, Jun 26, 2018 at 10:25 AM Hans Wennborg <[hidden email]> wrote:
It works for me:

https://godbolt.org/g/3QzqAH

Can you share an example?

On Tue, Jun 26, 2018 at 5:21 PM, Soham Sinha via llvm-dev
<[hidden email]> wrote:
> For the same reason GCC allowed the attribute. Even if I want to use/inline
> a function later on in the pipeline.
>
> Regards,
> Soham Sinha
> PhD Student, Department of Computer Science
> Boston University
>
>
> On Tue, Jun 26, 2018 at 8:30 AM mayuyu.io <[hidden email]> wrote:
>>
>> Out of curiosity, how does an unused declaration affect the emitted object
>> file
>>
>> Zhang
>>
>> > 在 2018年6月26日,08:48,Soham Sinha via llvm-dev <[hidden email]>
>> > 写道:
>> >
>> > clang doesn't seem to respect __attribute__((used)) in C functions. Even
>> > if I declare a function like the following:   __attribute__((used)) void
>> > function(), then also it doesn't declare the function in its IR file if I
>> > don't use the function. Is there any other way to force the declaration of
>> > "unused" function declarations with clang.
>> >
>> > I have hacked in clang 6.0.0 in
>> > tools/clang/lib/CodeGen/CodeGenModule.cpp file. I changed in EmitGlobal
>> > function to detect if the function has "used" attribute and then if it has
>> > then use "GetOrCreateLLVMFunction" this to create the function declaration.
>> > However, this hack fails in some other cases. Therefore I am looking for a
>> > proper way to do this.
>> >
>> > 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
>

_______________________________________________
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 force an unused function declaration in clang

Robin Eklind via llvm-dev
I realize that gcc also doesn't provide this feature if the function doesn't have a body. I was wondering if there is any elegant way to achieve this, instead of the "hack" I mentioned earlier.

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


On Tue, Jun 26, 2018 at 10:58 AM Soham Sinha <[hidden email]> wrote:
It does, when the function has a body. When it doesn't, it ignores. The body might be provided later on in the toolchain via linking a library.

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


On Tue, Jun 26, 2018 at 10:25 AM Hans Wennborg <[hidden email]> wrote:
It works for me:

https://godbolt.org/g/3QzqAH

Can you share an example?

On Tue, Jun 26, 2018 at 5:21 PM, Soham Sinha via llvm-dev
<[hidden email]> wrote:
> For the same reason GCC allowed the attribute. Even if I want to use/inline
> a function later on in the pipeline.
>
> Regards,
> Soham Sinha
> PhD Student, Department of Computer Science
> Boston University
>
>
> On Tue, Jun 26, 2018 at 8:30 AM mayuyu.io <[hidden email]> wrote:
>>
>> Out of curiosity, how does an unused declaration affect the emitted object
>> file
>>
>> Zhang
>>
>> > 在 2018年6月26日,08:48,Soham Sinha via llvm-dev <[hidden email]>
>> > 写道:
>> >
>> > clang doesn't seem to respect __attribute__((used)) in C functions. Even
>> > if I declare a function like the following:   __attribute__((used)) void
>> > function(), then also it doesn't declare the function in its IR file if I
>> > don't use the function. Is there any other way to force the declaration of
>> > "unused" function declarations with clang.
>> >
>> > I have hacked in clang 6.0.0 in
>> > tools/clang/lib/CodeGen/CodeGenModule.cpp file. I changed in EmitGlobal
>> > function to detect if the function has "used" attribute and then if it has
>> > then use "GetOrCreateLLVMFunction" this to create the function declaration.
>> > However, this hack fails in some other cases. Therefore I am looking for a
>> > proper way to do this.
>> >
>> > 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
>

_______________________________________________
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 force an unused function declaration in clang

Robin Eklind via llvm-dev

Define a variable that takes the address of the otherwise-unused function.

--paulr

 

From: llvm-dev [mailto:[hidden email]] On Behalf Of Soham Sinha via llvm-dev
Sent: Tuesday, June 26, 2018 12:20 PM
To: [hidden email]
Cc: via llvm-dev
Subject: Re: [llvm-dev] How to force an unused function declaration in clang

 

I realize that gcc also doesn't provide this feature if the function doesn't have a body. I was wondering if there is any elegant way to achieve this, instead of the "hack" I mentioned earlier.


Regards,

Soham Sinha

PhD Student, Department of Computer Science

Boston University

 

 

On Tue, Jun 26, 2018 at 10:58 AM Soham Sinha <[hidden email]> wrote:

It does, when the function has a body. When it doesn't, it ignores. The body might be provided later on in the toolchain via linking a library.


Regards,

Soham Sinha

PhD Student, Department of Computer Science

Boston University

 

 

On Tue, Jun 26, 2018 at 10:25 AM Hans Wennborg <[hidden email]> wrote:

It works for me:

https://godbolt.org/g/3QzqAH

Can you share an example?

On Tue, Jun 26, 2018 at 5:21 PM, Soham Sinha via llvm-dev
<[hidden email]> wrote:
> For the same reason GCC allowed the attribute. Even if I want to use/inline
> a function later on in the pipeline.
>
> Regards,
> Soham Sinha
> PhD Student, Department of Computer Science
> Boston University
>
>
> On Tue, Jun 26, 2018 at 8:30 AM mayuyu.io <[hidden email]> wrote:
>>
>> Out of curiosity, how does an unused declaration affect the emitted object
>> file
>>
>> Zhang
>>
>> > 2018626日,08:48Soham Sinha via llvm-dev <[hidden email]>
>> > 写道:
>> >
>> > clang doesn't seem to respect __attribute__((used)) in C functions. Even
>> > if I declare a function like the following:   __attribute__((used)) void
>> > function(), then also it doesn't declare the function in its IR file if I
>> > don't use the function. Is there any other way to force the declaration of
>> > "unused" function declarations with clang.
>> >
>> > I have hacked in clang 6.0.0 in
>> > tools/clang/lib/CodeGen/CodeGenModule.cpp file. I changed in EmitGlobal
>> > function to detect if the function has "used" attribute and then if it has
>> > then use "GetOrCreateLLVMFunction" this to create the function declaration.
>> > However, this hack fails in some other cases. Therefore I am looking for a
>> > proper way to do this.
>> >
>> > 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
>


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