accelerating ruby with llvmllvm.org/devmtg/2009-10/phoenix_acceleratingruby.pdf · every context is...

135
Accelerating Ruby with LLVM Oct 2, 2009 Evan Phoenix Tuesday, October 6, 2009

Upload: others

Post on 12-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Accelerating Rubywith LLVM

Oct 2, 2009

Evan Phoenix

Tuesday, October 6, 2009

Page 2: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

RUBY

Tuesday, October 6, 2009

Page 3: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Strongly, dynamically typed

RUBY

Tuesday, October 6, 2009

Page 4: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Unified Model

RUBY

Tuesday, October 6, 2009

Page 5: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Everything is an object

RUBY

Tuesday, October 6, 2009

Page 6: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

3.class # => FixnumRUBY

Tuesday, October 6, 2009

Page 7: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Every code context is equal

RUBY

Tuesday, October 6, 2009

Page 8: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Every context is a method

RUBY

Tuesday, October 6, 2009

Page 9: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Garbage Collected

RUBY

Tuesday, October 6, 2009

Page 10: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

A lot of syntax

RUBY

Tuesday, October 6, 2009

Page 11: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Every code context is equalEvery context is a method

Garbage collectedA lot of syntax

RUBYStrongly, dynamically typed

Unified modelEverything is an object

3.class

Tuesday, October 6, 2009

Page 12: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Rubinius

Tuesday, October 6, 2009

Page 13: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Started in 2006

Rubinius

Tuesday, October 6, 2009

Page 14: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Build a ruby environment for fun

Rubinius

Tuesday, October 6, 2009

Page 15: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Unlike most “scripting” languages,write as much in ruby as possible

Rubinius

Tuesday, October 6, 2009

Page 16: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Core functionality of perl/python/ruby in C, NOT in their respective language.

Rubinius

Tuesday, October 6, 2009

Page 17: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

C => ruby => C => rubyRubinius

Tuesday, October 6, 2009

Page 18: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Language boundaries suck

Rubinius

Tuesday, October 6, 2009

Page 19: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Started in 2006Built for fun

Turtles all the way down

Rubinius

Tuesday, October 6, 2009

Page 20: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Evolution

Tuesday, October 6, 2009

Page 21: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

100% ruby prototype running on 1.8

Evolution

Tuesday, October 6, 2009

Page 22: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Hand translated VM to C

Evolution

Tuesday, October 6, 2009

Page 23: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Rewrote VM in C++

Evolution

Tuesday, October 6, 2009

Page 24: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Switch away from stackless

Evolution

Tuesday, October 6, 2009

Page 25: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Experimented with handwrittenassembler for x86

Evolution

Tuesday, October 6, 2009

Page 26: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Switch to LLVM for JIT

Evolution

Tuesday, October 6, 2009

Page 27: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Evolution100% ruby prototype

Hand translated VM to CRewrote VM in C++

Switch away from stacklessExperiment with assembler

Switch to LLVM for JIT

Tuesday, October 6, 2009

Page 28: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Features

Tuesday, October 6, 2009

Page 29: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Bytecode VM

Features

Tuesday, October 6, 2009

Page 30: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Simple interface to native code

Features

Tuesday, October 6, 2009

Page 31: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Accurate, generational garbage collector

Features

Tuesday, October 6, 2009

Page 32: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Integrated FFI API

Features

Tuesday, October 6, 2009

Page 33: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

FeaturesBytecode VM

Interface to native codeGenerational GC

Integrated FFI

Tuesday, October 6, 2009

Page 34: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Benchmarks

Tuesday, October 6, 2009

Page 35: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

def foo() ary = [] 100.times { |i| ary << i }end

300,000 timesTuesday, October 6, 2009

Page 36: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

0

2.25

4.5

6.75

9

1.8 1.9 rbx rbx jit rbx jit +blocks

2.59

3.60

5.90

5.30

8.02

Seconds

Tuesday, October 6, 2009

Page 37: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

def foo() hsh = {} 100.times { |i| hsh[i] = 0 }end

100,000 timesTuesday, October 6, 2009

Page 38: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

0

7.5

15

22.5

30

1.8 1.9 rbx rbx jit rbx jit +blocks

12.0112.54

25.36

5.264.85

Seconds

Tuesday, October 6, 2009

Page 39: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

def foo() hsh = { 47 => true } 100.times { |i| hsh[i] }end

100,000 timesTuesday, October 6, 2009

Page 40: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

0

1.75

3.5

5.25

7

1.8 1.9 rbx rbx jit rbx jit +blocks

2.662.68

6.26

2.09

3.64

Seconds

Tuesday, October 6, 2009

Page 41: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Early LLVM Usage

Tuesday, October 6, 2009

Page 42: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Compiled all methods up front

Early LLVM Usage

Tuesday, October 6, 2009

Page 43: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Simple opcode-to-function translationwith inlining

Early LLVM Usage

Tuesday, October 6, 2009

