raz / commit 84647412

commit
846474122e732d59e3fa6ea94d240c4587b9dbe4
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
3c704507
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%.

patch · log

3 files changed, +74 -14

doc/compiler.md (history) modified +42 -3

@@ -18,9 +18,9 @@
1818 | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout |
1919 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2020 | `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 |
2222 | `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 |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -67,7 +67,8 @@
6767 of System V, the calling convention of x86-64 Linux, which a called
6868 procedure must give back as it found them (`rbx` and `r12` to `r15`).
6969 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
7172 loaded from there after a call, as System V has no xmm register that
7273 a call leaves alone.
7374
@@ -835,6 +836,44 @@
835836 The sum 15% faster, in the same instructions. Search, whose `for` is
836837 compared once a round of its inner `while`, is 4% slower in a loop
837838 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.
838877
839878 ## Diag.raz: messages
840879

src/Gen.raz modified +17 -7

@@ -118,11 +118,14 @@
118118 ShownRegs*: [7]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15, X.RDI, X.RSI];
119119 (* the registers a procedure keeps its busiest reals in, xmm12 to
120120 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
122124 report finds it, and from which it is loaded again after a call, as
123125 System V has no callee-saved xmm register (xHome). *)
124126 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];
126129 (* a procedure's registers of variables, as bits: bit i is VarRegs[i],
127130 bit XBit + i XVarRegs[i] *)
128131 XBit* = 5;
@@ -217,7 +220,7 @@
217220 regMask: INT; (* the registers of variables, as bits (XBit) *)
218221 kept: INT; (* bit r: register r holds a parameter, as it arrived *)
219222 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 *)
221224 frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *)
222225 fails: [4096]Fail;
223226 numFails: INT;
@@ -641,10 +644,17 @@
641644 computed(kWord);
642645 end;
643646
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+
644654 (* The slot of the variable in xmm register x *)
645655 proc homeOf(x: INT): X.Mem;
646656 begin
647- return X.mem(RBP, homes[x + XmmVar - XVarRegs[0]]);
657+ return X.mem(RBP, homes[xIndex(x + XmmVar)]);
648658 end;
649659
650660 (* Pops a value into the variable at a; an address on the stack is under
@@ -1847,7 +1857,7 @@
18471857 if k + pad > 0 then X.aluI(X.aluAdd, RSP, 8 * (k + pad)); end;
18481858 (* the variables in xmm registers, which the call may have changed,
18491859 from their slots *)
1850- for i := 0 to 3 do
1860+ for i := 0 to LEN(XVarRegs) - 1 do
18511861 if (regMask & (1 shl (XBit + i))) # 0 then
18521862 X.movsXM(XVarRegs[i] - XmmVar, X.mem(RBP, homes[i]), false);
18531863 end;
@@ -2049,7 +2059,7 @@
20492059 slot, kept as it changes *)
20502060 proc xHome*(r: INT; slot: I64);
20512061 begin
2052- homes[r - XVarRegs[0]] := slot;
2062+ homes[xIndex(r)] := slot;
20532063 end;
20542064
20552065 proc markCode*(var m: CodeMark);
@@ -2137,7 +2147,7 @@
21372147 end;
21382148 (* and each xmm register that holds a variable, zeroed, as its slot
21392149 is: the caller keeps its own *)
2140- for i := 0 to 3 do
2150+ for i := 0 to LEN(XVarRegs) - 1 do
21412151 if (regMask & (1 shl (XBit + i))) # 0 then
21422152 X.movRI(RAX, 0);
21432153 X.movqXR(XVarRegs[i] - XmmVar, RAX);

src/Parse.raz (history) modified +15 -4

@@ -2485,7 +2485,7 @@
24852485 var
24862486 isLoop: [256]BOOL;
24872487 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;
24892489 afterDot, wasSkipping, leaf: BOOL;
24902490 begin
24912491 numCand := 0;
@@ -2569,13 +2569,24 @@
25692569 o := o^.next;
25702570 end;
25712571 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;
25722583 (* a slice's weight is shared by its two registers *)
25732584 for i := 0 to numCand - 1 do
25742585 if cand[i]^.typ^.form = Sym.fSlice then weight[i] := weight[i] div 2; end;
25752586 end;
25762587 (* the heaviest, each used at least three times a register, take the
25772588 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 *)
25792590 k := 0; kx := 0;
25802591 repeat
25812592 best := -1;
@@ -2586,7 +2597,7 @@
25862597 need := 1;
25872598 if cand[best]^.typ^.form = Sym.fSlice then need := 2; end;
25882599 if cand[best]^.typ^.form = Sym.fReal then
2589- if kx < 4 then
2600+ if kx < xmax then
25902601 cand[best]^.reg := Gen.XVarRegs[kx];
25912602 INC(kx);
25922603 else
@@ -2614,7 +2625,7 @@
26142625 for k := 0 to 4 do
26152626 if (o^.reg = Gen.VarRegs[k]) or (o^.reg2 = Gen.VarRegs[k]) then m := m | (1 shl k); end;
26162627 end;
2617- for k := 0 to 3 do
2628+ for k := 0 to LEN(Gen.XVarRegs) - 1 do
26182629 if o^.reg = Gen.XVarRegs[k] then m := m | (1 shl (Gen.XBit + k)); end;
26192630 end;
26202631 return m;