raz / commit 84cb543c

commit
84cb543cbd4a6281a6a37dd0f77a88ab678cd175
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
99e7ccbf
An offset as the machine has it

s[i * N + j - 1] worked out its index in full, the constant added
too, and (i - 1) * N subtracted before it shifted. Now a word in a
register may carry an offset not yet added (off, in TEntry): Op notes
x + c and x - c there, carries it through + and -, and through * by a
constant, as (x + a) * k is x * k + a * k; an element's operand takes
it as its displacement (IndexOff, for IndexSlice and IndexArray), as
does a field's through a pointer (Place); anything else adds it with
a lea where the value is read (Loaded, Peek, ValueTo). A checked
index has it added first.

Jacobi 129 -> 106 ms (FPC -O2 81); rat 493 -> 482 ms, within noise;
the integer kernels unchanged. test/enctest takes the encoder's new
real forms (and the renamed movsd). 9,082 lines of code.

patch · log

4 files changed, +140 -24

PLAN.md (history) modified +7 -1

@@ -35,7 +35,13 @@
3535 its load. Now an element of an array in the frame or a chunk is the
3636 operand `[base + i * 8]`, as one of a slice in registers is. NBody
3737 225 ms to 147; rat within noise.
38-4. **Real variables in xmm registers**, where that pays: measured, and
38+4. Done: **An offset as the machine has it.** Jacobi's index
39+ arithmetic, `s[i * N + j - 1]`, added its constant before the load;
40+ now a word's constant offset waits on the stack, carried through
41+ `+`, `-` and `*` by a constant, and an element's operand takes it as
42+ its displacement: `[r13 + r9 * 8 - 8]`. Jacobi 129 ms to 106; rat
43+ within noise.
44+5. **Real variables in xmm registers**, where that pays: measured, and
3945 kept only if it does.
4046
4147 ## Next, when the owner says

doc/compiler.md (history) modified +43 -2

