263-2810: advanced compiler design - eth z

32
263-2810: Advanced Compiler Design Thomas R. Gross Computer Science Department ETH Zurich, Switzerland

Upload: others

Post on 26-Nov-2021

29 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 263-2810: Advanced Compiler Design - ETH Z

263-2810:AdvancedCompilerDesign

ThomasR.GrossComputerScienceDepartmentETHZurich,Switzerland

Page 2: 263-2810: Advanced Compiler Design - ETH Z

Topics

§  ProgramopGmizaGon§  “Op%miza%on”§  Op%mizefor(execu%on)speed§  Op%mizefor(code)size§  Op%mizeforenergyconsump%on

§  Programanalysis

§  ProgramtransformaGon

§  Compilerengineering§  Compile%mevs.codequality

3

Page 3: 263-2810: Advanced Compiler Design - ETH Z

Topics--conGnued

§  Ahead-of-Gmecompilers

§  Just-in-Gmecompilers§  Mul%-%ercompila%onsystems

§  Binarytranslators§  “Specialpurposecompilers”

§  Sourcelanguage(s):imperaGve,staGcallytypedobject-oriented(class-based)programminglanguages§  Excursionstootherlanguageworlds

4

Page 4: 263-2810: Advanced Compiler Design - ETH Z

LogisGcs

§  Lectures§  Wednesday10-12§  Friday9-10

§  RecitaGons§  Friday13-15

§  Place:CABG51

www.lst.inf.ethz.ch

WebsitemigraGngtonewETHCorporateDesign–notuptodatefornow

5

Page 5: 263-2810: Advanced Compiler Design - ETH Z

Learningbydoing

§  Lecturespresentconcepts,algorithms,tradeoffs

§  RecitaGonsrepeat,providecontext

§  Coursecreditgivenifyou§  Dohomework§  Homework(3planed)

§  40%ofthegrade§  Developandpresenta(compiler)project

§  60%ofthegrade

§  Homeworkandprojectsdoneinteamsof2students

7

Page 6: 263-2810: Advanced Compiler Design - ETH Z

Homework

§  Build/extendacompiler

§  FocusonprogramopGmizaGon§  Prepara%onforproject

§  Homework:buildcompilerinfrastructure,implementopGmizaGons

§  Project:defineandinvesGgateacompilerquesGon

8

Page 7: 263-2810: Advanced Compiler Design - ETH Z

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

Page 8: 263-2810: Advanced Compiler Design - ETH Z

Homework

§  Bothmembersofateammustcontribute§  Bothofyouareresponsibleforthesolu%onyouturnin§  Wereservetherighttoaskques%onsaboutthedesign,

implementa%on,tes%ngplan,etc.

§  Youmustturninyourownwork.§  “Finding”asolu%oninasomeoneelse’sdirectoryischea.ng. §  ETHhasapolicyonchea%ng.

10

Page 9: 263-2810: Advanced Compiler Design - ETH Z

11

Page 10: 263-2810: Advanced Compiler Design - ETH Z

12

Page 11: 263-2810: Advanced Compiler Design - ETH Z

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

Page 12: 263-2810: Advanced Compiler Design - ETH Z

Homework

§  Wedonotwanttoplaypolice.

§  Dothehomeworkyourself.

§  Discussionsarefine(andencouraged)–copyingcodeisnot.

14

Page 13: 263-2810: Advanced Compiler Design - ETH Z

RecitaGons

§  Reviewmaterial,introduceframework,etc.

§  MeeGngswhenannouncedinclass

§  MayswaplecturesandrecitaGonsonFriday§  Esp.earlyinsemester§  Covermaterialforlaterprojects§  Fridayisnotop.onal

15

Page 14: 263-2810: Advanced Compiler Design - ETH Z

17

Page 15: 263-2810: Advanced Compiler Design - ETH Z

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

Page 16: 263-2810: Advanced Compiler Design - ETH Z

1.1IntermediaterepresentaGon

§  IR(IntermediaterepresentaGon):outputoffront-end,inputtobackend

