263-2810: Advanced Compiler Design - ETH Z
Transcript of 263-2810: Advanced Compiler Design - ETH Z
263-2810:AdvancedCompilerDesign
ThomasR.GrossComputerScienceDepartmentETHZurich,Switzerland
Topics
§ ProgramopGmizaGon§ “Op%miza%on”§ Op%mizefor(execu%on)speed§ Op%mizefor(code)size§ Op%mizeforenergyconsump%on
§ Programanalysis
§ ProgramtransformaGon
§ Compilerengineering§ Compile%mevs.codequality
3
Topics--conGnued
§ Ahead-of-Gmecompilers
§ Just-in-Gmecompilers§ Mul%-%ercompila%onsystems
§ Binarytranslators§ “Specialpurposecompilers”
§ Sourcelanguage(s):imperaGve,staGcallytypedobject-oriented(class-based)programminglanguages§ Excursionstootherlanguageworlds
4
LogisGcs
§ Lectures§ Wednesday10-12§ Friday9-10
§ RecitaGons§ Friday13-15
§ Place:CABG51
www.lst.inf.ethz.ch
WebsitemigraGngtonewETHCorporateDesign–notuptodatefornow
5
Learningbydoing
§ Lecturespresentconcepts,algorithms,tradeoffs
§ RecitaGonsrepeat,providecontext
§ Coursecreditgivenifyou§ Dohomework§ Homework(3planed)
§ 40%ofthegrade§ Developandpresenta(compiler)project
§ 60%ofthegrade
§ Homeworkandprojectsdoneinteamsof2students
7
Homework
§ Build/extendacompiler
§ FocusonprogramopGmizaGon§ Prepara%onforproject
§ Homework:buildcompilerinfrastructure,implementopGmizaGons
§ Project:defineandinvesGgateacompilerquesGon
8
Learningbydoing
§ Theworkyouturninmustbeyourwork.§ Youmustreviewyourpartner’swork
§ TurningintheworkofothersischeaGng.
§ ETHhasasetofrulesthatyoushouldknow.Bo^omline:donotcopyadesignorcode(ortext…)withoutpropera^ribuGon.§ Inpapersorareportyoumustiden%fyandciteworknotperformed
byyou.§ Forprograms,youmustiden%fyyoursource(s)
§ A]ribu%onmustbeclearlyvisible§ Codereuseisfine–butnotpresen%ngtheworkofothersasyourwork
§ Talktotheinstructor/TAifnecessary9
Homework
§ Bothmembersofateammustcontribute§ Bothofyouareresponsibleforthesolu%onyouturnin§ Wereservetherighttoaskques%onsaboutthedesign,
implementa%on,tes%ngplan,etc.
§ Youmustturninyourownwork.§ “Finding”asolu%oninasomeoneelse’sdirectoryischea.ng. § ETHhasapolicyonchea%ng.
10
11
12
Discussion
§ JohnandJaneformteamYandtheyworkonhomeworkN.TheydecidethatJaneshouldimplementthe(joint)design,Johnwillworkmoreonafuturehomework.
§ AsJanestartsworkingontheimplementa@on,sherealizesthatteamXmesseduptheprotec@onofthesvndirectoryandshecopiesasnapshot.SheimprovesitaliEle(beEerdocumenta@on,re-arrangmentofmethods,etc)andturnsinthesolu@on.
§ TheTAdetectsthatteamYcopiedfromteamX;Janeadmitshermistake,andthecaseisforwardedtotheRector.
§ Ques@on:IsJohnaguiltyparty(asdefinedbyETHregula@ons)?Discussthisques@onwithyourneighbor.
13
Homework
§ Wedonotwanttoplaypolice.
§ Dothehomeworkyourself.
§ Discussionsarefine(andencouraged)–copyingcodeisnot.
14
RecitaGons
§ Reviewmaterial,introduceframework,etc.
§ MeeGngswhenannouncedinclass
§ MayswaplecturesandrecitaGonsonFriday§ Esp.earlyinsemester§ Covermaterialforlaterprojects§ Fridayisnotop.onal
15
17
1.0IntroducGon
§ Centraltopic:opGmizaGontechniquesformodernprogramminglanguages§ Keyissuesforrealcompilers§ Techniqueshavemanyuses
§ “OpGmizaGon”–resulGngcodeisrarelyopGmal§ Odenimpossibletoproveop%mality§ Mayhaveproofsforanabstractexecu%onengine(virtualmachine)
but%etorealsystemsdifficult§ Interestedinimprovement
§ “be]erthannaïvetransla%on”§ “pre]ygood”§ incrementaltransforma%ons
18
1.1IntermediaterepresentaGon
§ IR(IntermediaterepresentaGon):outputoffront-end,inputtobackend
§ OpGmizerIRàIR§ OrseparateIRs:IR1andIR2(orhigh-levelIR,low-levelIR)
19
IRconcerns
§ Abstractfromunnecessaryfeaturesofprogramminglanguage§ Supportmul%plefront-ends
§ Decouplefront-endandback-end§ Mul%pleback-ends
§ KeepessenGalinformaGon§ Structure§ Types
§ ExposeoperaGons
20
Simplecompiler:Tree-basedIR
§ Oneassignmentstmt:onebinarytreeint A, B, C, D;
A = B + C * D;
§ Atreehasaroot§ Topnode§ Rightandledsubtrees
§ Sequencesofstatements:forestoftrees
21
23
26
29
Assessmentoftrees
§ Simple
§ NotagoodIRforopGmizaGon§ Noreuseofvalues
§ Valuesflowthroughdesignatedvariables
§ Overspecifica%on
§ Globalaspects(e.g.,lastwriterforavariable)hidden30
1.2RequirementsforIR
§ Makedatadependencesexplicit§ Captureproducer–consumerrela%onship
§ Makecontroldependencesexplicit§ Determineessen%alorderofexecu%onthatmustbehonored
31
Controldependence
CapturesifexecuGonofone{}dependsonthe
execuGonofsomeother{}
32
statementblockfuncGoncall
statementblockfuncGoncall
RequirementsforIR
§ Makedatadependencesexplicit§ Captureproducer–consumerrela%onship
§ Makecontroldependencesexplicit§ Determineessen%alorderofexecu%onthatmustbehonored
§ IdeallytheIRcontainsonlytrue(essenGal)constraints§ Nosuperfluousconstraints
33
IR(&language)restricGons
§ NomulG-threading§ Singlethread–nochangesoutsideofcontrolofthisfunc%on/program
§ Noaliasingofmethod-localvariables§ Variablesinheapmaybealiased–butrestric%onsmayapply
§ E.g.,referencescanonlyrefertoobjects/arraysbutnottofields/elements
§ Focusfirstonmethod-localscalarvariables§ Importantgroup:includescompiler-generatedtemporaryvariablesfor
addressarithme%c
34
Controldependencegraph
§ CDG(controldependencegraph)capturescontroldependences
§ Nodes:basicblocks(BB)§ Edges:possiblecontroltransfers
§ Abasicblockisamaximalsequenceofstatementsthatarealwaysexecutedtogether§ Unlessthereisanexcep%on–theseareusuallyhandleddifferently
35
36
39
IRconcerns
§ Makedatadependencesexplicit§ Captureproducer–consumerrela%onship
// 1 // x = a + b ;
// other basic blocks //
// 2 // d = x + 1;
// 3 // b = a + c;
§ xproducedby1,consumedby2
§ bin1mustbereadbeforebiswri^enin340
§ SimplesoluGon:execute1,2,and3inorder
§ Whatma^ersis1readstheoldvalueand3producesanewvalue
§ Idea:renamevariableand/ormakecopies
// 1 // x = a + foo ;
// 2 // d = x + 1;
// 3 // bar = a + c;
43
§ Compilerintroducesanewnamewheneveranewvalueisproduced.
§ Coulduseanynamebutusuallyusesubscripts:b1,b2,…
// 1 // x = a + b34 ;
// 2 // d = x + 1;
// 3 // b56 = a + c;
§ IRusedinopGmizingcompilers:staGcsingleassignment(SSA)
44