From 16eaf7cba88dfae9032480f989bc829494c11013 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:56:39 +0100 Subject: [PATCH] A parameter kept in rdi or rsi that a copy wants takes callee-saved registers no variable has, if it is used enough for one, not memory: unkeep, on lost (ChaCha's block that starts out := st 1,132 ms to 684) --- diff --git a/doc/compiler.md b/doc/compiler.md index 19a958f..cc06cf7 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -23,7 +23,7 @@ | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,800 | 1,963 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,592 | 3,942 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Parse.raz` | 4,611 | 3,956 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -335,8 +335,9 @@ call or a copy needs: nothing is saved or moved, and `s[i]` is read through `rdi` itself (`keepArrived`). Where Gen needs them after all, to copy a record or compare two slices, it sets `lost`, and - `body` compiles the procedure again with the parameters in memory, - as for a variable demoted. The trap report shows a parameter kept + `body` compiles the procedure again with the parameters in + callee-saved registers no variable has, or in memory (`unkeep`, in + "Array parameters, measured"), as for a variable demoted. The trap report shows a parameter kept so: `Rt.fail` is given `rdi` and `rsi` as the failing code had them, and the signal handler takes them from the context the kernel saved. The search 9% faster, rat 1.5%, its text 0.7% @@ -914,6 +915,29 @@ the sum over a slice; ChaCha's `quarter(var x: [16]U32; ...)` 560 ms to 283 (2.0 times), in 58% fewer instructions. +2. **A parameter that loses `rdi` or `rsi` takes a free register.** + A procedure that calls nothing keeps its parameters where they + arrive (`keepArrived`); when it copies an array after all, `lost` + sends them to memory for the second try at the body, though `rbx` + and `r12` to `r15` may be free. `block(const st: [16]U32; var out: + [16]U32)`, which starts `out := st`, loaded `out`'s pointer at + each use: + + ``` + mov r8, [rbp - 16] + mov r8d, [r8] now: mov r9d, [r13 + 16] + mov r9, [rbp - 16] mov r8d, [r13] + mov r9d, [r9 + 16] add r8, r9 + add r8, r9 + ``` + + `unkeep` gives such a parameter the registers of `VarRegs` that + `regsOf` finds free, one, or two for a slice, if it is used enough + for a register by `chooseRegs`'s count; else it goes to memory as + before. The prologue saves those registers and moves the parameter + there, as it does for a procedure that calls. ChaCha's block with + the copy 1,132 ms to 684, 35% fewer instructions. + ## Diag.raz: messages Every message the compiler prints is made here. A position, `Pos`, is a @@ -1917,7 +1941,9 @@ `Item.address`, which puts the variable back in memory and sets `Item.demoted`: `body` then throws the procedure's code away (`Gen.markCode`, `Gen.redo`), places the locals again, and compiles its -statements again. Its notes for +statements again. So it does when Gen wanted a parameter's `rdi` or +`rsi` (`Gen.lost`), the parameter moved to a free register or memory +(`unkeep`). Its notes for the trap report are made after, by `varKind`, which marks a variable in a register with the register's number. diff --git a/src/Parse.raz b/src/Parse.raz index e1f6623..175ae8a 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3148,6 +3148,25 @@ return m; end; +(* Parameter o, kept where it arrived until a call or a copy wanted rdi + or rsi, takes the registers of VarRegs that m, the taken ones, leaves, + if it is used enough for them (chooseRegs's weight of 3) and there + are enough; else it goes to memory. *) +proc unkeep(o: PObj; m: INT); +var k: INT; two: BOOL; +begin + two := o^.reg2 # 0; + o^.reg := 0; o^.reg2 := 0; + if weight[candidate(o^.name)] < 3 then return; end; + for k := 0 to 4 do + if (m & (1 shl k)) = 0 then + if o^.reg = 0 then o^.reg := Gen.VarRegs[k]; + elsif two and (o^.reg2 = 0) then o^.reg2 := Gen.VarRegs[k]; end; + end; + end; + if two and (o^.reg2 = 0) then o^.reg := 0; end; +end; + (* Gives each local not in a register its slot in the frame, the slots the prologue zeroes, and then tells Gen which registers the variables are in. A variable in a register has no slot: it starts at @@ -3221,12 +3240,12 @@ Chunk.noteLine(Scan.curLine(), Scan.curCol()); try expect(Scan.sEnd); if not (Item.demoted or Gen.lost) or (Diag.errors # errs) then break; end; - (* a parameter kept where it arrived goes to memory: a call or a copy - wanted its register *) + (* a parameter kept where it arrived goes to a free register of + VarRegs, or to memory: a call or a copy wanted its register *) if Gen.lost then o := params; while o # nil do - if Gen.keptIn(o) then o^.reg := 0; o^.reg2 := 0; end; + if Gen.keptIn(o) then unkeep(o, regsOf(params, before)); end; o := o^.next; end; end; diff --git a/test/run/KeptCopy.expect b/test/run/KeptCopy.expect new file mode 100644 index 0000000..f9f7a4c --- /dev/null +++ b/test/run/KeptCopy.expect @@ -0,0 +1,8 @@ +16 22 26 36 60 106 182 296 +1638 +-10 -9 -6 -1 6 15 26 39 +-350 +7396 +12 51 138 326 729 1591 3436 7396 +99 -9 -6 -1 6 15 26 39 +-12 diff --git a/test/run/KeptCopy.raz b/test/run/KeptCopy.raz new file mode 100644 index 0000000..0aad445 --- /dev/null +++ b/test/run/KeptCopy.raz @@ -0,0 +1,102 @@ +(* A procedure that calls nothing keeps its first parameters in rdi and + rsi; when it copies an array, which needs them, it is compiled again + with those parameters in callee-saved registers where some are free + and they are used enough, or in memory. The copy and the uses after + it must agree either way, and a failed check still says their + values. *) +module KeptCopy; +import Out; + +const N = 8; + +type Row = [N]I64; + +var r1, r2: Row; total: I64; + +(* out a copy of st, then each element worked on through both pointers *) +proc mix(const st: Row; var out: Row; k: INT); +var i: INT; +begin + out := st; + for i := 0 to N - 1 do out[i] := out[i] * 3 + st[N - 1 - i] + k; end; + for i := 1 to N - 1 do out[i] := out[i] + out[i - 1]; end; +end; + +(* a slice, kept in both registers, then a copy, then the slice read: + one callee-saved register is left, too few for it, so it goes to + memory *) +proc sumAfter(s: []I64; var a: Row; const b: Row): I64; +var i: INT; t: I64; +begin + a := b; + t := 0; + for i := 0 to LEN(s) - 1 do t := t + s[i] * a[i mod N]; end; + return t; +end; + +(* the same, with registers enough left for the slice's two *) +proc sumThen(s: []I64; var a: Row): I64; +var i: INT; t: I64; +begin + a := r1; + t := 0; + for i := 0 to LEN(s) - 1 do t := t + s[i] * s[LEN(s) - 1 - i]; end; + return t + a[0]; +end; + +(* five busy locals take every callee-saved register: the pointers go + to memory *) +proc busy(var out: Row; const st: Row): I64; +var a, b, c, d, e, i: INT; +begin + out := st; + a := 1; b := 2; c := 3; d := 4; e := 5; + for i := 0 to N - 1 do + a := a + b; b := b + c; c := c + d; d := d + e; e := e + a; + out[i] := out[i] + a + b + c + d + e + st[i]; + end; + return out[N - 1]; +end; + +(* a pointer used once only goes to memory *) +proc once(var out: Row; const st: Row); +begin + out := st; + out[0] := 99; +end; + +(* the element at k of a copy, which may be out of range *) +proc pick(const st: Row; var out: Row; k: INT): I64; +var i: INT; +begin + out := st; + for i := 0 to N - 1 do out[i] := out[i] + st[i]; end; + return out[k] + st[0]; +end; + +proc say(const r: Row); +var i: INT; +begin + for i := 0 to N - 1 do Out.int(r[i]); Out.char(' '); end; + Out.ln; +end; + +proc main*; +var i: INT; +begin + for i := 0 to N - 1 do r1[i] := I64(i * i - 10); end; + mix(r1, r2, 7); + say(r2); + total := sumAfter(r1[1..N], r2, r1); + Out.int(total); Out.ln; + say(r2); + Out.int(sumThen(r1[2..N], r2)); Out.ln; + Out.int(busy(r2, r1)); Out.ln; + say(r2); + once(r2, r1); + say(r2); + Out.int(pick(r1, r2, 3)); Out.ln; + Out.int(pick(r1, r2, N + 1)); Out.ln; +end; + +end KeptCopy. diff --git a/test/run/KeptCopy.status b/test/run/KeptCopy.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/KeptCopy.status @@ -0,0 +1 @@ +2 diff --git a/test/run/KeptCopy.stderr b/test/run/KeptCopy.stderr new file mode 100644 index 0000000..fd3fa2f --- /dev/null +++ b/test/run/KeptCopy.stderr @@ -0,0 +1,3 @@ +test/run/KeptCopy.raz:74:16: index 9 is out of range for a length of 8 + in KeptCopy.pick at test/run/KeptCopy.raz:74:16: k = 9, i = 7 + in KeptCopy.main at test/run/KeptCopy.raz:99:5: i = 7