raz / commit 87e3c487

commit
87e3c487421b8db4e2fca12ce1d6541f17e9235c
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
a1d537b4
An element of a var or const array parameter, of 1, 2, 4 or 8 bytes, is the operand [pointer + index * size], as one in the frame is: indexArray takes bPtr's register as the base (a sum over const s: [N]INT 145 ms to 82, a slice's speed; ChaCha's quarter on var x: [16]U32 560 ms to 283)

patch · log

7 files changed, +267 -7

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

@@ -21,7 +21,7 @@
2121 | `src/Sym.raz` | 723 | 581 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,605 | 1,318 | The instruction encoder, jumps within a procedure, the assembler |
24-| `src/Gen.raz` | 2,797 | 1,961 | The stack machine, for x86-64: values, operations, checks, calls, frames |
24+| `src/Gen.raz` | 2,800 | 1,963 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2525 | `src/Item.raz` | 2,414 | 1,938 | Items: types, constants, variables, operators, conversions, calls |
2626 | `src/Parse.raz` | 4,592 | 3,942 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
@@ -884,6 +884,36 @@
884884 ahead. The compiler, built by itself, runs 0.9% fewer instructions on
885885 the 348,000-line program, in the same time.
886886
887+## Array parameters, measured
888+
889+Two gaps in what the earlier sections built, found by timing loops
890+over arrays passed as parameters, which the library's `const`
891+convention (report 2026-10.33) made common.
892+
893+1. **An array parameter's element as an operand.** `indexArray` noted
894+ an element as `[base + index * size]` (`lIdx`) for an array in the
895+ frame or a chunk, but an array reached through a pointer (`bPtr`,
896+ a `var` or `const` parameter whose address is in a register) was
897+ added up, though the pointer was in a register already. A sum over
898+ `const s: [N]INT`:
899+
900+ ```
901+ mov r9, r12
902+ shl r9, 3
903+ lea r10, [rdi] now: add rbx, [rdi + r12 * 8]
904+ add r9, r10
905+ mov r9, [r9]
906+ add rbx, r9
907+ ```
908+
909+ The pointer's register is the base, as `rbp` is for the frame. It
910+ is a variable's register, not one of the stack's, so the value
911+ owns only its index's, as for the frame. One in a frame slot
912+ (`bSlot`) is added up still: the pointer would need a register of
913+ its own. The sum over a `const` array 145 ms to 82, the speed of
914+ the sum over a slice; ChaCha's `quarter(var x: [16]U32; ...)` 560
915+ ms to 283 (2.0 times), in 58% fewer instructions.
916+
887917 ## Diag.raz: messages
888918
889919 Every message the compiler prints is made here. A position, `Pos`, is a
@@ -1097,8 +1127,8 @@
10971127 `and` and `or`, `fix` where the label is, and `joinJumps`;
10981128 - checks: `checkIndex`, `checkView`, `checkEnum`, and `indexSlice`, `slice` and
10991129 `slicePtr`, which check as they make their address or slice; and
1100- `indexArray`, an element's address in an array in the frame or a
1101- chunk, the index checked before;
1130+ `indexArray`, an element's address in an array in the frame, a
1131+ chunk or behind a parameter's pointer, the index checked before;
11021132 - calls (`call`), frames (`beginProc`, `enterBody`, `returnJump`,
11031133 `endProc`, the temporaries of `allocTemp`), deferred statements
11041134 (`deferBegin`, `deferEnd`, `runDefer`), the built-ins that are

src/Gen.raz modified +7 -4

@@ -1785,18 +1785,21 @@
17851785 (* Pops an index, checked, and makes the address of the element it
17861786 picks of the array at a. An element of 1, 2,
17871787 4 or 8 bytes is only noted, as the operand [base + index * size]
1788- (lIdx): an array in the frame has rbp as its base, and one in a
1789- chunk its address as the displacement (X.memAbs), with no lea. Any
1790- other is added up. *)
1788+ (lIdx): an array in the frame has rbp as its base, one reached
1789+ through a pointer in a register (a var or const parameter) that
1790+ register, and one in a chunk its address as the displacement
1791+ (X.memAbs), with no lea. Any other is added up. *)
17911792 proc indexArray*(a: Adr; size: I64);
17921793 var r: INT; o: I64;
17931794 begin
1794- if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and ((a.base = bFrame) or (a.base = bGlobal)) then
1795+ if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base in {bFrame, bGlobal, bPtr}) then
17951796 o := indexOff(size, a.off);
17961797 r := peek(depth - 1);
17971798 drop();
17981799 if a.base = bFrame then
17991800 newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false);
1801+ elsif a.base = bPtr then
1802+ newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false);
18001803 else
18011804 newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false);
18021805 end;

