Devirtualization of method calls

Preview:

DESCRIPTION

JIT and method call devirtualization Slide from meet up, 18 Apr 2013, Moscow

Citation preview

Devirtualization of method calls

Dirty tricks of JIT compilers

Alexey Ragozinalexey.ragozin@gmail.com

Apr 2013

Old good C++

010110010010101010100110101010100101010101001010101010101010101010101010101010101010101010100010

00: methodA

02: methodC

03: methodD

CODEOBJECT

VTABLE

01: methodB

Simple case

Old good C++

010110010010101010100110101010100101010101001010101010101010101010101010101010101010101010100010111010100100011110000010101001010100

00: methodA

02: methodC

03: methodD

CODEOBJECT

VTABLE

01: methodB

00: methodX

02: methodZ

01: methodY

VTABLE

Multiple inheritance

Old good C++Multiple inheritance – more fun

A

B C

D D

A

B C

D

Vs

Problems

Two memory reads before jump Memory access is serialized instruction pipeline is blocked

Dynamic languages

Every call is virtual No predefined classes Multiple inheritance

Nightmare for super scalar CPU

Call site polymorphism

A particular call sitein most case

would be callingexactly one method instance

Call sites are mostly monomorthic!

Exploiting monomorthism

Tracing JIT JavaScript – Mozila TraceMonkey Python – PyPy

Profiling for whole method JIT JVM

Tracing JIT

Interpretation phase Record actions, record branch conditions

Compiling trace Actions compiled into branchless machine code Guards added for conditions If guard is broken – fall back to interpretation

Tracing JIT

Mozila’s TraceMonkey1. Incremental Dynamic Code Generation with Trace Trees

http://www.ics.uci.edu/~franz/Site/pubs-pdf/ICS-TR-06-16.pdf

PyPy RPython – restrict python dialect compiling to C Rpython based interpreter trace JITed by runtime1. http://tratt.net/laurie/research/pubs/papers/bolz_tratt__the_impact_of_metatrac

ing_on_vm_design_and_implementation.pdf

Tracing JIT

Limitation Trace is EXPENSIVE Falling out of path is EXPENSIVE A lot of code is executed just once

– wasting resources on tracing

Whole method JIT

JVM Classic whole method JIT Profile “morphism” of call sites Multi tier compiler Dynamics recompilation / decompilation

Whole method JIT

Call sites Monomorphic Bimorphic Megamorphic

Invoke dynamic

Problem Interpreters tends to produce

megamorphic sites

Invoke dynamic Detach call site into manageable object

More JVM back magic

On stack replacement Code for method could be replaced without

reentering to a method

Scalar replacement Object not leaving method scope

can be reduced to a number of local variables

THANK YOU

alexey.ragozin@gmail.com

Recommended