§  OpGmizerIRàIR§  OrseparateIRs:IR1andIR2(orhigh-levelIR,low-levelIR)

19

Page 17: 263-2810: Advanced Compiler Design - ETH Z

IRconcerns

§  Abstractfromunnecessaryfeaturesofprogramminglanguage§  Supportmul%plefront-ends

§  Decouplefront-endandback-end§  Mul%pleback-ends

§  KeepessenGalinformaGon§  Structure§  Types

§  ExposeoperaGons

20

Page 18: 263-2810: Advanced Compiler Design - ETH Z

Simplecompiler:Tree-basedIR

§  Oneassignmentstmt:onebinarytreeint A, B, C, D;

A = B + C * D;

§  Atreehasaroot§  Topnode§  Rightandledsubtrees

§  Sequencesofstatements:forestoftrees

21

Page 19: 263-2810: Advanced Compiler Design - ETH Z

23

Page 20: 263-2810: Advanced Compiler Design - ETH Z

26

Page 21: 263-2810: Advanced Compiler Design - ETH Z

29

Page 22: 263-2810: Advanced Compiler Design - ETH Z

Assessmentoftrees

§  Simple

§  NotagoodIRforopGmizaGon§  Noreuseofvalues

§  Valuesflowthroughdesignatedvariables

§  Overspecifica%on

§  Globalaspects(e.g.,lastwriterforavariable)hidden30

Page 23: 263-2810: Advanced Compiler Design - ETH Z

1.2RequirementsforIR

§  Makedatadependencesexplicit§  Captureproducer–consumerrela%onship

§  Makecontroldependencesexplicit§  Determineessen%alorderofexecu%onthatmustbehonored

31

Page 24: 263-2810: Advanced Compiler Design - ETH Z

Controldependence

CapturesifexecuGonofone{}dependsonthe

execuGonofsomeother{}

32

statementblockfuncGoncall

statementblockfuncGoncall

Page 25: 263-2810: Advanced Compiler Design - ETH Z

RequirementsforIR

§  Makedatadependencesexplicit§  Captureproducer–consumerrela%onship

§  Makecontroldependencesexplicit§  Determineessen%alorderofexecu%onthatmustbehonored

§  IdeallytheIRcontainsonlytrue(essenGal)constraints§  Nosuperfluousconstraints

33

Page 26: 263-2810: Advanced Compiler Design - ETH Z

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

Page 27: 263-2810: Advanced Compiler Design - ETH Z

Controldependencegraph

§  CDG(controldependencegraph)capturescontroldependences

§  Nodes:basicblocks(BB)§  Edges:possiblecontroltransfers

§  Abasicblockisamaximalsequenceofstatementsthatarealwaysexecutedtogether§  Unlessthereisanexcep%on–theseareusuallyhandleddifferently

35

Page 28: 263-2810: Advanced Compiler Design - ETH Z

36

Page 29: 263-2810: Advanced Compiler Design - ETH Z

39

Page 30: 263-2810: Advanced Compiler Design - ETH Z

IRconcerns

§  Makedatadependencesexplicit§  Captureproducer–consumerrela%onship

// 1 // x = a + b ;

// other basic blocks //

// 2 // d = x + 1;

// 3 // b = a + c;

§  xproducedby1,consumedby2

§  bin1mustbereadbeforebiswri^enin340

Page 31: 263-2810: Advanced Compiler Design - ETH Z

§  SimplesoluGon:execute1,2,and3inorder

§  Whatma^ersis1readstheoldvalueand3producesanewvalue

§  Idea:renamevariableand/ormakecopies

// 1 // x = a + foo ;

// 2 // d = x + 1;

// 3 // bar = a + c;

43

Page 32: 263-2810: Advanced Compiler Design - ETH Z

§  Compilerintroducesanewnamewheneveranewvalueisproduced.

§  Coulduseanynamebutusuallyusesubscripts:b1,b2,…

// 1 // x = a + b34 ;

// 2 // d = x + 1;

// 3 // b56 = a + c;

§  IRusedinopGmizingcompilers:staGcsingleassignment(SSA)

44