raz / commit 36e782df

commit
36e782dfb76b6ea26a5f697677ae23e6051fbd66
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
84cb543c
Real variables in xmm registers

NBody's dx, dy, dz and mag lived in the frame, each assignment a
store and each use a load on the chain each round waits on. Now
ChooseRegs gives a procedure's busiest four reals xmm12 to xmm15
(XVarRegs, numbered from XmmVar, 16, up so that xmm12 isn't taken for
r12), and gen reads and writes them where they are, as it does an
integer's register (lHeld, lInto; XValueTo, PopVar, Peek in RealOp
and Compare). Each has a slot as well, which PopVar writes as it
writes the register: the trap report finds the value there after a
failed check or a signal alike, and Call loads the register from it
after each call, as System V keeps no xmm register through one
(XHome).

NBody 147 -> 116 ms (FPC -O2 85); a copy without its call to sqrt
114 -> 81 (66). Storing the slots only before a call, the trap report
then blind to them, bought 3% more on the copy and nothing on NBody.
rat 468 -> 463 ms, within noise; the other kernels unchanged. 9,131
lines of code.

patch · log

4 files changed, +155 -32

PLAN.md (history) modified +5 -2

@@ -41,8 +41,11 @@
4141 `+`, `-` and `*` by a constant, and an element's operand takes it as
4242 its displacement: `[r13 + r9 * 8 - 8]`. Jacobi 129 ms to 106; rat
4343 within noise.
44-5. **Real variables in xmm registers**, where that pays: measured, and
45- kept only if it does.
44+5. Done: **Real variables in xmm registers.** The busiest four reals
45+ of a procedure live in xmm12 to xmm15, each written through to its
46+ slot, from which the trap report reads it and a call's return loads
47+ it, as System V keeps no xmm register through a call. NBody 147 ms
48+ to 116; rat within noise.
4649
4750 ## Next, when the owner says
4851

doc/compiler.md (history) modified +61 -8

