raz / commit d0aa106b

commit
d0aa106b94c649b4828825295e1cd1eeafbd82da
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
090fc959
An element of an array as the machine has it

x[i], of an array of the module with i in a register, was a copy of
i, a shift, a lea and an add before its load. Now an element of 1, 2,
4 or 8 bytes of an array in a chunk, its index a variable's register,
is noted as the operand [base + i * 8], the array's address made in
the value's own register; one in the frame is [rbp + i * 8 + off],
whatever its index (IndexArray). CheckIndex compares a variable's
register as it is.

NBody 225 -> 147 ms (FPC -O2 85); rat 464 -> 463, within noise; the
other kernels unchanged. 9,036 lines of code.

patch · log

4 files changed, +72 -8

PLAN.md (history) modified +7 -1

@@ -29,7 +29,13 @@
2929 `movsd`, a constant read from read-only data, and an operation takes
3030 its right operand from memory as it is. Jacobi 251 ms to 129, NBody
3131 375 to 225; rat within noise.
32-3. **Real variables in xmm registers**, where that pays: measured, and
32+3. Done: **An element of an array as the machine has it.** NBody's
33+ time went mostly to its addresses, not its reals: `x[i]`, of an
34+ array of the module, was a copy, a shift, a `lea` and an add before
35+ its load. Now an element of an array in the frame or a chunk is the
36+ operand `[base + i * 8]`, as one of a slice in registers is. NBody
37+ 225 ms to 147; rat within noise.
38+4. **Real variables in xmm registers**, where that pays: measured, and
3339 kept only if it does.
3440
3541 ## Next, when the owner says

doc/compiler.md (history) modified +35 -3

@@ -15,8 +15,8 @@
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,093 | 1,522 | The stack machine, for x86-64: values, operations, checks, calls, frames |
19-| `src/item.pas` | 1,579 | 1,302 | Items: types, constants, variables, operators, conversions, calls |
18+| `src/gen.pas` | 2,121 | 1,542 | The stack machine, for x86-64: values, operations, checks, calls, frames |
19+| `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 |
2222 | `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers |
@@ -489,6 +489,36 @@
489489 under it as it does the integers. Jacobi 251 ms to 129, NBody 375
490490 to 225; rat within noise.
491491
492+2. **An element of an array as the machine has it.** That left
493+ NBody's reals waiting on their addresses: `x[i]`, of an array of
494+ the module, was worked out as an address, and the index copied
495+ from its register to be scaled:
496+
497+ ```
498+ mov r8, rbx ; i
499+ shl r8, 3
500+ lea r9, [NBody.x]
501+ add r8, r9
502+ movsd xmm8, [r8]
503+ ```
504+
505+ Now an element of 1, 2, 4 or 8 bytes of an array in a chunk, with
506+ its index in a variable's register, is noted as the operand that
507+ reads it, its base the array's address, made in the value's own
508+ register; one in the frame has `rbp` as its base, whatever its
509+ index (`IndexArray`, as `IndexSlice` does for a slice in
510+ registers):
511+
512+ ```
513+ lea r8, [NBody.x]
514+ movsd xmm8, [r8 + rbx * 8]
515+ ```
516+
517+ A computed index in a chunk's array is added up as before, as it
518+ would need a third register. `CheckIndex` compares a variable's
519+ register as it is (`Peek`), not a copy. NBody 225 ms to 147; rat
520+ within noise.
521+
492522 ## diag.pas: messages
493523
494524 Every message the compiler prints is made here. A position, `TPos`, is
@@ -656,7 +686,9 @@
656686 `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for
657687 `and` and `or`, `Fix` where the label is, and `JoinJumps`;
658688 - checks: `CheckIndex`, `CheckEnum`, and `IndexSlice`, `Slice` and
659- `SlicePtr`, which check as they make their address or slice;
689+ `SlicePtr`, which check as they make their address or slice; and
690+ `IndexArray`, an element's address in an array in the frame or a
691+ chunk, the index checked before;
660692 - calls (`Call`), frames (`BeginProc`, `EnterBody`, `ReturnJump`,
661693 `EndProc`, the temporaries of `AllocTemp`), deferred statements
662694 (`DeferBegin`, `DeferEnd`, `RunDefer`), the built-ins that are

src/gen.pas modified +28 -1

@@ -169,6 +169,7 @@
169169 procedure CheckIndex(n: int64; const p: TPos);
170170 procedure CheckEnum(n: int64; name: longint; const p: TPos);
171171 procedure CheckNil(reach: int64; const p: TPos);
172+procedure IndexArray(const a: TAdr; size: int64);
172173 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
173174 procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos);
174175 procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos);
@@ -1352,7 +1353,7 @@
13521353 procedure CheckIndex(n: int64; const p: TPos);
13531354 var r: longint;
13541355 begin
1355- r := Top;
1356+ r := Peek(depth - 1);
13561357 CmpI(r, n);
13571358 Check(ccB, 0, p, r, NoMem, n, -1);
13581359 end;
@@ -1424,6 +1425,32 @@
14241425 if inRax then MovRR(Target, RAX)
14251426 else Claim(depth, kWord); { the index's register again, now for the address }
14261427 Computed(kWord);
1428+end;
1429+
1430+{ Pops an index, checked, and makes the address of the element it
1431+ picks of the array at a. An element of 1, 2,
1432+ 4 or 8 bytes is only noted, as the operand [base + index * size]
1433+ (lIdx): an array in the frame has rbp as its base, and one in a
1434+ chunk, if the index is a variable's register, its address made in
1435+ the value's own register. Any other is added up. }
1436+procedure IndexArray(const a: TAdr; size: int64);
1437+var r, b: longint;
1438+begin
1439+ if (size in [1, 2, 4, 8]) and (a.base = bFrame) then begin
1440+ r := Peek(depth - 1);
1441+ Drop;
1442+ NewMem(lIdx, MemIdx(RBP, r, size, a.off), kWord, 8, false);
1443+ 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;
1445+ Drop;
1446+ b := Target;
1447+ Lea(b, MemRip(a.chunk, a.off));
1448+ NewMem(lIdx, MemIdx(b, r, size, 0), kWord, 8, false);
1449+ end else begin
1450+ MulI(Top, size);
1451+ PushAdr(a);
1452+ Op(sPlus, kWord, 8, false);
1453+ end;
14271454 end;
14281455
14291456 { Pops lo and hi, and the address of the array or slice at a if it is

src/item.pas modified +2 -3

@@ -640,9 +640,8 @@
640640 end else if t^.form = fArray then begin
641641 Value(y);
642642 if checks then CheckIndex(t^.len, y.pos);
643- ScaleBy(s);
644- if x.base <> bStack then begin PushAdr(AdrOf(x)); x.off := 0 end;
645- gen.Op(sPlus, kWord, 8, false);
643+ if x.base = bStack then begin ScaleBy(s); gen.Op(sPlus, kWord, 8, false) end
644+ else begin IndexArray(AdrOf(x), s); x.off := 0 end;
646645 x.base := bStack;
647646 end else begin
648647 Value(y);