the state of tclquadcode
play

The State of TclQuadcode Kevin B. Kenny Kevin B. Kenny Donal K. - PowerPoint PPT Presentation

The State of TclQuadcode Kevin B. Kenny Kevin B. Kenny Donal K. Fellows Donal K. Fellows Tcl Core Team Tcl Core Team th Annual Tcl/Tk Conference 24 th Annual Tcl/Tk Conference 24 16-20 October 2017 16-20 October 2017 What TclQuadcode is:


  1. The State of TclQuadcode Kevin B. Kenny Kevin B. Kenny Donal K. Fellows Donal K. Fellows Tcl Core Team Tcl Core Team th Annual Tcl/Tk Conference 24 th Annual Tcl/Tk Conference 24 16-20 October 2017 16-20 October 2017

  2. What TclQuadcode is: Native code compiler for Tcl Multi-year collaboration  Procedures only  Kevin Kenny, Donal Fellows, Jos Decoster, others  Not yet methods, λ -forms 45k lines of Tcl, 3k lines of C++  Probably never global scripts  And ≈10k lines of generated code Running ahead of time Still a work in progress  Too slow for JIT!  But a piece of software is never Using advanced technology “done!”  Many recent papers  Data flow analysis in Static Single Assigment (SSA)

  3. Why TclQuadCode? Bytecode interpreter is too slow Discussed among Tcl’ers for years  Delicate: changes make it slower!  Donal Fellows  Unmaintainable: maze of goto  Kevin Kenny  Close to achievable speed  Don Porter  Miguel Sofer Making it much faster needs native  Jos Decoster code.  Others… Very hard problem Limited time to devote

  4. Getting started 2010: Ozgur Ugurlu (GSoC student) 2012: Karl Lehenbauer issues the implements bytecode assembler FlightAware challenges  Shows that bytecode can be  2× and 10× performance bogeys manipulated without  Got everyone moving! compromising safety. 2013: TclQuadcode project launched ≈2011: Compiler backend embeddings in Tcl appear  llvm, tcc  Generate code without leaving Tcl

  5. Early progress 2014: Kevin studies translation of Donal works out translation of bytecode to quadcode quadcode to LLVM IR  Easier to analyze and manipulate  Machine-focused rather than Tcl- focused  Explicit variables rather than stack  Huge amount of ‘glue’ needed Kevin studies data flow analysis Kevin and Donal integrate code at  No SSA yet 2014 conference  Datalog implemented to aid in  Successfully run the first program: difficult analysis [fib]  Datalog paper at Tcl conference pre-announces TclQuadcode

  6. The long slog 2015: Add bytecode operations and 2016: Largely spend consolidating builtin commands, one by one. and refactoring  Limited developer time Implement SSA and eliminate Datalog 2017: Big gains:  Datalog not quite fast enough  Node splitting/loop peeling  SSA enabled analysis with  Global/namespace variables relatively simple algorithms  [upvar] Donal announces project formally at  Near-complete support for Tcl conference ordinary built-in commands (≈200 non-bytecode commands)

  7. Measured results Name Description Speedup fib 85 Test simple loops 24.6× cos 1.2 Test simple floating point 10.9× wordcounter3 $sentence Dicts, string operations 5.4× H9fast $longWord Compute a hash code on a string 4.9× mrtest::calc $tree Recursive tree traversal and arithmetic on nodes 10.8× impure-caller Best-case numeric code 66.1× linesearch::getAllLines2 $size Larger numeric-intensive code, collinearity testing 10.3× flightawarebench::test $size Karl’s first benchmark: geographic calculations 15.5× Typical: 3-6× for general code, 10× and beyond for numeric-intensive code Little or no speedup for string and I/O operations (Tcl is pretty good at strings)

  8. How it works Code out has Procedure Function Procedure Function same interface as Definition Definitions Definition Definitions input procedures (string) (native code) (string) (native code) Standard bytecode Optimise (inline code) compiler and issue code Procedure Standard Function Procedure Standard Function Definition Library Definitions Definition Library Definitions Quadcode (bytecode) (LLVM IR) (LLVM IR) (bytecode) (LLVM IR) (LLVM IR) impls Basic convert IR code issue Procedure Procedure Function Procedure Procedure Function Definition Definition Definitions Definition Definition Definitions Type (untyped quadcode) (typed quadcode) (typed quadcode) (untyped quadcode) (typed quadcode) (typed quadcode) Analysis Quadcode specialise

  9. Why it works Avoid overheads Control flow analysis  Memory management, type  Some code paths exclude others checking, value conversion  After [expr {$x + 1}] succeeds, we Enabled by type analysis know $x is numeric! Cross-procedure analysis  int64_t, double, bool  Check with [string is]  Including specialization by type  Propagate through operations  One implementation always such as + string-based Path splitting

  10. Path splitting Look at x when called from Tcl proc x {a} {  $a is a string set y 0  $i is a string for {set i $a} {$i <= 10} {incr i} {  ($i <= 10) is complicated incr y $i  [incr y $i] has to extract the } integer from a Tcl_Obj return $y  Bottom of loop has to put the } integer back in a Tcl_Obj

  11. Path Splitting, continued y ← 0 i $a ← complicated: (i > 10)? throw error is $i numeric? i IntFromObj($i) ← y $y + $i ← i ← $i + 1 i ← NewIntObj($i) goto return $y

  12. Path Splitting, continued y ← 0 i $a ← complicated: (i > 10)? complicated: (i > 10)? throw error is $i numeric? is $i numeric? i IntFromObj($i) ← i IntFromObj($i) ← y $y + $i ← y $y + $i ← i $i + 1 ← i ← $i + 1 i NewIntObj($i) ← i ← NewIntObj($i) goto goto return $y

  13. Path Splitting, continued y ← 0 integer i $a ← complicated: (i > 10)? complicated: (i > 10)? throw error is $i numeric? is $i numeric? i IntFromObj($i) ← i IntFromObj($i) ← y $y + $i ← y $y + $i ← i $i + 1 ← i ← $i + 1 i NewIntObj($i) ← i ← NewIntObj($i) goto goto return $y

  14. Nonlocal Variable Access What’s done: What’s not done:  [namespace upvar]  Non-constant local names  [variable], [global]  [upvar #n], n>0  [upvar 1 $arg name] gets  [upvar 0] – special handling  $namespace::variable  [upvar 1 constantName name] – Why? gets special handling  Potential to create aliases for local  [upvar $n …] vars  [upvar #0 …]  Aliases wreck assumptions!  $::path::to::variable Also: Access to nonlocal variables is still slow!

  15. May have to change code to take best advantage Slower: Faster: proc accum {list} { proc accum {list} { global n; global s; global ss global n; global s; global ss set n_ $n; set s_ $s; set ss_ $ss foreach a $args { foreach a $args { incr n incr n_ set s [expr {$s + $a}] set s_ [expr {$s_ + $a}] set ss [expr {$ss + $a}] set ss_ [expr {$ss_ + $a}] } } set n $n_; set s $s_; set ss $ss_ } }

  16. There’s still a lot to do! Long compilation time Incomplete language support  LLVM is slow  Many things we think we know how to do  TclQuadcode is slower  Some things are too dynamic to ● Written in Tcl compile Large generated code volumes  Interpreter will always be  Many copies of procedures after available type specialization  Long procedures ● Stresses downstream compiler

  17. Next steps [uplevel] Non-hacky arrays  Limited initially to constant scripts  Currently, arrays are implemented and constant args in a caller as dicts.  Limited initially to [uplevel 1] Procedure inlining Better alias treatment  May be required for [uplevel]  Lift most of the penalty on nonlocal variables NRE Get user experience!  Coroutines, unbounded recursion

  18. Would language changes help? TIP 283: “Fix variable name Help from the programmer about resolution quirks” aliases and types  Ambiguity in how  tcl::pragma::type int $value $namespace::variable resolved  tcl::pragma::noalias var1 var2 …  Current behaviour absolutely  Maybe others… insane, source of bugs  Current behaviour also insanely difficult to implement in compiled code

  19. tcl::pragma::type Works on values, not variables. Simplifies compiled code called from Tcl. Asserts that at a given point in execution, a value has a given Forward type analysis on args type. possible Throws error on wrong type Type checking outside loops Useful for documenting API’s and Much less node splitting simpler – parameter checking and smaller code.

  20. tcl::pragma::noalias Asserts that a given set of variable Can compile much better code names refer to distinct variables  Uncontrolled aliases are all strings (because types are unknown)  Can make exceptions for known  Changing any potentially aliased aliases. variable requires converting all  Throws a runtime error if the potential aliases back from constraint is violated strings  Useful check few procs can –  Aliasing therefore has pervasive survive unexpected aliasing! effects. Cannot analyze in general without help Turing-complete problem! –

  21. Thank you! Where TclQuadcode is: Source code repository: https://core.tcl.tk/tclquadcode/ Mailing list: https://sourceforge.net/p/tcl/mailman/tcl-quadcode/

Recommend


More recommend