@@ -15,7 +15,7 @@
1515 | `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout |
1616 | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
1717 | `src/x64.pas` | 1,201 | 992 | The instruction encoder, jumps within a procedure, the assembler |
18-| `src/gen.pas` | 2,121 | 1,542 | The stack machine, for x86-64: values, operations, checks, calls, frames |
18+| `src/gen.pas` | 2,188 | 1,588 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1919 | `src/item.pas` | 1,578 | 1,301 | Items: types, constants, variables, operators, conversions, calls |
2020 | `src/parse.pas` | 3,103 | 2,707 | Declarations, statements, expressions, built-ins, recovery, warnings |
2121 | `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places |
@@ -518,7 +518,47 @@
518518 would need a third register. `CheckIndex` compares a variable's
519519 register as it is (`Peek`), not a copy. NBody 225 ms to 147; rat
520520 within noise.
521+
522+3. **An offset as the machine has it.** The sweep's four neighbours,
523+ `s[i * N + j - 1]` and the others, each worked out its index in
524+ full, the constant added too:
521525
526+ ```
527+ mov r9, r12 ; i * N + j - 1
528+ shl r9, 9
529+ add r9, rbx
530+ sub r9, 1
531+ ...
532+ mov r10, r12 ; (i - 1) * N + j
533+ sub r10, 1
534+ shl r10, 9
535+ add r10, rbx
536+ addsd xmm9, [r13 + r10 * 8]
537+ ```
538+
539+ Now a word in a register may carry an offset not yet added (`off`,
540+ in `TEntry`): `Op` notes `x + c` and `x - c` there, carries it
541+ through `+` and `-`, and through `*` by a constant, as `(x + a) * k`
542+ is `x * k + a * k`. An element's operand takes it as its
543+ displacement (`IndexOff`, for `IndexSlice` and `IndexArray`), and so
544+ does a field's through a pointer (`Place`); anything else adds it
545+ where the value is read (`Loaded`, `Peek`, `ValueTo`, with a `lea`,
546+ which leaves the flags alone). A value changed in place, `lInto`,
547+ carries none, and an index that is checked has it added first:
548+
549+ ```
550+ mov r9, r12
551+ shl r9, 9
552+ add r9, rbx
553+ ...
554+ movsd xmm9, [r13 + r9 * 8 - 8]
555+ ...
556+ addsd xmm9, [r13 + r10 * 8 - 4096]
557+ ```
558+
559+ Jacobi 129 ms to 106; rat 2% faster, within noise; the integer
560+ kernels unchanged.
561+
522562 ## diag.pas: messages
523563
524564 Every message the compiler prints is made here. A position, `TPos`, is
@@ -709,7 +749,8 @@
709749 variable's own register, read in place (`lHeld`), the value's own
710750 register (`lReg`) or a frame slot (`lFrame`); `owner` says which
711751 value each register holds, `lIdx` and `lMem` holding theirs when it
712-is their index. The value at depth `d` belongs in
752+is their index. A word in a register may have an offset not yet
753+added (`off`), which an operand takes as its displacement. The value at depth `d` belongs in
713754 `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division
714755 and shifts need, and `rsi` and `rdi`, which copying needs, aren't among
715756 them. A real belongs in `XRegs[d mod 4]`, `xmm8` to `xmm11`, which

src/gen.pas modified +87 -20

@@ -213,10 +213,11 @@
213213 nothing in an expression can change }
214214 lAdr, { not loaded: the address of chunk, or of the frame
215215 if chunk is -1, plus val }
216- lIdx, { not loaded: the address m, an element of a slice
217- in registers: its pointer's register as the base,
218- and the index in a register variable's or the
219- value's own register (IndexSlice) }
216+ lIdx, { not loaded: the address m, an element of an array
217+ or of a slice in registers: the array's or the
218+ pointer's register, or rbp, as the base, and the
219+ index in a register variable's or the value's
220+ own register (IndexSlice, IndexArray) }
220221 lMem, { not loaded: the value of size bytes at m, in the
221222 frame, a chunk, or at an address lAdr or lIdx
222223 noted, read where it is used (PushVar) }
@@ -235,6 +236,10 @@
235236 size: longint; { lMem }
236237 signed: boolean; { lMem }
237238 k: TKind;
239+ off: int64; { lHeld, lReg: a word, its register plus off, the
240+ constant of x + 1 not yet added (Op), so that an
241+ operand that uses it can take it as its
242+ displacement: [r13 + r9 * 8 - 8] }
238243 end;
239244
240245 const
@@ -306,6 +311,11 @@
306311 from xmm8 up carry no arguments, so that Call can move a real into
307312 xmm0 to xmm7 without overwriting another still to go. }
308313
314+function Fits32(v: int64): boolean;
315+begin
316+ Fits32 := (v >= low(longint)) and (v <= high(longint));
317+end;
318+
309319 { The register of kind k for the value at depth i }
310320 function RegIn(k: TKind; i: longint): longint;
311321 begin
@@ -353,7 +363,7 @@
353363 procedure Spill(i: longint);
354364 var s: int64;
355365 begin
356- if stack[i].loc <> lReg then Loaded(i);
366+ if (stack[i].loc <> lReg) or (stack[i].off <> 0) then Loaded(i);
357367 s := AllocTemp(8, 8);
358368 if IsReal(i) then MovsMX(Mem(RBP, s), RegOf(i), false)
359369 else Store(Mem(RBP, s), RegOf(i), 8);
@@ -419,6 +429,10 @@
419429 end;
420430 stack[i].loc := lReg;
421431 end;
432+ if stack[i].off <> 0 then begin
433+ Lea(RegOf(i), Mem(RegOf(i), stack[i].off));
434+ stack[i].off := 0;
435+ end;
422436 Loaded := RegOf(i);
423437 end;
424438
@@ -426,9 +440,28 @@
426440 its register }
427441 function Peek(i: longint): longint;
428442 begin
429- if stack[i].loc = lHeld then Peek := stack[i].val else Peek := Loaded(i);
443+ if (stack[i].loc = lHeld) and (stack[i].off = 0) then Peek := stack[i].val else Peek := Loaded(i);
430444 end;
431445
446+{ The offset of value i, taken from it, for the caller to add or to
447+ make a displacement of }
448+function TakeOff(i: longint): int64;
449+begin
450+ TakeOff := stack[i].off;
451+ stack[i].off := 0;
452+end;
453+
454+{ The word on top, in a register, has o added: noted, if o can be a
455+ displacement, else added now }
456+procedure PutOff(o: int64);
457+begin
458+ if Fits32(stack[depth - 1].off + o) then stack[depth - 1].off := stack[depth - 1].off + o
459+ else begin
460+ MovRI(RCX, o);
461+ Alu(aluAdd, Loaded(depth - 1), RCX);
462+ end;
463+end;
464+
432465 function Top: longint;
433466 begin
434467 if depth = 0 then Fatal('internal error: the stack is empty');
@@ -442,6 +475,7 @@
442475 stack[depth].loc := loc;
443476 stack[depth].val := v;
444477 stack[depth].chunk := -1;
478+ stack[depth].off := 0;
445479 inc(depth);
446480 end;
447481
@@ -505,6 +539,7 @@
505539 else if RegOf(i) <> r then MovRR(r, RegOf(i));
506540 else Load(r, Mem(RBP, stack[i].val), 8, false);
507541 end;
542+ if stack[i].off <> 0 then Lea(r, Mem(r, stack[i].off));
508543 end;
509544
510545 { The value on top, popped into register r }
@@ -534,11 +569,6 @@
534569 NewEntry(k, lConst, v);
535570 end;
536571
537-function Fits32(v: int64): boolean;
538-begin
539- Fits32 := (v >= low(longint)) and (v <= high(longint));
540-end;
541-
542572 { The register of the word at a, a variable in registers: the variable's
543573 own, or for a slice its pointer's (off 0) or its length's (off 8) }
544574 function RegAt(const a: TAdr): longint;
@@ -549,7 +579,7 @@
549579 { The memory a names. An address on the stack is popped, and is in its
550580 register still; one in a slot is loaded into r. }
551581 function Place(const a: TAdr; r: longint): TMem;
552-var ra: longint;
582+var ra: longint; o: int64;
553583 begin
554584 if a.base = bReg then Fatal('internal error: a register has no address');
555585 case a.base of
@@ -564,9 +594,12 @@
564594 Place.disp := Place.disp + a.off;
565595 Drop;
566596 end else begin
597+ { an offset noted on the address is the operand's, as
598+ p^.f + 8: [r8 + 8] }
599+ if Fits32(a.off + stack[depth - 1].off) then o := TakeOff(depth - 1) else o := 0;
567600 ra := Peek(depth - 1);
568601 Drop;
569- Place := Mem(ra, a.off)
602+ Place := Mem(ra, a.off + o)
570603 end;
571604 else { bSlot: the address is in the frame slot a.chunk }
572605 Load(r, Mem(RBP, a.chunk), 8, false);
@@ -876,7 +909,7 @@
876909 { The two values on top, the left op the right, an integer of size
877910 bytes, signed or not, brought back to its width. }
878911 procedure Op(op: TSym; k: TKind; size: longint; signed: boolean);
879-var v: int64; r, y, sh, bits: longint; c: boolean; m: TMem;
912+var v, o: int64; r, y, sh, bits: longint; c: boolean; m: TMem;
880913 begin
881914 if k in [kF32, kF64] then begin RealOp(op, k); exit end;
882915 bits := size * 8;
@@ -885,6 +918,26 @@
885918 if (op in [sPlus, sTimes, sAmp, sBar, sXor]) and (stack[depth - 2].loc = lConst)
886919 and (stack[depth - 1].loc <> lConst) then Swap;
887920 c := TopConst(v);
921+ { A word's offset (see TEntry) is carried through + and -, and
922+ through * by a constant, as (x + a) * k is x * k + a * k: the
923+ operation is done on the registers, the offsets worked out here,
924+ and put on the result. x + 1 is only noted. A value changed in
925+ place, lInto, has no offset. }
926+ o := 0;
927+ if (bits = 64) and (stack[depth - 2].loc <> lInto)
928+ and ((op in [sPlus, sMinus]) or (op = sTimes) and c and Fits32(v)) then begin
929+ o := TakeOff(depth - 2);
930+ if op = sTimes then o := o * v
931+ else if c and Fits32(v) then begin
932+ if op = sPlus then o := o + v else o := o - v;
933+ Drop;
934+ if not (stack[depth - 1].loc in [lReg, lHeld]) then Top;
935+ PutOff(o);
936+ exit;
937+ end
938+ else if op = sPlus then o := o + TakeOff(depth - 1)
939+ else o := o - TakeOff(depth - 1);
940+ end;
888941 sh := -1;
889942 if c and (op in [sTimes, sDiv, sMod]) then sh := ExactLog2(v);
890943 if c and (sh >= 1) then begin
@@ -932,6 +985,7 @@
932985 end;
933986 if bits < 64 then Extend(r, size, signed);
934987 stack[depth - 1].k := k;
988+ if o <> 0 then PutOff(o);
935989 end;
936990
937991 { The left op the right of the two values on top, as a BOOL }
@@ -1379,6 +1433,15 @@
13791433 Check(ccNE, 4, p, r, NoMem, 0, -1);
13801434 end;
13811435
1436+{ The displacement disp of an element of size bytes, with the offset of
1437+ the index on top taken into it if it fits: s[k - 1] is [r13 + r9 * 8
1438+ - 8] }
1439+function IndexOff(size, disp: int64): int64;
1440+begin
1441+ IndexOff := disp;
1442+ if Fits32(disp + stack[depth - 1].off * size) then IndexOff := disp + TakeOff(depth - 1) * size;
1443+end;
1444+
13821445 { Pops an index, and the address of the slice at a if it is on the
13831446 stack, and pushes the address of its element of size bytes: the
13841447 index, checked against the length, times size, plus the pointer.
@@ -1390,16 +1453,18 @@
13901453 bytes, its address is only noted, as the operand [pointer + index *
13911454 size], to be read or written where it is used (lIdx). }
13921455 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
1393-var m, ml: TMem; r: longint; inRax: boolean;
1456+var m, ml: TMem; r: longint; inRax: boolean; o: int64;
13941457 begin
13951458 if (a.base = bReg) and (size in [1, 2, 4, 8]) then begin
1459+ o := 0;
1460+ if not checked then o := IndexOff(size, 0);
13961461 r := Peek(depth - 1);
13971462 if checked then begin
13981463 Alu(aluCmp, r, a.hi);
13991464 Check(ccB, 0, p, r, InRegister(a.hi), 0, -1);
14001465 end;
14011466 Drop;
1402- NewMem(lIdx, MemIdx(a.chunk, r, size, 0), kWord, 8, false);
1467+ NewMem(lIdx, MemIdx(a.chunk, r, size, o), kWord, 8, false);
14031468 exit;
14041469 end;
14051470 inRax := a.base = bStack;
@@ -1434,18 +1499,20 @@
14341499 chunk, if the index is a variable's register, its address made in
14351500 the value's own register. Any other is added up. }
14361501 procedure IndexArray(const a: TAdr; size: int64);
1437-var r, b: longint;
1502+var r, b: longint; o: int64;
14381503 begin
14391504 if (size in [1, 2, 4, 8]) and (a.base = bFrame) then begin
1505+ o := IndexOff(size, a.off);
14401506 r := Peek(depth - 1);
14411507 Drop;
1442- NewMem(lIdx, MemIdx(RBP, r, size, a.off), kWord, 8, false);
1508+ NewMem(lIdx, MemIdx(RBP, r, size, o), kWord, 8, false);
14431509 end else if (size in [1, 2, 4, 8]) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then begin
1444- r := stack[depth - 1].val;
1510+ o := IndexOff(size, 0);
1511+ r := Peek(depth - 1);
14451512 Drop;
14461513 b := Target;
14471514 Lea(b, MemRip(a.chunk, a.off));
1448- NewMem(lIdx, MemIdx(b, r, size, 0), kWord, 8, false);
1515+ NewMem(lIdx, MemIdx(b, r, size, o), kWord, 8, false);
14491516 end else begin
14501517 MulI(Top, size);
14511518 PushAdr(a);

test/enctest.pas modified +3 -1

@@ -23,7 +23,9 @@
2323 SetCC(ccL, RAX); SetCC(ccA, RCX); SetCC(ccE, RDI); TestRR(RAX, RAX);
2424 Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); CallM(Mem(RCX, 8)); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb;
2525 LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX);
26- MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsdXM(2, Mem(RBP, -8)); MovsdMX(Mem(RBP, -8), 7);
26+ MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsXM(2, Mem(RBP, -8), false); MovsMX(Mem(RBP, -8), 7, false);
27+ MovX(9, 12); MovX(0, 8); MovsXM(8, MemIdx(R13, R9, 8, -8), true); MovsMX(Mem(RBP, -8), 11, true);
28+ SseM($58, 9, MemIdx(R13, R10, 8, 4096), false); SseM($59, 0, Mem(RBP, -16), true); Sse($5C, 8, 11, false);
2729 Sse($58, 0, 1, false); Sse($5E, 0, 1, true); Sse($5A, 0, 0, false); Sse($5A, 0, 0, true);
2830 Ucomis(0, 1, false); Ucomis(0, 1, true); Cvtsi2s(0, RAX, false); Cvtsi2s(0, RAX, true);
2931 Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63);