16
Devirtualization of method calls Dirty tricks of JIT compilers Alexey Ragozin [email protected] Apr 2013

Devirtualization of method calls

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Devirtualization of method calls

Devirtualization of method calls

Dirty tricks of JIT compilers

Alexey [email protected]

Apr 2013

Page 2: Devirtualization of method calls

Old good C++

010110010010101010100110101010100101010101001010101010101010101010101010101010101010101010100010

00: methodA

02: methodC

03: methodD

CODEOBJECT

VTABLE

01: methodB

Simple case

Page 3: Devirtualization of method calls

Old good C++

010110010010101010100110101010100101010101001010101010101010101010101010101010101010101010100010111010100100011110000010101001010100

00: methodA

02: methodC

03: methodD

CODEOBJECT

VTABLE

01: methodB

00: methodX

02: methodZ

01: methodY

VTABLE

Multiple inheritance

Page 4: Devirtualization of method calls

Old good C++Multiple inheritance – more fun

A

B C

D D

A

B C

D

Vs

Page 5: Devirtualization of method calls

Problems

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

Page 6: Devirtualization of method calls

Dynamic languages

Every call is virtual No predefined classes Multiple inheritance

Nightmare for super scalar CPU

Page 7: Devirtualization of method calls

Call site polymorphism

A particular call sitein most case

would be callingexactly one method instance

Call sites are mostly monomorthic!

Page 8: Devirtualization of method calls

Exploiting monomorthism

Tracing JIT JavaScript – Mozila TraceMonkey Python – PyPy

Profiling for whole method JIT JVM

Page 9: Devirtualization of method calls

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

Page 10: Devirtualization of method calls

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

Page 11: Devirtualization of method calls

Tracing JIT

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

– wasting resources on tracing

Page 12: Devirtualization of method calls

Whole method JIT

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

Page 13: Devirtualization of method calls

Whole method JIT

Call sites Monomorphic Bimorphic Megamorphic

Page 14: Devirtualization of method calls

Invoke dynamic

Problem Interpreters tends to produce

megamorphic sites

Invoke dynamic Detach call site into manageable object

Page 15: Devirtualization of method calls

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

Page 16: Devirtualization of method calls

THANK YOU

[email protected]