@@ -15,9 +15,9 @@
1515 | `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout |
1616 | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
1717 | `src/x64.pas` | 1,201 | 992 | The instruction encoder, jumps within a procedure, the assembler |
18-| `src/gen.pas` | 2,188 | 1,588 | The stack machine, for x86-64: values, operations, checks, calls, frames |
18+| `src/gen.pas` | 2,238 | 1,622 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1919 | `src/item.pas` | 1,578 | 1,301 | Items: types, constants, variables, operators, conversions, calls |
20-| `src/parse.pas` | 3,103 | 2,707 | Declarations, statements, expressions, built-ins, recovery, warnings |
20+| `src/parse.pas` | 3,120 | 2,722 | Declarations, statements, expressions, built-ins, recovery, warnings |
2121 | `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places |
2222 | `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers |
2323 | `src/raz.pas` | 245 | 188 | The command line |
@@ -60,7 +60,10 @@
6060 five, live in registers that calls leave alone: the callee-saved ones
6161 of System V, the calling convention of x86-64 Linux, which a called
6262 procedure must give back as it found them (`rbx` and `r12` to `r15`).
63-Nothing but their own assignments can change them.
63+Nothing but their own assignments can change them. Up to four reals
64+live in `xmm12` to `xmm15`, each written to the frame as well and
65+loaded from there after a call, as System V has no xmm register that
66+a call leaves alone.
6467
6568 The output is a set of *chunks*: one for each procedure, each module
6669 variable, each string. Code refers to other chunks through *fixups*,
@@ -559,6 +562,47 @@
559562 Jacobi 129 ms to 106; rat 2% faster, within noise; the integer
560563 kernels unchanged.
561564
565+4. **Real variables in xmm registers.** NBody's `dx`, `dy`, `dz` and
566+ `mag` lived in the frame, each assignment a store and each use a
567+ load, on the chain of operations that each round waits on:
568+
569+ ```
570+ subsd xmm8, [r9 + r12 * 8]
571+ movsd [rbp - 8], xmm8 ; dx := x[i] - x[j]
572+ ...
573+ movsd xmm8, [rbp - 8] ; dx * dx
574+ mulsd xmm8, [rbp - 8]
575+ ```
576+
577+ Now `ChooseRegs` gives the busiest reals of a procedure `xmm12` to
578+ `xmm15` (`XVarRegs`), as it gives the busiest integers `rbx` and
579+ `r12` to `r15`, and gen reads and writes them where they are: a
580+ real in a variable's register is `lHeld` or `lInto`, as an
581+ integer's is (`XValueTo`, `PopVar`, `Peek` for `RealOp` and
582+ `Compare`):
583+
584+ ```
585+ subsd xmm8, [r9 + r12 * 8]
586+ movaps xmm13, xmm8
587+ ...
588+ movaps xmm8, xmm13
589+ mulsd xmm8, xmm13
590+ ```
591+
592+ Each such variable has its slot too, which `PopVar` writes as it
593+ writes the register (`movsd [rbp - 8], xmm13` after the `movaps`),
594+ off the chain the round waits on: the trap report finds the value
595+ there, as it finds any variable's, whether a check failed or a
596+ signal stopped the program, and `Call` loads the register from it
597+ after each call (`XHome`, `HomeOf`), as System V keeps no xmm
598+ register through one. A register number from `XmmVar`, 16, up is
599+ an xmm register's, so that `xmm12` isn't taken for `r12` where the
600+ parser counts its registers (`RegsOf`, `RegAt`). NBody 147 ms to
601+ 116, and a copy of it without the call to `sqrt` 114 to 81; rat
602+ within noise. Storing the slot only before a call, and so showing
603+ nothing in the trap report, bought 3% more on that copy and nothing
604+ on NBody.
605+
562606 ## diag.pas: messages
563607
564608 Every message the compiler prints is made here. A position, `TPos`, is
@@ -851,6 +895,12 @@
851895 `cmp rbx, r12`, and `PopVar` and `IncDecReg` write it. Rt finds the
852896 values for the trap report from the saves: each procedure's record in
853897 the places says which it saved, and where (`saveMask`, `saveAt`).
898+A real takes one of `xmm12` to `xmm15` (`XVarRegs`, numbered from
899+`XmmVar` up, so that `xmm12` isn't taken for `r12`), which the prologue
900+zeroes and saves nothing for, and a slot as well, which `PopVar`
901+writes as it writes the register (`XHome`): the trap report finds it
902+there, and `Call` loads the register from it after each call, as
903+System V has no callee-saved xmm register.
854904
855905 ## item.pas: items
856906
@@ -1042,7 +1092,9 @@
10421092 uses: 1 for each, 4 in a loop, 16 in two, at most 64 (loops nested three
10431093 deep or more); one `ADR` takes is out. The
10441094 busiest used at least three times get the five registers (`o^.reg`),
1045-a slice two (`o^.reg2` too), its count halved for each. An
1095+a slice two (`o^.reg2` too), its count halved for each, and the
1096+busiest reals the four of `XVarRegs`, each with a slot as well
1097+(`PlaceLocals`, `XHome`). An
10461098 address wanted after all, by a `var` parameter, say, comes to
10471099 `Address` (`item.pas`), which puts the variable back in memory and
10481100 sets `demoted`: `Body` then throws the procedure's code away
@@ -1183,10 +1235,11 @@
11831235 `_ := f(x)` drops one on purpose. `-Werror` makes each an error.
11841236 - **Registers for variables.** Up to five of a procedure's single
11851237 values, not reals, whose address is never taken, live in `rbx` and
1186- `r12` to `r15`, a slice taking two of them; in a procedure that
1187- calls nothing, a parameter that arrives in `rdi` or `rsi` stays
1188- there. The trap report shows them as it shows the others, but for
1189- slices, which it never shows.
1238+ `r12` to `r15`, a slice taking two of them, and up to four reals in
1239+ `xmm12` to `xmm15`, each written to its slot in the frame too; in a
1240+ procedure that calls nothing, a parameter that arrives in `rdi` or
1241+ `rsi` stays there. The trap report shows them as it shows the
1242+ others, but for slices, which it never shows.
11901243 - **Several results** that are two words, and a slice, come back in
11911244 rax and rdx, as from a code procedure; others through a hidden
11921245 pointer to the caller's slots.

src/gen.pas modified +58 -8

@@ -105,6 +105,16 @@
105105 rdi and rsi, where a procedure that calls nothing keeps the
106106 parameters that arrive in them (KeptIn) }
107107 ShownRegs: array[0..6] of longint = (RBX, R12, R13, R14, R15, RDI, RSI);
108+ { the registers a procedure keeps its busiest reals in, xmm12 to
109+ xmm15, numbered from XmmVar up so that they aren't taken for r12 to
110+ r15 (RegAt). Each has a slot too, written as it is, where the trap
111+ report finds it, and from which it is loaded again after a call, as
112+ System V has no callee-saved xmm register (XHome). }
113+ XmmVar = 16;
114+ XVarRegs: array[0..3] of longint = (XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15);
115+ { a procedure's registers of variables, as bits: bit i is VarRegs[i],
116+ bit XBit + i XVarRegs[i] }
117+ XBit = 5;
108118
109119 var
110120 rtFail, argcChunk, argvChunk: longint;
@@ -188,6 +198,7 @@
188198 procedure KeepArrived(o: PObj);
189199 function KeptIn(o: PObj): boolean;
190200 procedure UseRegs(mask: longint);
201+procedure XHome(r: longint; slot: int64);
191202 procedure MarkCode(var m: TCodeMark);
192203 procedure Redo(const m: TCodeMark);
193204 procedure Rewind(const m: TCodeMark);
@@ -269,9 +280,10 @@
269280 inObj: array[0..13] of PObj;
270281 numIn: longint;
271282 procParams: PObj; { the parameters, for EnterBody }
272- regMask: longint; { the registers of variables: bit i is VarRegs[i] }
283+ regMask: longint; { the registers of variables, as bits (XBit) }
273284 kept: longint; { bit r: register r holds a parameter, as it arrived }
274285 saveAt: int64; { where they are saved, a quadword each }
286+ homes: array[0..3] of int64; { the slots of the variables in XVarRegs }
275287 frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations }
276288 { the checks of the procedure, whose failures EndFails makes }
277289 fails: array[0..4095] of record
@@ -351,6 +363,7 @@
351363 case stack[i].loc of
352364 lConst: MovsXM(x, RealConst(stack[i].val), single);
353365 lMem: MovsXM(x, stack[i].m, stack[i].size = 4);
366+ lHeld, lInto: if stack[i].val <> x then MovX(x, stack[i].val);
354367 lReg: if RegOf(i) <> x then MovX(x, RegOf(i));
355368 else MovsXM(x, Mem(RBP, stack[i].val), false); { spilled, as 8 bytes }
356369 end;
@@ -528,7 +541,8 @@
528541 value where it was, so r is its only new copy. }
529542 procedure ValueTo(i, r: longint);
530543 begin
531- case stack[i].loc of
544+ if IsReal(i) and (stack[i].loc in [lHeld, lInto]) then MovqRX(r, stack[i].val)
545+ else case stack[i].loc of
532546 lConst: MovRI(r, stack[i].val);
533547 lHeld, lInto: MovRR(r, stack[i].val);
534548 lAdr: Lea(r, AdrMem(i, 0));
@@ -574,6 +588,7 @@
574588 function RegAt(const a: TAdr): longint;
575589 begin
576590 if a.off = 0 then RegAt := a.chunk else RegAt := a.hi;
591+ if RegAt >= XmmVar then RegAt := RegAt - XmmVar; { an xmm register, XVarRegs }
577592 end;
578593
579594 { The memory a names. An address on the stack is popped, and is in its
@@ -631,6 +646,7 @@
631646 begin
632647 r := inPlace;
633648 inPlace := -1;
649+ if r >= XmmVar then r := r - XmmVar;
634650 if (a.base = bReg) and (RegAt(a) = r) then begin
635651 NewEntry(k, lInto, r);
636652 exit;
@@ -663,11 +679,23 @@
663679 Computed(kWord);
664680 end;
665681
682+{ The slot of the variable in xmm register x }
683+function HomeOf(x: longint): TMem;
684+begin
685+ HomeOf := Mem(RBP, homes[x + XmmVar - XVarRegs[0]]);
686+end;
687+
666688 { Pops a value into the variable at a; an address on the stack is under
667689 the value. }
668690 procedure PopVar(const a: TAdr; size: longint);
669691 var r: longint; v: int64; real: boolean;
670692 begin
693+ if (a.base = bReg) and IsReal(depth - 1) then begin
694+ XValueTo(depth - 1, RegAt(a));
695+ Drop;
696+ MovsMX(HomeOf(RegAt(a)), RegAt(a), false); { written through, as XHome says }
697+ exit;
698+ end;
671699 if a.base = bReg then begin
672700 if TopConst(v) then MovRI(RegAt(a), v)
673701 else if Peek(depth - 1) <> RegAt(a) then MovRR(RegAt(a), Peek(depth - 1)); { else it is there, lInto }
@@ -887,7 +915,7 @@
887915 Drop;
888916 SseM(c, Top, m, f);
889917 end else begin
890- y := Loaded(depth - 1);
918+ y := Peek(depth - 1);
891919 Drop;
892920 x := Top;
893921 Sse(c, x, y, f);
@@ -997,9 +1025,9 @@
9971025 begin
9981026 if k in [kF32, kF64] then begin
9991027 f := k = kF32;
1000- y := Loaded(depth - 1);
1028+ y := Peek(depth - 1);
10011029 Drop;
1002- x := Top;
1030+ x := Peek(depth - 1);
10031031 Drop;
10041032 r := Target; { the BOOL, in the left's integer register }
10051033 { ucomis sets the flags as an unsigned comparison would, but for an
@@ -1697,6 +1725,10 @@
16971725 CallR(R11);
16981726 end;
16991727 if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad));
1728+ { the variables in xmm registers, which the call may have changed,
1729+ from their slots }
1730+ for i := 0 to 3 do
1731+ if regMask and (1 shl (XBit + i)) <> 0 then MovsXM(XVarRegs[i] - XmmVar, Mem(RBP, homes[i]), false);
17001732 { the result: a slice in rax and rdx goes to the caller's temporary,
17011733 two results on the stack, the first under the second, and a single
17021734 value on top }
@@ -1881,6 +1913,13 @@
18811913 frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop;
18821914 end;
18831915
1916+{ The variable in xmm register r of XVarRegs has a slot of 8 bytes at
1917+ slot, kept as it changes }
1918+procedure XHome(r: longint; slot: int64);
1919+begin
1920+ homes[r - XVarRegs[0]] := slot;
1921+end;
1922+
18841923 procedure MarkCode(var m: TCodeMark);
18851924 begin
18861925 m.pc := pc; m.fixups := numFixups; m.lines := numLines; m.said := SaidCount;
@@ -1939,7 +1978,7 @@
19391978 end;
19401979 { each register that holds a variable is saved, for the caller, and
19411980 zeroed, as a variable starts at zero }
1942- chunks^[c].saveMask := regMask;
1981+ chunks^[c].saveMask := regMask and (1 shl XBit - 1);
19431982 chunks^[c].saveAt := saveAt;
19441983 n := 0;
19451984 for i := 0 to 4 do
@@ -1947,12 +1986,22 @@
19471986 Store(Mem(RBP, saveAt + 8 * n), VarRegs[i], 8);
19481987 MovRI(VarRegs[i], 0);
19491988 inc(n);
1989+ end;
1990+ { and each xmm register that holds a variable, zeroed, as its slot
1991+ is: the caller keeps its own }
1992+ for i := 0 to 3 do
1993+ if regMask and (1 shl (XBit + i)) <> 0 then begin
1994+ MovRI(RAX, 0);
1995+ MovqXR(XVarRegs[i] - XmmVar, RAX);
19501996 end;
19511997 { the parameters that came in registers, to their slots or, for a
19521998 register variable, to its register: a slice's second word, the one
19531999 for the slot 8 bytes into its own, to its length's }
19542000 for i := 0 to numIn - 1 do
1955- if inReg[i] < 0 then MovsMX(Mem(RBP, inSlot[i]), -1 - inReg[i], false)
2001+ if inReg[i] < 0 then begin
2002+ MovsMX(Mem(RBP, inSlot[i]), -1 - inReg[i], false);
2003+ if inObj[i]^.reg <> 0 then MovX(inObj[i]^.reg - XmmVar, -1 - inReg[i]);
2004+ end
19562005 else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin
19572006 r := inObj[i]^.reg;
19582007 if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2;
@@ -1964,7 +2013,8 @@
19642013 p := procParams;
19652014 while p <> nil do begin
19662015 if (p^.reg <> 0) and (p^.val > 0) then
1967- if p^.typ^.form in [fInt, fBool, fEnum, fSet] then
2016+ if p^.typ^.form = fReal then MovsXM(p^.reg - XmmVar, Mem(RBP, p^.val), p^.typ^.size = 4)
2017+ else if p^.typ^.form in [fInt, fBool, fEnum, fSet] then
19682018 Load(p^.reg, Mem(RBP, p^.val), p^.typ^.size, p^.typ^.signed)
19692019 else begin
19702020 Load(p^.reg, Mem(RBP, p^.val), 8, false);

src/parse.pas modified +31 -14

@@ -1878,12 +1878,12 @@
18781878 if o^.reg = ShownRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512;
18791879 end;
18801880
1881-{ Whether variable o could live in registers: a single value, not a
1882- real, or a slice, in two, that isn't C's }
1881+{ Whether variable o could live in registers: a single value, a real
1882+ in an xmm register, or a slice, in two, that isn't C's }
18831883 function RegFit(o: PObj): boolean;
18841884 begin
18851885 RegFit := (o^.cls = oVar) and not o^.ind and not o^.ext
1886- and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError, fSlice])
1886+ and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError, fSlice, fReal])
18871887 or (o^.typ^.form = fSet) and (o^.typ^.size <= 8));
18881888 end;
18891889
@@ -1900,15 +1900,16 @@
19001900 its "begin" to its end, a use counts 1, in a loop 4, in two 16, in
19011901 three or more 64, and a variable ADR is taken of can't have one. A register costs a save
19021902 and a restore, so a variable used less than three times keeps its
1903- place in memory. A slice takes two, its pointer's and its length's.
1904- In a procedure that names no procedure, so calls nothing, the
1903+ place in memory. A slice takes two, its pointer's and its length's;
1904+ a real one of the xmm registers, stored and loaded again around each
1905+ call, as System V keeps none through one. In a procedure that names no procedure, so calls nothing, the
19051906 parameters that arrive in the registers no call needs stay there
19061907 (KeepArrived), at no cost. }
19071908 procedure ChooseRegs(params, before: PObj);
19081909 var
19091910 cand: array[0..63] of PObj; weight: array[0..63] of longint;
19101911 isLoop: array[0..255] of boolean;
1911- n, i, k, d, loops, best, need: longint; o, q, qual: PObj; st: TScanState;
1912+ n, i, k, kx, d, loops, best, need: longint; o, q, qual: PObj; st: TScanState;
19121913 afterDot, wasSkipping, leaf: boolean;
19131914
19141915 function Candidate(name: longint): longint;
@@ -1993,25 +1994,29 @@
19931994 for i := 0 to n - 1 do
19941995 if cand[i]^.typ^.form = fSlice then weight[i] := weight[i] div 2;
19951996 { the heaviest, each used at least three times a register, take the
1996- five registers in turn, while there are enough for each }
1997- k := 0;
1997+ five registers in turn, while there are enough for each, and the
1998+ reals the four xmm registers of XVarRegs }
1999+ k := 0; kx := 0;
19982000 repeat
19992001 best := -1;
20002002 for i := 0 to n - 1 do
20012003 if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i;
20022004 if best < 0 then break;
20032005 need := 1 + ord(cand[best]^.typ^.form = fSlice);
2004- if k + need <= 5 then begin
2006+ if cand[best]^.typ^.form = fReal then begin
2007+ if kx < 4 then begin cand[best]^.reg := XVarRegs[kx]; inc(kx) end
2008+ else weight[best] := 0;
2009+ end else if k + need <= 5 then begin
20052010 cand[best]^.reg := VarRegs[k];
20062011 if need = 2 then cand[best]^.reg2 := VarRegs[k + 1];
20072012 inc(k, need);
20082013 end else weight[best] := 0; { too big for what is left }
2009- until k = 5;
2014+ until false;
20102015 end;
20112016 end;
20122017
2013-{ The registers the variables of the procedure are in, as bits of
2014- VarRegs }
2018+{ The registers the variables of the procedure are in, as bits: bit i
2019+ is VarRegs[i], bit XBit + i XVarRegs[i] }
20152020 function RegsOf(params, before: PObj): longint;
20162021 var o: PObj; m, k: longint;
20172022 begin
@@ -2019,12 +2024,15 @@
20192024 o := params;
20202025 while o <> nil do begin
20212026 for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k);
2027+ for k := 0 to 3 do if o^.reg = XVarRegs[k] then m := m or (1 shl (XBit + k));
20222028 o := o^.next;
20232029 end;
20242030 o := FirstLocal(before);
20252031 while o <> nil do begin
2026- if o^.cls = oVar then
2032+ if o^.cls = oVar then begin
20272033 for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k);
2034+ for k := 0 to 3 do if o^.reg = XVarRegs[k] then m := m or (1 shl (XBit + k));
2035+ end;
20282036 o := o^.link;
20292037 end;
20302038 RegsOf := m;
@@ -2040,8 +2048,17 @@
20402048 ResetLocals;
20412049 o := FirstLocal(before);
20422050 while o <> nil do begin
2043- if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align);
2051+ if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align)
2052+ else if (o^.cls = oVar) and (o^.reg >= XmmVar) then begin
2053+ o^.val := AllocPerm(8, 8);
2054+ XHome(o^.reg, o^.val);
2055+ end;
20442056 o := o^.link;
2057+ end;
2058+ o := params;
2059+ while o <> nil do begin
2060+ if o^.reg >= XmmVar then XHome(o^.reg, o^.val);
2061+ o := o^.next;
20452062 end;
20462063 UseRegs(RegsOf(params, before));
20472064 end;