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)
| @@ -23,7 +23,7 @@ | ||
| 23 | 23 | | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | 24 | | `src/Gen.raz` | 2,800 | 1,963 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 25 | 25 | | `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 | | |
| 27 | 27 | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | |
| 28 | 28 | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | |
| 29 | 29 | | `src/Raz.raz` | 418 | 333 | The command line | |
| @@ -335,8 +335,9 @@ | ||
| 335 | 335 | call or a copy needs: nothing is saved or moved, and `s[i]` is read |
| 336 | 336 | through `rdi` itself (`keepArrived`). Where Gen needs them after |
| 337 | 337 | 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 | |
| 340 | 341 | so: `Rt.fail` is given `rdi` and `rsi` as the failing code had |
| 341 | 342 | them, and the signal handler takes them from the context the |
| 342 | 343 | kernel saved. The search 9% faster, rat 1.5%, its text 0.7% |
| @@ -914,6 +915,29 @@ | ||
| 914 | 915 | the sum over a slice; ChaCha's `quarter(var x: [16]U32; ...)` 560 |
| 915 | 916 | ms to 283 (2.0 times), in 58% fewer instructions. |
| 916 | 917 | |
| 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 | + | |
| 917 | 941 | ## Diag.raz: messages |
| 918 | 942 | |
| 919 | 943 | Every message the compiler prints is made here. A position, `Pos`, is a |
| @@ -1917,7 +1941,9 @@ | ||
| 1917 | 1941 | `Item.address`, which puts the variable back in memory and |
| 1918 | 1942 | sets `Item.demoted`: `body` then throws the procedure's code away |
| 1919 | 1943 | (`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 | |
| 1921 | 1947 | the trap report are made after, by `varKind`, which marks a variable |
| 1922 | 1948 | in a register with the register's number. |
| 1923 | 1949 |
| @@ -3148,6 +3148,25 @@ | ||
| 3148 | 3148 | return m; |
| 3149 | 3149 | end; |
| 3150 | 3150 | |
| 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 | + | |
| 3151 | 3170 | (* Gives each local not in a register its slot in the frame, the slots |
| 3152 | 3171 | the prologue zeroes, and then tells Gen which registers the |
| 3153 | 3172 | variables are in. A variable in a register has no slot: it starts at |
| @@ -3221,12 +3240,12 @@ | ||
| 3221 | 3240 | Chunk.noteLine(Scan.curLine(), Scan.curCol()); |
| 3222 | 3241 | try expect(Scan.sEnd); |
| 3223 | 3242 | 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 *) | |
| 3226 | 3245 | if Gen.lost then |
| 3227 | 3246 | o := params; |
| 3228 | 3247 | 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; | |
| 3230 | 3249 | o := o^.next; |
| 3231 | 3250 | end; |
| 3232 | 3251 | end; |
| @@ -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 |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -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 |