Page 1 of 1

Linking inline functions

Posted: Fri Jan 16, 2009 6:07 pm
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

Re: Linking inline functions

Posted: Fri Jan 16, 2009 6:20 pm
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.

Re: Linking inline functions

Posted: Fri Jan 16, 2009 6:25 pm
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.

Re: Linking inline functions

Posted: Sun Jan 18, 2009 11:10 am
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

Re: Linking inline functions

Posted: Sun Jan 18, 2009 10:30 pm
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.

Re: Linking inline functions

Posted: Mon Jan 19, 2009 7:29 am
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