raz / commit 16eaf7cb

commit
16eaf7cba88dfae9032480f989bc829494c11013
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
87e3c487
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)

patch · log

6 files changed, +166 -7

doc/compiler.md (history) modified +30 -4

@@ -23,7 +23,7 @@
2323 | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler |
2424 | `src/Gen.raz` | 2,800 | 1,963 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,592 | 3,942 | Declarations, statements, expressions, built-ins, recovery, warnings |
26+| `src/Parse.raz` | 4,611 | 3,956 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -335,8 +335,9 @@
335335 call or a copy needs: nothing is saved or moved, and `s[i]` is read
336336 through `rdi` itself (`keepArrived`). Where Gen needs them after
337337 all, to copy a record or compare two slices, it sets `lost`, and
338- `body` compiles the procedure again with the parameters in memory,
339- as for a variable demoted. The trap report shows a parameter kept
338+ `body` compiles the procedure again with the parameters in
339+ callee-saved registers no variable has, or in memory (`unkeep`, in
340+ "Array parameters, measured"), as for a variable demoted. The trap report shows a parameter kept
340341 so: `Rt.fail` is given `rdi` and `rsi` as the failing code had
341342 them, and the signal handler takes them from the context the
342343 kernel saved. The search 9% faster, rat 1.5%, its text 0.7%
@@ -914,6 +915,29 @@
914915 the sum over a slice; ChaCha's `quarter(var x: [16]U32; ...)` 560
915916 ms to 283 (2.0 times), in 58% fewer instructions.
916917
918+2. **A parameter that loses `rdi` or `rsi` takes a free register.**
919+ A procedure that calls nothing keeps its parameters where they
920+ arrive (`keepArrived`); when it copies an array after all, `lost`
921+ sends them to memory for the second try at the body, though `rbx`
922+ and `r12` to `r15` may be free. `block(const st: [16]U32; var out:
923+ [16]U32)`, which starts `out := st`, loaded `out`'s pointer at
924+ each use:
925+
926+ ```
927+ mov r8, [rbp - 16]
928+ mov r8d, [r8] now: mov r9d, [r13 + 16]
929+ mov r9, [rbp - 16] mov r8d, [r13]
930+ mov r9d, [r9 + 16] add r8, r9
931+ add r8, r9
932+ ```
933+
934+ `unkeep` gives such a parameter the registers of `VarRegs` that
935+ `regsOf` finds free, one, or two for a slice, if it is used enough
936+ for a register by `chooseRegs`'s count; else it goes to memory as
937+ before. The prologue saves those registers and moves the parameter
938+ there, as it does for a procedure that calls. ChaCha's block with
939+ the copy 1,132 ms to 684, 35% fewer instructions.
940+
917941 ## Diag.raz: messages
918942
919943 Every message the compiler prints is made here. A position, `Pos`, is a
@@ -1917,7 +1941,9 @@
19171941 `Item.address`, which puts the variable back in memory and
19181942 sets `Item.demoted`: `body` then throws the procedure's code away
19191943 (`Gen.markCode`, `Gen.redo`), places the locals again, and compiles its
1920-statements again. Its notes for
1944+statements again. So it does when Gen wanted a parameter's `rdi` or
1945+`rsi` (`Gen.lost`), the parameter moved to a free register or memory
1946+(`unkeep`). Its notes for
19211947 the trap report are made after, by `varKind`, which marks a variable
19221948 in a register with the register's number.
19231949

src/Parse.raz (history) modified +22 -3

