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 - There is some silly stuff happening with the Sp. We end up with code like:
\r
19 Sp = Sp + 8; R1 = _vwf::I64; Sp = Sp -8
\r
20 Seems to be perhaps caused by the issue above but also maybe a optimisation
\r
23 - Proc pass all arguments on the stack, adding more code and slowing down things
\r
24 a lot. We either need to fix this or even better would be to get rid of
\r
27 - CmmInfo.cmmToRawCmm uses Old.Cmm, so it is called after converting Cmm.Cmm to
\r
28 Old.Cmm. We should abstract it to work on both representations, it needs only to
\r
29 convert a CmmInfoTable to [CmmStatic].
\r
31 - The MkGraph currenty uses a different semantics for <*> than Hoopl. Maybe
\r
32 we could convert codeGen/StgCmm* clients to the Hoopl's semantics?
\r
33 It's all deeply unsatisfactory.
\r
35 - Improve preformance of Hoopl.
\r
37 A nofib comparison of -fasm vs -fnewcodegen nofib compilation parameters
\r
38 (using the same ghc-cmm branch +libraries compiled by the old codegenerator)
\r
39 is at http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.oldghchoopl.txt
\r
40 - the code produced is 10.9% slower, the compilation is +118% slower!
\r
42 The same comparison with ghc-head with zip representation is at
\r
43 http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.oldghczip.txt
\r
44 - the code produced is 11.7% slower, the compilation is +78% slower.
\r
46 When compiling nofib, ghc-cmm + libraries compiled with -fnew-codegen
\r
47 is 23.7% slower (http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.hooplghcoldgen.txt).
\r
48 When compiling nofib, ghc-head + libraries compiled with -fnew-codegen
\r
49 is 31.4% slower (http://fox.auryn.cz/msrc/0517_hoopl/32bit.oldghcoldgen.zipghcoldgen.txt).
\r
51 So we generate a bit better code, but it takes us longer!
\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 - loweSafeForeignCall 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
80 - AsmCodeGen has post-native-cg branch eliminator (shortCutBranches);
\r
81 we ultimately want to share this with the Cmm branch eliminator.
\r
83 - At the moment, references to global registers like Hp are "lowered"
\r
84 late (in CgUtils.fixStgRegisters). We should do this early, in the
\r
85 new native codegen, much in the way that we lower calling conventions.
\r
86 Might need to be a bit sophisticated about aliasing.
\r
88 - Question: currently we lift procpoints to become separate
\r
89 CmmProcs. Do we still want to do this?
\r
91 NB: and advantage of continuing to do this is that
\r
92 we can do common-proc elimination!
\r
94 - Move to new Cmm rep:
\r
95 * Make native CG consume New Cmm;
\r
96 * Convert Old Cmm->New Cmm to keep old path alive
\r
97 * Produce New Cmm when reading in .cmm files
\r
99 - Consider module names
\r
101 - Top-level SRT threading is a bit ugly
\r
103 - Add type/newtype for CmmModule = [CmmGroup] -- A module
\r
104 CmmGroup = [CmmTop] -- A .o file
\r
105 CmmTop = Proc | Data -- A procedure or data
\r
107 - This is a *change*: currently a CmmGroup is one function's-worth of code
\r
108 regardless of SplitObjs. Question: can we *always* generate M.o if there
\r
109 is just one element in the list (rather than M/M1.o, M/M2.o etc)
\r
113 - See "CAFs" below; we want to totally refactor the way SRTs are calculated
\r
115 - Pull out Areas into its own module
\r
116 Parameterise AreaMap
\r
117 Add ByteWidth = Int
\r
118 type SubArea = (Area, ByteOff, ByteWidth)
\r
119 ByteOff should not be defined in SMRep -- that is too high up the hierarchy
\r
121 - SMRep should not be imported by any module in cmm/! Make it so.
\r
122 -- ByteOff etc ==> CmmExpr
\r
123 -- rET_SMALL etc ==> CmmInfo
\r
124 Check that there are no other imports from codeGen in cmm/
\r
126 - If you eliminate a label by branch chain elimination,
\r
127 what happens if there's an Area associated with that label?
\r
129 - Think about a non-flattened representation?
\r
132 * Use record fields for LastCall!
\r
133 * cml_ret_off should be a ByteOff
\r
135 LastCall (which has a successor) and
\r
136 LastJump (which does not, includes return?)
\r
137 - does not have cml_cont, cml_ret_args, cml_ret_off
\r
140 - expands into save/MidForeignCall/restore/goto
\r
141 - like any LastCall, target of the call gets an info table
\r
143 - JD: remind self of what goes wrong if you turn off the
\r
144 liveness of the update frame
\r
146 - Garbage-collect http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/CPS
\r
147 moving good stuff into
\r
148 http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/NewCodeGenPipeline
\r
151 - We believe that all of CmmProcPoint.addProcPointProtocols is dead. What
\r
152 goes wrong if we simply never call it?
\r
154 - Something fishy in CmmStackLayout.hs
\r
155 * In particular, 'getAreaSize' returns an AreaMap, but we *know* the width of
\r
156 LocalRegs, so it'd be better to return FiniteMap AreaId ByteWidth
\r
157 * setSuccSPs looks fishy. Rather than lookin in procPoints, it could
\r
158 just lookup the block in areaSize which, after all, has a binding
\r
159 for precisely successors of calls. All other blocks (including proc
\r
160 points that are not successors of a call, we think) can be treated
\r
161 uniformly: zero-size Area, and use inSP.
\r
164 - Currently AsmCodeGen top level calls AsmCodeGen.cmmToCmm, which is a small
\r
165 C-- optimiser. It has quite a lot of boilerplate folding code in AsmCodeGen
\r
166 (cmmBlockConFold, cmmStmtConFold, cmmExprConFold), before calling out to
\r
167 CmmOpt. ToDo: see what optimisations are being done; and do them before
\r
170 - Modularise the CPS pipeline; instead of ...; A;B;C; ...
\r
173 - Most of HscMain.tryNewCodeGen does not belong in HscMain. Instead
\r
176 processCmm [including generating "raw" cmm]
\r
182 - If we stick CAF and stack liveness info on a LastCall node (not LastRet/Jump)
\r
183 then all CAF and stack liveness stuff be completed before we split
\r
184 into separate C procedures.
\r
187 compute and attach liveness into to LastCall
\r
188 right at end, split, cvt to old rep
\r
189 [must split before cvt, because old rep is not expressive enough]
\r
192 when old rep disappears,
\r
193 move the whole splitting game into the C back end *only*
\r
194 (guided by the procpoint set)
\r
196 ----------------------------------------------------
\r
198 ----------------------------------------------------
\r
200 -------- Testing stuff ------------
\r
201 HscMain.optionallyConvertAndOrCPS
\r
203 DynFlags: -fconvert-to-zipper-and-back, -frun-cpsz
\r
205 -------- Moribund stuff ------------
\r
206 OldCmm.hs Definition of flowgraph of old representation
\r
207 OldCmmUtil.hs Utilites that operates mostly on on CmmStmt
\r
208 OldPprCmm.hs Pretty print for CmmStmt, GenBasicBlock and ListGraph
\r
209 CmmCvt.hs Conversion between old and new Cmm reps
\r
210 CmmOpt.hs Hopefully-redundant optimiser
\r
212 -------- Stuff to keep ------------
\r
213 CmmCPS.hs Driver for new pipeline
\r
215 CmmLive.hs Liveness analysis, dead code elim
\r
216 CmmProcPoint.hs Identifying and splitting out proc-points
\r
218 CmmSpillReload.hs Save and restore across calls
\r
220 CmmCommonBlockElim.hs Common block elim
\r
221 CmmContFlowOpt.hs Other optimisations (branch-chain, merging)
\r
223 CmmBuildInfoTables.hs New info-table
\r
224 CmmStackLayout.hs and stack layout
\r
226 CmmInfo.hs Defn of InfoTables, and conversion to exact byte layout
\r
228 ---------- Cmm data types --------------
\r
229 Cmm.hs Cmm instantiations of dataflow graph framework
\r
230 MkGraph.hs Interface for building Cmm for codeGen/Stg*.hs modules
\r
232 CmmDecl.hs Shared Cmm types of both representations
\r
233 CmmExpr.hs Type of Cmm expression
\r
234 CmmType.hs Type of Cmm types and their widths
\r
235 CmmMachOp.hs MachOp type and accompanying utilities
\r
240 PprC.hs Pretty print Cmm in C syntax
\r
241 PprCmm.hs Pretty printer for CmmGraph.
\r
242 PprCmmDecl.hs Pretty printer for common Cmm types.
\r
243 PprCmmExpr.hs Pretty printer for Cmm expressions.
\r
246 BlockId.hs BlockId, BlockEnv, BlockSet
\r
248 ----------------------------------------------------
\r
249 Top-level structure
\r
250 ----------------------------------------------------
\r
252 * New codgen called in HscMain.hscGenHardCode, by calling HscMain.tryNewCodeGen,
\r
253 enabled by -fnew-codegen (Opt_TryNewCodeGen)
\r
255 THEN it calls CmmInfo.cmmToRawCmm to lay out the details of info tables
\r
256 type Cmm = GenCmm CmmStatic CmmInfo (ListGraph CmmStmt)
\r
257 type RawCmm = GenCmm CmmStatic [CmmStatic] (ListGraph CmmStmt)
\r
259 * HscMain.tryNewCodeGen
\r
260 - STG->Cmm: StgCmm.codeGen (new codegen)
\r
261 - Optimise: CmmContFlowOpt (simple optimisations, very self contained)
\r
262 - Cps convert: CmmCPS.protoCmmCPS
\r
263 - Optimise: CmmContFlowOpt again
\r
264 - Convert: CmmCvt.cmmOfZgraph (convert to old rep) very self contained
\r
266 * StgCmm.hs The new STG -> Cmm conversion code generator
\r
267 Lots of modules StgCmmXXX
\r
270 ----------------------------------------------------
\r
271 CmmCPS.protoCmmCPS The new pipeline
\r
272 ----------------------------------------------------
\r
274 CmmCPS.protoCmmCPS:
\r
275 1. Do cpsTop for each procedures separately
\r
276 2. Build SRT representation; this spans multiple procedures
\r
277 (unless split-objs)
\r
280 * CmmCommonBlockElim.elimCommonBlocks:
\r
281 eliminate common blocks
\r
283 * CmmProcPoint.minimalProcPointSet
\r
284 identify proc-points
\r
287 * CmmProcPoint.addProcPointProtocols
\r
288 something to do with the MA optimisation
\r
289 probably entirely unnecessary
\r
291 * Spill and reload:
\r
292 - CmmSpillReload.dualLivenessWithInsertion
\r
293 insert spills/reloads across
\r
295 Branches to proc-points
\r
296 Now sink those reloads:
\r
297 - CmmSpillReload.insertLateReloads
\r
298 - CmmSpillReload.removeDeadAssignmentsAndReloads
\r
300 * CmmStackLayout.stubSlotsOnDeath
\r
301 debug only: zero out dead slots when they die
\r
304 - CmmStackLayout.lifeSlotAnal:
\r
305 find which sub-areas are live on entry to each block
\r
307 - CmmStackLayout.layout
\r
308 Lay out the stack, returning an AreaMap
\r
309 type AreaMap = FiniteMap Area ByteOff
\r
310 -- Byte offset of the oldest byte of the Area,
\r
311 -- relative to the oldest byte of the Old Area
\r
313 - CmmStackLayout.manifestSP
\r
314 Manifest the stack pointer
\r
316 * Split into separate procedures
\r
317 - CmmProcPoint.procPointAnalysis
\r
318 Given set of proc points, which blocks are reachable from each
\r
319 Claim: too few proc-points => code duplication, but program still works??
\r
321 - CmmProcPoint.splitAtProcPoints
\r
322 Using this info, split into separate procedures
\r
324 - CmmBuildInfoTables.setInfoTableStackMap
\r
325 Attach stack maps to each info table
\r
328 ----------------------------------------------------
\r
330 ----------------------------------------------------
\r
332 Consider this program, which has a diamond control flow,
\r
333 with a call on one branch
\r
336 if b then { ... f(x) ...; q=5; goto J }
\r
337 else { ...; q=7; goto J }
\r
340 then the join point J is a "proc-point". So, is 'p' passed to J
\r
341 as a parameter? Or, if 'p' was saved on the stack anyway, perhaps
\r
342 to keep it alive across the call to h(), maybe 'p' gets communicated
\r
343 to J that way. This is an awkward choice. (We think that we currently
\r
344 never pass variables to join points via arguments.)
\r
346 Furthermore, there is *no way* to pass q to J in a register (other
\r
347 than a paramter register).
\r
349 What we want is to do register allocation across the whole caboodle.
\r
350 Then we could drop all the code that deals with the above awkward
\r
351 decisions about spilling variables across proc-points.
\r
353 Note that J doesn't need an info table.
\r
355 What we really want is for each LastCall (not LastJump/Ret)
\r
356 to have an info table. Note that ProcPoints that are not successors
\r
357 of calls don't need an info table.
\r
359 Figuring out proc-points
\r
360 ~~~~~~~~~~~~~~~~~~~~~~~~
\r
361 Proc-points are identified by
\r
362 CmmProcPoint.minimalProcPointSet/extendPPSet Although there isn't
\r
363 that much code, JD thinks that it could be done much more nicely using
\r
364 a dominator analysis, using the Dataflow Engine.
\r
366 ----------------------------------------------------
\r
368 ----------------------------------------------------
\r
370 * The code for a procedure f may refer to either the *closure*
\r
371 or the *entry point* of another top-level procedure g.
\r
372 If f is live, then so is g. f's SRT must include g's closure.
\r
374 * The CLabel for the entry-point/closure reveals whether g is
\r
375 a CAF (or refers to CAFs). See the IdLabel constructor of CLabel.
\r
377 * The CAF-ness of the original top-level defininions is figured out
\r
378 (by TidyPgm) before we generate C--. This CafInfo is only set for
\r
379 top-level Ids; nested bindings stay with MayHaveCafRefs.
\r
381 * Currently an SRT contains (only) pointers to (top-level) closures.
\r
383 * Consider this Core code
\r
384 f = \x -> let g = \y -> ...x...y...h1...
\r
386 and suppose that h1, h2 have IdInfo of MayHaveCafRefs.
\r
387 Therefore, so will f, But g will not (since it's nested).
\r
389 This generates C-- roughly like this:
\r
390 f_closure: .word f_entry
\r
391 f_entry() [info-tbl-for-f] { ...jump g_entry...jump h2... }
\r
392 g_entry() [info-tbl-for-g] { ...jump h1... }
\r
394 Note that there is no top-level closure for g (only an info table).
\r
395 This fact (whether or not there is a top-level closure) is recorded
\r
396 in the InfoTable attached to the CmmProc for f, g
\r
398 Any out-of-Group references to an IdLabel goes to
\r
399 a Proc whose InfoTable says "I have a top-level closure".
\r
401 A CmmProc whose InfoTable says "I do not have a top-level
\r
402 closure" is referred to only from its own Group.
\r
404 * So: info-tbl-for-f must have an SRT that keeps h1,h2 alive
\r
405 info-tbl-for-g must have an SRT that keeps h1 (only) alive
\r
407 But if we just look for the free CAF refs, we get:
\r
411 So we need to do a transitive closure thing to flesh out
\r
412 f's keep-alive refs to include h1.
\r
414 * The SRT info is the C_SRT field of Cmm.ClosureTypeInfo in a
\r
415 CmmInfoTable attached to each CmmProc. CmmCPS.toTops actually does
\r
416 the attaching, right at the end of the pipeline. The C_SRT part
\r
417 gives offsets within a single, shared table of closure pointers.
\r
419 * DECIDED: we can generate SRTs based on the final Cmm program
\r
420 without knowledge of how it is generated.
\r
422 ----------------------------------------------------
\r
424 ----------------------------------------------------
\r
426 See Note [Foreign calls] in CmmNode! This explains that a safe
\r
427 foreign call must do this:
\r
429 push info table (on thread stack) to describe frame
\r
430 make call (via C stack)
\r
432 restore thread state
\r
433 and explains why this expansion must be done late in the day.
\r
436 - Every foreign call is represented as a middle node
\r
438 - *Unsafe* foreign calls are simply "fat machine instructions"
\r
439 and are passed along to the native code generator
\r
441 - *Safe* foreign calls are "lowered" to unsafe calls by wrapping
\r
442 them in the above save/restore sequence. This step is done
\r
443 very late in the pipeline, just before handing to the native
\r
446 This lowering is done by BuildInfoTables.lowerSafeForeignCalls
\r
449 NEW PLAN for foreign calls:
\r
450 - Unsafe foreign calls remain as a middle node (fat machine instruction)
\r
451 Even the parameter passing is not lowered (just as machine instrs
\r
454 - Initially, safe foreign calls appear as LastCalls with
\r
457 ----------------------------------------------------
\r
458 Cmm representations
\r
459 ----------------------------------------------------
\r
462 The type [GenCmm d h g] represents a whole module,
\r
463 ** one list element per .o file **
\r
464 Without SplitObjs, the list has exactly one element
\r
466 newtype GenCmm d h g = Cmm [GenCmmTop d h g] -- A whole .o file
\r
467 data GenCmmTop d h g
\r
468 = CmmProc h g -- One procedure, graph d
\r
469 | CmmData <stuff> [d] -- Initialised data, items d
\r
471 Old and new piplines use different representations
\r
472 (CmmCvt.hs converts between the two)
\r
476 OLD BACK END representations (OldCmm.hs):
\r
477 type Cmm = GenCmm CmmStatic CmmInfo (ListGraph CmmStmt)
\r
479 newtype ListGraph i = ListGraph [GenBasicBlock i]
\r
481 data CmmStmt = Assign | Store | Return etc -- OLD BACK END ONLY
\r
484 Once the info tables are laid out, we replace CmmInfo with [CmmStatic]
\r
485 type RawCmm = GenCmm CmmStatic [CmmStatic] (ListGraph CmmStmt)
\r
486 which represents the info tables as data, that should
\r
487 immediately precede the code
\r
490 NEW BACK END representations
\r
491 * Uses Hoopl library, a zero-boot package
\r
492 * CmmNode defines a node of a flow graph.
\r
493 * Cmm defines CmmGraph, CmmTop, Cmm
\r
494 - CmmGraph is a closed/closed graph + an entry node.
\r
496 data CmmGraph = CmmGraph { g_entry :: BlockId
\r
497 , g_graph :: Graph CmmNode C C }
\r
499 - CmmTop is a top level chunk, specialization of GenCmmTop from CmmDecl.hs
\r
500 with CmmGraph as a flow graph.
\r
501 - Cmm is a collection of CmmTops.
\r
503 type Cmm = GenCmm CmmStatic CmmTopInfo CmmGraph
\r
504 type CmmTop = GenCmmTop CmmStatic CmmTopInfo CmmGraph
\r
506 - CmmTop uses CmmTopInfo, which is a CmmInfoTable and CmmStackInfo
\r
508 data CmmTopInfo = TopInfo {info_tbl :: CmmInfoTable, stack_info :: CmmStackInfo}
\r
512 data CmmStackInfo = StackInfo {arg_space :: ByteOff, updfr_space :: Maybe ByteOff}
\r
514 * arg_space = SP offset on entry
\r
515 * updfr_space space = SP offset on exit
\r
516 Once the staci is manifested, we could drom CmmStackInfo, ie. get
\r
517 GenCmm CmmStatic CmmInfoTable CmmGraph, but we do not do that currently.
\r
520 * MkGraph.hs: smart constructors for Cmm.hs
\r
521 Beware, the CmmAGraph defined here does not use AGraph from Hoopl,
\r
522 as CmmAGraph can be opened or closed at exit, See the notes in that module.
\r
526 CmmDecl.hs - GenCmm and GenCmmTop types
\r
527 CmmExpr.hs - defines the Cmm expression types
\r
528 - CmmExpr, CmmReg, CmmLit, LocalReg, GlobalReg
\r
529 - Area, AreaId etc (separate module?)
\r
530 CmmType.hs - CmmType, Width etc (saparate module?)
\r
531 CmmMachOp.hs - MachOp and CallishMachOp types
\r
533 BlockId.hs defines BlockId, BlockEnv, BlockSet
\r