Four more registers for reals in a procedure that calls nothing xmm0 to xmm7 carry a call's arguments and Gen uses no other of them but xmm0, so in a procedure that calls nothing and has at most four real parameters, chooseRegs may give the reals xmm4 to xmm7 too, the last four of XVarRegs. NBody's d2 is no longer stored and loaded on the chain each round waits on. NBody 4% faster. doc/compiler.md sums up the four steps: NBody 25% faster, now 1.05 times FPC -O2's time, the sum 9%, Lines 6%.
| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout | |
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | -| `src/Gen.raz` | 2,506 | 1,794 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 21 | +| `src/Gen.raz` | 2,516 | 1,800 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 22 | 22 | | `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls | |
| 23 | -| `src/Parse.raz` | 3,896 | 3,395 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 23 | +| `src/Parse.raz` | 3,907 | 3,405 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| @@ -67,7 +67,8 @@ | ||
| 67 | 67 | of System V, the calling convention of x86-64 Linux, which a called |
| 68 | 68 | procedure must give back as it found them (`rbx` and `r12` to `r15`). |
| 69 | 69 | Nothing but their own assignments can change them. Up to four reals |
| 70 | -live in `xmm12` to `xmm15`, each written to the frame as well and | |
| 70 | +live in `xmm12` to `xmm15` (eight, with `xmm4` to `xmm7`, in a | |
| 71 | +procedure that calls nothing), each written to the frame as well and | |
| 71 | 72 | loaded from there after a call, as System V has no xmm register that |
| 72 | 73 | a call leaves alone. |
| 73 | 74 | |
| @@ -835,6 +836,44 @@ | ||
| 835 | 836 | The sum 15% faster, in the same instructions. Search, whose `for` is |
| 836 | 837 | compared once a round of its inner `while`, is 4% slower in a loop |
| 837 | 838 | that didn't change, at the same address; why is not known. |
| 839 | + | |
| 840 | +4. **Four more registers for reals, where nothing calls.** NBody's | |
| 841 | + fifth real, `d2`, had no register: XVarRegs had four, `xmm12` to | |
| 842 | + `xmm15`. It was stored and loaded on the chain each round waits on: | |
| 843 | + | |
| 844 | + ``` | |
| 845 | + addsd xmm8, [literal1024] | |
| 846 | + movsd [rbp - 32], xmm8 ; d2 := ... | |
| 847 | + movsd xmm9, [rbp - 32] ; Math.sqrt(d2) | |
| 848 | + sqrtsd xmm9, xmm9 | |
| 849 | + movsd xmm8, [rbp - 32] ; d2 * ... | |
| 850 | + ``` | |
| 851 | + | |
| 852 | + `xmm0` to `xmm7` carry a call's arguments, and nothing else of Gen's | |
| 853 | + but `xmm0`; so in a procedure that calls nothing (`chooseRegs`'s | |
| 854 | + `leaf`) and has at most four real parameters, which arrive in `xmm0` | |
| 855 | + to `xmm3`, the reals may have `xmm4` to `xmm7` as well, the last four | |
| 856 | + of XVarRegs (`xmax`; `xIndex` finds a register's slot): | |
| 857 | + | |
| 858 | + ``` | |
| 859 | + addsd xmm8, [literal1024] | |
| 860 | + movaps xmm4, xmm8 | |
| 861 | + movsd [rbp - 32], xmm4 | |
| 862 | + movaps xmm9, xmm4 | |
| 863 | + sqrtsd xmm9, xmm9 | |
| 864 | + movaps xmm8, xmm4 | |
| 865 | + ``` | |
| 866 | + | |
| 867 | + NBody 4% faster, in 1.4% more instructions: a load is a `movaps` | |
| 868 | + now, and the slot is still written for the trap report. | |
| 869 | + | |
| 870 | +All told, against v0.12.0, the best of thirty runs each: NBody 1.68 | |
| 871 | +to 1.16 billion instructions and 202 ms to 151, now 1.05 times FPC | |
| 872 | +`-O2`'s time; the sum 163 ms to 148, as fast as FPC's; Lines 6% | |
| 873 | +faster; the rest within noise, Grep and Search too, whose losses above | |
| 874 | +came with their loops unchanged. Grep (1.14 times FPC's) and Jacobi (1.24) are where FPC is still | |
| 875 | +ahead. The compiler, built by itself, runs 0.9% fewer instructions on | |
| 876 | +the 348,000-line program, in the same time. | |
| 838 | 877 | |
| 839 | 878 | ## Diag.raz: messages |
| 840 | 879 |
| @@ -118,11 +118,14 @@ | ||
| 118 | 118 | ShownRegs*: [7]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15, X.RDI, X.RSI]; |
| 119 | 119 | (* the registers a procedure keeps its busiest reals in, xmm12 to |
| 120 | 120 | xmm15, numbered from XmmVar up so that they aren't taken for r12 to |
| 121 | - r15 (regAt). Each has a slot too, written as it is, where the trap | |
| 121 | + r15 (regAt); and in a procedure that calls nothing, which has no | |
| 122 | + real parameter in them, xmm4 to xmm7 too, as nothing else uses | |
| 123 | + them there. Each has a slot too, written as it is, where the trap | |
| 122 | 124 | report finds it, and from which it is loaded again after a call, as |
| 123 | 125 | System V has no callee-saved xmm register (xHome). *) |
| 124 | 126 | XmmVar* = 16; |
| 125 | - XVarRegs*: [4]INT = [XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15]; | |
| 127 | + XVarRegs*: [8]INT = [XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15, | |
| 128 | + XmmVar + 4, XmmVar + 5, XmmVar + 6, XmmVar + 7]; | |
| 126 | 129 | (* a procedure's registers of variables, as bits: bit i is VarRegs[i], |
| 127 | 130 | bit XBit + i XVarRegs[i] *) |
| 128 | 131 | XBit* = 5; |
| @@ -217,7 +220,7 @@ | ||
| 217 | 220 | regMask: INT; (* the registers of variables, as bits (XBit) *) |
| 218 | 221 | kept: INT; (* bit r: register r holds a parameter, as it arrived *) |
| 219 | 222 | saveAt: I64; (* where they are saved, a quadword each *) |
| 220 | - homes: [4]I64; (* the slots of the variables in XVarRegs *) | |
| 223 | + homes: [8]I64; (* the slots of the variables in XVarRegs *) | |
| 221 | 224 | frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *) |
| 222 | 225 | fails: [4096]Fail; |
| 223 | 226 | numFails: INT; |
| @@ -641,10 +644,17 @@ | ||
| 641 | 644 | computed(kWord); |
| 642 | 645 | end; |
| 643 | 646 | |
| 647 | +(* Where xmm register r is in XVarRegs *) | |
| 648 | +proc xIndex(r: INT): INT; | |
| 649 | +begin | |
| 650 | + if r >= XVarRegs[0] then return r - XVarRegs[0]; end; | |
| 651 | + return r - XVarRegs[4] + 4; | |
| 652 | +end; | |
| 653 | + | |
| 644 | 654 | (* The slot of the variable in xmm register x *) |
| 645 | 655 | proc homeOf(x: INT): X.Mem; |
| 646 | 656 | begin |
| 647 | - return X.mem(RBP, homes[x + XmmVar - XVarRegs[0]]); | |
| 657 | + return X.mem(RBP, homes[xIndex(x + XmmVar)]); | |
| 648 | 658 | end; |
| 649 | 659 | |
| 650 | 660 | (* Pops a value into the variable at a; an address on the stack is under |
| @@ -1847,7 +1857,7 @@ | ||
| 1847 | 1857 | if k + pad > 0 then X.aluI(X.aluAdd, RSP, 8 * (k + pad)); end; |
| 1848 | 1858 | (* the variables in xmm registers, which the call may have changed, |
| 1849 | 1859 | from their slots *) |
| 1850 | - for i := 0 to 3 do | |
| 1860 | + for i := 0 to LEN(XVarRegs) - 1 do | |
| 1851 | 1861 | if (regMask & (1 shl (XBit + i))) # 0 then |
| 1852 | 1862 | X.movsXM(XVarRegs[i] - XmmVar, X.mem(RBP, homes[i]), false); |
| 1853 | 1863 | end; |
| @@ -2049,7 +2059,7 @@ | ||
| 2049 | 2059 | slot, kept as it changes *) |
| 2050 | 2060 | proc xHome*(r: INT; slot: I64); |
| 2051 | 2061 | begin |
| 2052 | - homes[r - XVarRegs[0]] := slot; | |
| 2062 | + homes[xIndex(r)] := slot; | |
| 2053 | 2063 | end; |
| 2054 | 2064 | |
| 2055 | 2065 | proc markCode*(var m: CodeMark); |
| @@ -2137,7 +2147,7 @@ | ||
| 2137 | 2147 | end; |
| 2138 | 2148 | (* and each xmm register that holds a variable, zeroed, as its slot |
| 2139 | 2149 | is: the caller keeps its own *) |
| 2140 | - for i := 0 to 3 do | |
| 2150 | + for i := 0 to LEN(XVarRegs) - 1 do | |
| 2141 | 2151 | if (regMask & (1 shl (XBit + i))) # 0 then |
| 2142 | 2152 | X.movRI(RAX, 0); |
| 2143 | 2153 | X.movqXR(XVarRegs[i] - XmmVar, RAX); |
| @@ -2485,7 +2485,7 @@ | ||
| 2485 | 2485 | var |
| 2486 | 2486 | isLoop: [256]BOOL; |
| 2487 | 2487 | forAt: [256]INT; (* the for loop opened at each depth, or -1 *) |
| 2488 | - i, k, kx, d, loops, best, need, nf, inLim: INT; o, q, qual: PObj; st: Scan.State; | |
| 2488 | + i, k, kx, xmax, d, loops, best, need, nf, inLim: INT; o, q, qual: PObj; st: Scan.State; | |
| 2489 | 2489 | afterDot, wasSkipping, leaf: BOOL; |
| 2490 | 2490 | begin |
| 2491 | 2491 | numCand := 0; |
| @@ -2569,13 +2569,24 @@ | ||
| 2569 | 2569 | o := o^.next; |
| 2570 | 2570 | end; |
| 2571 | 2571 | end; |
| 2572 | + (* xmm4 to xmm7 too, where no call or real parameter needs them *) | |
| 2573 | + xmax := 4; | |
| 2574 | + if leaf then | |
| 2575 | + xmax := 8; | |
| 2576 | + o := params; i := 0; | |
| 2577 | + while o # nil do | |
| 2578 | + if (o^.typ^.form = Sym.fReal) and not o^.isVar then INC(i); end; | |
| 2579 | + o := o^.next; | |
| 2580 | + end; | |
| 2581 | + if i > 4 then xmax := 4; end; | |
| 2582 | + end; | |
| 2572 | 2583 | (* a slice's weight is shared by its two registers *) |
| 2573 | 2584 | for i := 0 to numCand - 1 do |
| 2574 | 2585 | if cand[i]^.typ^.form = Sym.fSlice then weight[i] := weight[i] div 2; end; |
| 2575 | 2586 | end; |
| 2576 | 2587 | (* the heaviest, each used at least three times a register, take the |
| 2577 | 2588 | five registers in turn, while there are enough for each, and the |
| 2578 | - reals the four xmm registers of XVarRegs *) | |
| 2589 | + reals xmax of the xmm registers of XVarRegs *) | |
| 2579 | 2590 | k := 0; kx := 0; |
| 2580 | 2591 | repeat |
| 2581 | 2592 | best := -1; |
| @@ -2586,7 +2597,7 @@ | ||
| 2586 | 2597 | need := 1; |
| 2587 | 2598 | if cand[best]^.typ^.form = Sym.fSlice then need := 2; end; |
| 2588 | 2599 | if cand[best]^.typ^.form = Sym.fReal then |
| 2589 | - if kx < 4 then | |
| 2600 | + if kx < xmax then | |
| 2590 | 2601 | cand[best]^.reg := Gen.XVarRegs[kx]; |
| 2591 | 2602 | INC(kx); |
| 2592 | 2603 | else |
| @@ -2614,7 +2625,7 @@ | ||
| 2614 | 2625 | for k := 0 to 4 do |
| 2615 | 2626 | if (o^.reg = Gen.VarRegs[k]) or (o^.reg2 = Gen.VarRegs[k]) then m := m | (1 shl k); end; |
| 2616 | 2627 | end; |
| 2617 | - for k := 0 to 3 do | |
| 2628 | + for k := 0 to LEN(Gen.XVarRegs) - 1 do | |
| 2618 | 2629 | if o^.reg = Gen.XVarRegs[k] then m := m | (1 shl (Gen.XBit + k)); end; |
| 2619 | 2630 | end; |
| 2620 | 2631 | return m; |