@@ -3148,6 +3148,25 @@
31483148 return m;
31493149 end;
31503150
3151+(* Parameter o, kept where it arrived until a call or a copy wanted rdi
3152+ or rsi, takes the registers of VarRegs that m, the taken ones, leaves,
3153+ if it is used enough for them (chooseRegs's weight of 3) and there
3154+ are enough; else it goes to memory. *)
3155+proc unkeep(o: PObj; m: INT);
3156+var k: INT; two: BOOL;
3157+begin
3158+ two := o^.reg2 # 0;
3159+ o^.reg := 0; o^.reg2 := 0;
3160+ if weight[candidate(o^.name)] < 3 then return; end;
3161+ for k := 0 to 4 do
3162+ if (m & (1 shl k)) = 0 then
3163+ if o^.reg = 0 then o^.reg := Gen.VarRegs[k];
3164+ elsif two and (o^.reg2 = 0) then o^.reg2 := Gen.VarRegs[k]; end;
3165+ end;
3166+ end;
3167+ if two and (o^.reg2 = 0) then o^.reg := 0; end;
3168+end;
3169+
31513170 (* Gives each local not in a register its slot in the frame, the slots
31523171 the prologue zeroes, and then tells Gen which registers the
31533172 variables are in. A variable in a register has no slot: it starts at
@@ -3221,12 +3240,12 @@
32213240 Chunk.noteLine(Scan.curLine(), Scan.curCol());
32223241 try expect(Scan.sEnd);
32233242 if not (Item.demoted or Gen.lost) or (Diag.errors # errs) then break; end;
3224- (* a parameter kept where it arrived goes to memory: a call or a copy
3225- wanted its register *)
3243+ (* a parameter kept where it arrived goes to a free register of
3244+ VarRegs, or to memory: a call or a copy wanted its register *)
32263245 if Gen.lost then
32273246 o := params;
32283247 while o # nil do
3229- if Gen.keptIn(o) then o^.reg := 0; o^.reg2 := 0; end;
3248+ if Gen.keptIn(o) then unkeep(o, regsOf(params, before)); end;
32303249 o := o^.next;
32313250 end;
32323251 end;

test/run/KeptCopy.expect (history) added +8 -0

@@ -0,0 +1,8 @@
1+16 22 26 36 60 106 182 296
2+1638
3+-10 -9 -6 -1 6 15 26 39
4+-350
5+7396
6+12 51 138 326 729 1591 3436 7396
7+99 -9 -6 -1 6 15 26 39
8+-12

test/run/KeptCopy.raz (history) added +102 -0

@@ -0,0 +1,102 @@
1+(* A procedure that calls nothing keeps its first parameters in rdi and
2+ rsi; when it copies an array, which needs them, it is compiled again
3+ with those parameters in callee-saved registers where some are free
4+ and they are used enough, or in memory. The copy and the uses after
5+ it must agree either way, and a failed check still says their
6+ values. *)
7+module KeptCopy;
8+import Out;
9+
10+const N = 8;
11+
12+type Row = [N]I64;
13+
14+var r1, r2: Row; total: I64;
15+
16+(* out a copy of st, then each element worked on through both pointers *)
17+proc mix(const st: Row; var out: Row; k: INT);
18+var i: INT;
19+begin
20+ out := st;
21+ for i := 0 to N - 1 do out[i] := out[i] * 3 + st[N - 1 - i] + k; end;
22+ for i := 1 to N - 1 do out[i] := out[i] + out[i - 1]; end;
23+end;
24+
25+(* a slice, kept in both registers, then a copy, then the slice read:
26+ one callee-saved register is left, too few for it, so it goes to
27+ memory *)
28+proc sumAfter(s: []I64; var a: Row; const b: Row): I64;
29+var i: INT; t: I64;
30+begin
31+ a := b;
32+ t := 0;
33+ for i := 0 to LEN(s) - 1 do t := t + s[i] * a[i mod N]; end;
34+ return t;
35+end;
36+
37+(* the same, with registers enough left for the slice's two *)
38+proc sumThen(s: []I64; var a: Row): I64;
39+var i: INT; t: I64;
40+begin
41+ a := r1;
42+ t := 0;
43+ for i := 0 to LEN(s) - 1 do t := t + s[i] * s[LEN(s) - 1 - i]; end;
44+ return t + a[0];
45+end;
46+
47+(* five busy locals take every callee-saved register: the pointers go
48+ to memory *)
49+proc busy(var out: Row; const st: Row): I64;
50+var a, b, c, d, e, i: INT;
51+begin
52+ out := st;
53+ a := 1; b := 2; c := 3; d := 4; e := 5;
54+ for i := 0 to N - 1 do
55+ a := a + b; b := b + c; c := c + d; d := d + e; e := e + a;
56+ out[i] := out[i] + a + b + c + d + e + st[i];
57+ end;
58+ return out[N - 1];
59+end;
60+
61+(* a pointer used once only goes to memory *)
62+proc once(var out: Row; const st: Row);
63+begin
64+ out := st;
65+ out[0] := 99;
66+end;
67+
68+(* the element at k of a copy, which may be out of range *)
69+proc pick(const st: Row; var out: Row; k: INT): I64;
70+var i: INT;
71+begin
72+ out := st;
73+ for i := 0 to N - 1 do out[i] := out[i] + st[i]; end;
74+ return out[k] + st[0];
75+end;
76+
77+proc say(const r: Row);
78+var i: INT;
79+begin
80+ for i := 0 to N - 1 do Out.int(r[i]); Out.char(' '); end;
81+ Out.ln;
82+end;
83+
84+proc main*;
85+var i: INT;
86+begin
87+ for i := 0 to N - 1 do r1[i] := I64(i * i - 10); end;
88+ mix(r1, r2, 7);
89+ say(r2);
90+ total := sumAfter(r1[1..N], r2, r1);
91+ Out.int(total); Out.ln;
92+ say(r2);
93+ Out.int(sumThen(r1[2..N], r2)); Out.ln;
94+ Out.int(busy(r2, r1)); Out.ln;
95+ say(r2);
96+ once(r2, r1);
97+ say(r2);
98+ Out.int(pick(r1, r2, 3)); Out.ln;
99+ Out.int(pick(r1, r2, N + 1)); Out.ln;
100+end;
101+
102+end KeptCopy.

test/run/KeptCopy.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/KeptCopy.stderr (history) added +3 -0

@@ -0,0 +1,3 @@
1+test/run/KeptCopy.raz:74:16: index 9 is out of range for a length of 8
2+ in KeptCopy.pick at test/run/KeptCopy.raz:74:16: k = 9, i = 7
3+ in KeptCopy.main at test/run/KeptCopy.raz:99:5: i = 7