[Vm-dev] Interpreter>>isContextHeader: optimization

bryce at kampjes.demon.co.uk bryce at kampjes.demon.co.uk
Sun Mar 1 21:46:28 UTC 2009


Eliot Miranda writes:
 >  Brice,
 >     please forgive my earlier reply which I realise was extraordinarily rude
 > and unnecessarily critical.  Let me try and engage more constructively.  I
 > think I'll be making the same points but hopefulyl I'll do so while being
 > less of an a***hole.
 > 
 > On Mon, Feb 23, 2009 at 1:49 PM, <bryce at kampjes.demon.co.uk> wrote:
 > 
 > >
 > > Eliot Miranda writes:
 > >  >  On Sun, Feb 22, 2009 at 12:54 PM, <bryce at kampjes.demon.co.uk> wrote:
 > >  > > All you need is the optimiser to run early in compilation for it to be
 > >  > > portable.
 > >  >
 > >  >
 > >  > ...and for it to be untimely.  An adaptive optimizer by definition needs
 > > to
 > >  > be running intermittently all the time.  It optimizes what is happening
 > > now,
 > >  > not what happened at start-up.
 > >
 > > Exupery runs as a Smalltalk background thread, it already uses dynamic
 > > feed back to inline some primitives including #at: and #at:put.
 > 
 > 
 > The background thread was used by Typed Smalltalk and is also used by some
 > Java jits.  But how does it running in a background thread help it be
 > portable?  Surely if it targets native code it needs to be split into a
 > front-end and a back-end of which only the front end will be portable right?

Most of the code in the back end is portable too. Exupery's back end
is split into three stages, instruction selection, register
allocation, then assembly. The register allocator is the biggest and
most complex part of Exupery so far and is portable.

Running as a background thread doesn't make it portable but it does
make it more timely than a compile on load system.

 > 
 > >  > > I see only one sixth of the time going into context creation for the
 > >  > > send benchmark which is about as send heavy as you can get. That's
 > >  > > running native code at about twice Squeak's speed. Also there's still
 > >  > > plenty of inefficiency in Exupery's call return sequences.
 > >
 > 
 > As your VM gets faster so that 1/6th will loom ever larger.  If you triple
 > the speed of your VM while keeping that same context creation scheme then
 > that 1/6 will become 1/2 of entire execution time.  So if you want truly
 > high performance you're going to have to tackle context elimination at some
 > stage.  Since it is so integral to the central issue of call/return design I
 > would encourage you to address it earlier rather than later.
 > 
 > 
 > >
 > >  >
 > >  >
 > >  > So you could get a 17% speedup if you could remove the context overhead.
 > >  >  That's quite a tidy gain.  I see a 26% increase in benchFib performance
 > >  > between base Squeak and the StackVM with no native code at all.
 > >  >
 > >  > What are the inefficiences in Exupery's call return sequences?
 > >
 > > Exupery uses a C call sequence so it's easy to enter from the
 > > interpreter, that C call frame is torn down when exiting each
 > > compiled method then re-created when reentering native code. That's
 > > a complete waste when going from one native method to another.
 > 
 > 
 > One sage piece of advice is to optimize for the common case.  Try and make
 > the common case as fast as possible.  You know this since you're also
 > interested in adaptive optimization which is fundamentally to do with
 > optimizing the common case.  So design your calling convention around
 > machine-code to machine-code calls and make the uncommon
 > interpreter/machine-code call do the necessary work to interface with the
 > calling-convention not the other way around.  The dog should wag the tail
 > (although try telling my father-in-law's labrador puppy that).
 > 
 > The way I've done this in Cog is to generate a trivial piece of machine
 > code, a thunk/trampoline etc, that I actually call an enilopmart because it
 > jumps from the interpreter/run-time into machine-code whereas jumps in the
 > other direction are via trampolines.  The interpreter uses this by pushing
 > the pc of the first instruction past the in-line cache checking code in the
 > method, followed by the values of the register(s) that need loading and then
 > calls the enilopmart. The enilopmart assigns stack and frame pointers with
 > that of the machine-code frame being called, pops all the register values
 > that are live on entry to the method off the stack and returns.  The return
 > is effectively a jump to the start of the machine-code method.

Why does Cog still rely on the interpreter? Is this just a
bootstrapping phase?

 > To arrange that returns form machine-code frames don't have to check for a
 > return to the interpreter the interpreter saves its own instruction pointer
 > in a slot in its frame, and substitutes the address of a routine that
 > handles returning to the interpreter as the return address.  So when the
 > machine code frame returns it'll return to the trampoline that retrieves the
 > saved instruction pointer form the slot and longjmps back to the
 > interpreter.  I use a lngjmp to avoid stack growth in any dance between
 > interpreter and machine code.
 > 
 > 
 > > Also the send/return sequence isn't yet that optimised, there's still
 > > plenty of inefficiencies due to lack of addressing modes etc and because
 > > it's fairly naive translation of the interpreters send code.
 > 
 > 
 > I would sit down and draw what you want the calling convention to look like
 > and do it sooner rather than later.  This is crucial to overall performance
 > until you solve the more difficult problem of doing significant adaptive
 > optimization so that call/return is eliminated.  However, as mentioned
 > previously eliminating call/return isn't such a great idea per se and so
 > keeping a fast call/return sequence is probably a very good idea anyway.
 >  Its not as if modern processors don't do call/return well.

For Exupery to make sense it needs significant adaptive optimisation
to expose enough code to allow heavy optimisation to justify the
slower compiler. Otherwise it would make more sense to move the
compiler into the VM and allow compilation for all execution like VW.

The current system's primary goal is enable the development of the
adaptive optimisation. I'll tune to provide decent performance now but
not if it makes it harder to add key features later.

I've thought about using a context stack several times over the years.
The key benefits are faster returns and possibly faster
de-optimisation of inlined contexts. After inlining it's possible that
a code change will break an optimisation. If an inlined method is
modified then it will continue to be entered until all contexts have
died or it is actively removed. Removing inlined contexts from object
memory requires a full memory scan to find them (allInstances).

 > 17% would be rather optimistic, some of the work required to set up a
 > > context will always be required. Temporaries will still need to be
 > > nilled out etc.
 > 
 > 
 > Aim higher :)  I'm hoping for 10x current Squeak performance for
 > Smalltalk-intensive benchmarks some time later this year.

My original and current aim is double VW's performance or be roughly
equivalent to C.

Bryce


More information about the Vm-dev mailing list