Re: Merging identical functions in GCC

2006-09-20 Thread Lothar Werzinger
Did anybody do some metrics how big the savings would be? I think we should loom at the metrics before jumping to conclusions about implementing such a feature.

Re: Merging identical functions in GCC

2006-09-20 Thread Alexandre Oliva
On Sep 20, 2006, Joe Buck <[EMAIL PROTECTED]> wrote: > On Wed, Sep 20, 2006 at 02:52:40AM -0300, Alexandre Oliva wrote: >> For extra bonus points, it may short-circuit relocations that call foo >> (as opposed to taking its address) when it binds locally and skip the >> jmp altogether. Ditto for

Re: Merging identical functions in GCC

2006-09-20 Thread Joe Buck
On Wed, Sep 20, 2006 at 02:52:40AM -0300, Alexandre Oliva wrote: > The linker already has code to merge sections. There's nothing to > stop us from compiling every function into say: > > foo: > jmp .foo.impl > .section .gnu.impl.foo, "axM" > .foo.impl: > [actual code for foo emitted] > .previ

Re: Merging identical functions in GCC

2006-09-19 Thread Alexandre Oliva
On Sep 18, 2006, Roman Kononov <[EMAIL PROTECTED]> wrote: > Merge can be safely done like this: > If both functions A and B are not inlined functions, and they have > single entry points, and memcmp of their asm code is zero, and the > code is large enough, then the asm code of the functions B is

Re: Merging identical functions in GCC

2006-09-18 Thread Ross Ridge
Daniel Berlin writes >Please go away and stop trolling. I'm not the one who's being rude and abusive. >If your concern is function pointers or global functions, you can >never eliminate any global function, unless your application doesn't >call dlopen, or otherwise load anything dynamically, incl

Re: Merging identical functions in GCC

2006-09-18 Thread Mike Stump
On Sep 18, 2006, at 4:17 PM, Jed Davis wrote: As a convenient side-effect, setting breakpoints on only one variant will also still work. Well, not quite. For it to work nicely, you'd want the jmp version, and you'd want to defeat the gdb set breakpoint after prologue logic, and even then,

Re: Merging identical functions in GCC

2006-09-18 Thread Joe Buck
On Mon, Sep 18, 2006 at 12:09:36PM -0700, Mark Mitchell wrote: > Mike Stump wrote: > >On Sep 15, 2006, at 2:32 PM, Ross Ridge wrote: > >>Also, I don't think it's safe if you merge only functions in COMDAT > >>sections. > > > >Sure it is, one just needs to merge them as: > > > >variant1: nop > >var

Re: Merging identical functions in GCC

2006-09-18 Thread Jed Davis
Mike Stump <[EMAIL PROTECTED]> writes: > On Sep 15, 2006, at 2:32 PM, Ross Ridge wrote: >> Also, I don't think it's safe if you merge only functions in COMDAT >> sections. > > Sure it is, one just needs to merge them as: > > variant1: nop > variant2: nop > variant3: nop > [ ... ] > > this wa

Re: Merging identical functions in GCC

2006-09-18 Thread Mark Mitchell
Mike Stump wrote: On Sep 15, 2006, at 2:32 PM, Ross Ridge wrote: Also, I don't think it's safe if you merge only functions in COMDAT sections. Sure it is, one just needs to merge them as: variant1: nop variant2: nop variant3: nop [ ... ] this way important inequalities still work. Yes,

Re: Merging identical functions in GCC

2006-09-18 Thread Mike Stump
On Sep 15, 2006, at 2:32 PM, Ross Ridge wrote: Also, I don't think it's safe if you merge only functions in COMDAT sections. Sure it is, one just needs to merge them as: variant1: nop variant2: nop variant3: nop [ ... ] this way important inequalities still work. This requires multi

Re: Merging identical functions in GCC

2006-09-18 Thread Roman Kononov
On 09/15/2006 04:32 PM, Ross Ridge wrote: Also, I don't think it's safe if you merge only functions in COMDAT sections. Consider: #include template T foo(T a) { return a; } template T bar(T a) { return a; } int main() { assert((int (*

Re: Merging identical functions in GCC