test/run/IndexParam.expect (history) added +10 -0

@@ -0,0 +1,10 @@
1+3246 26250000 2485999775
2+3246 26250000 2485999775
3+1 -17 -50 -111 -226 -447 -878 -1727
4+1 -17 -50 -111 -226 -447 -878 -1727
5+-17 -50 -111 -226 -447 -878 -1727
6+8 227 190 153 116 79 42 5
7+5 42 79 116 153 190 227 8
8+148
9+49
10+3500

test/run/IndexParam.raz (history) added +154 -0

@@ -0,0 +1,154 @@
1+(* An element of a var or const array parameter, of 1, 2, 4 or 8 bytes,
2+ is read and written as the operand [pointer + index * size], the
3+ index checked first; here in a leaf, which keeps the pointer in rdi or
4+ rsi, and in a procedure that calls, which keeps it in a callee-saved
5+ register. IndexParamOff has the same with no checks. An element of
6+ another size is added up. The last index is out of range. *)
7+module IndexParam;
8+import Out, IndexParamOff;
9+
10+const N = 8;
11+
12+type
13+ Bytes = IndexParamOff.Bytes;
14+ Shorts = IndexParamOff.Shorts;
15+ Words = IndexParamOff.Words;
16+ Longs = IndexParamOff.Longs;
17+ Odd = record a, b, c: U8; end; (* 3 bytes *)
18+ Pair = record tag: U8; v: [N]I32; end; (* v at an offset *)
19+
20+var
21+ bs: Bytes;
22+ ss: Shorts;
23+ ws: Words;
24+ ls, ds: Longs;
25+ os: [N]Odd;
26+ pr: Pair;
27+
28+proc sumBytes(const a: Bytes): INT;
29+var i, t: INT;
30+begin
31+ t := 0;
32+ for i := 0 to N - 1 do t := t + a[i] + INT(a[N - 1 - i]) * 2; end;
33+ for i := 0 to N div 2 - 1 do t := t + a[2 * i + 1] + a[a[i] mod N]; end;
34+ return t;
35+end;
36+
37+proc sumShorts(const a: Shorts): INT;
38+var i, t: INT;
39+begin
40+ t := 0;
41+ for i := 1 to N - 1 do t := t + INT(a[i]) * a[i - 1]; end;
42+ return t;
43+end;
44+
45+proc sumWords(const a: Words): U32;
46+var i: INT; t: U32;
47+begin
48+ t := 0;
49+ for i := 0 to N - 2 do t := t + a[i + 1] xor a[i]; end;
50+ return t;
51+end;
52+
53+proc double(var a: Longs; const d: Longs);
54+var i: INT;
55+begin
56+ for i := 1 to N - 1 do a[i] := a[i - 1] * 2 + d[i]; end;
57+end;
58+
59+(* as double, but calling between: the pointers are kept past calls *)
60+proc doubleSay(var a: Longs; const d: Longs);
61+var i: INT;
62+begin
63+ for i := 1 to N - 1 do
64+ a[i] := a[i - 1] * 2 + d[i];
65+ Out.int(a[i]);
66+ Out.char(' ');
67+ end;
68+ Out.ln;
69+end;
70+
71+proc swap(var a: Bytes);
72+var i: INT; c: U8;
73+begin
74+ for i := 0 to N div 2 - 1 do
75+ c := a[i];
76+ a[i] := a[N - 1 - i];
77+ a[N - 1 - i] := c;
78+ end;
79+end;
80+
81+(* the elements of 3 bytes, added up as before *)
82+proc sumOdd(var a: [N]Odd): INT;
83+var i, t: INT;
84+begin
85+ t := 0;
86+ for i := 0 to N - 1 do
87+ a[i].c := U8(a[N - 1 - i].a + 1);
88+ t := t + a[i].a + INT(a[i].b) * a[i].c;
89+ end;
90+ return t;
91+end;
92+
93+(* an array at an offset in a record parameter, read and written *)
94+proc pairSum(var p: Pair): INT;
95+var i, t: INT;
96+begin
97+ t := 0;
98+ for i := 0 to N - 1 do p.v[i] := I32(i - 3) * I32(p.tag); end;
99+ for i := 1 to N - 1 do t := t + INT(p.v[i - 1]) * 10 + INT(p.v[i]); end;
100+ return t;
101+end;
102+
103+(* a's element at k, which may be out of range *)
104+proc at(const a: Shorts; k: INT): INT;
105+begin
106+ return a[k + 1];
107+end;
108+
109+proc reset;
110+var i: INT;
111+begin
112+ for i := 0 to N - 1 do
113+ bs[i] := U8(i * 37 + 5);
114+ ss[i] := I16(i * 1000 - 3500);
115+ ws[i] := U32(i) * 9E3779B9H;
116+ ls[i] := I64(i + 1);
117+ ds[i] := I64(i * i) - 20;
118+ os[i].a := U8(i); os[i].b := U8(i + 1); os[i].c := 0;
119+ end;
120+end;
121+
122+proc main*;
123+var i: INT;
124+begin
125+ reset();
126+ Out.int(sumBytes(bs)); Out.char(' ');
127+ Out.int(sumShorts(ss)); Out.char(' ');
128+ Out.int(I64(sumWords(ws))); Out.ln;
129+ Out.int(IndexParamOff.sumBytes(bs)); Out.char(' ');
130+ Out.int(IndexParamOff.sumShorts(ss)); Out.char(' ');
131+ Out.int(I64(IndexParamOff.sumWords(ws))); Out.ln;
132+ double(ls, ds);
133+ for i := 0 to N - 1 do Out.int(ls[i]); Out.char(' '); end;
134+ Out.ln;
135+ reset();
136+ IndexParamOff.double(ls, ds);
137+ for i := 0 to N - 1 do Out.int(ls[i]); Out.char(' '); end;
138+ Out.ln;
139+ reset();
140+ doubleSay(ls, ds);
141+ swap(bs);
142+ for i := 0 to N - 1 do Out.int(I64(bs[i])); Out.char(' '); end;
143+ Out.ln;
144+ IndexParamOff.swap(bs);
145+ for i := 0 to N - 1 do Out.int(I64(bs[i])); Out.char(' '); end;
146+ Out.ln;
147+ Out.int(sumOdd(os)); Out.ln;
148+ pr.tag := 7;
149+ Out.int(pairSum(pr)); Out.ln;
150+ Out.int(at(ss, 6)); Out.ln;
151+ Out.int(at(ss, 7)); Out.ln;
152+end;
153+
154+end IndexParam.

test/run/IndexParam.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/IndexParam.stderr (history) added +3 -0

@@ -0,0 +1,3 @@
1+test/run/IndexParam.raz:106:14: index 8 is out of range for a length of 8
2+ in IndexParam.at at test/run/IndexParam.raz:106:14: k = 7
3+ in IndexParam.main at test/run/IndexParam.raz:151:5: i = 7

test/run/mods/IndexParamOff.raz (history) added +59 -0

@@ -0,0 +1,59 @@
1+(* IndexParam's procedures again, with no checks: an element of an array
2+ parameter of 1, 2, 4 or 8 bytes is read and written as the operand
3+ [pointer + index * size], where the index comes with no check. *)
4+module IndexParamOff;
5+check off;
6+
7+const N = 8;
8+
9+type
10+ Bytes* = [N]U8;
11+ Shorts* = [N]I16;
12+ Words* = [N]U32;
13+ Longs* = [N]I64;
14+
15+(* a sum of a's elements, picked by indices worked out several ways *)
16+proc sumBytes*(const a: Bytes): INT;
17+var i, t: INT;
18+begin
19+ t := 0;
20+ for i := 0 to N - 1 do t := t + a[i] + INT(a[N - 1 - i]) * 2; end;
21+ for i := 0 to N div 2 - 1 do t := t + a[2 * i + 1] + a[a[i] mod N]; end;
22+ return t;
23+end;
24+
25+proc sumShorts*(const a: Shorts): INT;
26+var i, t: INT;
27+begin
28+ t := 0;
29+ for i := 1 to N - 1 do t := t + INT(a[i]) * a[i - 1]; end;
30+ return t;
31+end;
32+
33+proc sumWords*(const a: Words): U32;
34+var i: INT; t: U32;
35+begin
36+ t := 0;
37+ for i := 0 to N - 2 do t := t + a[i + 1] xor a[i]; end;
38+ return t;
39+end;
40+
41+(* each element of a made twice the one before, plus d[i] *)
42+proc double*(var a: Longs; const d: Longs);
43+var i: INT;
44+begin
45+ for i := 1 to N - 1 do a[i] := a[i - 1] * 2 + d[i]; end;
46+end;
47+
48+(* the bytes of a turned round, two at a time from either end *)
49+proc swap*(var a: Bytes);
50+var i: INT; c: U8;
51+begin
52+ for i := 0 to N div 2 - 1 do
53+ c := a[i];
54+ a[i] := a[N - 1 - i];
55+ a[N - 1 - i] := c;
56+ end;
57+end;
58+
59+end IndexParamOff.