[MLton] fixing -codegen c -profile time for the release

skaller skaller@users.sourceforge.net
Thu, 17 Nov 2005 20:32:31 +1100

On Wed, 2005-11-16 at 17:45 -0500, Matthew Fluet wrote:
> > It isn't luck :) I mean I am lucky, but the design
> > is almost certain to prevent optimisation.
> You're still walking on thin ice.

Yeah, my feet are cut up, that's what you get
with Bleeding Edge technology :)

> Same with MLton: the C functions we produce can be many hundreds of lines 
> long, much too large to be inlined into any other piece of code.

Since both Felix and MLton are whole program analysers, it isn't
entirely suprising many things are done the same.

> Florian's example was very small to demonstrate the problem.  

Yes, but that is good.

> But the 
> realization of the problem in MLton's emitted code shows that gcc is being 
> a lot more aggressive in it's optimizations.  Note that gcc is well within 
> its rights to transform:
>   void foo() {
>     body
>   }
> to
>   void foo() {
>     static int _z = 1;
>     _z = (_z + 1) % 2;
>     if (_z) {
>       body
>     } else {
>       copy_body
>     }
>   }
> where 'copy_body' is identical to 'body' except that C labels are renamed. 

Indeed, gcc could do any kind of things, if you are not writing
code strictly according to the language specifications.

In the above case, however, you're not quite correct:

	registry *get_registry() {
		static registry *p = NULL;
		return p?p: (p = new registry);

gcc would have to lift the pointer p out of the body before
duplication. This trick is used extensively in C++ to dynamically
initialise global variables in the correct order.

But I see no way to use this with 'assembler labels' problem.

> So, I don't see any way to prevent gcc from duplicating code, though you 
> can easily prevent gcc from dropping code.

Right. I agree. It cannot duplicate globals, but it doesn't 
know assembler labels are global.

However I am a bit surprised MLton ran into this -- but no,
I believe it and don't really want to see the real world
example :)

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net