| Store | Cart

Re: [TCLCORE] Tcl GSoC 2011 - Ideas anyone ?

From: Frédéric Bonnet <fred...@free.fr>
Mon, 14 Mar 2011 23:43:01 +0100
Le 11/03/2011 06:37, Karl Lehenbauer a écrit :
>     * If you have a genius type and want to give her something she'll probably>       fail at, then a project to compile Tcl bytecode on the fly into native>       code using LLVM would be really badass. (I'm not a bytecode internals guy>       and I know the variable tracing issue really kills a lot of the>       optimization possibilities, so I don't know how viable this is.)

Trace tree compiling might solve these issues. We already have some of the 
infrastructure for experimentation with [trace add execution] as well as 
bytecode introspection. As this technique inlines procedure calls, this 
addresses Tcl traces and dynamic command dispatching (inc. command redefinition) 
quite well IMHO. For example if you have a read trace on a variable used in a 
loop body, you're likely to trigger the trace at each iteration until it is 
removed. Since commands are inlined by the TTC, this means the trace body would 
be inlined & unrolled as part of the loop, and the guard condition would fail 
when the trace is deleted.

See:
http://andreasgal.wordpress.com/2008/06/02/trace-trees-faq/

So in the following code:

     proc log {name1 name2 op} {upvar $name1 v; puts stderr "$name1 becomes $v"}
     trace add variable sum write log
     set sum 0
     for {set i 0} {$i < 10} {incr i} {
         incr sum $i
     }

the loop becomes (pseudocode, you get the idea):

     /* type-inferred local variables at trace recording time */
     int sum = 0;
     int i = 0;

     /* guard conditions */
     while (i < 10 && isIntrinsic("incr") && isIntrinsic("puts")
             && traceVarExists("sum", "write", "log"))
     /* loop body */
     {
	/* incr was an intrinsic at trace recording time */
         sum += i;

         /* inlined trace call at trace recording time */
         {
             /* type-inferred arguments and local variables */
             string name1 = "sum";
             int v = sum;

             /* command body */

             /* puts was an intrinsic at trace recording time */
             printf("%s becomes %d", name1, v);
         }

	/* incr was an intrinsic at trace recording time */
         i++;
     }

     /* back to interpreter & trace recorder */
     setIntVar("sum", sum);

There can be extra steps of optimization to remove unnecessary guard conditions, 
but this can also be done by the compilation framework (e.g. LLVM).

So here you can see that the code remains valid as long as the trace exists and 
the [incr] and [puts] commands don't get redefined. Since this is the case, the 
optimizer may remove the noise and end up with this core code:

     int sum = 0;
     int i = 0;
     while (i < 10) {
         sum += i;
         printf("%s becomes %d", "sum", sum);
         i++;
     }
     setIntVar("sum", sum);

But even without these optimization steps, the result is way better that 
anything traditional compiling techniques can provide in a dynamic environment.


------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
Tcl-Core mailing list
Tcl-...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tcl-core

Recent Messages in this Thread
Andreas Kupries Mar 10, 2011 10:52 pm
Larry McVoy Mar 11, 2011 01:15 am
Robert Mar 11, 2011 01:35 am
Kevin Walzer Mar 11, 2011 01:52 am
Larry McVoy Mar 11, 2011 02:12 am
sigzero Mar 11, 2011 02:22 am
Jeff Hobbs Mar 16, 2011 12:52 am
Damon Courtney Mar 16, 2011 01:07 am
Kevin Walzer Mar 16, 2011 01:45 am
Alexandre Ferrieux Mar 16, 2011 08:13 am
Gustaf Neumann Mar 16, 2011 10:57 am
Kevin Walzer Mar 16, 2011 02:25 pm
Larry McVoy Mar 17, 2011 01:21 am
Kevin Walzer Mar 17, 2011 02:15 am
Jeff Hobbs Mar 17, 2011 06:18 pm
Andy Goth Mar 23, 2011 05:15 pm
Larry McVoy Mar 23, 2011 06:03 pm
L.W...@surrey.ac.uk Mar 23, 2011 06:12 pm
Jeff Hobbs Mar 23, 2011 06:14 pm
Massimo Manghi Mar 24, 2011 08:43 am
Kevin Walzer Mar 24, 2011 01:51 pm
Adrian Robert Mar 23, 2011 09:18 am
Andreas Kupries Mar 23, 2011 04:37 pm
Larry McVoy Mar 23, 2011 04:47 pm
Adrian Robert Mar 26, 2011 10:48 am
Larry McVoy Mar 26, 2011 04:05 pm
Pat Thoyts Mar 26, 2011 10:05 pm
Twylite Mar 27, 2011 03:22 pm
Donal K. Fellows Mar 24, 2011 06:47 am
L.W...@surrey.ac.uk Mar 24, 2011 07:49 am
Kevin Walzer Mar 11, 2011 02:41 am
Larry McVoy Mar 11, 2011 02:49 am
L.W...@surrey.ac.uk Mar 11, 2011 11:03 am
Robert Mar 11, 2011 02:56 am
Larry McVoy Mar 11, 2011 04:07 am
Karl Lehenbauer Mar 11, 2011 05:37 am
Neil Madden Mar 11, 2011 06:59 am
Massimo Manghi Mar 11, 2011 10:45 am
Larry McVoy Mar 11, 2011 02:24 pm
Frédéric Bonnet Mar 14, 2011 10:43 pm
Jeff Hobbs Mar 16, 2011 12:45 am
jemptymethod Mar 12, 2011 12:44 am
Mark Roseman Mar 15, 2011 05:00 pm
Damon Courtney Mar 15, 2011 09:06 pm
Steve Landers Mar 15, 2011 10:54 pm
Jeff Hobbs Mar 16, 2011 12:16 am
Harald Oehlmann Mar 16, 2011 08:03 am
Arnulf Wiedemann Mar 16, 2011 10:14 am
Twylite Mar 11, 2011 07:37 am
Pat Thoyts Mar 13, 2011 10:25 pm
Thomas Perschak Mar 13, 2011 07:48 am
Arnulf Wiedemann Mar 13, 2011 03:52 pm
Will Duquette Mar 13, 2011 04:20 pm
Arnulf Wiedemann Mar 13, 2011 04:39 pm
Arjen Markus Mar 11, 2011 07:53 am
Andy Goth Mar 11, 2011 05:30 pm
Andreas Kupries Mar 11, 2011 05:29 pm
Joe English Mar 13, 2011 05:49 pm
Will Duquette Mar 13, 2011 08:51 pm
Messages in this thread