R Byte Code Optimization Compiler (1)

13
1 R Byte Code Opmizaon Compiler (1) March 7. 2012

description

R Byte Code Optimization Compiler (1). March 7. 2012. Outline. Basic Compiling Process Compiler Structure Decoding Pass Type Annotation Pass Unbox Opportunity Identification Pass. Basic Byte-Code Optimization Compiling Process. Typical Compiler Structure. Optimization Rules. - PowerPoint PPT Presentation

Transcript of R Byte Code Optimization Compiler (1)

R Byte Code Optimization Compiler (1)

R Byte Code Optimization Compiler (1)March 7. 20121OutlineBasic Compiling ProcessCompiler StructureDecoding PassType Annotation PassUnbox Opportunity Identification Pass2R Byte Code Optimization Compiler (1)Basic Byte-Code Optimization Compiling ProcessTypical Compiler Structure3INTSXPOrg Byte-Code seqBC_STMTPBase StmtsBC_STMTPOpt StmtsINTSXPNew Byte-Code seqSeveral PassesProfile TableAddr of GETVAR1Addr of LDCONST2Addr of ADD3Addr of SETVAR4Addr of POPAddr of GETFUN5Addr of MAKEPROM6Addr of CALL7Addr of RETRUNDecode passEncode passOptimizationRulesAddr of GETVAR1Addr of LDCONST2Addr of ADD3Addr of SETVAR4Addr of POPAddr of GETFUN5Addr of MAKEPROM6Addr of CALL7Addr of RETRUNWork on a new IR for the R byte-CodeR Byte Code Optimization Compiler (1)Compiler Structure New Components RequiredA new IR is neededThe current R byte-code is just instructionsopcode and operand in int array structureNo addition information attachedHard to manipulate A Very Simple IR for Current Optimization RequirementsAttach profile informationAttach type informationSupport simple type inferenceSupport simple unbox opportunity identificationBasic Compiler InfrastructurePasses definitionEngine for run all the passesStmt Printer: printing stmt as human readable text4R Byte Code Optimization Compiler (1)Compiler Structure IRIR and Stmts

