1 Notes on new codegen (Aug 10)
\r
2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\r
5 - We insert spills for variables before the stack check! This is the reason for
\r
6 some fishy code in StgCmmHeap.entryHeapCheck where we are doing some strange
\r
7 things to fix up the stack pointer before GC calls/jumps.
\r
9 The reason spills are inserted before the sp check is that at the entry to a
\r
10 function we always store the parameters passed in registers to local variables.
\r
11 The spill pass simply inserts spills at variable definitions. We instead should
\r
12 sink the spills so that we can avoid spilling them on branches that never
\r
15 This will fix the spill before stack check problem but only really as a side
\r
16 effect. A 'real fix' probably requires making the spiller know about sp checks.
\r
18 EZY: I don't understand this comment. David Terei, can you clarify?
\r
20 - Proc points pass all arguments on the stack, adding more code and
\r
21 slowing down things a lot. We either need to fix this or even better
\r
22 would be to get rid of proc points.
\r
24 - CmmInfo.cmmToRawCmm uses Old.Cmm, so it is called after converting Cmm.Cmm to
\r
25 Old.Cmm. We should abstract it to work on both representations, it needs only to
\r
26 convert a CmmInfoTable to [CmmStatic].
\r
28 - The MkGraph currenty uses a different semantics for <*> than Hoopl. Maybe
\r
29 we could convert codeGen/StgCmm* clients to the Hoopl's semantics?
\r
30 It's all deeply unsatisfactory.
\r
32 - Improve performance of Hoopl.
\r
34 A nofib comparison of -fasm vs -fnewcodegen nofib compilation parameters
\r
35 (using the same ghc-cmm branch +libraries compiled by the old codegenerator)
\r
36 is at http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.oldghchoopl.txt
\r
37 - the code produced is 10.9% slower, the compilation is +118% slower!
\r
39 The same comparison with ghc-head with zip representation is at
\r
40 http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.oldghczip.txt
\r
41 - the code produced is 11.7% slower, the compilation is +78% slower.
\r
43 When compiling nofib, ghc-cmm + libraries compiled with -fnew-codegen
\r
44 is 23.7% slower (http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.hooplghcoldgen.txt).
\r
45 When compiling nofib, ghc-head + libraries compiled with -fnew-codegen
\r
46 is 31.4% slower (http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.zipghcoldgen.txt).
\r
48 So we generate a bit better code, but it takes us longer!
\r
50 EZY: Also importantly, Hoopl uses dramatically more memory than the
\r
53 - Are all blockToNodeList and blockOfNodeList really needed? Maybe we could
\r
54 splice blocks instead?
\r
56 In the CmmContFlowOpt.blockConcat, using Dataflow seems too clumsy. Still,
\r
57 a block catenation function would be probably nicer than blockToNodeList
\r
58 / blockOfNodeList combo.
\r
60 - lowerSafeForeignCall seems too lowlevel. Just use Dataflow. After that
\r
61 delete splitEntrySeq from HooplUtils.
\r
63 - manifestSP seems to touch a lot of the graph representation. It is
\r
64 also slow for CmmSwitch nodes O(block_nodes * switch_statements).
\r
65 Maybe rewrite manifestSP to use Dataflow?
\r
67 - Sort out Label, LabelMap, LabelSet versus BlockId, BlockEnv, BlockSet
\r
68 dichotomy. Mostly this means global replace, but we also need to make
\r
69 Label an instance of Outputable (probably in the Outputable module).
\r
71 - NB that CmmProcPoint line 283 has a hack that works around a GADT-related
\r
74 - SDM (2010-02-26) can we remove the Foreign constructor from Convention?
\r
75 Reason: we never generate code for a function with the Foreign
\r
76 calling convention, and the code for calling foreign calls is generated
\r
78 - AsmCodeGen has a generic Cmm optimiser; move this into new pipeline
\r
79 EZY (2011-04-16): The mini-inliner has been generalized and ported,
\r
80 but the constant folding and other optimizations need to still be
\r
83 - AsmCodeGen has post-native-cg branch eliminator (shortCutBranches);
\r
84 we ultimately want to share this with the Cmm branch eliminator.
\r
86 - At the moment, references to global registers like Hp are "lowered"
\r
87 late (in CgUtils.fixStgRegisters). We should do this early, in the
\r
88 new native codegen, much in the way that we lower calling conventions.
\r
89 Might need to be a bit sophisticated about aliasing.
\r
91 - Question: currently we lift procpoints to become separate
\r
92 CmmProcs. Do we still want to do this?
\r
94 NB: and advantage of continuing to do this is that
\r
95 we can do common-proc elimination!
\r
97 - Move to new Cmm rep:
\r
98 * Make native CG consume New Cmm;
\r
99 * Convert Old Cmm->New Cmm to keep old path alive
\r
100 * Produce New Cmm when reading in .cmm files
\r
102 - Consider module names
\r
104 - Top-level SRT threading is a bit ugly
\r
106 - Add type/newtype for CmmModule = [CmmGroup] -- A module
\r
107 CmmGroup = [CmmTop] -- A .o file
\r
108 CmmTop = Proc | Data -- A procedure or data
\r
110 - This is a *change*: currently a CmmGroup is one function's-worth of code
\r
111 regardless of SplitObjs. Question: can we *always* generate M.o if there
\r
112 is just one element in the list (rather than M/M1.o, M/M2.o etc)
\r
116 - See "CAFs" below; we want to totally refactor the way SRTs are calculated
\r
118 - Pull out Areas into its own module
\r
119 Parameterise AreaMap (note there are type synonyms in CmmStackLayout!)
\r
120 Add ByteWidth = Int
\r
121 type SubArea = (Area, ByteOff, ByteWidth)
\r
122 ByteOff should not be defined in SMRep -- that is too high up the hierarchy
\r
124 - SMRep should not be imported by any module in cmm/! Make it so.
\r
125 -- ByteOff etc ==> CmmExpr
\r
126 -- rET_SMALL etc ==> CmmInfo
\r
127 Check that there are no other imports from codeGen in cmm/
\r
129 - If you eliminate a label by branch chain elimination,
\r
130 what happens if there's an Area associated with that label?
\r
132 - Think about a non-flattened representation?
\r
135 * Use record fields for LastCall!
\r
136 * cml_ret_off should be a ByteOff
\r
138 LastCall (which has a successor) and
\r
139 LastJump (which does not, includes return?)
\r
140 - does not have cml_cont, cml_ret_args, cml_ret_off
\r
143 - expands into save/MidForeignCall/restore/goto
\r
144 - like any LastCall, target of the call gets an info table
\r
146 - JD: remind self of what goes wrong if you turn off the
\r
147 liveness of the update frame
\r
149 - Garbage-collect http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/CPS
\r
150 moving good stuff into
\r
151 http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/NewCodeGenPipeline
\r
154 - We believe that all of CmmProcPoint.addProcPointProtocols is dead. What
\r
155 goes wrong if we simply never call it?
\r
157 - Something fishy in CmmStackLayout.hs
\r
158 * In particular, 'getAreaSize' returns an AreaMap, but we *know* the width of
\r
159 LocalRegs, so it'd be better to return FiniteMap AreaId ByteWidth
\r
160 * setSuccSPs looks fishy. Rather than lookin in procPoints, it could
\r
161 just lookup the block in areaSize which, after all, has a binding
\r
162 for precisely successors of calls. All other blocks (including proc
\r
163 points that are not successors of a call, we think) can be treated
\r
164 uniformly: zero-size Area, and use inSP.
\r
167 - Currently AsmCodeGen top level calls AsmCodeGen.cmmToCmm, which is a small
\r
168 C-- optimiser. It has quite a lot of boilerplate folding code in AsmCodeGen
\r
169 (cmmBlockConFold, cmmStmtConFold, cmmExprConFold), before calling out to
\r
170 CmmOpt. ToDo: see what optimisations are being done; and do them before
\r
173 - Modularise the CPS pipeline; instead of ...; A;B;C; ...
\r
176 - Most of HscMain.tryNewCodeGen does not belong in HscMain. Instead
\r
179 processCmm [including generating "raw" cmm]
\r
185 - If we stick CAF and stack liveness info on a LastCall node (not LastRet/Jump)
\r
186 then all CAF and stack liveness stuff be completed before we split
\r
187 into separate C procedures.
\r
190 compute and attach liveness into to LastCall
\r
191 right at end, split, cvt to old rep
\r
192 [must split before cvt, because old rep is not expressive enough]
\r
195 when old rep disappears,
\r
196 move the whole splitting game into the C back end *only*
\r
197 (guided by the procpoint set)
\r
199 ----------------------------------------------------
\r
201 ----------------------------------------------------
\r
203 -------- Testing stuff ------------
\r
204 HscMain.optionallyConvertAndOrCPS
\r
206 DynFlags: -fconvert-to-zipper-and-back, -frun-cpsz
\r
208 -------- Moribund stuff ------------
\r
209 OldCmm.hs Definition of flowgraph of old representation
\r
210 OldCmmUtil.hs Utilites that operates mostly on on CmmStmt
\r
211 OldPprCmm.hs Pretty print for CmmStmt, GenBasicBlock and ListGraph
\r
212 CmmCvt.hs Conversion between old and new Cmm reps
\r
213 CmmOpt.hs Hopefully-redundant optimiser
\r
215 -------- Stuff to keep ------------
\r
216 CmmCPS.hs Driver for new pipeline
\r
218 CmmLive.hs Liveness analysis, dead code elim
\r
219 CmmProcPoint.hs Identifying and splitting out proc-points
\r
221 CmmSpillReload.hs Save and restore across calls
\r
223 CmmCommonBlockElim.hs Common block elim
\r
224 CmmContFlowOpt.hs Other optimisations (branch-chain, merging)
\r
226 CmmBuildInfoTables.hs New info-table
\r
227 CmmStackLayout.hs and stack layout
\r
229 CmmInfo.hs Defn of InfoTables, and conversion to exact byte layout
\r
231 ---------- Cmm data types --------------
\r
232 Cmm.hs Cmm instantiations of dataflow graph framework
\r
233 MkGraph.hs Interface for building Cmm for codeGen/Stg*.hs modules
\r
235 CmmDecl.hs Shared Cmm types of both representations
\r
236 CmmExpr.hs Type of Cmm expression
\r
237 CmmType.hs Type of Cmm types and their widths
\r
238 CmmMachOp.hs MachOp type and accompanying utilities
\r
243 PprC.hs Pretty print Cmm in C syntax
\r
244 PprCmm.hs Pretty printer for CmmGraph.
\r
245 PprCmmDecl.hs Pretty printer for common Cmm types.
\r
246 PprCmmExpr.hs Pretty printer for Cmm expressions.
\r
249 BlockId.hs BlockId, BlockEnv, BlockSet
\r
251 ----------------------------------------------------
\r
252 Top-level structure
\r
253 ----------------------------------------------------
\r
255 * New codgen called in HscMain.hscGenHardCode, by calling HscMain.tryNewCodeGen,
\r
256 enabled by -fnew-codegen (Opt_TryNewCodeGen)
\r
258 THEN it calls CmmInfo.cmmToRawCmm to lay out the details of info tables
\r
259 type Cmm = GenCmm CmmStatic CmmInfo (ListGraph CmmStmt)
\r
260 type RawCmm = GenCmm CmmStatic [CmmStatic] (ListGraph CmmStmt)
\r
262 * HscMain.tryNewCodeGen
\r
263 - STG->Cmm: StgCmm.codeGen (new codegen)
\r
264 - Optimise: CmmContFlowOpt (simple optimisations, very self contained)
\r
265 - Cps convert: CmmCPS.protoCmmCPS
\r
266 - Optimise: CmmContFlowOpt again
\r
267 - Convert: CmmCvt.cmmOfZgraph (convert to old rep) very self contained
\r
269 * StgCmm.hs The new STG -> Cmm conversion code generator
\r
270 Lots of modules StgCmmXXX
\r
273 ----------------------------------------------------
\r
274 CmmCPS.protoCmmCPS The new pipeline
\r
275 ----------------------------------------------------
\r
277 CmmCPS.protoCmmCPS:
\r
278 1. Do cpsTop for each procedures separately
\r
279 2. Build SRT representation; this spans multiple procedures
\r
280 (unless split-objs)
\r
283 * CmmCommonBlockElim.elimCommonBlocks:
\r
284 eliminate common blocks
\r
286 * CmmProcPoint.minimalProcPointSet
\r
287 identify proc-points
\r
290 * CmmProcPoint.addProcPointProtocols
\r
291 something to do with the MA optimisation
\r
292 probably entirely unnecessary
\r
294 * Spill and reload:
\r
295 - CmmSpillReload.dualLivenessWithInsertion
\r
296 insert spills/reloads across
\r
298 Branches to proc-points
\r
299 Now sink those reloads (and other instructions):
\r
300 - CmmSpillReload.rewriteAssignments
\r
301 - CmmSpillReload.removeDeadAssignmentsAndReloads
\r
303 * CmmStackLayout.stubSlotsOnDeath
\r
304 debug only: zero out dead slots when they die
\r
307 - CmmStackLayout.lifeSlotAnal:
\r
308 find which sub-areas are live on entry to each block
\r
310 - CmmStackLayout.layout
\r
311 Lay out the stack, returning an AreaMap
\r
312 type AreaMap = FiniteMap Area ByteOff
\r
313 -- Byte offset of the oldest byte of the Area,
\r
314 -- relative to the oldest byte of the Old Area
\r
316 - CmmStackLayout.manifestSP
\r
317 Manifest the stack pointer
\r
319 * Split into separate procedures
\r
320 - CmmProcPoint.procPointAnalysis
\r
321 Given set of proc points, which blocks are reachable from each
\r
322 Claim: too few proc-points => code duplication, but program still works??
\r
324 - CmmProcPoint.splitAtProcPoints
\r
325 Using this info, split into separate procedures
\r
327 - CmmBuildInfoTables.setInfoTableStackMap
\r
328 Attach stack maps to each info table
\r
331 ----------------------------------------------------
\r
333 ----------------------------------------------------
\r
335 Consider this program, which has a diamond control flow,
\r
336 with a call on one branch
\r
339 if b then { ... f(x) ...; q=5; goto J }
\r
340 else { ...; q=7; goto J }
\r
343 then the join point J is a "proc-point". So, is 'p' passed to J
\r
344 as a parameter? Or, if 'p' was saved on the stack anyway, perhaps
\r
345 to keep it alive across the call to h(), maybe 'p' gets communicated
\r
346 to J that way. This is an awkward choice. (We think that we currently
\r
347 never pass variables to join points via arguments.)
\r
349 Furthermore, there is *no way* to pass q to J in a register (other
\r
350 than a parameter register).
\r
352 What we want is to do register allocation across the whole caboodle.
\r
353 Then we could drop all the code that deals with the above awkward
\r
354 decisions about spilling variables across proc-points.
\r
356 Note that J doesn't need an info table.
\r
358 What we really want is for each LastCall (not LastJump/Ret)
\r
359 to have an info table. Note that ProcPoints that are not successors
\r
360 of calls don't need an info table.
\r
362 Figuring out proc-points
\r
363 ~~~~~~~~~~~~~~~~~~~~~~~~
\r
364 Proc-points are identified by
\r
365 CmmProcPoint.minimalProcPointSet/extendPPSet Although there isn't
\r
366 that much code, JD thinks that it could be done much more nicely using
\r
367 a dominator analysis, using the Dataflow Engine.
\r
369 ----------------------------------------------------
\r
371 ----------------------------------------------------
\r
373 * The code for a procedure f may refer to either the *closure*
\r
374 or the *entry point* of another top-level procedure g.
\r
375 If f is live, then so is g. f's SRT must include g's closure.
\r
377 * The CLabel for the entry-point/closure reveals whether g is
\r
378 a CAF (or refers to CAFs). See the IdLabel constructor of CLabel.
\r
380 * The CAF-ness of the original top-level defininions is figured out
\r
381 (by TidyPgm) before we generate C--. This CafInfo is only set for
\r
382 top-level Ids; nested bindings stay with MayHaveCafRefs.
\r
384 * Currently an SRT contains (only) pointers to (top-level) closures.
\r
386 * Consider this Core code
\r
387 f = \x -> let g = \y -> ...x...y...h1...
\r
389 and suppose that h1, h2 have IdInfo of MayHaveCafRefs.
\r
390 Therefore, so will f, But g will not (since it's nested).
\r
392 This generates C-- roughly like this:
\r
393 f_closure: .word f_entry
\r
394 f_entry() [info-tbl-for-f] { ...jump g_entry...jump h2... }
\r
395 g_entry() [info-tbl-for-g] { ...jump h1... }
\r
397 Note that there is no top-level closure for g (only an info table).
\r
398 This fact (whether or not there is a top-level closure) is recorded
\r
399 in the InfoTable attached to the CmmProc for f, g
\r
401 Any out-of-Group references to an IdLabel goes to
\r
402 a Proc whose InfoTable says "I have a top-level closure".
\r
404 A CmmProc whose InfoTable says "I do not have a top-level
\r
405 closure" is referred to only from its own Group.
\r
407 * So: info-tbl-for-f must have an SRT that keeps h1,h2 alive
\r
408 info-tbl-for-g must have an SRT that keeps h1 (only) alive
\r
410 But if we just look for the free CAF refs, we get:
\r
414 So we need to do a transitive closure thing to flesh out
\r
415 f's keep-alive refs to include h1.
\r
417 * The SRT info is the C_SRT field of Cmm.ClosureTypeInfo in a
\r
418 CmmInfoTable attached to each CmmProc. CmmCPS.toTops actually does
\r
419 the attaching, right at the end of the pipeline. The C_SRT part
\r
420 gives offsets within a single, shared table of closure pointers.
\r
422 * DECIDED: we can generate SRTs based on the final Cmm program
\r
423 without knowledge of how it is generated.
\r
425 ----------------------------------------------------
\r
427 ----------------------------------------------------
\r
429 See Note [Foreign calls] in CmmNode! This explains that a safe
\r
430 foreign call must do this:
\r
432 push info table (on thread stack) to describe frame
\r
433 make call (via C stack)
\r
435 restore thread state
\r
436 and explains why this expansion must be done late in the day.
\r
439 - Every foreign call is represented as a middle node
\r
441 - *Unsafe* foreign calls are simply "fat machine instructions"
\r
442 and are passed along to the native code generator
\r
444 - *Safe* foreign calls are "lowered" to unsafe calls by wrapping
\r
445 them in the above save/restore sequence. This step is done
\r
446 very late in the pipeline, just before handing to the native
\r
449 This lowering is done by BuildInfoTables.lowerSafeForeignCalls
\r
452 NEW PLAN for foreign calls:
\r
453 - Unsafe foreign calls remain as a middle node (fat machine instruction)
\r
454 Even the parameter passing is not lowered (just as machine instrs
\r
457 - Initially, safe foreign calls appear as LastCalls with
\r
460 ----------------------------------------------------
\r
461 Cmm representations
\r
462 ----------------------------------------------------
\r
465 The type [GenCmm d h g] represents a whole module,
\r
466 ** one list element per .o file **
\r
467 Without SplitObjs, the list has exactly one element
\r
469 newtype GenCmm d h g = Cmm [GenCmmTop d h g] -- A whole .o file
\r
470 data GenCmmTop d h g
\r
471 = CmmProc h g -- One procedure, graph d
\r
472 | CmmData <stuff> [d] -- Initialised data, items d
\r
474 Old and new piplines use different representations
\r
475 (CmmCvt.hs converts between the two)
\r
479 OLD BACK END representations (OldCmm.hs):
\r
480 type Cmm = GenCmm CmmStatic CmmInfo (ListGraph CmmStmt)
\r
482 newtype ListGraph i = ListGraph [GenBasicBlock i]
\r
484 data CmmStmt = Assign | Store | Return etc -- OLD BACK END ONLY
\r
487 Once the info tables are laid out, we replace CmmInfo with [CmmStatic]
\r
488 type RawCmm = GenCmm CmmStatic [CmmStatic] (ListGraph CmmStmt)
\r
489 which represents the info tables as data, that should
\r
490 immediately precede the code
\r
493 NEW BACK END representations
\r
494 * Uses Hoopl library, a zero-boot package
\r
495 * CmmNode defines a node of a flow graph.
\r
496 * Cmm defines CmmGraph, CmmTop, Cmm
\r
497 - CmmGraph is a closed/closed graph + an entry node.
\r
499 data CmmGraph = CmmGraph { g_entry :: BlockId
\r
500 , g_graph :: Graph CmmNode C C }
\r
502 - CmmTop is a top level chunk, specialization of GenCmmTop from CmmDecl.hs
\r
503 with CmmGraph as a flow graph.
\r
504 - Cmm is a collection of CmmTops.
\r
506 type Cmm = GenCmm CmmStatic CmmTopInfo CmmGraph
\r
507 type CmmTop = GenCmmTop CmmStatic CmmTopInfo CmmGraph
\r
509 - CmmTop uses CmmTopInfo, which is a CmmInfoTable and CmmStackInfo
\r
511 data CmmTopInfo = TopInfo {info_tbl :: CmmInfoTable, stack_info :: CmmStackInfo}
\r
515 data CmmStackInfo = StackInfo {arg_space :: ByteOff, updfr_space :: Maybe ByteOff}
\r
517 * arg_space = SP offset on entry
\r
518 * updfr_space space = SP offset on exit
\r
519 Once the staci is manifested, we could drom CmmStackInfo, ie. get
\r
520 GenCmm CmmStatic CmmInfoTable CmmGraph, but we do not do that currently.
\r
523 * MkGraph.hs: smart constructors for Cmm.hs
\r
524 Beware, the CmmAGraph defined here does not use AGraph from Hoopl,
\r
525 as CmmAGraph can be opened or closed at exit, See the notes in that module.
\r
529 CmmDecl.hs - GenCmm and GenCmmTop types
\r
530 CmmExpr.hs - defines the Cmm expression types
\r
531 - CmmExpr, CmmReg, CmmLit, LocalReg, GlobalReg
\r
532 - Area, AreaId etc (separate module?)
\r
533 CmmType.hs - CmmType, Width etc (saparate module?)
\r
534 CmmMachOp.hs - MachOp and CallishMachOp types
\r
536 BlockId.hs defines BlockId, BlockEnv, BlockSet
\r