/
McLab McLab

McLab - PowerPoint Presentation

alexa-scheidler
alexa-scheidler . @alexa-scheidler
Follow
371 views
Uploaded On 2017-06-11

McLab - PPT Presentation

Tutorial wwwsablemcgillcamclab Part 7 McVM implementation example ifelse construct Implementation in interpreter Implementation in JIT compiler 642011 Example 1 McLab Tutorial Laurie ID: 558387

tutorial mclab nurudeen lameed mclab tutorial lameed nurudeen garg hendren laurie rahul 2011 analysis test block code mcvm class

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "McLab" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

McLab Tutorialwww.sable.mcgill.ca/mclab

Part 7 – McVM implementation example: if/else constructImplementation in interpreterImplementation in JIT compiler

6/4/2011

Example-

1

McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

TexPoint fonts used in EMF.

Read the TexPoint manual before you delete this box.:

A

A

A

A

A

A

A

A

A

A

ASlide2

Before we startMcVM is written in C++, but “clean” C++ 

Nearly everything is a classClass names start in capital lettersTypically one header and one implementation file for each classMethod names are camel cased (getThisName)Members are usually private and named m_likeThis

6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-2Slide3

Before we start …Makefile providedHandwritten, very simple to read or editScons

can also be usedATLAS/CLAPACK is not essential. Alternatives:Intel MKL, AMD ACML, any CBLAS + Lapacke (eg. GotoBLAS2 + Lapacke)Use your favourite development toolI use Eclipse CDT, switched from Vim Virtualbox image with everything pre-installed available on request for private use6/4/2011

McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-3Slide4

Implementing if/else in McVMA new class to represent if/else

XML parserLoop simplifierInterpreterVarious analysis Reach-def, live variable analysisType checkingCode generation6/4/2011

McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-4Slide5

1. A class to represent If/ElseClass IfElseStmtWe will derive this class from “Statement”Form two files:

ifelsestmt.h and ifelsestmt.cppNeed fields to represent:Test expressionIf bodyElse body6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-

5Slide6

Ifelsestmt.hclass IfElseStmt: public StatementMethods:copy(),

toString(), getSymbolUses(), getSymbolDefs()getCondition(), getIfBlock(), getElseBlock()Private members:Expression *m_pCondition;StmtSequence *m_pIfBlock;

StmtSequence *m_pElseBlock;6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-6Slide7

Modify statements.hEach statement has a field called m_typeThis contains a type tag

Tag used throughout compiler for switch/caseenum StmtType{ IF_ELSE, SWITCH, FOR, ….};6/4/2011

McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-7Slide8

2. Modify XML ParserLook in parser.h, parser.cppBefore anything happens, must parse from XML generated by frontend

XML parser is a simple recursive descent parserAdd a case to parseStmt()Look at the element name in the XMLIf it is “IfStmt”, it is a If/ElseWrite a parseIfStmt() function6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-8Slide9

3. Modify transform loopsMcVM simplifies for-loops to a lower level constructTo achieve this, we need to first find loopsDone via a depth first search in the tree

So add a case to this search to say:Search in the if blockSearch in the else blockReturntransform_loops.cpp6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-

9Slide10

4. Add to interpreterAlways implement in interpreter before implementing in JIT compilerIt is a simple evaluator: no byte-code tricks, no direct-threaded dispatch etc.Add a case to statement evaluation:

Evaluate test conditionIf true, evaluate if blockIf false, evaluate else blockinterpreter.cpp : Case in execStatement() Calls evalIfElseStmt() 6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-10Slide11

Moment of silence .. Or reviewAt this point, if/else has been implemented in the interpreterIf you don’t enable JIT compilation, then you can now run if/else Good checkpoint for testing and development

6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-11Slide12

Flow analysis recap

Compute program property at each program point

6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-

12

If block

Test expr

Else blockSlide13

Flow analysis recapWe want to compute property at each program pointTypically want to compute a map of some kind at each program pointProgram points are not inside statements, but just before and after

Usually unions computed at join pointsCan be forward or backwards depending on the analysis6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-13Slide14

Reaching definitions analysis6/4/2011

McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-14

If block

Test

expr

Else blockSlide15

McVM reach-defs analysisLook in analysis_reachdefs

(.h/.cpp) getReachDefs() is an overloaded function to compute reach-defsReachDefInfo class to store analysis infoIf/Else:Record reach-defs for test expressionCompute reach-defs for if and else blocks by calling getReachDefs() for StmtSequence

Compute union at post-if/else point6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-15Slide16

Live variable analysis6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-16

If block

Test

expr

Else blockSlide17

McVM live vars analysis Look in analysis_livevars

(.h/.cpp)getLiveVars() is an overloaded functionLiveVarInfo is a class to store live-vars infoIf/Else:Information flows backwards from post-if/elseFlow live-vars through the if and else blocksCompute union at post-test expressionRecord live-vars info of test expression

6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-17Slide18

Type inference analysis6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-18

If block

Test

expr

Else blockSlide19

Type inferenceLook in analysis_typeinfer (.h/.cpp)

inferTypes() is an overloaded function to perform type inference for most node-typesFor If/else:Infer type of test expressionInfer type of if and else blocksMerge information at post-if/else point6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-19Slide20

Flow analysis tipsWe define a few typedefs for data structures like maps, setseg:

VarDefSet: typedef of set of IIRNode* with appropriate comparison operators and allocatorWhen trying to understand flow analysis code, start from code for assignment statementsPay attention to statements like return and break6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-20Slide21

Code generation and LLVMLLVM is based upon a typed SSA representationLLVM can either be accessed through a C++ API, or you can generate LLVM byte-code directlyWe use the C++ API

Much of the complexity of the code generator due to SSA representation required by LLVMHowever, we don’t do an explicit SSA conversion pass6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen LameedExample-21Slide22

Code generation in McVMSSA conversion is not explicitly represented in the IRSSA conversion done while doing code generationAssignment instructions are

usually not generated directly if Lvalue is a symbolIn SSA form, values of expressions are important, not what they are assigned toWe store mapping of symbols to values in an execution environment6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-22Slide23

Compiling if/elseFour steps:Compile test expression Compile if block (compStmtSeq

)Compile else block (compStmtSeq)Call matchBranchPoints() to do appropriate SSA book-keeping at merge pointRest of the code is book-keeping for LLVMSuch as forming proper basic blocks when required6/4/2011McLab Tutorial, Laurie Hendren, Rahul Garg and Nurudeen Lameed

Example-23