5typedef struct { enum OP_CODEopcode;//the op_code char* op_name; //name of the instruction unsignedoperand_num; //number of operands unsignedstack_use; //number of operands consumed on stack unsignedstack_gen; //number of operands produced on stack intneed_profile; //whether the instruction need profile void*addr;//used for decode and encode} BC_INSTR, *BC_INSTRP;//the instructiontypedef struct { BC_INSTRP instr; //pointer to the instruction unsigned pc;//pc value, relative pc value int operands[4]; //we only support max 4 operands. In fact, only switch has 4 operands int type;//e.g. 0, unknown, logic, int, real, non-scalar. If the code gen more than one stac int type_source; //e.g. fixed or from const, profile based, derived(from reasoning) int output_shape; //Whether the output need box/unbox. 2bits, [need box][may unbox]} BC_STMT, *BC_STMTP;R Byte Code Optimization Compiler (1)Compiler Structure Passes and EnginePass function type and Examples

Skeleton code to run a pass6int(*pass_fun)(BC_STMTP, SEXP, unsigned*, PT_STACKP)

roc_type_annotate_pass(BC_STMTP stmt, //The current statement SEXP constants, //Constant table unsigned * profile_table, //Profile table PT_STACKP type_stack); //Current simulated stack void roc_run_pass(PT_LISTP stmts, SEXP constants, unsigned * profile_table, int(*pass_fun)(BC_STMTP, SEXP, unsigned*, PT_STACKP)) {

PT_STACKP type_stack = rou_create_pointer_stack(); //Prepare the simulated stack

for (unsigned i = 0; i < stmts->length; i++) { BC_STMTP stmt = rou_pointer_arraylist_get(stmts, i); (*pass_fun)(stmt, constants, profile_table, type_stack); //call the pass function

//Update the stack int stack_use = stmt->instr->stack_use; int stack_gen = stmt->instr->stack_gen; for (int i = 0; i < stack_use; i++) { rou_pointer_stack_pop(type_stack); } for (int i = 0; i < stack_gen; i++) { rou_pointer_stack_push(type_stack, stmt); } } rou_remove_pointer_stack(type_stack);}R Byte Code Optimization Compiler (1)Compiler Structure One PassSkeleton Code for One Pass7int roc_type_annotate_pass(BC_STMTP stmt, SEXP constants, unsigned * profile_table, PT_STACKP type_stack) { SEXP arg0; unsigned* prof_cell; BC_STMTP op1_stmt, op2_stmt;

enum OP_CODE op_code = stmt->instr->opcode; switch (op_code) { case LDCONST_OP:... ...break; case GETVAR_OP:... ...break; case DDVAL_OP:... ...break; default: ... ...break; } return xxx;}

R Byte Code Optimization Compiler (1)Decoding PassTransform original int Array into StmtsTransform addr back to opcodeOrganize opcode and operands into stmts8#Instructions Vec7L, # code versionAddr of LDCONST.OP, 1L, Addr of SETVAR.OP, 2L, Addr of POP.OP, Addr of GETVAR.OP,2L, Addr of LDCONST.OP, 3L, Addr of ADD.OP, 4L, Addr of SETVAR.OP, 5L, Addr of POP.OP, Addr of GETFUN.OP,6L, Addr of MAKEPROM.OP, 7L, Addr of CALL.OP, 8L, Addr of RETURN.OP PC STMT 1 LDCONST, 1 Type:Unknown, Type Source:Fixed 3 SETVAR, 2 Type:Unknown, Type Source:Fixed 5 POP Type:Unknown, Type Source:Fixed 6 GETVAR, 2 Type:Unknown, Type Source:Fixed 8 LDCONST, 3 Type:Unknown, Type Source:Fixed 10 ADD, 4 Type:Unknown, Type Source:Fixed 12 SETVAR, 5 Type:Unknown, Type Source:Fixed 14 POP Type:Unknown, Type Source:Fixed 15 GETFUN, 6 Type:Unknown, Type Source:Fixed 17 MAKEPROM, 7 Type:Unknown, Type Source:Fixed 19 CALL, 8 Type:Unknown, Type Source:Fixed 21 RETURN Type:Unknown, Type Source:FixedR Byte Code Optimization Compiler (1)Type Annotation PassA very simple type inference engineInput: profile table, and some simple rulesOutput:Type of the object on top of the stack after executing the stmtOptimize the runtime profile by simple type inferenceReduce the runtime profile requirements9[Stack TOP]SEXP [...]LDCONST , 1IdxValue11012rJust Check the constants type staticallyCALL, 8GETVAR , 2[Stack TOP]SEXP [???]Must to profile to get the typeADD , 4[Stack TOP]SEXP [...]SEXP [...][Stack TOP]SEXP [...]If we know the types of the objects on top of the stack before add, we could reason the outputs type staticallyR Byte Code Optimization Compiler (1)Type Annotation Pass Output Example10PC STMT 1 LDCONST, 1 Type:Real Scalar, Type Source:Constant 3 SETVAR, 2 Type:Real Scalar, Type Source:Derived 5 POP Type:Non-Simple Type, Type Source:Derived 6 GETVAR, 2 Type:Real Scalar, Type Source:Profiled[0, 0, 1, 0] 8 LDCONST, 3 Type:Real Scalar, Type Source:Constant10 ADD, 4 Type:Real Scalar, Type Source:Derived12 SETVAR, 5 Type:Real Scalar, Type Source:Derived14 POP Type:Non-Simple Type, Type Source:Derived15 GETFUN, 6 Type:Non-Simple Type, Type Source:Fixed17 MAKEPROM, 7 Type:Non-Simple Type, Type Source:Fixed19 CALL, 8 Type:Real Scalar, Type Source:Profiled[0, 0, 1, 0]21 RETURN Type:Real Scalar, Type Source:DerivedFrom profile, the values are counts of [Logical scalar, Int Scalar, Real Scalar, Non-simple type]Derived Type. Because the two objects on stack are all real scalarR Byte Code Optimization Compiler (1)Unbox Opportunity Identification PassTwo bits to mark one opcodes output should be boxed or unboxed[Must boxed, May Unboxed]If only May unboxed is set, we can add unbox after the stmt

Identify some opcode(e.g. ADD) that could work on unboxed objectsIdentify the source stmts that generate the objects on top of the stackMark the stmts as May unboxedIdentify some opcode (e.g. SETVAR, RETURN) that must work on boxed objectsIdentify the source stmts that generate the object on top of the stackMark the stmts as Must boxed11ADD , 4[Stack TOP]SEXP [...]SEXP [...]GETVAR, 2LDCONST, 3ADD could work on unboxed objects, because the two objects on stack are real scalar Mark the source stmt as May unboxedR Byte Code Optimization Compiler (1)Unbox Opportunity Identification Pass Output Example12 PC STMT 1 LDCONST, 1 Type:Real Scalar, Type Source:Constant Output shape:Box 3 SETVAR, 2 Type:Real Scalar, Type Source:Derived 5 POP Type:Non-Simple Type, Type Source:Derived 6 GETVAR, 2 Type:Real Scalar, Type Source:Profiled[0, 0, 1, 0] Output shape:Unbox 8 LDCONST, 3 Type:Real Scalar, Type Source:Constant Output shape:Unbox 10 ADD, 4 Type:Real Scalar, Type Source:Derived Output shape:Box 12 SETVAR, 5 Type:Real Scalar, Type Source:Derived 14 POP Type:Non-Simple Type, Type Source:Derived 15 GETFUN, 6 Type:Non-Simple Type, Type Source:Fixed 17 MAKEPROM, 7 Type:Non-Simple Type, Type Source:Fixed17 MAKEPROM, 7 Type:Non-Simple Type, Type Source:Fixed19 CALL, 8 Type:Real Scalar, Type Source:Profiled[0, 0, 1, 0] Output shape:Box21 RETURN Type:Real Scalar, Type Source:DerivedCould unbox, because only used in a unbox situationCould unbox, because only used in a unbox situationMust box after the add, because it will be used by SETVARMust box after the add, because it will be used by RETURNR Byte Code Optimization Compiler (1)Implementation Status and ChallengesStatusImplemented the simple compiler infrastructure as describeImplemented the three passes as describedWork well on the first example (RealAdd)ChallengesThere is nothing in R Need implement them allR is Pure ANIS C implementedNot C++, No OO, No STL, My current simple run pass engine can only support single Basic BlockHandling control flows Need a lot of additional effortIdentify all Basic Blocks, Reverse poster order traverse,Iteration until stable, Questions?Is there a stack VM based compiler infrastructure available?

13R Byte Code Optimization Compiler (1)