From 36e782dfb76b6ea26a5f697677ae23e6051fbd66 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:59:41 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index cad10ed..3a32956 100644 --- a/PLAN.md +++ b/PLAN.md @@ -41,8 +41,11 @@ `+`, `-` and `*` by a constant, and an element's operand takes it as its displacement: `[r13 + r9 * 8 - 8]`. Jacobi 129 ms to 106; rat within noise. -5. **Real variables in xmm registers**, where that pays: measured, and - kept only if it does. +5. Done: **Real variables in xmm registers.** The busiest four reals + of a procedure live in xmm12 to xmm15, each written through to its + slot, from which the trap report reads it and a call's return loads + it, as System V keeps no xmm register through a call. NBody 147 ms + to 116; rat within noise. ## Next, when the owner says diff --git a/doc/compiler.md b/doc/compiler.md index 536a292..1830094 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -15,9 +15,9 @@ | `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,201 | 992 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 2,188 | 1,588 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/gen.pas` | 2,238 | 1,622 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/item.pas` | 1,578 | 1,301 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 3,103 | 2,707 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/parse.pas` | 3,120 | 2,722 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 245 | 188 | The command line | @@ -60,7 +60,10 @@ five, live in registers that calls leave alone: the callee-saved ones of System V, the calling convention of x86-64 Linux, which a called procedure must give back as it found them (`rbx` and `r12` to `r15`). -Nothing but their own assignments can change them. +Nothing but their own assignments can change them. Up to four reals +live in `xmm12` to `xmm15`, each written to the frame as well and +loaded from there after a call, as System V has no xmm register that +a call leaves alone. The output is a set of *chunks*: one for each procedure, each module variable, each string. Code refers to other chunks through *fixups*, @@ -559,6 +562,47 @@ Jacobi 129 ms to 106; rat 2% faster, within noise; the integer kernels unchanged. +4. **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 of operations that each round waits on: + + ``` + subsd xmm8, [r9 + r12 * 8] + movsd [rbp - 8], xmm8 ; dx := x[i] - x[j] + ... + movsd xmm8, [rbp - 8] ; dx * dx + mulsd xmm8, [rbp - 8] + ``` + + Now `ChooseRegs` gives the busiest reals of a procedure `xmm12` to + `xmm15` (`XVarRegs`), as it gives the busiest integers `rbx` and + `r12` to `r15`, and gen reads and writes them where they are: a + real in a variable's register is `lHeld` or `lInto`, as an + integer's is (`XValueTo`, `PopVar`, `Peek` for `RealOp` and + `Compare`): + + ``` + subsd xmm8, [r9 + r12 * 8] + movaps xmm13, xmm8 + ... + movaps xmm8, xmm13 + mulsd xmm8, xmm13 + ``` + + Each such variable has its slot too, which `PopVar` writes as it + writes the register (`movsd [rbp - 8], xmm13` after the `movaps`), + off the chain the round waits on: the trap report finds the value + there, as it finds any variable's, whether a check failed or a + signal stopped the program, and `Call` loads the register from it + after each call (`XHome`, `HomeOf`), as System V keeps no xmm + register through one. A register number from `XmmVar`, 16, up is + an xmm register's, so that `xmm12` isn't taken for `r12` where the + parser counts its registers (`RegsOf`, `RegAt`). NBody 147 ms to + 116, and a copy of it without the call to `sqrt` 114 to 81; rat + within noise. Storing the slot only before a call, and so showing + nothing in the trap report, bought 3% more on that copy and nothing + on NBody. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is @@ -851,6 +895,12 @@ `cmp rbx, r12`, and `PopVar` and `IncDecReg` write it. Rt finds the values for the trap report from the saves: each procedure's record in the places says which it saved, and where (`saveMask`, `saveAt`). +A real takes one of `xmm12` to `xmm15` (`XVarRegs`, numbered from +`XmmVar` up, so that `xmm12` isn't taken for `r12`), which the prologue +zeroes and saves nothing for, and a slot as well, which `PopVar` +writes as it writes the register (`XHome`): the trap report finds it +there, and `Call` loads the register from it after each call, as +System V has no callee-saved xmm register. ## item.pas: items @@ -1042,7 +1092,9 @@ uses: 1 for each, 4 in a loop, 16 in two, at most 64 (loops nested three deep or more); one `ADR` takes is out. The busiest used at least three times get the five registers (`o^.reg`), -a slice two (`o^.reg2` too), its count halved for each. An +a slice two (`o^.reg2` too), its count halved for each, and the +busiest reals the four of `XVarRegs`, each with a slot as well +(`PlaceLocals`, `XHome`). An address wanted after all, by a `var` parameter, say, comes to `Address` (`item.pas`), which puts the variable back in memory and sets `demoted`: `Body` then throws the procedure's code away @@ -1183,10 +1235,11 @@ `_ := f(x)` drops one on purpose. `-Werror` makes each an error. - **Registers for variables.** Up to five of a procedure's single values, not reals, whose address is never taken, live in `rbx` and - `r12` to `r15`, a slice taking two of them; in a procedure that - calls nothing, a parameter that arrives in `rdi` or `rsi` stays - there. The trap report shows them as it shows the others, but for - slices, which it never shows. + `r12` to `r15`, a slice taking two of them, and up to four reals in + `xmm12` to `xmm15`, each written to its slot in the frame too; in a + procedure that calls nothing, a parameter that arrives in `rdi` or + `rsi` stays there. The trap report shows them as it shows the + others, but for slices, which it never shows. - **Several results** that are two words, and a slice, come back in rax and rdx, as from a code procedure; others through a hidden pointer to the caller's slots. diff --git a/src/gen.pas b/src/gen.pas index 2fc05f8..5bb39b3 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -105,6 +105,16 @@ rdi and rsi, where a procedure that calls nothing keeps the parameters that arrive in them (KeptIn) } ShownRegs: array[0..6] of longint = (RBX, R12, R13, R14, R15, RDI, RSI); + { the registers a procedure keeps its busiest reals in, xmm12 to + xmm15, numbered from XmmVar up so that they aren't taken for r12 to + r15 (RegAt). Each has a slot too, written as it is, where the trap + report finds it, and from which it is loaded again after a call, as + System V has no callee-saved xmm register (XHome). } + XmmVar = 16; + XVarRegs: array[0..3] of longint = (XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15); + { a procedure's registers of variables, as bits: bit i is VarRegs[i], + bit XBit + i XVarRegs[i] } + XBit = 5; var rtFail, argcChunk, argvChunk: longint; @@ -188,6 +198,7 @@ procedure KeepArrived(o: PObj); function KeptIn(o: PObj): boolean; procedure UseRegs(mask: longint); +procedure XHome(r: longint; slot: int64); procedure MarkCode(var m: TCodeMark); procedure Redo(const m: TCodeMark); procedure Rewind(const m: TCodeMark); @@ -269,9 +280,10 @@ inObj: array[0..13] of PObj; numIn: longint; procParams: PObj; { the parameters, for EnterBody } - regMask: longint; { the registers of variables: bit i is VarRegs[i] } + regMask: longint; { the registers of variables, as bits (XBit) } kept: longint; { bit r: register r holds a parameter, as it arrived } saveAt: int64; { where they are saved, a quadword each } + homes: array[0..3] of int64; { the slots of the variables in XVarRegs } frameAt: array[0..2] of int64; { frameTop, frameMax, permTop after the declarations } { the checks of the procedure, whose failures EndFails makes } fails: array[0..4095] of record @@ -351,6 +363,7 @@ case stack[i].loc of lConst: MovsXM(x, RealConst(stack[i].val), single); lMem: MovsXM(x, stack[i].m, stack[i].size = 4); + lHeld, lInto: if stack[i].val <> x then MovX(x, stack[i].val); lReg: if RegOf(i) <> x then MovX(x, RegOf(i)); else MovsXM(x, Mem(RBP, stack[i].val), false); { spilled, as 8 bytes } end; @@ -528,7 +541,8 @@ value where it was, so r is its only new copy. } procedure ValueTo(i, r: longint); begin - case stack[i].loc of + if IsReal(i) and (stack[i].loc in [lHeld, lInto]) then MovqRX(r, stack[i].val) + else case stack[i].loc of lConst: MovRI(r, stack[i].val); lHeld, lInto: MovRR(r, stack[i].val); lAdr: Lea(r, AdrMem(i, 0)); @@ -574,6 +588,7 @@ function RegAt(const a: TAdr): longint; begin if a.off = 0 then RegAt := a.chunk else RegAt := a.hi; + if RegAt >= XmmVar then RegAt := RegAt - XmmVar; { an xmm register, XVarRegs } end; { The memory a names. An address on the stack is popped, and is in its @@ -631,6 +646,7 @@ begin r := inPlace; inPlace := -1; + if r >= XmmVar then r := r - XmmVar; if (a.base = bReg) and (RegAt(a) = r) then begin NewEntry(k, lInto, r); exit; @@ -663,11 +679,23 @@ Computed(kWord); end; +{ The slot of the variable in xmm register x } +function HomeOf(x: longint): TMem; +begin + HomeOf := Mem(RBP, homes[x + XmmVar - XVarRegs[0]]); +end; + { Pops a value into the variable at a; an address on the stack is under the value. } procedure PopVar(const a: TAdr; size: longint); var r: longint; v: int64; real: boolean; begin + if (a.base = bReg) and IsReal(depth - 1) then begin + XValueTo(depth - 1, RegAt(a)); + Drop; + MovsMX(HomeOf(RegAt(a)), RegAt(a), false); { written through, as XHome says } + exit; + end; if a.base = bReg then begin if TopConst(v) then MovRI(RegAt(a), v) else if Peek(depth - 1) <> RegAt(a) then MovRR(RegAt(a), Peek(depth - 1)); { else it is there, lInto } @@ -887,7 +915,7 @@ Drop; SseM(c, Top, m, f); end else begin - y := Loaded(depth - 1); + y := Peek(depth - 1); Drop; x := Top; Sse(c, x, y, f); @@ -997,9 +1025,9 @@ begin if k in [kF32, kF64] then begin f := k = kF32; - y := Loaded(depth - 1); + y := Peek(depth - 1); Drop; - x := Top; + x := Peek(depth - 1); Drop; r := Target; { the BOOL, in the left's integer register } { ucomis sets the flags as an unsigned comparison would, but for an @@ -1697,6 +1725,10 @@ CallR(R11); end; if k + pad > 0 then AluI(aluAdd, RSP, 8 * (k + pad)); + { the variables in xmm registers, which the call may have changed, + from their slots } + for i := 0 to 3 do + if regMask and (1 shl (XBit + i)) <> 0 then MovsXM(XVarRegs[i] - XmmVar, Mem(RBP, homes[i]), false); { the result: a slice in rax and rdx goes to the caller's temporary, two results on the stack, the first under the second, and a single value on top } @@ -1881,6 +1913,13 @@ frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; end; +{ The variable in xmm register r of XVarRegs has a slot of 8 bytes at + slot, kept as it changes } +procedure XHome(r: longint; slot: int64); +begin + homes[r - XVarRegs[0]] := slot; +end; + procedure MarkCode(var m: TCodeMark); begin m.pc := pc; m.fixups := numFixups; m.lines := numLines; m.said := SaidCount; @@ -1939,7 +1978,7 @@ end; { each register that holds a variable is saved, for the caller, and zeroed, as a variable starts at zero } - chunks^[c].saveMask := regMask; + chunks^[c].saveMask := regMask and (1 shl XBit - 1); chunks^[c].saveAt := saveAt; n := 0; for i := 0 to 4 do @@ -1947,12 +1986,22 @@ Store(Mem(RBP, saveAt + 8 * n), VarRegs[i], 8); MovRI(VarRegs[i], 0); inc(n); + end; + { and each xmm register that holds a variable, zeroed, as its slot + is: the caller keeps its own } + for i := 0 to 3 do + if regMask and (1 shl (XBit + i)) <> 0 then begin + MovRI(RAX, 0); + MovqXR(XVarRegs[i] - XmmVar, RAX); end; { the parameters that came in registers, to their slots or, for a register variable, to its register: a slice's second word, the one for the slot 8 bytes into its own, to its length's } for i := 0 to numIn - 1 do - if inReg[i] < 0 then MovsMX(Mem(RBP, inSlot[i]), -1 - inReg[i], false) + if inReg[i] < 0 then begin + MovsMX(Mem(RBP, inSlot[i]), -1 - inReg[i], false); + if inObj[i]^.reg <> 0 then MovX(inObj[i]^.reg - XmmVar, -1 - inReg[i]); + end else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin r := inObj[i]^.reg; if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2; @@ -1964,7 +2013,8 @@ p := procParams; while p <> nil do begin if (p^.reg <> 0) and (p^.val > 0) then - if p^.typ^.form in [fInt, fBool, fEnum, fSet] then + if p^.typ^.form = fReal then MovsXM(p^.reg - XmmVar, Mem(RBP, p^.val), p^.typ^.size = 4) + else if p^.typ^.form in [fInt, fBool, fEnum, fSet] then Load(p^.reg, Mem(RBP, p^.val), p^.typ^.size, p^.typ^.signed) else begin Load(p^.reg, Mem(RBP, p^.val), 8, false); diff --git a/src/parse.pas b/src/parse.pas index be36e4d..9848441 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1878,12 +1878,12 @@ if o^.reg = ShownRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512; end; -{ Whether variable o could live in registers: a single value, not a - real, or a slice, in two, that isn't C's } +{ Whether variable o could live in registers: a single value, a real + in an xmm register, or a slice, in two, that isn't C's } function RegFit(o: PObj): boolean; begin RegFit := (o^.cls = oVar) and not o^.ind and not o^.ext - and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError, fSlice]) + and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError, fSlice, fReal]) or (o^.typ^.form = fSet) and (o^.typ^.size <= 8)); end; @@ -1900,15 +1900,16 @@ its "begin" to its end, a use counts 1, in a loop 4, in two 16, in three or more 64, and a variable ADR is taken of can't have one. A register costs a save and a restore, so a variable used less than three times keeps its - place in memory. A slice takes two, its pointer's and its length's. - In a procedure that names no procedure, so calls nothing, the + place in memory. A slice takes two, its pointer's and its length's; + a real one of the xmm registers, stored and loaded again around each + call, as System V keeps none through one. In a procedure that names no procedure, so calls nothing, the parameters that arrive in the registers no call needs stay there (KeepArrived), at no cost. } procedure ChooseRegs(params, before: PObj); var cand: array[0..63] of PObj; weight: array[0..63] of longint; isLoop: array[0..255] of boolean; - n, i, k, d, loops, best, need: longint; o, q, qual: PObj; st: TScanState; + n, i, k, kx, d, loops, best, need: longint; o, q, qual: PObj; st: TScanState; afterDot, wasSkipping, leaf: boolean; function Candidate(name: longint): longint; @@ -1993,25 +1994,29 @@ for i := 0 to n - 1 do if cand[i]^.typ^.form = fSlice then weight[i] := weight[i] div 2; { the heaviest, each used at least three times a register, take the - five registers in turn, while there are enough for each } - k := 0; + five registers in turn, while there are enough for each, and the + reals the four xmm registers of XVarRegs } + k := 0; kx := 0; repeat best := -1; for i := 0 to n - 1 do if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i; if best < 0 then break; need := 1 + ord(cand[best]^.typ^.form = fSlice); - if k + need <= 5 then begin + if cand[best]^.typ^.form = fReal then begin + if kx < 4 then begin cand[best]^.reg := XVarRegs[kx]; inc(kx) end + else weight[best] := 0; + end else if k + need <= 5 then begin cand[best]^.reg := VarRegs[k]; if need = 2 then cand[best]^.reg2 := VarRegs[k + 1]; inc(k, need); end else weight[best] := 0; { too big for what is left } - until k = 5; + until false; end; end; -{ The registers the variables of the procedure are in, as bits of - VarRegs } +{ The registers the variables of the procedure are in, as bits: bit i + is VarRegs[i], bit XBit + i XVarRegs[i] } function RegsOf(params, before: PObj): longint; var o: PObj; m, k: longint; begin @@ -2019,12 +2024,15 @@ o := params; while o <> nil do begin for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k); + for k := 0 to 3 do if o^.reg = XVarRegs[k] then m := m or (1 shl (XBit + k)); o := o^.next; end; o := FirstLocal(before); while o <> nil do begin - if o^.cls = oVar then + if o^.cls = oVar then begin for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k); + for k := 0 to 3 do if o^.reg = XVarRegs[k] then m := m or (1 shl (XBit + k)); + end; o := o^.link; end; RegsOf := m; @@ -2040,8 +2048,17 @@ ResetLocals; o := FirstLocal(before); while o <> nil do begin - if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align); + if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align) + else if (o^.cls = oVar) and (o^.reg >= XmmVar) then begin + o^.val := AllocPerm(8, 8); + XHome(o^.reg, o^.val); + end; o := o^.link; + end; + o := params; + while o <> nil do begin + if o^.reg >= XmmVar then XHome(o^.reg, o^.val); + o := o^.next; end; UseRegs(RegsOf(params, before)); end;