From 787085c35d7458f4a0fef34d3d6906d61a68f22d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 19:52:56 +0000 Subject: [PATCH] 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. --- diff --git a/doc/compiler.md b/doc/compiler.md index 617e2a2..2c4840e 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of raz, about 10,700 lines of code (the +The compiler is eleven files of raz, about 10,800 lines of code (the budget is a soft 10,000, never past 11,000). It is its own compiler: the frozen Pascal compiler in `seed/` builds `src/` the first time, and after that a raz builds `src/` (`make fixpoint` checks that it makes @@ -15,12 +15,12 @@ |---|---|---|---| | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,062 | 886 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 684 | 555 | Types, objects, scopes, layout | +| `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout | | `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes | -| `src/X64.raz` | 1,493 | 1,237 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,478 | 1,780 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 1,763 | 1,458 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 3,846 | 3,355 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/X64.raz` | 1,493 | 1,238 | The instruction encoder, jumps within a procedure, the assembler | +| `src/Gen.raz` | 2,509 | 1,797 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 3,853 | 3,361 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 305 | 238 | The command line | @@ -741,6 +741,44 @@ would remove), the reading of every procedure twice, and a record parameter copied at each call that passes it on (`Mem`, in the encoder). + +## Cheap wins, measured + +Each change below was measured on the kernels of `test/bench`, by the +instructions each runs (valgrind's `cachegrind`, which counts the +same every time) and by the best of fifteen runs of the old program and +the new, taken in turn; a kernel whose code didn't change moves by up +to 2% in time, which is the noise. + +1. **A square root in place.** `Math.sqrt` is a code procedure of one + instruction, and NBody's `mag := Dt / (d2 * Math.sqrt(d2))` called + it, with all that a call costs a real: the reals on the stack + spilled, the variables' xmm registers loaded again from their slots + after it, as System V keeps none through one, and the procedure no + longer a leaf: + + ``` + movsd [rbp - 64], xmm8 ; d2, spilled under the argument + movsd xmm0, [rbp - 32] ; the argument + call Math.sqrt ; sqrtsd xmm0, xmm0; ret + movsd xmm12, [rbp - 40] ; mag, dx, dy, dz back from their slots + movsd xmm13, [rbp - 8] + ... + ``` + + Now a code procedure of one real whose body is `sqrtsd xmm0, xmm0` + and `ret` (or `sqrtss`), as `codeBody` finds from its bytes + (`Gen.oneRealOp`, kept in `inl`), is made in place: the argument + computed as for a call, then the instruction on its own register + (`realInPlace`, from `endCall`), and `chooseRegs` doesn't count it a + call: + + ``` + movsd xmm9, [rbp - 32] + sqrtsd xmm9, xmm9 + ``` + + NBody 8.6% fewer instructions, 2.8% faster; the rest unchanged. ## Diag.raz: messages diff --git a/src/Gen.raz b/src/Gen.raz index 3cd350b..09ff7d3 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -2464,6 +2464,37 @@ return nil; end; +(* Whether the code procedure in chunk c is a square root of xmm0 in + itself, then ret, as Math.sqrt is: + + sqrtsd xmm0, xmm0 F2 0F 51 C0 + ret C3 + + If so, its prefix and opcode, F251H (F351H for sqrtss), which a call of it does in place + on the value's own register (realInPlace); else 0. A call would spill + the stack's reals and reload the variables' xmm registers around it, + as System V keeps none through one, for an instruction of four bytes. *) +proc oneRealOp*(c: INT): INT; +var s: INT; +begin + s := Chunk.chunks[c].start; + if (Chunk.chunks[c].size # 5) or (Chunk.chunks[c].numFix # 0) then return 0; end; + if ((Chunk.text[s] # 0F2H) and (Chunk.text[s] # 0F3H)) or (Chunk.text[s + 1] # 0FH) + or (Chunk.text[s + 2] # 51H) or (Chunk.text[s + 3] # 0C0H) or (Chunk.text[s + 4] # 0C3H) then + return 0; + end; + return INT(Chunk.text[s]) * 256 + INT(Chunk.text[s + 2]); +end; + +(* The operation op, of oneRealOp, on the real on top, in its register: + Math.sqrt(d2) is sqrtsd xmm8, xmm8, with no call. *) +proc realInPlace*(op: INT); +var x: INT; +begin + x := top(); + X.sse(op & 0FFH, x, x, op shr 8 = 0F3H); +end; + var i: INT; begin for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; diff --git a/src/Item.raz b/src/Item.raz index 8a6f786..7e8e02f 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1378,7 +1378,11 @@ var res: ^Sym.Type; begin if c.param # nil then try wrongCount(c, Scan.tokPos(), "too few arguments"); end; - Gen.call(c); + if (c.obj # nil) and (c.obj^.inl # 0) then + Gen.realInPlace(c.obj^.inl); (* Math.sqrt: an instruction, no call *) + else + Gen.call(c); + end; res := c.sig^.base; x := {}; if res = nil then diff --git a/src/Parse.raz b/src/Parse.raz index a2f5c1e..cc90b5b 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -2448,7 +2448,7 @@ proc calls(o: PObj): BOOL; begin if o = nil then return false; end; - return (o^.cls = Sym.oProc) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) + return ((o^.cls = Sym.oProc) and (o^.inl = 0)) or ((o^.cls = Sym.oVar) and (o^.typ^.form = Sym.fProc)) or ((o^.cls = Sym.oBuiltin) and ((o^.val = Sym.bNew) or (o^.val = Sym.bSyscall) or (o^.val = Sym.bCopy))); end; @@ -2747,6 +2747,13 @@ end; end; try Gen.codeProc(p^.chunk, asmConst); + (* a square root of one real, of its own kind, is made in place *) + q := sig^.fields; + if (q # nil) and (q^.next = nil) and not q^.isVar and (q^.typ^.form = Sym.fReal) + and (sig^.base # nil) and Sym.same(sig^.base, q^.typ) then + p^.inl := Gen.oneRealOp(p^.chunk); + if (p^.inl shr 8 = 0F3H) # (q^.typ^.size = 4) then p^.inl := 0; end; + end; return expect(Scan.sEnd); end; diff --git a/src/Sym.raz b/src/Sym.raz index b57cd96..de9db87 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -64,6 +64,9 @@ read*: BOOL; (* oVar: named other than as what := assigns *) fwd*: BOOL; (* oProc: declared forward, no body yet *) isCode*: BOOL; (* oProc: a code procedure *) + inl*: INT; (* oProc: a code procedure that is a square root of + xmm0, made in place of a call (Gen.oneRealOp); + 0 if not *) openEnd*: BOOL; (* oProc: has results, and may reach its end *) reg*: INT; (* oVar: the register it lives in, or 0 if in memory; for a slice, the one its pointer is in *) diff --git a/src/X64.raz b/src/X64.raz index 560cc2b..2e0bd85 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -751,6 +751,7 @@ if op = 59H then return Diag.say("mul{}", [Fmt.str(realEnd(single))]); end; if op = 5CH then return Diag.say("sub{}", [Fmt.str(realEnd(single))]); end; if op = 5EH then return Diag.say("div{}", [Fmt.str(realEnd(single))]); end; + if op = 51H then return Diag.say("sqrt{}", [Fmt.str(realEnd(single))]); end; return Diag.say("cvt{}2{}", [Fmt.str(realEnd(single)), Fmt.str(realEnd(not single))]); end;