Getting LLVM IR from clang

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting LLVM IR from clang

Oleg Smolsky via cfe-dev
How do I get the LLVM IR produced by clang?  I know that -emit-llvm
produces IR but it seems to be after processing by LLVM (things are
optimized out, for example).  Is there an "official" way to get the IR
coming right out of clang's codegen?  I can do a hack with -mllvm
-print-before-all but that's icky.

Thanks!

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

Re: Getting LLVM IR from clang

Oleg Smolsky via cfe-dev
Use -O0 -Xclang -disable-llvm-optzns

Best regards,
Alexey Bataev

> 28 нояб. 2018 г., в 17:22, David Greene via cfe-dev <[hidden email]> написал(а):
>
> How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> produces IR but it seems to be after processing by LLVM (things are
> optimized out, for example).  Is there an "official" way to get the IR
> coming right out of clang's codegen?  I can do a hack with -mllvm
> -print-before-all but that's icky.
>
> Thanks!
>
>                          -David
>
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Getting LLVM IR from clang

Oleg Smolsky via cfe-dev
In reply to this post by Oleg Smolsky via cfe-dev
Hi David,

There are at least two robust ways to get the Bitcode/IR:

1. LTO. You can enable link-time optimization by adding -flto compiler flags, in the end all object files (*.o) will in fact be bitcode files. I.e.:

    > file gmock.dir/gmock-gtest-all.cc.o
    gmock.dir/gmock-gtest-all.cc.o: LLVM bitcode, wrapper x86_6

2. Embedded bitcode. Clang 3.9 and higher has an option -fembed-bitcode. When enabled, the resulting executable will contain additional section containing bitcode.
You can use this great tool[1] to extract the bitcode from an executable.

Both approaches produce bitcode (binary format), if you need IR (human-readable format), then you can post-process the bitcode by running llvm-dis against each bitcode file.

I hope it helps.

[1] https://github.com/JDevlieghere/LibEBC

> On 28. Nov 2018, at 23:22, David Greene via cfe-dev <[hidden email]> wrote:
>
> How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> produces IR but it seems to be after processing by LLVM (things are
> optimized out, for example).  Is there an "official" way to get the IR
> coming right out of clang's codegen?  I can do a hack with -mllvm
> -print-before-all but that's icky.
>
> Thanks!
>
>                          -David
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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

Re: Getting LLVM IR from clang

Oleg Smolsky via cfe-dev
Collecting IR from LTO probably isn't what's desired - since that'll be already optimized in the individual compilation stage (to then be further optimized in the LTO stage).

As others mentioned, -Xclang -disable-llvm-optzns. Also note that changing optimization flags (-O0 versus -O3, etc) will change the IR that's generated - most notably, at -O0, all the functions in the IR will be attributed with 'nodebug', so running this IR through opt -O3 will be basically a no-op. This catches folks by surprise sometimes. So use the -O flag you want, but pass disable-llvm-optzns to get that 'optimized' IR (as far as Clang's codegen is concerned) prior to the LLVM optimizations running on it.

On Thu, Nov 29, 2018 at 12:49 AM Alex Denisov via cfe-dev <[hidden email]> wrote:
Hi David,

There are at least two robust ways to get the Bitcode/IR:

1. LTO. You can enable link-time optimization by adding -flto compiler flags, in the end all object files (*.o) will in fact be bitcode files. I.e.:

    > file gmock.dir/gmock-gtest-all.cc.o
    gmock.dir/gmock-gtest-all.cc.o: LLVM bitcode, wrapper x86_6

2. Embedded bitcode. Clang 3.9 and higher has an option -fembed-bitcode. When enabled, the resulting executable will contain additional section containing bitcode.
You can use this great tool[1] to extract the bitcode from an executable.

Both approaches produce bitcode (binary format), if you need IR (human-readable format), then you can post-process the bitcode by running llvm-dis against each bitcode file.

I hope it helps.

[1] https://github.com/JDevlieghere/LibEBC

> On 28. Nov 2018, at 23:22, David Greene via cfe-dev <[hidden email]> wrote:
>
> How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> produces IR but it seems to be after processing by LLVM (things are
> optimized out, for example).  Is there an "official" way to get the IR
> coming right out of clang's codegen?  I can do a hack with -mllvm
> -print-before-all but that's icky.
>
> Thanks!
>
>                          -David
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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

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

Re: Getting LLVM IR from clang

Oleg Smolsky via cfe-dev

most notably, at -O0, all the functions in the IR will be attributed with 'nodebug',

 

Dave means 'optnone' here. This prevents (most) optimization.

--paulr

 

From: cfe-dev [mailto:[hidden email]] On Behalf Of David Blaikie via cfe-dev
Sent: Thursday, November 29, 2018 2:33 PM
To: Alex Denisov
Cc: David Greene; [hidden email]
Subject: Re: [cfe-dev] Getting LLVM IR from clang

 

Collecting IR from LTO probably isn't what's desired - since that'll be already optimized in the individual compilation stage (to then be further optimized in the LTO stage).

