raz / commit 787085c3

commit
787085c35d7458f4a0fef34d3d6906d61a68f22d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
fde43f53
A square root in place of a call

A code procedure of one real whose body is sqrtsd xmm0, xmm0 and ret
(or sqrtss), as Math.sqrt is, is found from its bytes (Gen.oneRealOp,
Sym.Obj's inl) and made in place: the argument as for a call, then the
instruction on its own register, with no spills, no reloads of the
variables' xmm registers, and the procedure still a leaf.

NBody 8.6% fewer instructions, 2.8% faster; doc/compiler.md says how
it was measured.

patch · log

6 files changed, +92 -8

doc/compiler.md (history) modified +44 -6

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of raz, about 10,700 lines of code (the
3+The compiler is eleven files of raz, about 10,800 lines of code (the
44 budget is a soft 10,000, never past 11,000). It is its own compiler: the
55 frozen Pascal compiler in `seed/` builds `src/` the first time, and
66 after that a raz builds `src/` (`make fixpoint` checks that it makes
@@ -15,12 +15,12 @@
1515 |---|---|---|---|
1616 | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts |
1717 | `src/Scan.raz` | 1,062 | 886 | Symbols, literals, comments, positions |
18-| `src/Sym.raz` | 684 | 555 | Types, objects, scopes, layout |
18+| `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout |
1919 | `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes |
20-| `src/X64.raz` | 1,493 | 1,237 | The instruction encoder, jumps within a procedure, the assembler |
21-| `src/Gen.raz` | 2,478 | 1,780 | The stack machine, for x86-64: values, operations, checks, calls, frames |
22-| `src/Item.raz` | 1,763 | 1,458 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 3,846 | 3,355 | Declarations, statements, expressions, built-ins, recovery, warnings |
20+| `src/X64.raz` | 1,493 | 1,238 | The instruction encoder, jumps within a procedure, the assembler |
21+| `src/Gen.raz` | 2,509 | 1,797 | The stack machine, for x86-64: values, operations, checks, calls, frames |
22+| `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls |
23+| `src/Parse.raz` | 3,853 | 3,361 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -741,6 +741,44 @@
741741 would remove), the reading of every procedure twice, and
742742 a record parameter copied at each call that passes it on (`Mem`, in the
743743 encoder).
744+
745+## Cheap wins, measured
746+
747+Each change below was measured on the kernels of `test/bench`, by the
748+instructions each runs (valgrind's `cachegrind`, which counts the
749+same every time) and by the best of fifteen runs of the old program and
750+the new, taken in turn; a kernel whose code didn't change moves by up
751+to 2% in time, which is the noise.
752+
753+1. **A square root in place.** `Math.sqrt` is a code procedure of one
754+ instruction, and NBody's `mag := Dt / (d2 * Math.sqrt(d2))` called
755+ it, with all that a call costs a real: the reals on the stack
756+ spilled, the variables' xmm registers loaded again from their slots
757+ after it, as System V keeps none through one, and the procedure no
758+ longer a leaf:
759+
760+ ```
761+ movsd [rbp - 64], xmm8 ; d2, spilled under the argument
762+ movsd xmm0, [rbp - 32] ; the argument
763+ call Math.sqrt ; sqrtsd xmm0, xmm0; ret
764+ movsd xmm12, [rbp - 40] ; mag, dx, dy, dz back from their slots
765+ movsd xmm13, [rbp - 8]
766+ ...
767+ ```
768+
769+ Now a code procedure of one real whose body is `sqrtsd xmm0, xmm0`
770+ and `ret` (or `sqrtss`), as `codeBody` finds from its bytes
771+ (`Gen.oneRealOp`, kept in `inl`), is made in place: the argument
772+ computed as for a call, then the instruction on its own register
773+ (`realInPlace`, from `endCall`), and `chooseRegs` doesn't count it a
774+ call:
775+
776+ ```
777+ movsd xmm9, [rbp - 32]
778+ sqrtsd xmm9, xmm9
779+ ```
780+
781+ NBody 8.6% fewer instructions, 2.8% faster; the rest unchanged.
744782
745783 ## Diag.raz: messages
746784

src/Gen.raz modified +31 -0

@@ -2464,6 +2464,37 @@
24642464 return nil;
24652465 end;
24662466
2467+(* Whether the code procedure in chunk c is a square root of xmm0 in
2468+ itself, then ret, as Math.sqrt is:
2469+
2470+ sqrtsd xmm0, xmm0 F2 0F 51 C0
2471+ ret C3
2472+
2473+ If so, its prefix and opcode, F251H (F351H for sqrtss), which a call of it does in place
2474+ on the value's own register (realInPlace); else 0. A call would spill
2475+ the stack's reals and reload the variables' xmm registers around it,
2476+ as System V keeps none through one, for an instruction of four bytes. *)
2477+proc oneRealOp*(c: INT): INT;
2478+var s: INT;
2479+begin
2480+ s := Chunk.chunks[c].start;
2481+ if (Chunk.chunks[c].size # 5) or (Chunk.chunks[c].numFix # 0) then return 0; end;
2482+ if ((Chunk.text[s] # 0F2H) and (Chunk.text[s] # 0F3H)) or (Chunk.text[s + 1] # 0FH)
2483+ or (Chunk.text[s + 2] # 51H) or (Chunk.text[s + 3] # 0C0H) or (Chunk.text[s + 4] # 0C3H) then
2484+ return 0;
2485+ end;
2486+ return INT(Chunk.text[s]) * 256 + INT(Chunk.text[s + 2]);
2487+end;
2488+
2489+(* The operation op, of oneRealOp, on the real on top, in its register:
2490+ Math.sqrt(d2) is sqrtsd xmm8, xmm8, with no call. *)
2491+proc realInPlace*(op: INT);
2492+var x: INT;
2493+begin
2494+ x := top();
2495+ X.sse(op & 0FFH, x, x, op shr 8 = 0F3H);
2496+end;
2497+
24672498 var i: INT;
24682499 begin
24692500 for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end;

src/Item.raz (history) modified +5 -1

@@ -1378,7 +1378,11 @@
13781378 var res: ^Sym.Type;
13791379 begin
13801380 if c.param # nil then try wrongCount(c, Scan.tokPos(), "too few arguments"); end;
1381- Gen.call(c);
1381+ if (c.obj # nil) and (c.obj^.inl # 0) then
1382+ Gen.realInPlace(c.obj^.inl); (* Math.sqrt: an instruction, no call *)
1383+ else
1384+ Gen.call(c);
1385+ end;
13821386 res := c.sig^.base;
13831387 x := {};
13841388 if res = nil then

src/Parse.raz (history) modified +8 -1

@@ -2448,7 +2448,7 @@
24482448 proc calls(o: PObj): BOOL;
24492449 begin
24502450 if o = nil then return false; end;
2451- return (o^.cls = Sym.oProc) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc))
2451+ return ((o^.cls = Sym.oProc) and (o^.inl = 0)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc))
24522452 or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy)));
24532453 end;
24542454
@@ -2747,6 +2747,13 @@
27472747 end;
27482748 end;
27492749 try Gen.codeProc(p^.chunk, asmConst);
2750+ (* a square root of one real, of its own kind, is made in place *)
2751+ q := sig^.fields;
2752+ if (q # nil) and (q^.next = nil) and not q^.isVar and (q^.typ^.form = Sym.fReal)
2753+ and (sig^.base # nil) and Sym.same(sig^.base, q^.typ) then
2754+ p^.inl := Gen.oneRealOp(p^.chunk);
2755+ if (p^.inl shr 8 = 0F3H) # (q^.typ^.size = 4) then p^.inl := 0; end;
2756+ end;
27502757 return expect(Scan.sEnd);
27512758 end;
27522759

src/Sym.raz (history) modified +3 -0

@@ -64,6 +64,9 @@
6464 read*: BOOL; (* oVar: named other than as what := assigns *)
6565 fwd*: BOOL; (* oProc: declared forward, no body yet *)
6666 isCode*: BOOL; (* oProc: a code procedure *)
67+ inl*: INT; (* oProc: a code procedure that is a square root of
68+ xmm0, made in place of a call (Gen.oneRealOp);
69+ 0 if not *)
6770 openEnd*: BOOL; (* oProc: has results, and may reach its end *)
6871 reg*: INT; (* oVar: the register it lives in, or 0 if in memory;
6972 for a slice, the one its pointer is in *)

src/X64.raz (history) modified +1 -0

@@ -751,6 +751,7 @@
751751 if op = 59H then return Diag.say("mul{}", [Fmt.str(realEnd(single))]); end;
752752 if op = 5CH then return Diag.say("sub{}", [Fmt.str(realEnd(single))]); end;
753753 if op = 5EH then return Diag.say("div{}", [Fmt.str(realEnd(single))]); end;
754+ if op = 51H then return Diag.say("sqrt{}", [Fmt.str(realEnd(single))]); end;
754755 return Diag.say("cvt{}2{}", [Fmt.str(realEnd(single)), Fmt.str(realEnd(not single))]);
755756 end;
756757