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.
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 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 | |
| 4 | 4 | budget is a soft 10,000, never past 11,000). It is its own compiler: the |
| 5 | 5 | frozen Pascal compiler in `seed/` builds `src/` the first time, and |
| 6 | 6 | after that a raz builds `src/` (`make fixpoint` checks that it makes |
| @@ -15,12 +15,12 @@ | ||
| 15 | 15 | |---|---|---|---| |
| 16 | 16 | | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | |
| 17 | 17 | | `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 | | |
| 19 | 19 | | `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 | | |
| 24 | 24 | | `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| @@ -741,6 +741,44 @@ | ||
| 741 | 741 | would remove), the reading of every procedure twice, and |
| 742 | 742 | a record parameter copied at each call that passes it on (`Mem`, in the |
| 743 | 743 | 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. | |
| 744 | 782 | |
| 745 | 783 | ## Diag.raz: messages |
| 746 | 784 |
| @@ -2464,6 +2464,37 @@ | ||
| 2464 | 2464 | return nil; |
| 2465 | 2465 | end; |
| 2466 | 2466 | |
| 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 | + | |
| 2467 | 2498 | var i: INT; |
| 2468 | 2499 | begin |
| 2469 | 2500 | for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; |
| @@ -1378,7 +1378,11 @@ | ||
| 1378 | 1378 | var res: ^Sym.Type; |
| 1379 | 1379 | begin |
| 1380 | 1380 | 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; | |
| 1382 | 1386 | res := c.sig^.base; |
| 1383 | 1387 | x := {}; |
| 1384 | 1388 | if res = nil then |
| @@ -2448,7 +2448,7 @@ | ||
| 2448 | 2448 | proc calls(o: PObj): BOOL; |
| 2449 | 2449 | begin |
| 2450 | 2450 | 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)) | |
| 2452 | 2452 | or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy))); |
| 2453 | 2453 | end; |
| 2454 | 2454 | |
| @@ -2747,6 +2747,13 @@ | ||
| 2747 | 2747 | end; |
| 2748 | 2748 | end; |
| 2749 | 2749 | 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; | |
| 2750 | 2757 | return expect(Scan.sEnd); |
| 2751 | 2758 | end; |
| 2752 | 2759 |
| @@ -64,6 +64,9 @@ | ||
| 64 | 64 | read*: BOOL; (* oVar: named other than as what := assigns *) |
| 65 | 65 | fwd*: BOOL; (* oProc: declared forward, no body yet *) |
| 66 | 66 | 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 *) | |
| 67 | 70 | openEnd*: BOOL; (* oProc: has results, and may reach its end *) |
| 68 | 71 | reg*: INT; (* oVar: the register it lives in, or 0 if in memory; |
| 69 | 72 | for a slice, the one its pointer is in *) |
| @@ -751,6 +751,7 @@ | ||
| 751 | 751 | if op = 59H then return Diag.say("mul{}", [Fmt.str(realEnd(single))]); end; |
| 752 | 752 | if op = 5CH then return Diag.say("sub{}", [Fmt.str(realEnd(single))]); end; |
| 753 | 753 | 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; | |
| 754 | 755 | return Diag.say("cvt{}2{}", [Fmt.str(realEnd(single)), Fmt.str(realEnd(not single))]); |
| 755 | 756 | end; |
| 756 | 757 |