As others mentioned, -Xclang -disable-llvm-optzns. Also note that changing optimization flags (-O0 versus -O3, etc) will change the IR that's generated - most notably, at -O0, all the functions in the IR will be attributed with 'nodebug', so running this IR through opt -O3 will be basically a no-op. This catches folks by surprise sometimes. So use the -O flag you want, but pass disable-llvm-optzns to get that 'optimized' IR (as far as Clang's codegen is concerned) prior to the LLVM optimizations running on it.

 

On Thu, Nov 29, 2018 at 12:49 AM Alex Denisov via cfe-dev <[hidden email]> wrote:

Hi David,

There are at least two robust ways to get the Bitcode/IR:

1. LTO. You can enable link-time optimization by adding -flto compiler flags, in the end all object files (*.o) will in fact be bitcode files. I.e.:

    > file gmock.dir/gmock-gtest-all.cc.o
    gmock.dir/gmock-gtest-all.cc.o: LLVM bitcode, wrapper x86_6

2. Embedded bitcode. Clang 3.9 and higher has an option -fembed-bitcode. When enabled, the resulting executable will contain additional section containing bitcode.
You can use this great tool[1] to extract the bitcode from an executable.

Both approaches produce bitcode (binary format), if you need IR (human-readable format), then you can post-process the bitcode by running llvm-dis against each bitcode file.

I hope it helps.

[1] https://github.com/JDevlieghere/LibEBC

> On 28. Nov 2018, at 23:22, David Greene via cfe-dev <[hidden email]> wrote:
>
> How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> produces IR but it seems to be after processing by LLVM (things are
> optimized out, for example).  Is there an "official" way to get the IR
> coming right out of clang's codegen?  I can do a hack with -mllvm
> -print-before-all but that's icky.
>
> Thanks!
>
>                          -David
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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


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

Re: Getting LLVM IR from clang

Oleg Smolsky via cfe-dev
Yup - thanks for the correction!

On Thu, Nov 29, 2018 at 11:37 AM <[hidden email]> wrote:

most notably, at -O0, all the functions in the IR will be attributed with 'nodebug',

 

Dave means 'optnone' here. This prevents (most) optimization.

--paulr

 

From: cfe-dev [mailto:[hidden email]] On Behalf Of David Blaikie via cfe-dev
Sent: Thursday, November 29, 2018 2:33 PM
To: Alex Denisov
Cc: David Greene; [hidden email]


Subject: Re: [cfe-dev] Getting LLVM IR from clang

 

Collecting IR from LTO probably isn't what's desired - since that'll be already optimized in the individual compilation stage (to then be further optimized in the LTO stage).



As others mentioned, -Xclang -disable-llvm-optzns. Also note that changing optimization flags (-O0 versus -O3, etc) will change the IR that's generated - most notably, at -O0, all the functions in the IR will be attributed with 'nodebug', so running this IR through opt -O3 will be basically a no-op. This catches folks by surprise sometimes. So use the -O flag you want, but pass disable-llvm-optzns to get that 'optimized' IR (as far as Clang's codegen is concerned) prior to the LLVM optimizations running on it.

 

On Thu, Nov 29, 2018 at 12:49 AM Alex Denisov via cfe-dev <[hidden email]> wrote:

Hi David,

There are at least two robust ways to get the Bitcode/IR:

1. LTO. You can enable link-time optimization by adding -flto compiler flags, in the end all object files (*.o) will in fact be bitcode files. I.e.:

    > file gmock.dir/gmock-gtest-all.cc.o
    gmock.dir/gmock-gtest-all.cc.o: LLVM bitcode, wrapper x86_6

2. Embedded bitcode. Clang 3.9 and higher has an option -fembed-bitcode. When enabled, the resulting executable will contain additional section containing bitcode.
You can use this great tool[1] to extract the bitcode from an executable.

Both approaches produce bitcode (binary format), if you need IR (human-readable format), then you can post-process the bitcode by running llvm-dis against each bitcode file.

I hope it helps.

[1] https://github.com/JDevlieghere/LibEBC

> On 28. Nov 2018, at 23:22, David Greene via cfe-dev <[hidden email]> wrote:
>
> How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> produces IR but it seems to be after processing by LLVM (things are
> optimized out, for example).  Is there an "official" way to get the IR
> coming right out of clang's codegen?  I can do a hack with -mllvm
> -print-before-all but that's icky.
>
> Thanks!
>
>                          -David
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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


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

Re: Getting LLVM IR from clang

Oleg Smolsky via cfe-dev
In reply to this post by Oleg Smolsky via cfe-dev
> Collecting IR from LTO probably isn't what's desired - since that'll be already optimized in the individual compilation stage (to then be further optimized in the LTO stage).

Hm, I'm a bit surprised now. So even if I call "clang -flto -O0 -c main.c" the actual bitcode will be optimized? Then which optimizations will be applied?
Does it also affect -fembed-bitcode?

