12

libFirm, postgres, JIT compilation and forthase/firm-postgres-jit-forth.pdf · Postgres is in need of JIT compilation Query plans (trees) are interpreted by the executor By calling

Embed Size (px)

Citation preview

libFirm, postgres, JIT compilation and forth

[email protected]

May 27, 2016

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 1 / 12

libFirm

Compiler library providing IR, optimiziations and code generatorsResearch project at ipd.kit.edu, LGPL (lib�rm), GPL (frontends)

libFirm

cparser

Firm

ARM(alpha)

AMD64(beta)

SPARC

IA32optimizations

Java

X10

brainfuck

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 2 / 12

Postgres is in need of JIT compilation

Query plans (trees) are interpreted by the executor

By calling function pointers in plan nodesThe invoked functions themselves again invoke function pointers

To evaluate expression trees

To evaluate operators

For access method strategy functions

Some calls via funcall API ; even more overhead

PL/PgSQL is evaluated basically the same

E.g. comparison (int > int) may require thousands of instructionswhere it optimally would be only a few

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 3 / 12

Same algorithm implemented in C and PL/PgSQL

psql=> select proname, levenshtein(proname, 'levenstein')

from pg_proc order by 2 limit 1;

proname | levenshtein

-------------+-------------

levenshtein | 1

Time: 2.498 ms

psql=> select proname, plpgsql_lev(proname, 'levenstein')

from pg_proc order by 2 limit 1;

proname | plpgsql_lev

-------------+-------------

levenshtein | 1

Time: 837.526 ms

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 4 / 12

Competing work

Vitesse Data is selling a JIT-compiling postgres using LLVM

Unpublished work by ISP RAS using LLVM in postgres

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 5 / 12

JIT compilation with libFirm

Nobody's done JIT with libFirm before

Wrote a JIT forth system to study feasibility

Excursion: forth systems

Primitve, but usable (unlike brainfuck)

Read words which operate on a parameter stack

> 1 1 + .

2

Language can be extended with new words

> : foo 1 1 + . ;

> foo

2

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 6 / 12

Firmforth recipe

1 Keep IR for C-implemented words around

2 Model function calls in FIRM instead of building arrays of pointers tointerpret

3 Run libFirm-provided optimizations

4 Run libFirm code generator

5 Invoke assembler/linker

6 Link the shared object into the running program via dlopen()

7 Look up new function via dlsym() and put it in dictionary

8 Keep IR of new word around

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 7 / 12

Firmforth graphically

firmforth.c

cparser+libFirm

cc

firmforth.irfirmforth+libFirm

firmforth

foo_constructed.vcgfoo_optimized.vcg

debug

foo.s

: foo1 1 + . ;

assembler+linkerfoo.so

dlopen()

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 8 / 12

Firmforth results

Got it working without dirty hacks

Developers were extremely responsive (#�rm on freenode)

Minor changes required to libFirm (went upstream)

Benchmark:

: fibonacci

dup 0= if else

dup 1 = if else

1 - dup fibonacci

swap 1 - fibonacci +

then

then ;

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 9 / 12

Benchmark results

�rmforth has speedups of

5.5 over stubforth

Its interpreter uses computed gotos instead of calls

2.6 over GNU gforth-fast

Fastest C-forth known to me (and #forth on freenode)Heavily �ne optimized

17.6 over a non-optimizing �rmforth

I.e., performing a call for each word

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 10 / 12

Future work

1 Apply proven recipe for the PostgreSQL executor

maybe in the shape of an extensionprepare select ... as foo; pg_jit('foo');

2 Further extend JIT infrastructure

Formulate things beyond callsExpressionsPL/PgSQL

3 Implement additional optimizations on the IR

E.g. vectorization

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 11 / 12

References

http://pp.ipd.kit.edu/firm/

https://github.com/anse1/firmforth

https://www.postgresql.org/message-id/87lks946bc.fsf@

gate450.dyndns.org

http://vitessedata.com/vitesse-db

https://www.postgresql.org/message-id/5733405A.7060502@

postgrespro.ru

[email protected] libFirm, postgres, JIT compilation and forth May 27, 2016 12 / 12