Page 44: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Startup went from 0.3s to 80s

Early LLVM Usage

Tuesday, October 6, 2009

Page 45: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Early LLVM UsageCompiled all methods upfront

Simple opcode-to-function translationStartup from 0.3s to 80s

Tuesday, October 6, 2009

Page 46: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

True JIT

Tuesday, October 6, 2009

Page 47: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

JIT Goals

True JIT

Tuesday, October 6, 2009

Page 48: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

JIT Goals

Choose methods that benefit the most

True JIT

Tuesday, October 6, 2009

Page 49: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

JIT Goals

Compiling has minimum impact on performance

True JIT

Tuesday, October 6, 2009

Page 50: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

JIT Goals

Ability to make intelligent frontend decisions

True JIT

Tuesday, October 6, 2009

Page 51: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Choosing Methods

Tuesday, October 6, 2009

Page 52: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Simple call counters

Choosing Methods

Tuesday, October 6, 2009

Page 53: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

When counter trips, the fun starts

Choosing Methods

Tuesday, October 6, 2009

Page 54: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Room for improvement

Choosing Methods

Tuesday, October 6, 2009

Page 55: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Room for improvement

Increment counters in loops

Choosing Methods

Tuesday, October 6, 2009

Page 56: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Room for improvement

Weigh different invocations differently

Choosing Methods

Tuesday, October 6, 2009

Page 57: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Simple countersTrip the counters, do it

Choosing Methods

Room for improvementIncrement in loopsWeigh invocations

Tuesday, October 6, 2009

Page 58: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Which Method?

Tuesday, October 6, 2009

Page 59: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Leaf methods trip quickly

Which Method?

Tuesday, October 6, 2009

Page 60: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Leaf methods trip quickly

Consider the whole callstack

Which Methods?

Tuesday, October 6, 2009

Page 61: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Leaf methods trip quickly

Pick a parent expecting inlining

Which Methods?

Tuesday, October 6, 2009

Page 62: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Leaf methods tripConsider the callstack

Find a parent

Which Method?

Tuesday, October 6, 2009

Page 63: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Minimal Impact

Tuesday, October 6, 2009

Page 64: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

After the counters trip

Minimal Impact

Tuesday, October 6, 2009

Page 65: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Queue the method

Minimal Impact

Tuesday, October 6, 2009

Page 66: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Background thread drains queue

Minimal Impact

Tuesday, October 6, 2009

Page 67: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Frontend, passes, codegen in background

Minimal Impact

Tuesday, October 6, 2009

Page 68: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Install JIT’d function

Minimal Impact

Tuesday, October 6, 2009

Page 69: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Install JIT’d function

Requires GC interaction

Minimal Impact

Tuesday, October 6, 2009

Page 70: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Trip the countersQueue the method

Minimal ImpactCompile in backgroundInstall function pointer

Tuesday, October 6, 2009

Page 71: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Good Decisions

Tuesday, October 6, 2009

Page 72: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Naive translation yields fixed improvement

Good Decisions

Tuesday, October 6, 2009

Page 73: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Performance shifts to method dispatch

Good Decisions

Tuesday, October 6, 2009

Page 74: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Improve optimization horizon

Good Decisions

Tuesday, October 6, 2009

Page 75: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inline using type feedback

Good Decisions

Tuesday, October 6, 2009

Page 76: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Naive translation sucksInline using type feedback

Good Decisions

Performance in dispatchImprove optimizations

Tuesday, October 6, 2009

Page 77: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Type Feedback

Tuesday, October 6, 2009

Page 78: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Frontend translates to IR

Type Feedback

Tuesday, October 6, 2009

Page 79: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Read InlineCache information

Type Feedback

Tuesday, October 6, 2009

Page 80: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

InlineCaches contain profiling info

Type Feedback

Tuesday, October 6, 2009

Page 81: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Use profiling to drive inlining!

Type Feedback

Tuesday, October 6, 2009

Page 82: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Frontend generates IRReads InlineCaches

Type FeedbackInlineCaches have profiling

Use profiling to drive inlining!

Tuesday, October 6, 2009

Page 83: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inlining

Tuesday, October 6, 2009

Page 84: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Profiling info shows a dominant class

Inlining

Tuesday, October 6, 2009

Page 85: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

21%

1 class98%

Tuesday, October 6, 2009

Page 86: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Lookup method in compiler

Inlining

Tuesday, October 6, 2009

Page 87: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

For native functions, emit direct call

Inlining

Tuesday, October 6, 2009

Page 88: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

For FFI, inline conversions and call

Inlining

Tuesday, October 6, 2009

Page 89: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Emit direct calls if possible

InliningFind dominant class

Lookup method

Tuesday, October 6, 2009

Page 90: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inlining Ruby

Tuesday, October 6, 2009

Page 91: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Policy decides on inlining

Inlining Ruby

Tuesday, October 6, 2009

Page 92: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Drive sub-frontend at call site

Inlining Ruby