> On 29. Nov 2018, at 20:33, David Blaikie <[hidden email]> wrote:
>
> Collecting IR from LTO probably isn't what's desired - since that'll be already optimized in the individual compilation stage (to then be further optimized in the LTO stage).
>
> As others mentioned, -Xclang -disable-llvm-optzns. Also note that changing optimization flags (-O0 versus -O3, etc) will change the IR that's generated - most notably, at -O0, all the functions in the IR will be attributed with 'nodebug', so running this IR through opt -O3 will be basically a no-op. This catches folks by surprise sometimes. So use the -O flag you want, but pass disable-llvm-optzns to get that 'optimized' IR (as far as Clang's codegen is concerned) prior to the LLVM optimizations running on it.
>
> On Thu, Nov 29, 2018 at 12:49 AM Alex Denisov via cfe-dev <[hidden email]> wrote:
> Hi David,
>
> There are at least two robust ways to get the Bitcode/IR:
>
> 1. LTO. You can enable link-time optimization by adding -flto compiler flags, in the end all object files (*.o) will in fact be bitcode files. I.e.:
>
>     > file gmock.dir/gmock-gtest-all.cc.o
>     gmock.dir/gmock-gtest-all.cc.o: LLVM bitcode, wrapper x86_6
>
> 2. Embedded bitcode. Clang 3.9 and higher has an option -fembed-bitcode. When enabled, the resulting executable will contain additional section containing bitcode.
> You can use this great tool[1] to extract the bitcode from an executable.
>
> Both approaches produce bitcode (binary format), if you need IR (human-readable format), then you can post-process the bitcode by running llvm-dis against each bitcode file.
>
> I hope it helps.
>
> [1] https://github.com/JDevlieghere/LibEBC
>
> > On 28. Nov 2018, at 23:22, David Greene via cfe-dev <[hidden email]> wrote:
> >
> > How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> > produces IR but it seems to be after processing by LLVM (things are
> > optimized out, for example).  Is there an "official" way to get the IR
> > coming right out of clang's codegen?  I can do a hack with -mllvm
> > -print-before-all but that's icky.
> >
> > Thanks!
> >
> >                          -David
> > _______________________________________________
> > cfe-dev mailing list
> > [hidden email]
> > http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

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

Re: Getting LLVM IR from clang

Oleg Smolsky via cfe-dev
-O0 -flto would still run the always-inliner, for example (& would skip any frontend (clang IR generation-level) optimizations you might be interested in), and would add the "optnone" attribute to the functions which may not be what you want if you're trying to investigate the -O3 pipeline

On Fri, Nov 30, 2018 at 12:12 AM Alex Denisov <[hidden email]> wrote:
> Collecting IR from LTO probably isn't what's desired - since that'll be already optimized in the individual compilation stage (to then be further optimized in the LTO stage).

Hm, I'm a bit surprised now. So even if I call "clang -flto -O0 -c main.c" the actual bitcode will be optimized? Then which optimizations will be applied?
Does it also affect -fembed-bitcode?

> On 29. Nov 2018, at 20:33, David Blaikie <[hidden email]> wrote:
>
> Collecting IR from LTO probably isn't what's desired - since that'll be already optimized in the individual compilation stage (to then be further optimized in the LTO stage).
>
> As others mentioned, -Xclang -disable-llvm-optzns. Also note that changing optimization flags (-O0 versus -O3, etc) will change the IR that's generated - most notably, at -O0, all the functions in the IR will be attributed with 'nodebug', so running this IR through opt -O3 will be basically a no-op. This catches folks by surprise sometimes. So use the -O flag you want, but pass disable-llvm-optzns to get that 'optimized' IR (as far as Clang's codegen is concerned) prior to the LLVM optimizations running on it.
>
> On Thu, Nov 29, 2018 at 12:49 AM Alex Denisov via cfe-dev <[hidden email]> wrote:
> Hi David,
>
> There are at least two robust ways to get the Bitcode/IR:
>
> 1. LTO. You can enable link-time optimization by adding -flto compiler flags, in the end all object files (*.o) will in fact be bitcode files. I.e.:
>
>     > file gmock.dir/gmock-gtest-all.cc.o
>     gmock.dir/gmock-gtest-all.cc.o: LLVM bitcode, wrapper x86_6
>
> 2. Embedded bitcode. Clang 3.9 and higher has an option -fembed-bitcode. When enabled, the resulting executable will contain additional section containing bitcode.
> You can use this great tool[1] to extract the bitcode from an executable.
>
> Both approaches produce bitcode (binary format), if you need IR (human-readable format), then you can post-process the bitcode by running llvm-dis against each bitcode file.
>
> I hope it helps.
>
> [1] https://github.com/JDevlieghere/LibEBC
>
> > On 28. Nov 2018, at 23:22, David Greene via cfe-dev <[hidden email]> wrote:
> >
> > How do I get the LLVM IR produced by clang?  I know that -emit-llvm
> > produces IR but it seems to be after processing by LLVM (things are
> > optimized out, for example).  Is there an "official" way to get the IR
> > coming right out of clang's codegen?  I can do a hack with -mllvm
> > -print-before-all but that's icky.
> >
> > Thanks!
> >
> >                          -David
> > _______________________________________________
> > cfe-dev mailing list
> > [hidden email]
> > http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


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