Mikolaj Konarski pushed to branch wip/ncg-comment-fixes at Glasgow Haskell Compiler / GHC
Commits:
-
5403c34f
by Mikolaj Konarski at 2026-11-16T18:00:07+01:00
29 changed files:
- compiler/GHC/Cmm/Node.hs
- compiler/GHC/Cmm/Reg.hs
- compiler/GHC/CmmToAsm.hs
- compiler/GHC/CmmToAsm/BlockLayout.hs
- compiler/GHC/CmmToAsm/CFG.hs
- compiler/GHC/CmmToAsm/Instr.hs
- compiler/GHC/CmmToAsm/Monad.hs
- compiler/GHC/CmmToAsm/PIC.hs
- compiler/GHC/CmmToAsm/Reg/Graph.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Base.hs
- compiler/GHC/CmmToAsm/Reg/Graph/Spill.hs
- compiler/GHC/CmmToAsm/Reg/Graph/SpillCost.hs
- compiler/GHC/CmmToAsm/Reg/Linear.hs
- compiler/GHC/CmmToAsm/Reg/Linear/AArch64.hs
- compiler/GHC/CmmToAsm/Reg/Linear/Base.hs
- compiler/GHC/CmmToAsm/Reg/Linear/FreeRegs.hs
- compiler/GHC/CmmToAsm/Reg/Linear/JoinToTargets.hs
- compiler/GHC/CmmToAsm/Reg/Linear/StackMap.hs
- compiler/GHC/CmmToAsm/Reg/Linear/State.hs
- compiler/GHC/CmmToAsm/Reg/Liveness.hs
- compiler/GHC/CmmToAsm/Reg/Target.hs
- compiler/GHC/CmmToAsm/X86/CodeGen.hs
- compiler/GHC/CmmToAsm/X86/Instr.hs
- compiler/GHC/CmmToAsm/X86/Ppr.hs
- compiler/GHC/CmmToAsm/X86/Regs.hs
- compiler/GHC/Platform/Reg.hs
- compiler/GHC/Platform/Reg/Class.hs
- compiler/GHC/StgToCmm/Monad.hs
- docs/users_guide/using-optimisation.rst
Changes:
| ... | ... | @@ -894,8 +894,8 @@ instance Eq CmmTickScope where |
| 894 | 894 | -- This is still deterministic because
|
| 895 | 895 | -- the order is the same for equal lists
|
| 896 | 896 | |
| 897 | --- This is non-deterministic but we do not currently support deterministic
|
|
| 898 | --- code-generation. See Note [Unique Determinism and code generation]
|
|
| 897 | +-- This compares uniques, so the order is deterministic only if the uniques are:
|
|
| 898 | +-- see Note [Unique Determinism and code generation].
|
|
| 899 | 899 | -- See Note [No Ord for Unique]
|
| 900 | 900 | instance Ord CmmTickScope where
|
| 901 | 901 | compare GlobalScope GlobalScope = EQ
|
| ... | ... | @@ -121,8 +121,8 @@ instance Eq LocalReg where |
| 121 | 121 | instance Outputable LocalReg where
|
| 122 | 122 | ppr e = pprLocalReg e
|
| 123 | 123 | |
| 124 | --- This is non-deterministic but we do not currently support deterministic
|
|
| 125 | --- code-generation. See Note [Unique Determinism and code generation]
|
|
| 124 | +-- This compares uniques, so the order is deterministic only if the uniques are:
|
|
| 125 | +-- see Note [Unique Determinism and code generation].
|
|
| 126 | 126 | -- See Note [No Ord for Unique]
|
| 127 | 127 | instance Ord LocalReg where
|
| 128 | 128 | compare (LocalReg u1 _) (LocalReg u2 _) = nonDetCmpUnique u1 u2
|
| ... | ... | @@ -890,8 +890,8 @@ apply_mapping ncgImpl ufm (CmmProc info lbl live (ListGraph blocks)) |
| 890 | 890 | -- -----------------------------------------------------------------------------
|
| 891 | 891 | -- Instruction selection
|
| 892 | 892 | |
| 893 | --- Native code instruction selection for a chunk of stix code. For
|
|
| 894 | --- this part of the computation, we switch from the UniqSM monad to
|
|
| 893 | +-- Native code instruction selection for a chunk of Cmm. For
|
|
| 894 | +-- this part of the computation, we switch from the UniqDSM monad to
|
|
| 895 | 895 | -- the NatM monad. The latter carries not only a Unique, but also an
|
| 896 | 896 | -- Int denoting the current C stack pointer offset in the generated
|
| 897 | 897 | -- code; this is needed for creating correct spill offsets on
|
| ... | ... | @@ -56,7 +56,7 @@ import GHC.Types.Unique.DSM (UniqDSM) |
| 56 | 56 | * Compute a CFG based on the Cmm AST, see getCfgProc.
|
| 57 | 57 | This CFG will have edge weights representing a guess
|
| 58 | 58 | on how important they are.
|
| 59 | - * After we convert Cmm to Asm we run `optimizeCFG` which
|
|
| 59 | + * After register allocation and shortcutting we run `optimizeCFG` which
|
|
| 60 | 60 | adds a few more "educated guesses" to the equation.
|
| 61 | 61 | * Then we run loop analysis on the CFG (`loopInfo`) which tells us
|
| 62 | 62 | about loop headers, loop nesting levels and the sort.
|
| ... | ... | @@ -265,7 +265,8 @@ import GHC.Types.Unique.DSM (UniqDSM) |
| 265 | 265 | We apply a small modifier to them to ensure edges for which we can
|
| 266 | 266 | eliminate the overhead completely are considered first. See also #18053.
|
| 267 | 267 | |
| 268 | - * Edges constituted by a call are ignored.
|
|
| 268 | + * Edges constituted by a call are ignored while the chains are built, and
|
|
| 269 | + count a 64th of their weight when the chains are merged at the end.
|
|
| 269 | 270 | |
| 270 | 271 | Considering these hardly helped with performance and ignoring
|
| 271 | 272 | them helps quite a bit to improve compiler performance.
|
| ... | ... | @@ -560,22 +560,29 @@ addNodesBetween weights m updates = |
| 560 | 560 | * Unconditional Control Transfer - They will always
|
| 561 | 561 | transfer control to their target. Unless there is a info table
|
| 562 | 562 | we can turn the jump into a fallthrough as well.
|
| 563 | - We use 20k as default, so it's easy to spot if values have been
|
|
| 564 | - modified but unlikely that we run into issues with overflow.
|
|
| 563 | + We use 1000 as default ('defaultWeights' in GHC.CmmToAsm.CFG.Weight).
|
|
| 565 | 564 | * If branches (likely) - We assume branches marked as likely
|
| 566 | - are taken more than 80% of the time.
|
|
| 565 | + are taken 75% of the time: the default weights give the likely side 900 and
|
|
| 566 | + the other side 300.
|
|
| 567 | 567 | By ranking them below unconditional jumps we make sure we
|
| 568 | 568 | prefer the unconditional if there is a conditional and
|
| 569 | 569 | unconditional edge towards a block.
|
| 570 | - * If branches (regular) - The false branch can potentially be turned
|
|
| 571 | - into a fallthrough so we prefer it slightly over the true branch.
|
|
| 572 | - * Unlikely branches - These can be assumed to be taken less than 20%
|
|
| 573 | - of the time. So we given them one of the lowest priorities.
|
|
| 570 | + * If branches (regular) - Both sides get the same weight, but on x86-64 the
|
|
| 571 | + instruction selector adds 3 to the weight of a floating-point comparison's
|
|
| 572 | + false edge (genCondBranch' in GHC.CmmToAsm.X86.CodeGen). If the weights are
|
|
| 573 | + still equal after optHsPatterns' other adjustments, favourFewerPreds adds
|
|
| 574 | + one to the side with fewer predecessors that could fall through into it, and
|
|
| 575 | + subtracts one from the other side.
|
|
| 576 | + * Unlikely branches - By the same weights we assume that these are taken 25%
|
|
| 577 | + of the time. So we give them one of the lowest priorities.
|
|
| 574 | 578 | * Switches - Switches at this level are implemented as jump tables
|
| 575 | 579 | so have a larger number of successors. So without more information
|
| 576 | 580 | we can only say that each individual successor is unlikely to be
|
| 577 | 581 | jumped to and we rank them accordingly.
|
| 578 | - * Calls - We currently ignore calls completely:
|
|
| 582 | + * Calls - A call's edge to its continuation gets callWeight. The
|
|
| 583 | + layout leaves these edges out when it builds chains (directEdges in
|
|
| 584 | + GHC.CmmToAsm.BlockLayout), and divides their weight by 64 when it merges the
|
|
| 585 | + chains at the end:
|
|
| 579 | 586 | * By the time we return from a call there is a good chance
|
| 580 | 587 | that the address we return to has already been evicted from
|
| 581 | 588 | cache eliminating a main advantage sequential placement brings.
|
| ... | ... | @@ -659,7 +666,7 @@ getCfg platform weights graph = |
| 659 | 666 | |
| 660 | 667 | blocks = revPostorder graph :: [CmmBlock]
|
| 661 | 668 | |
| 662 | ---Find back edges by BFS
|
|
| 669 | +--Find back edges by a depth-first search (classifyEdges)
|
|
| 663 | 670 | findBackEdges :: HasDebugCallStack => BlockId -> CFG -> Edges
|
| 664 | 671 | findBackEdges root cfg =
|
| 665 | 672 | --pprTraceIt "Backedges:" $
|
| ... | ... | @@ -991,9 +998,9 @@ check we do not apply any of the heuristics. |
| 991 | 998 | The reason is simple: They look like loops in the control flow graph
|
| 992 | 999 | but are usually never entered, and if at most once.
|
| 993 | 1000 | |
| 994 | -Currently implemented is a heuristic to predict that we do not exit
|
|
| 995 | -loops (lehPredicts) and one to predict that backedges are more likely
|
|
| 996 | -than any other edge.
|
|
| 1001 | +Three heuristics are implemented: that we do not exit loops (lehPredicts), that
|
|
| 1002 | +a comparison with a literal is unequal (ohPredicts), and that a back edge is
|
|
| 1003 | +more likely than any other edge.
|
|
| 997 | 1004 | |
| 998 | 1005 | The back edge case is special as it supersedes any other heuristic if it
|
| 999 | 1006 | applies.
|
| ... | ... | @@ -1055,7 +1062,9 @@ staticBranchPrediction _root (LoopInfo l_backEdges loopLevels l_loops) cfg = |
| 1055 | 1062 | | length successors /= 2
|
| 1056 | 1063 | = cfg
|
| 1057 | 1064 | |
| 1058 | - -- Only backedges - no need to adjust
|
|
| 1065 | + -- Only backedges - no need to adjust. This case also keeps the weights
|
|
| 1066 | + -- of a back edge beside a heap or stack check's edge, which the mix
|
|
| 1067 | + -- case above excludes.
|
|
| 1059 | 1068 | | length m > 0
|
| 1060 | 1069 | = cfg
|
| 1061 | 1070 | |
| ... | ... | @@ -1116,7 +1125,9 @@ staticBranchPrediction _root (LoopInfo l_backEdges loopLevels l_loops) cfg = |
| 1116 | 1125 | |
| 1117 | 1126 | -- Heuristics return nothing if they don't say anything about this branch
|
| 1118 | 1127 | -- or Just (prob_s1) where prob_s1 is the likelihood for s1 to be the
|
| 1119 | - -- taken branch. s1 is the branch in the true case.
|
|
| 1128 | + -- taken branch. s1 is the successor with the smaller label, as
|
|
| 1129 | + -- getSuccessorEdges orders them, and need not be the true branch: a
|
|
| 1130 | + -- heuristic that needs the true branch looks it up, as ohPredicts does.
|
|
| 1120 | 1131 | |
| 1121 | 1132 | -- Loop exit heuristic.
|
| 1122 | 1133 | -- We are unlikely to leave a loop unless it's to enter another one.
|
| ... | ... | @@ -29,7 +29,15 @@ class Instruction instr where |
| 29 | 29 | |
| 30 | 30 | -- | Get the registers that are being used by this instruction.
|
| 31 | 31 | -- regUsage doesn't need to do any trickery for jumps and such.
|
| 32 | - -- Just state precisely the regs read and written by that insn.
|
|
| 32 | + -- State the regs read and written by that insn as the register
|
|
| 33 | + -- allocators are to see them, which need not be what the hardware
|
|
| 34 | + -- does. x86's regUsageOfInstr says that a zeroing XOR, VXOR,
|
|
| 35 | + -- PXOR or VPXOR reads nothing, and that at the format FF32 or
|
|
| 36 | + -- FF64 a MOV between registers or a MOVHLPS does not read its
|
|
| 37 | + -- destination, whose upper part the hardware keeps; it records
|
|
| 38 | + -- the float and vector registers a call clobbers at the format
|
|
| 39 | + -- FF64 whatever their width, and leaves out the real registers the
|
|
| 40 | + -- allocators do not hand out.
|
|
| 33 | 41 | -- The consequences of control flow transfers, as far as register
|
| 34 | 42 | -- allocation goes, are taken care of by the register allocator.
|
| 35 | 43 | --
|
| ... | ... | @@ -117,7 +117,7 @@ data NcgImpl statics instr jumpDest = NcgImpl { |
| 117 | 117 | -- ^ given the instruction sequence of a block, produce a list of
|
| 118 | 118 | -- the block's 'UnwindPoint's
|
| 119 | 119 | -- See Note [What is this unwinding business?] in "GHC.Cmm.DebugBlock"
|
| 120 | - -- and Note [Unwinding information in the NCG] in this module.
|
|
| 120 | + -- and Note [Unwinding information in the NCG] in "GHC.CmmToAsm".
|
|
| 121 | 121 | invertCondBranches :: Maybe CFG -> LabelMap RawCmmStatics -> [NatBasicBlock instr]
|
| 122 | 122 | -> [NatBasicBlock instr]
|
| 123 | 123 | -- ^ Turn the sequence of @jcc l1; jmp l2@ into @jncc l2; \<block_l1>@
|
| ... | ... | @@ -7,12 +7,12 @@ |
| 7 | 7 | |
| 8 | 8 | Things outside this module which are related to this:
|
| 9 | 9 | |
| 10 | - + module CLabel
|
|
| 10 | + + GHC.Cmm.CLabel
|
|
| 11 | 11 | - PIC base label (pretty printed as local label 1)
|
| 12 | 12 | - DynamicLinkerLabels - several kinds:
|
| 13 | 13 | CodeStub, SymbolPtr, GotSymbolPtr, GotSymbolOffset
|
| 14 | 14 | - labelDynamic predicate
|
| 15 | - + module Cmm
|
|
| 15 | + + GHC.Cmm.Reg and GHC.Cmm.Expr
|
|
| 16 | 16 | - The GlobalReg datatype has a PicBaseReg constructor
|
| 17 | 17 | - The CmmLit datatype has a CmmLabelDiffOff constructor
|
| 18 | 18 | + codeGen & RTS
|
| ... | ... | @@ -22,15 +22,18 @@ |
| 22 | 22 | because Win32 doesn't support external references in data sections.
|
| 23 | 23 | TODO: make sure this still works, it might be bitrotted
|
| 24 | 24 | + NCG
|
| 25 | - - The cmmToCmm pass in AsmCodeGen calls cmmMakeDynamicReference for all
|
|
| 25 | + - cmmToCmm in GHC.Cmm.GenericOpt calls cmmMakeDynamicReference for all
|
|
| 26 | 26 | labels.
|
| 27 | - - nativeCodeGen calls pprImportedSymbol and pprGotDeclaration to output
|
|
| 28 | - all the necessary stuff for imported symbols.
|
|
| 27 | + - makeImportsDoc in GHC.CmmToAsm calls pprImportedSymbol and
|
|
| 28 | + pprGotDeclaration to output all the necessary stuff for imported
|
|
| 29 | + symbols.
|
|
| 29 | 30 | - The NCG monad keeps track of a list of imported symbols.
|
| 30 | - - MachCodeGen invokes initializePicBase to generate code to initialize
|
|
| 31 | - the PIC base register when needed.
|
|
| 32 | - - MachCodeGen calls cmmMakeDynamicReference whenever it uses a CLabel
|
|
| 33 | - that wasn't in the original Cmm code (e.g. floating point literals).
|
|
| 31 | + - The x86 and PowerPC instruction selectors invoke initializePicBase_x86
|
|
| 32 | + and initializePicBase_ppc to generate code to initialize the PIC base
|
|
| 33 | + register when needed.
|
|
| 34 | + - The instruction selectors call cmmMakeDynamicReference whenever they
|
|
| 35 | + use a CLabel that wasn't in the original Cmm code (e.g. floating point
|
|
| 36 | + literals).
|
|
| 34 | 37 | -}
|
| 35 | 38 | |
| 36 | 39 | module GHC.CmmToAsm.PIC (
|
| ... | ... | @@ -131,9 +131,8 @@ regAlloc_spin config spinCount triv regsFree slotsFree slotsCount debug_codeGrap |
| 131 | 131 | $$ text "regsFree = " <> (hcat $ punctuate space $ map ppr
|
| 132 | 132 | $ nonDetEltsUniqSet $ unionManyUniqSets
|
| 133 | 133 | $ nonDetEltsUFM regsFree)
|
| 134 | - -- This is non-deterministic but we do not
|
|
| 135 | - -- currently support deterministic code-generation.
|
|
| 136 | - -- See Note [Unique Determinism and code generation]
|
|
| 134 | + -- The order decides only how the panic message lists the free
|
|
| 135 | + -- registers (Note [Unique Determinism and code generation])
|
|
| 137 | 136 | $$ text "slotsFree = " <> ppr (sizeUniqSet slotsFree))
|
| 138 | 137 | |
| 139 | 138 | -- Build the register conflict graph from the cmm code.
|
| ... | ... | @@ -193,7 +192,7 @@ regAlloc_spin config spinCount triv regsFree slotsFree slotsCount debug_codeGrap |
| 193 | 192 | |
| 194 | 193 | -- Coloring was successful because no registers needed to be spilled.
|
| 195 | 194 | then do
|
| 196 | - -- if -fasm-lint is turned on then validate the graph.
|
|
| 195 | + -- if -dasm-lint is turned on then validate the graph.
|
|
| 197 | 196 | -- This checks for bugs in the graph allocator itself.
|
| 198 | 197 | let graph_colored_lint =
|
| 199 | 198 | if ncgAsmLinting config
|
| ... | ... | @@ -254,7 +253,7 @@ regAlloc_spin config spinCount triv regsFree slotsFree slotsCount debug_codeGrap |
| 254 | 253 | -- Coloring was unsuccessful. We need to spill some register to the
|
| 255 | 254 | -- stack, make a new graph, and try to color it again.
|
| 256 | 255 | else do
|
| 257 | - -- if -fasm-lint is turned on then validate the graph
|
|
| 256 | + -- if -dasm-lint is turned on then validate the graph
|
|
| 258 | 257 | let graph_colored_lint =
|
| 259 | 258 | if ncgAsmLinting config
|
| 260 | 259 | then Color.validateGraph (text "")
|
| ... | ... | @@ -96,9 +96,8 @@ worst regsOfClass regAlias neighbors classN classC |
| 96 | 96 | = let regAliasS regs = unionManyUniqSets
|
| 97 | 97 | $ map regAlias
|
| 98 | 98 | $ nonDetEltsUniqSet regs
|
| 99 | - -- This is non-deterministic but we do not
|
|
| 100 | - -- currently support deterministic code-generation.
|
|
| 101 | - -- See Note [Unique Determinism and code generation]
|
|
| 99 | + -- The union does not depend on the order
|
|
| 100 | + -- (Note [Unique Determinism and code generation])
|
|
| 102 | 101 | |
| 103 | 102 | -- all the regs in classes N, C
|
| 104 | 103 | regsN = regsOfClass classN
|
| ... | ... | @@ -79,9 +79,9 @@ regSpill platform code slotsFree slotCount regs |
| 79 | 79 | -- See Note [UniqFM and the register allocator]
|
| 80 | 80 | $ listToUFM
|
| 81 | 81 | $ zip (nonDetEltsUniqSet regs) slots :: UniqFM Reg Int
|
| 82 | - -- This is non-deterministic but we do not
|
|
| 83 | - -- currently support deterministic code-generation.
|
|
| 84 | - -- See Note [Unique Determinism and code generation]
|
|
| 82 | + -- The order of both lists decides which free slots
|
|
| 83 | + -- are taken and which spilled vreg gets each
|
|
| 84 | + -- (Note [Unique Determinism and code generation])
|
|
| 85 | 85 | |
| 86 | 86 | -- Grab the unique supply from the monad.
|
| 87 | 87 | UDSM $ \us ->
|
| ... | ... | @@ -122,9 +122,9 @@ slurpSpillCostInfo platform cfg cmm |
| 122 | 122 | = do
|
| 123 | 123 | -- Increment the lifetime counts for regs live on entry to this instr.
|
| 124 | 124 | mapM_ incLifetime $ nonDetEltsUniqSet rsLiveEntry
|
| 125 | - -- This is non-deterministic but we do not
|
|
| 126 | - -- currently support deterministic code-generation.
|
|
| 127 | - -- See Note [Unique Determinism and code generation]
|
|
| 125 | + -- Each vreg's lifetime is counted on
|
|
| 126 | + -- its own, so the order does not matter
|
|
| 127 | + -- (Note [Unique Determinism and code generation])
|
|
| 128 | 128 | |
| 129 | 129 | -- Increment counts for what regs were read/written from.
|
| 130 | 130 | -- See Note [Cached register usage in liveness annotations] in GHC.CmmToAsm.Reg.Liveness
|
| ... | ... | @@ -178,8 +178,6 @@ chooseSpill info graph |
| 178 | 178 | -- cost = sum loadCost * freq (u) + sum storeCost * freq (d)
|
| 179 | 179 | -- u <- uses (v) d <- defs (v)
|
| 180 | 180 | --
|
| 181 | --- There are no loops in our code at the moment, so we can set the freq's to 1.
|
|
| 182 | ---
|
|
| 183 | 181 | -- If we don't have live range splitting then Chaitins function performs badly
|
| 184 | 182 | -- if we have lots of nested live ranges and very few registers.
|
| 185 | 183 | --
|
| ... | ... | @@ -201,7 +201,7 @@ regAlloc _ (CmmProc _ _ _ _) |
| 201 | 201 | |
| 202 | 202 | -- | Do register allocation on some basic blocks.
|
| 203 | 203 | -- But be careful to allocate a block in an SCC only if it has
|
| 204 | --- an entry in the block map or it is the first block.
|
|
| 204 | +-- an entry in the block map or it is an entry block of the procedure.
|
|
| 205 | 205 | --
|
| 206 | 206 | linearRegAlloc
|
| 207 | 207 | :: forall instr. (Instruction instr)
|
| ... | ... | @@ -286,11 +286,10 @@ linearRA_SCCs entry_ids block_live blocksAcc (CyclicSCC blocks : sccs) |
| 286 | 286 | before allocating one of its predecessors, which could lead to
|
| 287 | 287 | inconsistent allocations. Make it so a block is only allocated
|
| 288 | 288 | if a predecessor has set the "incoming" assignments for the block, or
|
| 289 | - if it's the procedure's entry block.
|
|
| 289 | + if it is one of the procedure's entry blocks.
|
|
| 290 | 290 | |
| 291 | - BL 2009/02: Careful. If the assignment for a block doesn't get set for
|
|
| 292 | - some reason then this function will loop. We should probably do some
|
|
| 293 | - more sanity checking to guard against this eventuality.
|
|
| 291 | + A block whose assignment never gets set is dropped: process stops after
|
|
| 292 | + a round in which it allocates no block.
|
|
| 294 | 293 | -}
|
| 295 | 294 | |
| 296 | 295 | process :: forall freeRegs instr. (OutputableRegConstraint freeRegs instr)
|
| ... | ... | @@ -701,9 +700,10 @@ saveClobberedTemps clobbered dying |
| 701 | 700 | -> RegM freeRegs (RegMap Loc,[instr])
|
| 702 | 701 | maybe_spill !temp !(assig,instrs) !loc =
|
| 703 | 702 | case loc of
|
| 704 | - -- This is non-deterministic but we do not
|
|
| 705 | - -- currently support deterministic code-generation.
|
|
| 706 | - -- See Note [Unique Determinism and code generation]
|
|
| 703 | + -- saveClobberedTemps folds maybe_spill over the vregs in the
|
|
| 704 | + -- order of their uniques, so the order decides which clobbered
|
|
| 705 | + -- values move to free registers and which are spilled once
|
|
| 706 | + -- those run out (Note [Unique Determinism and code generation])
|
|
| 707 | 707 | Loc (InReg reg) fmt
|
| 708 | 708 | | any (realRegsAlias reg) clobbered
|
| 709 | 709 | , temp `notElem` map getUnique dying
|
| ... | ... | @@ -775,9 +775,8 @@ clobberRegs clobbered |
| 775 | 775 | |
| 776 | 776 | assig <- getAssigR
|
| 777 | 777 | setAssigR $! clobber assig (nonDetUFMToList assig)
|
| 778 | - -- This is non-deterministic but we do not
|
|
| 779 | - -- currently support deterministic code-generation.
|
|
| 780 | - -- See Note [Unique Determinism and code generation]
|
|
| 778 | + -- Each vreg is updated on its own, so the list's order does
|
|
| 779 | + -- not matter (Note [Unique Determinism and code generation])
|
|
| 781 | 780 | |
| 782 | 781 | where
|
| 783 | 782 | -- if the temp was InReg and clobbered, then we will have
|
| ... | ... | @@ -871,8 +870,13 @@ allocateRegsAndSpill reading keep spills alloc (r@(VirtualRegWithFormat vr vrFmt |
| 871 | 870 | | otherwise -> doSpill WriteNew
|
| 872 | 871 | |
| 873 | 872 | -- | Given a virtual reg find a preferred real register.
|
| 874 | --- The preferred register is simply the first one the variable
|
|
| 875 | --- was assigned to (if any). This way when we allocate for a loop
|
|
| 873 | +-- A block's entry assignment is recorded at the first jump into it, and the
|
|
| 874 | +-- preferred register is the variable's register in the first entry assignment,
|
|
| 875 | +-- in allocation order, that maps it to a register ('updateBlockAssignment'
|
|
| 876 | +-- keeps the first). That need not be the first register the variable was
|
|
| 877 | +-- assigned to, and a variable that no entry assignment maps to a register has
|
|
| 878 | +-- none.
|
|
| 879 | +-- This way when we allocate for a loop
|
|
| 876 | 880 | -- variables are likely to end up in the same registers at the
|
| 877 | 881 | -- end and start of the loop, avoiding redundant reg-reg moves.
|
| 878 | 882 | -- Note: I tried returning a list of past assignments, but that
|
| ... | ... | @@ -932,9 +936,12 @@ allocRegsAndSpill_spill reading keep spills alloc r@(VirtualRegWithFormat vr vrF |
| 932 | 936 | flip delListFromUFM (fmap virtualRegWithFormat_reg keep) $
|
| 933 | 937 | filterUFM (inRegOrBoth . locWithFormat_loc) $
|
| 934 | 938 | assig
|
| 935 | - -- This is non-deterministic but we do not
|
|
| 936 | - -- currently support deterministic code-generation.
|
|
| 937 | - -- See Note [Unique Determinism and code generation]
|
|
| 939 | + -- candidates lists the vregs in a register, except those
|
|
| 940 | + -- in keep, in the order of their uniques, and that order
|
|
| 941 | + -- picks the vreg evicted below: of those whose register
|
|
| 942 | + -- is of the class needed (compat), the first in both a
|
|
| 943 | + -- register and a slot, else the first in a register only
|
|
| 944 | + -- (Note [Unique Determinism and code generation])
|
|
| 938 | 945 | let candidates = nonDetUFMToList candidates'
|
| 939 | 946 | |
| 940 | 947 | -- the vregs we could kick out that are already in a slot
|
| ... | ... | @@ -72,7 +72,8 @@ allocateReg (RealRegSingle r) (FreeRegs g f) |
| 72 | 72 | | r > 31 = panic $ "Linear.AArch64.allocReg: double allocation of float reg v" ++ show (r - 32) ++ "; " ++ showBits f
|
| 73 | 73 | | otherwise = pprPanic "Linear.AArch64.allocReg" $ text ("double allocation of gp reg x" ++ show r ++ "; " ++ showBits g)
|
| 74 | 74 | |
| 75 | --- we start from 28 downwards... the logic is similar to the ppc logic.
|
|
| 75 | +-- getFreeRegs starts from x18 downwards (x19 to x28 hold STG's registers,
|
|
| 76 | +-- below) and from v31 downwards... the logic is similar to the ppc logic.
|
|
| 76 | 77 | -- 31 is Stack Pointer
|
| 77 | 78 | -- 30 is Link Register
|
| 78 | 79 | -- 29 is Stack Frame (by convention)
|
| ... | ... | @@ -153,7 +153,7 @@ regsOfLoc (InBoth r _) = [r] |
| 153 | 153 | regsOfLoc (InMem _) = []
|
| 154 | 154 | |
| 155 | 155 | -- | Reasons why instructions might be inserted by the spiller.
|
| 156 | --- Used when generating stats for -ddrop-asm-stats.
|
|
| 156 | +-- Used when generating stats for -ddump-asm-stats.
|
|
| 157 | 157 | --
|
| 158 | 158 | data SpillReason
|
| 159 | 159 | -- | vreg was spilled to a slot so we could use its
|
| ... | ... | @@ -208,7 +208,7 @@ data RA_State freeRegs |
| 208 | 208 | -- | unique supply for generating names for join point fixup blocks.
|
| 209 | 209 | , ra_us :: DUniqSupply
|
| 210 | 210 | |
| 211 | - -- | Record why things were spilled, for -ddrop-asm-stats.
|
|
| 211 | + -- | Record why things were spilled, for -ddump-asm-stats.
|
|
| 212 | 212 | -- Just keep a list here instead of a map of regs -> reasons.
|
| 213 | 213 | -- We don't want to slow down the allocator if we're not going to emit the stats.
|
| 214 | 214 | , ra_spills :: [SpillReason]
|
| ... | ... | @@ -23,11 +23,13 @@ import GHC.Platform |
| 23 | 23 | -- Here's an inefficient 'executable specification' of the FreeRegs data type:
|
| 24 | 24 | --
|
| 25 | 25 | -- type FreeRegs = [RegNo]
|
| 26 | --- noFreeRegs = 0
|
|
| 26 | +-- noFreeRegs = []
|
|
| 27 | 27 | -- releaseReg n f = if n `elem` f then f else (n : f)
|
| 28 | 28 | -- initFreeRegs = allocatableRegs
|
| 29 | --- getFreeRegs cls f = filter ( (==cls) . regClass . RealReg ) f
|
|
| 29 | +-- getFreeRegs cls f = filter ( (==cls) . regClass . RealReg ) (inOrder f)
|
|
| 30 | 30 | -- allocateReg f r = filter (/= r) f
|
| 31 | +--
|
|
| 32 | +-- where inOrder sorts the registers into the instance's own fixed order.
|
|
| 31 | 33 | |
| 32 | 34 | import qualified GHC.CmmToAsm.Reg.Linear.PPC as PPC
|
| 33 | 35 | import qualified GHC.CmmToAsm.Reg.Linear.X86 as X86
|
| ... | ... | @@ -97,9 +97,9 @@ joinToTargets' block_live new_blocks block_id instr (dest:dests) |
| 97 | 97 | -- and free up those registers which are now free.
|
| 98 | 98 | let to_free =
|
| 99 | 99 | [ r | (reg, Loc loc _locFmt) <- nonDetUFMToList assig
|
| 100 | - -- This is non-deterministic but we do not
|
|
| 101 | - -- currently support deterministic code-generation.
|
|
| 102 | - -- See Note [Unique Determinism and code generation]
|
|
| 100 | + -- The registers are released into a bitmap of
|
|
| 101 | + -- free registers, so the order does not matter
|
|
| 102 | + -- (Note [Unique Determinism and code generation])
|
|
| 103 | 103 | , not (elemUniqSet_Directly reg $ getRegs live_set)
|
| 104 | 104 | , r <- regsOfLoc loc ]
|
| 105 | 105 | |
| ... | ... | @@ -159,9 +159,9 @@ joinToTargets_again |
| 159 | 159 | | equalIgnoringFormats
|
| 160 | 160 | (nonDetUFMToList dest_assig)
|
| 161 | 161 | (nonDetUFMToList src_assig)
|
| 162 | - -- This is non-deterministic but we do not
|
|
| 163 | - -- currently support deterministic code-generation.
|
|
| 164 | - -- See Note [Unique Determinism and code generation]
|
|
| 162 | + -- Both lists are in the order of the uniques, so
|
|
| 163 | + -- the comparison does not depend on that order
|
|
| 164 | + -- (Note [Unique Determinism and code generation])
|
|
| 165 | 165 | = joinToTargets' block_live new_blocks block_id instr dests
|
| 166 | 166 | |
| 167 | 167 | -- assignments don't match, need fixup code
|
| ... | ... | @@ -237,17 +237,15 @@ joinToTargets_again |
| 237 | 237 | |
| 238 | 238 | -- | Construct a graph of register\/spill movements.
|
| 239 | 239 | --
|
| 240 | --- Cyclic components seem to occur only very rarely.
|
|
| 241 | ---
|
|
| 242 | 240 | -- We cut some corners by not handling memory-to-memory moves.
|
| 243 | 241 | -- This shouldn't happen because every temporary gets its own stack slot.
|
| 244 | 242 | --
|
| 245 | 243 | makeRegMovementGraph :: RegMap Loc -> RegMap Loc -> [Node Loc Unique]
|
| 246 | 244 | makeRegMovementGraph adjusted_assig dest_assig
|
| 247 | 245 | = [ node | (vreg, src) <- nonDetUFMToList adjusted_assig
|
| 248 | - -- This is non-deterministic but we do not
|
|
| 249 | - -- currently support deterministic code-generation.
|
|
| 250 | - -- See Note [Unique Determinism and code generation]
|
|
| 246 | + -- The list's order, by unique, decides the order of the
|
|
| 247 | + -- fixup moves and which move of a cycle goes through the
|
|
| 248 | + -- stack (Note [Unique Determinism and code generation])
|
|
| 251 | 249 | -- source reg might not be needed at the dest:
|
| 252 | 250 | , Just loc <- [lookupUFM_Directly dest_assig vreg]
|
| 253 | 251 | , node <- expandNode vreg src loc ]
|
| ... | ... | @@ -323,8 +321,10 @@ handleComponent delta _ (AcyclicSCC (DigraphNode vreg src dsts)) |
| 323 | 321 | -- (vreg1, InReg r1, [InReg r2])
|
| 324 | 322 | -- (vreg2, InReg r2, [InReg r1])
|
| 325 | 323 | --
|
| 326 | --- To avoid needing temp register, we just spill all the source regs, then
|
|
| 327 | --- reaload them into their destination regs.
|
|
| 324 | +-- To avoid needing a temp register, we spill the first move's source
|
|
| 325 | +-- reg into its vreg's slot, do the other moves (handling any cycle left
|
|
| 326 | +-- among them the same way), and reload the slot into the first move's
|
|
| 327 | +-- destination reg last.
|
|
| 328 | 328 | --
|
| 329 | 329 | -- Note that we can not have cycles that involve memory locations as
|
| 330 | 330 | -- sources as single destination because memory locations (stack slots)
|
| ... | ... | @@ -7,8 +7,9 @@ |
| 7 | 7 | |
| 8 | 8 | -- The StackMap stack map keeps track of virtual register - stack slot
|
| 9 | 9 | -- associations and of which stack slots are still free. Once it has been
|
| 10 | --- associated, a stack slot is never "freed" or removed from the StackMap again,
|
|
| 11 | --- it remains associated until we are done with the current CmmProc.
|
|
| 10 | +-- associated, a stack slot is never "freed": no other virtual register
|
|
| 11 | +-- gets it before we are done with the current CmmProc, even after its
|
|
| 12 | +-- register has outgrown it and moved to new slots.
|
|
| 12 | 13 | --
|
| 13 | 14 | module GHC.CmmToAsm.Reg.Linear.StackMap (
|
| 14 | 15 | StackSlot,
|
| ... | ... | @@ -64,8 +64,8 @@ newtype RegM freeRegs a |
| 64 | 64 | = RegM { unReg :: RA_State freeRegs -> RA_Result freeRegs a }
|
| 65 | 65 | deriving (Functor, Applicative, Monad) via (Strict.State (RA_State freeRegs))
|
| 66 | 66 | |
| 67 | --- | Smart constructor for 'RegM', as described in Note [The one-shot state
|
|
| 68 | --- monad trick] in GHC.Utils.Monad.
|
|
| 67 | +-- | Smart constructor for 'RegM', as described in
|
|
| 68 | +-- Note [The one-shot state monad trick] in GHC.Utils.Monad.
|
|
| 69 | 69 | mkRegM :: (RA_State freeRegs -> RA_Result freeRegs a) -> RegM freeRegs a
|
| 70 | 70 | mkRegM f = RegM (oneShot f)
|
| 71 | 71 |
| ... | ... | @@ -522,9 +522,9 @@ slurpReloadCoalesce live |
| 522 | 522 | = listToUFM_Directly
|
| 523 | 523 | $ [ (k, r1)
|
| 524 | 524 | | (k, r1) <- nonDetUFMToList map1
|
| 525 | - -- This is non-deterministic but we do not
|
|
| 526 | - -- currently support deterministic code-generation.
|
|
| 527 | - -- See Note [Unique Determinism and code generation]
|
|
| 525 | + -- Each entry is kept or dropped on its own and the list
|
|
| 526 | + -- becomes a map again, so its order does not matter
|
|
| 527 | + -- (Note [Unique Determinism and code generation])
|
|
| 528 | 528 | , case lookupUFM_Directly map2 k of
|
| 529 | 529 | Nothing -> False
|
| 530 | 530 | Just r2 -> r1 == r2 ]
|
| 1 | 1 | |
| 2 | 2 | -- | Hard wired things related to registers.
|
| 3 | --- This is module is preventing the native code generator being able to
|
|
| 4 | --- emit code for non-host architectures.
|
|
| 5 | 3 | --
|
| 6 | 4 | -- TODO: Do a better job of the overloading, and eliminate this module.
|
| 7 | 5 | -- We'd probably do better with a Register type class, and hook this to
|
| ... | ... | @@ -344,8 +344,11 @@ stmtToInstrs bid stmt = do |
| 344 | 344 | |
| 345 | 345 | CmmBranch id -> return $ genBranch id
|
| 346 | 346 | |
| 347 | - --We try to arrange blocks such that the likely branch is the fallthrough
|
|
| 348 | - --in GHC.Cmm.ContFlowOpt. So we can assume the condition is likely false here.
|
|
| 347 | + -- The condition may be likely true here: GHC.Cmm.ContFlowOpt inverts a
|
|
| 348 | + -- condition whose true target has one predecessor if the condition is
|
|
| 349 | + -- likely true, but also, whatever its likelihood, if the false target
|
|
| 350 | + -- has more than one predecessor (Note [Invert Cmm conditionals]).
|
|
| 351 | + -- genCondBranch does not read the likelihood.
|
|
| 349 | 352 | CmmCondBranch arg true false _ -> genCondBranch bid true false arg
|
| 350 | 353 | CmmSwitch arg ids -> genSwitch arg ids bid
|
| 351 | 354 | CmmCall { cml_target = arg
|
| ... | ... | @@ -448,8 +448,12 @@ regUsageOfInstr platform instr |
| 448 | 448 | , OpReg {} <- src
|
| 449 | 449 | , OpReg {} <- dst
|
| 450 | 450 | -> readOp dst_fmt src <.> modifyOp dst_fmt dst
|
| 451 | - -- other MOV instructions zero any remaining upper part of the destination
|
|
| 452 | - -- (largely to avoid partial register stalls)
|
|
| 451 | + -- The usage of the other MOV instructions has a register destination
|
|
| 452 | + -- written, not read. Some of them zero any remaining upper part of the
|
|
| 453 | + -- destination (largely to avoid partial register stalls), such as a
|
|
| 454 | + -- 32-bit MOV into a general register and a load at FF32 or FF64; others
|
|
| 455 | + -- keep it, such as a MOV between registers at FF32 or FF64 (movss, movsd)
|
|
| 456 | + -- and an 8-bit or 16-bit MOV into a general register.
|
|
| 453 | 457 | | otherwise
|
| 454 | 458 | -> readOp dst_fmt src <.> writeOp dst_fmt dst
|
| 455 | 459 | MOVD fmt1 fmt2 src dst ->
|
| ... | ... | @@ -105,8 +105,8 @@ pprNatCmmDecl config proc@(CmmProc top_info entry_lbl _ (ListGraph blocks)) = |
| 105 | 105 | -- info table and not the entry code!
|
| 106 | 106 | , pprProcAlignment config
|
| 107 | 107 | |
| 108 | - -- Special label when ncgExposeInternalSymbols is enabled. See Note
|
|
| 109 | - -- [Internal proc labels] in GHC.Cmm.Label
|
|
| 108 | + -- Special label when ncgExposeInternalSymbols is enabled. See
|
|
| 109 | + -- Note [Internal proc labels] in GHC.Cmm.CLabel
|
|
| 110 | 110 | , pprExposedInternalProcLabel config entry_lbl
|
| 111 | 111 | |
| 112 | 112 | -- Subsections-via-symbols label. See Note [Subsections Via Symbols]
|
| ... | ... | @@ -225,10 +225,10 @@ allMachRegNos platform = intregnos (platformWordSize platform) ++ floatregnos pl |
| 225 | 225 | -- | Take the class of a register.
|
| 226 | 226 | {-# INLINE classOfRealReg #-}
|
| 227 | 227 | classOfRealReg :: Platform -> RealReg -> RegClass
|
| 228 | --- On x86, we might want to have an 8-bit RegClass, which would
|
|
| 229 | --- contain just regs 1-4 (the others don't have 8-bit versions).
|
|
| 228 | +-- On i386, we might want to have an 8-bit RegClass, which would
|
|
| 229 | +-- contain just regs 0-3 (the others don't have 8-bit versions).
|
|
| 230 | 230 | -- However, we can get away without this at the moment because the
|
| 231 | --- only allocatable integer regs are also 8-bit compatible (1, 3, 4).
|
|
| 231 | +-- only allocatable integer regs are also 8-bit compatible (0, 2, 3).
|
|
| 232 | 232 | classOfRealReg platform reg
|
| 233 | 233 | = case reg of
|
| 234 | 234 | RealRegSingle i
|
| ... | ... | @@ -72,8 +72,8 @@ data VirtualReg |
| 72 | 72 | |
| 73 | 73 | -- We can't derive Ord, because Unique doesn't have an Ord instance.
|
| 74 | 74 | -- Note nonDetCmpUnique in the implementation. See Note [No Ord for Unique].
|
| 75 | --- This is non-deterministic but we do not currently support deterministic
|
|
| 76 | --- code-generation. See Note [Unique Determinism and code generation]
|
|
| 75 | +-- This compares uniques, so the order is deterministic only if the uniques are:
|
|
| 76 | +-- see Note [Unique Determinism and code generation].
|
|
| 77 | 77 | instance Ord VirtualReg where
|
| 78 | 78 | compare vr1 vr2 =
|
| 79 | 79 | case compare (I# (dataToTag# vr1)) (I# (dataToTag# vr2)) of
|
| ... | ... | @@ -30,7 +30,9 @@ instance Outputable RegClass where |
| 30 | 30 | data RegArch
|
| 31 | 31 | -- | Floating-point and vector registers are unified (e.g. X86, AArch64).
|
| 32 | 32 | = Unified
|
| 33 | - -- | Floating-point and vector registers are separate (e.g. RISC-V).
|
|
| 33 | + -- | Floating-point and vector registers are separate. No architecture uses
|
|
| 34 | + -- this yet: registerArch gives RISC-V NoVectors, as its vector registers are
|
|
| 35 | + -- not supported.
|
|
| 34 | 36 | | Separate
|
| 35 | 37 | -- | No vector registers.
|
| 36 | 38 | | NoVectors
|
| ... | ... | @@ -835,10 +835,14 @@ mkCmmIfThenElse' e tbranch fbranch likely = do |
| 835 | 835 | let
|
| 836 | 836 | (test, then_, else_, likely') = case likely of
|
| 837 | 837 | Just False | Just e' <- maybeInvertCmmExpr e
|
| 838 | - -- currently NCG doesn't know about likely
|
|
| 839 | - -- annotations. We manually switch then and
|
|
| 840 | - -- else branch so the likely false branch
|
|
| 841 | - -- becomes a fallthrough.
|
|
| 838 | + -- The NCG uses likely annotations on x86-64, where getCfg in
|
|
| 839 | + -- GHC.CmmToAsm.CFG weights the CFG's edges by them for the block layout
|
|
| 840 | + -- and the order of some conditional branches' jumps, and on PowerPC,
|
|
| 841 | + -- which prints them as hints on its branches. Where maybeInvertCmmExpr
|
|
| 842 | + -- can invert it, a test annotated likely false becomes a likely-true
|
|
| 843 | + -- test of the inverse, with the branches swapped. GHC.Cmm.ContFlowOpt
|
|
| 844 | + -- swaps them back where the true target, after shortcutting, has one
|
|
| 845 | + -- predecessor (Note [Invert Cmm conditionals]).
|
|
| 842 | 846 | -> (e', fbranch, tbranch, Just True)
|
| 843 | 847 | _ -> (e, tbranch, fbranch, likely)
|
| 844 | 848 |
| ... | ... | @@ -250,8 +250,7 @@ as such you shouldn't need to set any of them explicitly. A flag |
| 250 | 250 | :default: off but enabled by :ghc-flag:`-O`.
|
| 251 | 251 | |
| 252 | 252 | Enables the sinking pass in the code generator.
|
| 253 | - This optimisation attempts to find identical Cmm blocks and
|
|
| 254 | - eliminate the duplicates attempts to move variable bindings closer
|
|
| 253 | + This optimisation attempts to move variable bindings closer
|
|
| 255 | 254 | to their usage sites. It also inlines simple expressions like
|
| 256 | 255 | literals or registers.
|
| 257 | 256 | |
| ... | ... | @@ -263,10 +262,11 @@ as such you shouldn't need to set any of them explicitly. A flag |
| 263 | 262 | |
| 264 | 263 | :default: off but enabled by :ghc-flag:`-O`.
|
| 265 | 264 | |
| 266 | - This enables static control flow prediction on the final Cmm
|
|
| 267 | - code. If enabled GHC will apply certain heuristics to identify
|
|
| 268 | - loops and hot code paths. This information is then used by the
|
|
| 269 | - register allocation and code layout passes.
|
|
| 265 | + This enables static control flow prediction in the native code generator
|
|
| 266 | + for x86-64. If enabled GHC will apply certain heuristics to identify loops
|
|
| 267 | + and hot code paths, and use this information to lay out the code and to
|
|
| 268 | + order the jumps of some conditional branches. It has no effect when
|
|
| 269 | + :ghc-flag:`-finfo-table-map` is on.
|
|
| 270 | 270 | |
| 271 | 271 | .. ghc-flag:: -fcmm-control-flow
|
| 272 | 272 | :shortdesc: Enable control flow optimisation in the Cmm backend. Implied by :ghc-flag:`-O`.
|