Tuesday, October 6, 2009

Page 93: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

All inlining occurs in the frontend

Inlining Ruby

Tuesday, October 6, 2009

Page 94: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Generated IR preserves runtime data

Inlining Ruby

Tuesday, October 6, 2009

Page 95: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Generated IR preserves runtime data

GC roots, backtraces, etc

Inlining Ruby

Tuesday, October 6, 2009

Page 96: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

No AST between bytecode and IR

Inlining Ruby

Tuesday, October 6, 2009

Page 97: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

No AST between bytecode and IR

Fast, but limits the ability to generate better IR

Inlining Ruby

Tuesday, October 6, 2009

Page 98: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Policy decidesDrive sub-frontend

Inlining RubyPreserve runtime dataGenerates fast, ugly IR

Tuesday, October 6, 2009

Page 99: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

LLVM

Tuesday, October 6, 2009

Page 100: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

IR uses operand stack

LLVM

Tuesday, October 6, 2009

Page 101: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

IR uses operand stack

Highlevel data flow not in SSA

LLVM

Tuesday, October 6, 2009

Page 102: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

IR uses operand stack

Passes eliminate redundencies

LLVM

Tuesday, October 6, 2009

Page 103: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

IR uses operand stack

Makes GC stack marking easy

LLVM

Tuesday, October 6, 2009

Page 104: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

IR uses operand stack

nocapture improves propagation

LLVM

Tuesday, October 6, 2009

Page 105: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Exceptions via sentinal value

LLVM

Tuesday, October 6, 2009

Page 106: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Exceptions via sentinal value

Nested handlers use branches for control

LLVM

Tuesday, October 6, 2009

Page 107: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Exceptions via sentinal value

Inlining exposes redundant checks

LLVM

Tuesday, October 6, 2009

Page 108: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inline guards

LLVM

Tuesday, October 6, 2009

Page 109: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inline guards

Simple type guards

LLVM

Tuesday, October 6, 2009

Page 110: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

if(obj->class->class_id == <integer constant>) {

Tuesday, October 6, 2009

Page 111: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inline guards

Custom AA pass for guard elimination

LLVM

Tuesday, October 6, 2009

Page 112: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inline guards

Teach pointsToConstantMemory about...

LLVM

Tuesday, October 6, 2009

Page 113: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

if(obj->class->class_id == <integer constant>) {

Tuesday, October 6, 2009

Page 114: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

if(obj->class->class_id == <integer constant>) {

Tuesday, October 6, 2009

Page 115: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Maximizing constant propagation

LLVM

Tuesday, October 6, 2009

Page 116: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Maximizing constant propagation

Type failures shouldn’t contribute values

LLVM

Tuesday, October 6, 2009

Page 117: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

if(obj->class->class_id == 0x33) { val = 0x7;} else { val = send_msg(state, obj, ...);}

Tuesday, October 6, 2009

Page 118: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

if(obj->class->class_id == 0x33) { val = 0x7;} else { return uncommon(state);}

Tuesday, October 6, 2009

Page 119: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Maximizing constant propagation

Makes JIT similar to tracing

LLVM

Tuesday, October 6, 2009

Page 120: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Use overflow intrinsics

LLVM

Tuesday, October 6, 2009

Page 121: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Use overflow intrinsics

Custom pass to fold constants arguments

LLVM

Tuesday, October 6, 2009

Page 122: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

AA knowledge for tagged pointers

LLVM

Tuesday, October 6, 2009

Page 123: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

AA knowledge of tagged pointers

0x5 is 2 as a tagged pointer

LLVM

Tuesday, October 6, 2009

Page 124: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Not in SSA formSimplistic exceptions

Inlining guards

LLVMMaximize constants

Use overflowTagged pointer AA

Tuesday, October 6, 2009

Page 125: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Issues

Tuesday, October 6, 2009

Page 126: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

How to link with LLVM?

Issues

Tuesday, October 6, 2009

Page 127: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

How to link with LLVM?

An important SCM issue

Issues

Tuesday, October 6, 2009

Page 128: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Ugly, confusing IR from frontend

Issues

Tuesday, October 6, 2009

Page 129: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

instcombine confuses basicaa

Issues

Tuesday, October 6, 2009

Page 130: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Operand stack confuses AA

Issues

Tuesday, October 6, 2009

Page 131: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Inability to communicate semantics

Issues

Tuesday, October 6, 2009

Page 132: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Object* new_object(state)

Tuesday, October 6, 2009

Page 133: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Returned pointer aliases nothing

Only modifies state

If return value is unused, remove the call

Semi-pure?

Tuesday, October 6, 2009

Page 134: Accelerating Ruby with LLVMllvm.org/devmtg/2009-10/Phoenix_AcceleratingRuby.pdf · Every context is a method Garbage collected A lot of syntax RUBY Strongly, dynamically typed Unified

Ugly IRLinking with LLVM

IssuesAA confusion

Highlevel semantics

Tuesday, October 6, 2009