Linking inline functions

Programming, for all ages and all languages.
Post Reply
CPLH
Member
Member
Posts: 28
Joined: Thu Jan 08, 2009 1:31 pm
Location: New Jersey, USA
Contact:

Linking inline functions

Post by CPLH »

gcc has a lovely option "-finline-functions" which inlines any functions that are appropriate to inline within the same file.. this reduces the amounts of "call"s and "ret"s in assembly.
When a program gets larger, usually it is better to separate the files and later link them all together into one binary file.
However the "-finline-functions" only seems to work on gcc.

Does anybody know how to get it to work in ld? ..it would probably make a lot of compiled codes a bit shorter.

Thank you,
Veniamin
Have a plan.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Linking inline functions

Post by ru2aqare »

CPLH wrote:Does anybody know how to get it to work in ld? ..it would probably make a lot of compiled codes a bit shorter.
I don't think there is any way to do this. Linkers usually take code produced and optimised by compilers, and link it together. Some linkers can do link-time code generation (the MS C compiler and linker can do this, but I think it's disabled by default), and if you have an option to enable this in ld, it should help.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Linking inline functions

Post by JohnnyTheDon »

What your talking about is called "Whole Program Optimization". ld doesn't have much support for it. The main reason for this is the same reason assemblers don't do much optimization: it doesn't know what you're doing. When your code gets to ld, it is a bunch of bitcode with some headers.

The way WPO is usually done involves special semi-compiled bitcode that contains extra information about the code generated by the C compiler. The linker can use this info to optimize and do a final assembly on the program. M$'s compiler and linker can do this easily (with the /GL switch).

On Linux, I recommend LLVM ( http://www.llvm.org ). It has a modified version of gcc that takes advantage of WPO. There are two (easy) ways to use LLVM.

The first is to use LLVM bitcode instead of normal object files. You do this by replacing

Code: Select all

gcc -c -o foo.o foo.c
with

Code: Select all

llvm-gcc -c -emit-llvm -o foo.bc foo.c
Then instead of linking with ld, do

Code: Select all

llvm-ld -native -o kernel (INSERT .bc FILES) 
Obviously replace kernel with the file you want to output, and insert the .bc files created in the last step instead of (INSERT .bc FILES)

The other method is to just use llvm-gcc instead of gcc and also use llvm-gcc instead of ld. This provides less optimized code, but it works.
CPLH
Member
Member
Posts: 28
Joined: Thu Jan 08, 2009 1:31 pm
Location: New Jersey, USA
Contact:

Re: Linking inline functions

Post by CPLH »

JohnnyTheDon, thank you for your interesting insight. I am on Linux.. I've looked at LLVM, and I tried installing it with the gcc front end. Unfortunately did not install because some of it's libraries conflict with gcc-libs libraries. I have gcc-libs 4.3.2.
Here is the output when trying it install llvm:
llvm-gcc: /usr/lib/libgomp.a exists in filesystem
llvm-gcc: /usr/lib/libgomp.spec exists in filesystem
llvm-gcc: /usr/lib/libmudflap.a exists in filesystem
llvm-gcc: /usr/lib/libmudflapth.a exists in filesystem
llvm-gcc: /usr/lib/libssp.a exists in filesystem
llvm-gcc: /usr/lib/libssp_nonshared.a exists in filesystem
...and I cannot remove gcc-libs because of all of the things that depend on it..
:: amsn: requires gcc-libs
:: aspell: requires gcc-libs
:: csup: requires gcc-libs
:: db: requires gcc-libs
:: fltk: requires gcc-libs
:: gettext: requires gcc-libs
:: gnutls: requires gcc-libs>=4.3.2
:: groff: requires gcc-libs
:: hunspell: requires gcc-libs
:: lftp: requires gcc-libs
:: libmp4v2: requires gcc-libs
:: libsmbios: requires gcc-libs>=4.3.0
:: libstdc++5: requires gcc-libs
:: libusb: requires gcc-libs
:: mesa: requires gcc-libs>=4.3.1
:: p7zip: requires gcc-libs
:: pcre: requires gcc-libs
:: poppler: requires gcc-libs>=4.3.2
:: thunderbird: requires gcc-libs>=4.3.1
:: xulrunner: requires gcc-libs>=4.3.2
...So I am not sure how to install this thing. How did you do it?

Thank you,
Veniamin
Have a plan.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Linking inline functions

Post by JohnnyTheDon »

I have Ubuntu, so I just used Synaptic.

Since you have Arch Linux, that isn't an option. I recommend downloading the source package from LLVM's website and just using 'make'. If that builds successfully (which your post on the Arch Linux forums suggests is the case) then attempt to run 'make install'. That might not work for the same reason it didn't when you tried doing it with a package manager, although it should because by convention makefiles install to /usr/local/bin. If that isn't the case, you should be able to copy the binaries by hand to /usr/local/bin and not have any issues.

PS:

I'm sorry if you don't have Arch Linux, but the steps I laid out should work on any distro. I searched the error you posted on google and turned up http://bbs.archlinux.org/viewtopic.php?pid=481885 and assumed it was you. I really hope that is the case, or this is very embarrassing.
CPLH
Member
Member
Posts: 28
Joined: Thu Jan 08, 2009 1:31 pm
Location: New Jersey, USA
Contact:

Re: Linking inline functions

Post by CPLH »

Yep, it's me. I guess its rare for people to be running the same software, having the same problem installing the same program, with the same log, and at pretty much the same time. :)
Well, thanks for the info. I'll try to run it manually and see if it works. ;)

Veniamin
Have a plan.
Post Reply