2006-09-17 Thread Daniel Berlin
>You can believe what you like about the idea. Until you are willing >to implement something *you* believe will help, or at the least >explain how you forsee it being done safely (which Microsoft >doesn't!), it's very hard to take you seriously As I've already said, it can be made safe by commu

Re: Merging identical functions in GCC

2006-09-16 Thread Ross Ridge
Daniel Berlin writes >Do you really want me to sit here and knock down every single one of >your arguments? Why would you think I would've wanted your "No, it isn't" responses instead? >Your functions you are trying to optimize for multiple cpu >types and compiled with different flags may be outp

Re: Merging identical functions in GCC

2006-09-16 Thread Daniel Berlin
On 9/16/06, Ross Ridge <[EMAIL PROTECTED]> wrote: Ross Ridge writes: >Microsoft's implementation has proven that "stupid" byte comparions can >generate significant savings. Daniel Berlin wrtes: >No they haven't. So Microsoft and everyone who says they've got significant savings using it is lyin

Re: Merging identical functions in GCC

2006-09-16 Thread Gabriel Dos Reis
"Daniel Berlin" <[EMAIL PROTECTED]> writes: | > >No it can't. It has no idea what a function consists of other than a | > >bunch of bytes, in pretty much all cases. ... Stupid byte | > >comparisons of functions generally won't save you anything truly | > >interesting. | > | > Microsoft's implemen

Re: Merging identical functions in GCC

2006-09-16 Thread Ross Ridge
Ross Ridge writes: >Microsoft's implementation has proven that "stupid" byte comparions can >generate significant savings. Daniel Berlin wrtes: >No they haven't. So Microsoft and everyone who says they've got significant savings using it is lying? >But have fun implementing it in your linker, an

Re: Merging identical functions in GCC

2006-09-16 Thread Daniel Berlin
>No it can't. It has no idea what a function consists of other than a >bunch of bytes, in pretty much all cases. ... Stupid byte >comparisons of functions generally won't save you anything truly >interesting. Microsoft's implementation has proven that "stupid" byte comparions can generate signif

Re: Merging identical functions in GCC

2006-09-16 Thread Ross Ridge
Gabriel Dos Reis write: >Not very logn ago I spoke with the VC++ manager about this, and he >said that their implementation currently is not conforming -- but >they are working on it. The issue has to with f and f >required to have difference addresses -- which is violated by their >implementation

Re: Merging identical functions in GCC

2006-09-16 Thread Gabriel Dos Reis
Ross Ridge <[EMAIL PROTECTED]> writes: [...] | >>I think this is best done by linker which | >>can much more reliably compare the contents of functions to see if they | >>are the same. | > | >No it can't. It has no idea what a function consists of other than a | >bunch of bytes, in pretty much al

Re: Merging identical functions in GCC

2006-09-16 Thread Ross Ridge
Ross Ridge writes: >No, and I can't see how how you've came up with such an abusurd >misintepretation of what I said. As I said clearly and explicity, >the example I gave was where you'd want to use function merging. Daniel Berlin writes: >Whatever. Why would you turn on function merging if you

Re: Merging identical functions in GCC

2006-09-16 Thread Michael Popov
Daniel Berlin wrote: Actually, even for non-POD types, it catches a lot of templatized member functions that mainly depend on size (but they are still container classes). Just another ( maybe stupid :-) ) idea. What about "partial merge"? static int x = 0; void a() { printf( "aaa" ); x

Re: Merging identical functions in GCC

2006-09-16 Thread Michael Popov
Mark Mitchell wrote: Anyhow, I think that a combination of compiler/linker help and programmer help are useful. There are some cases where you can do this automatically, and others where you might need programmer help. Just as -ffast-math is useful, so might -fmerge-functions or __attribute_

Re: Merging identical functions in GCC

2006-09-15 Thread Daniel Berlin
On 9/15/06, Ross Ridge <[EMAIL PROTECTED]> wrote: Ross Ridge writes: >I don't think this is a good idea. With different compiler options the >same RTL can generate different assembly instructions. Consider the case >of compiling the same function multiple times with different names and >differ

Re: Merging identical functions in GCC

2006-09-15 Thread Ross Ridge
Ross Ridge writes: >I don't think this is a good idea. With different compiler options the >same RTL can generate different assembly instructions. Consider the case >of compiling the same function multiple times with different names and >different CPU architectures selected. You'd actually want

Re: Merging identical functions in GCC

2006-09-15 Thread Daniel Berlin
On 9/15/06, Ross Ridge <[EMAIL PROTECTED]> wrote: Ian Lance Taylor wrote: >I think Danny has a 75% implementation based on hashing the RTL for a >section and using that to select the COMDAT section signature. I don't think this is a good idea. With different compiler options the same RTL can ge

Re: Merging identical functions in GCC

2006-09-15 Thread Mark Mitchell
Laurent GUERBY wrote: On Fri, 2006-09-15 at 13:54 -0700, Ian Lance Taylor wrote: Laurent GUERBY <[EMAIL PROTECTED]> writes: For code sections (I assume read-only), isn't the linker always able to merge identical ones? What can the compiler do better than the linker? The linker can't merge just

Re: Merging identical functions in GCC

2006-09-15 Thread Laurent GUERBY
On Fri, 2006-09-15 at 13:54 -0700, Ian Lance Taylor wrote: > Laurent GUERBY <[EMAIL PROTECTED]> writes: > > For code sections (I assume read-only), isn't the linker always able to > > merge identical ones? What can the compiler do better than the linker? > > The linker can't merge just any identi

Re: Merging identical functions in GCC

2006-09-15 Thread Ross Ridge
Ian Lance Taylor wrote: >I think Danny has a 75% implementation based on hashing the RTL for a >section and using that to select the COMDAT section signature. I don't think this is a good idea. With different compiler options the same RTL can generate different assembly instructions. Consider th

Re: Merging identical functions in GCC

2006-09-15 Thread Seongbae Park
On 15 Sep 2006 13:54:00 -0700, Ian Lance Taylor <[EMAIL PROTECTED]> wrote: Laurent GUERBY <[EMAIL PROTECTED]> writes: > On Fri, 2006-09-15 at 09:27 -0700, Ian Lance Taylor wrote: > > I think Danny has a 75% implementation based on hashing the RTL for a > > section and using that to select the CO

Re: Merging identical functions in GCC

2006-09-15 Thread Daniel Berlin
It seems to me that most candidates for merging that could be eliminated by this approach are of the form std::container::some_method(...) Actually, even for non-POD types, it catches a lot of templatized member functions that mainly depend on size (but they are still container classes).

Re: Merging identical functions in GCC

2006-09-15 Thread Ian Lance Taylor
Laurent GUERBY <[EMAIL PROTECTED]> writes: > On Fri, 2006-09-15 at 09:27 -0700, Ian Lance Taylor wrote: > > I think Danny has a 75% implementation based on hashing the RTL for a > > section and using that to select the COMDAT section signature. Or the > > hashing can be done in the linker, but it

Re: Merging identical functions in GCC

2006-09-15 Thread Joe Buck
On Fri, 2006-09-15 at 09:27 -0700, Ian Lance Taylor wrote: > >> I think Danny has a 75% implementation based on hashing the RTL for a > >> section and using that to select the COMDAT section signature. Or the > >> hashing can be done in the linker, but it still needs compiler help to > >> know wh

Re: Merging identical functions in GCC

2006-09-15 Thread Daniel Berlin
On 9/15/06, Laurent GUERBY <[EMAIL PROTECTED]> wrote: On Fri, 2006-09-15 at 09:27 -0700, Ian Lance Taylor wrote: > I think Danny has a 75% implementation based on hashing the RTL for a > section and using that to select the COMDAT section signature. Or the > hashing can be done in the linker, bu

Re: Merging identical functions in GCC

2006-09-15 Thread Daniel Berlin
On 9/15/06, Michael Popov <[EMAIL PROTECTED]> wrote: Daniel Berlin wrote: > There are of course, collisions possible in the hash, and in the > absolute worst case, it is possible to end up with no warning but > wrong results. In particular, if you get a collision and the length > of the function

Re: Merging identical functions in GCC

2006-09-15 Thread Michael Popov
Daniel Berlin wrote: There are of course, collisions possible in the hash, and in the absolute worst case, it is possible to end up with no warning but wrong results. In particular, if you get a collision and the length of the functions happens to be the same, i don't believe the linker will com

Re: Merging identical functions in GCC

2006-09-15 Thread Laurent GUERBY
On Fri, 2006-09-15 at 09:27 -0700, Ian Lance Taylor wrote: > I think Danny has a 75% implementation based on hashing the RTL for a > section and using that to select the COMDAT section signature. Or the > hashing can be done in the linker, but it still needs compiler help to > know when it is perm

Re: Merging identical functions in GCC

2006-09-15 Thread Daniel Berlin
On 15 Sep 2006 09:27:32 -0700, Ian Lance Taylor <[EMAIL PROTECTED]> wrote: "Steven Bosscher" <[EMAIL PROTECTED]> writes: > Isn't this what you describe here the same as COMDAT? Not exactly. COMDAT means that the compiler can generate multiple instances of, say, the list functions, and only one

Re: Merging identical functions in GCC

2006-09-15 Thread Ian Lance Taylor
"Steven Bosscher" <[EMAIL PROTECTED]> writes: > Isn't this what you describe here the same as COMDAT? Not exactly. COMDAT means that the compiler can generate multiple instances of, say, the list functions, and only one version will be included in the final executable. However, if you use both

Re: Merging identical functions in GCC

2006-09-15 Thread Michael Popov
Steven Bosscher wrote: Isn't this what you describe here the same as COMDAT? Well, MS VC have an option "enable COMDAT folding" - which turns ON merging of identical functions. BTW, this is linker option... should it be addressed to GCC's LTO? I'm not sure if function body is equivalent to "COM

Re: Merging identical functions in GCC

2006-09-15 Thread Steven Bosscher
Hi, Isn't this what you describe here the same as COMDAT? Gr. Steven On 9/15/06, Michael Popov <[EMAIL PROTECTED]> wrote: Hello! There is very good blog post about the problem: http://vladimir_prus.blogspot.com/2005/03/duplicate-function-bodies.html > A couple of days ago I've learned that