An Efficient Compilation Framework for Languages Based on a Concurrent Process Calculus Yoshihiro Oyama Kenjiro Taura Akinori Yonezawa Yonezawa Laboratory Department of Information Science University of Tokyo
implementation is difficult analysis is not general Compiling Programming Languages machine code surface language many primitives implementation becomes easy analysis becomes general several essential primitives intermediate language
Intermediate Languages n Sequential languages –Quadruple –Lambda calculus –Continuation Passing Style (CPS) [Appel 92] n Concurrent languages –Pi-calculus [Milner 93] –HACL [Kobayashi et al. 94] process calculus
Concurrent Process Calculus n Key component of calculation –Asynchronous process –Communication channel n Advantages –Clear syntax and clear semantics –Many theoretical results for optimization P P P P C P 3 3
Goal n Efficient implementation of a process calculus ML-like code Schematic code HACL code ….. focus of our research straightforward translation traditional techniques
Motivation Process calculus has some overheads which are not in sequential languages –Dynamic process scheduling –Channel communication Low efficiency with naïve implementation A demand on a sophisticated implementation reducing the overheads
Contribution n A framework for compiling process calculus efficiently n Optimization techniques applicable for software multithreading
Overview of This Presentation n Target language HACL n A basic execution n Our enhanced execution –To reduce scheduling overhead –To reduce communication overhead n Experimental results
Target Language HACL e ::= x | c | op ( e 1, …, e n ) expression P ::= P 1 | P 2 $ x. P$ x. P e ( x )=> P e 1 <= e 2 if e then P 1 else P 2 e 0 ( e 1, …, e n ) parallel execution channel creation receive from e send e 2 to e 1 process instantiation conditional process expression x 0 ( x 1, …, x n ) = P process definition
Basic Execution Model (1/2) - process scheduling - P 1 | P 2 ……… dynamic process creation P1P1 P2P2 both schedulable scheduling pool | P P P continuing execution P2P2 schedulable process
Basic Execution Model (2/2) - channel - channel ⇔ pointer to memory space r Q r ( y )=> Q r <= 8 r <= 12 r ( x )=> P r <= 12 r ( x )=> Pr ( y )=> Q r <= 8 12 value queue process queue { 12 / x } P {8 / y} Q
Inefficiencies of Basic Model n Scheduling overhead –Scheduling pool is manipulated every time a process is created n Communication overhead –Channel communication is always performed through memory
Our Enhanced Execution Model n Static process scheduling –reduces the frequency of the runtime scheduling pool manipulation –lines up code fragments for multiple process expressions n Unboxed channel framework –enables us to communicate values without memory –initially creates a channel on register –later allocates a channel on heap as necessary
Compilation Overview HACL program ML-like program translation rule execution flow scheduling pool execution flow scheduling pool = explicit = implicit F { P 1, P 2,..., P n } = a sequential ML-like program which schedules P 1, P 2,..., P n a set of schedulable process expressions
Compilation with Static Scheduling (1/2) = F P1P1 P2P2 P3P3 code fragment for P 1 F P2P2 P3P3 code fragment for P 2 F P3P3 code fragment for P 3
Compilation with Static Scheduling (2/2) = F P2P2 P1’P1’ r ( x )=> if (r has a value) then (* get the value from r *) else (* allocate a closure for on heap *) F P1’P1’P2P2 F P2P2 P1’P1’ code fragment for P 1 ’ F P2P2 code fragment for P 2
F { (r P) } F { ( $ r.( r P ) ) } F { r ( x )=> P } Compilation Example $ r.( r P ) r = new_channel(); if (r has a value) then x = get_value( r ); F { P } else (* allocate a closure P in heap and... *) F { } F { ( r P) } if (r has a waiting process) then (* wake up the process and … *) else put_value( r, 5 ) ;
Unboxed Channel Scheme n Unboxed channel = channel allocated on register –No memory access to manipulate an unboxed channel n All channels are created as an unboxed channel n An unboxed channel is elevated to a heap-allocated one as necessary
Example r = new_channel(); if (r has a process) then... else put_value( r, 5 ); if (r has a value) then x = get_value( r ); F { P } else... r = EMPTY_CHANNEL; if (...) {... } else { r = 5 + VAL_TAG; if (...) { x = r - VAL_TAG;... } else {... }... } ML-like codeCorresponding C code Channel allocation and communication on a register
When to allocate a space on heap? 6 8 P1P1 P2P2 123 two values are sent an unboxed channel is duplicated ???
Experimental Results (1/2) n HACL is used as an intermediate language of Schematic [Taura et al. 96] n ML-like program is compiled to assembly-like C n A native code is generated by GNU C compiler n Runtime type checks are omitted n SS20 (HyperSparc 150MHz) n Now implementing on a large-scale SMP
Experimental Results (2/2)
Related Work (1/2) n Id [Schauser et al. 95], Fleng [Araki et al. 97] –Static scheduling ≒ sequentialization –A does not depend on B ⇒ A | B → A ; B n Pict [Turner 96] –All channel communications need memory operation –A receive (input) expression always allocate a closure whether a value is present or not
Related Work (2/2) n StackThreads [Taura et al. 94, 97] –An original proposal of unboxed channel n Linear channel [Kobayashi et al. 96, Igarashi 97] –Linear channel = channel used only once –Some communications for linear channel is statically eliminated n CPS [Appel 92] –A compilation framework for sequential language
Conclusion n We proposed a framework compiling process calculus efficiently –Static scheduling –Unboxed channel n A language based on a process calculus is executed only a few times slower than C
Surface Languages for Process Calculi surface language intermediate language machine code concurrent functional language, concurrent OO language, etc... Schematic [Taura et al. 96] Pict [Turner 96] process calculus HACL Pi-calculus
A Schedulable Closure and The Scheduling Stack F { f ( r, x ), P } schedule f ( r, x ) first Can we schedule f and P statically ??? scheduling stack ≒ a set of schedulable closures Difficult. The scheduling pool is still necessary. P f(r, x)f(r, x)