raz / commit b14e8145

commit
b14e81459b1c0e5e708fafea8db0036942033e1a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
95b6de34
A prologue that zeroes only the locals in memory

v0.7.0 step 1. A local gets its frame slot only once the registers are
chosen (PlaceLocals), and only if it isn't in one; the save slots come
after the locals and aren't zeroed, as each is written first. So
Str.decode, all of whose variables are in registers, no longer zeroes
seven quadwords in a loop on every call.

rat: 701 -> 620 ms (-12%), text 558,060 -> 553,484.

patch · log

4 files changed, +94 -67

doc/compiler.md (history) modified +14 -10

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of Free Pascal, under 8,400 lines of code.
3+The compiler is eleven files of Free Pascal, under 10,000 lines of code.
44 This guide walks through them in the order they are best read: from the
55 characters of a source file to the bytes of an executable. It assumes
66 you know the language from `doc/raz.md`, and enough x86-64 to read a
@@ -13,9 +13,9 @@
1313 | `src/sym.pas` | 617 | 512 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,616 | 1,270 | The stack machine, for x86-64: values, operations, checks, calls, frames |
16+| `src/gen.pas` | 1,633 | 1,280 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1717 | `src/item.pas` | 1,444 | 1,204 | Items: types, constants, variables, operators, conversions, calls |
18-| `src/parse.pas` | 2,904 | 2,575 | Declarations, statements, expressions, built-ins, recovery, warnings |
18+| `src/parse.pas` | 2,913 | 2,579 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
2121 | `src/raz.pas` | 197 | 147 | The command line |
@@ -358,16 +358,19 @@
358358 results (`ResultTo`, `CopyResult`).
359359
360360 Frames: `BeginProc` gives the parameters their slots; `EnterBody`
361-writes the prologue, which zeroes the locals (with a few stores, or a
362-loop for more; there are none to zero in `scale`) but not the
363-temporaries, since each is written before it is read; `EndProc` writes
361+writes the prologue, which zeroes the locals in memory (with a few
362+stores, or a loop for more; there are none to zero in `scale`) but not
363+the temporaries, since each is written before it is read; `EndProc` writes
364364 the epilogue and patches the frame's size in. Within a statement,
365365 scratch space comes from `AllocTemp` and is given back by
366366 `ReleaseTemps`; `AllocPerm` reserves space for the whole procedure.
367367
368-Register variables: the parser chooses them (`ChooseRegs`, below) and
369-tells gen (`UseRegs`): the registers, as bits of `VarRegs`, and the
370-slots they are saved in, a quadword each after the variables. The
368+Register variables: the parser chooses them (`ChooseRegs`, below),
369+then gives the locals left in memory their slots (`PlaceLocals`), and
370+tells gen (`UseRegs`) the registers, as bits of `VarRegs`. A variable
371+in a register has no slot of its own. Gen saves the registers in slots
372+a quadword each, after the locals and not zeroed, as each is written
373+first. The
371374 prologue saves them, zeroes them, and takes a parameter's value from
372375 where it came; the epilogue restores them. A variable in one is an
373376 item at `bReg`: `PushVar` notes it (`lHeld`) rather than copying it,
@@ -553,7 +556,8 @@
553556 address wanted after all, by a `var` parameter, say, comes to
554557 `Address` (`item.pas`), which puts the variable back in memory and
555558 sets `demoted`: `Body` then throws the procedure's code away
556-(`MarkCode`, `Redo`) and compiles its statements again. Its notes for
559+(`MarkCode`, `Redo`), places the locals again, and compiles its
560+statements again. Its notes for
557561 the trap report are made after, by `VarKind`, which marks a variable
558562 in a register with the register's number.
559563

src/gen.pas modified +26 -9

@@ -154,7 +154,8 @@
154154 procedure KeepTemps;
155155
156156 procedure BeginProc(sig: PType; params: PObj);
157-procedure UseRegs(mask: longint; at: int64);
157+procedure LocalsFromScratch;
158+procedure UseRegs(mask: longint);
158159 procedure MarkCode(var m: TCodeMark);
159160 procedure Redo(const m: TCodeMark);
160161 procedure EnterBody(c, line: longint);
@@ -203,6 +204,7 @@
203204 frameTop, frameMax, permTop: int64;
204205 frameFix: longint; { where the frame size is patched in }
205206 paramTop: int64; { the parameters' slots end here }
207+ zeroTop: int64; { the locals the prologue zeroes end here }
206208 retSlot: int64; { holds the hidden result pointer }
207209 resType: PType; { the procedure's result, or nil }
208210 exitL: longint;
@@ -1352,12 +1354,26 @@
13521354 paramTop := frameTop;
13531355 end;
13541356
1355-{ The procedure keeps variables in the registers of mask, saved at at;
1356- the frame as it stands is where a second try at its body starts. }
1357-procedure UseRegs(mask: longint; at: int64);
1357+{ The frame as BeginProc left it, with the parameters' slots only: the
1358+ locals are given theirs again once their registers are chosen. }
1359+procedure LocalsFromScratch;
1360+begin
1361+ frameTop := paramTop; frameMax := paramTop; permTop := paramTop;
1362+end;
1363+
1364+{ The locals in memory have their slots: the prologue zeroes those and
1365+ nothing below. The procedure keeps variables in the registers of
1366+ mask, which are saved next, a quadword each, in slots that need no
1367+ zeroing; the frame as it then stands is where a second try at the
1368+ body starts. }
1369+procedure UseRegs(mask: longint);
1370+var i, k: longint;
13581371 begin
1372+ zeroTop := permTop;
13591373 regMask := mask;
1360- saveAt := at;
1374+ k := 0;
1375+ for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k);
1376+ if k > 0 then saveAt := AllocPerm(8 * k, 8);
13611377 frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop;
13621378 end;
13631379
@@ -1377,9 +1393,10 @@
13771393 end;
13781394
13791395 { The prologue, which is for the procedure's first line. Only the
1380- locals are zeroed: the parameters are stored over their slots next,
1381- and every temporary is written before it is read. The locals are all
1382- declared by now, below the parameters' slots. }
1396+ locals in memory are zeroed, from the parameters' slots down to
1397+ zeroTop: the parameters are stored over their slots next, the
1398+ registers of variables are zeroed in place, their save slots are
1399+ written before they are read, and so is every temporary. }
13831400 procedure EnterBody(c, line: longint);
13841401 var i, top: longint; n: int64; p: PObj;
13851402 begin
@@ -1391,7 +1408,7 @@
13911408 Say('sub rsp, frame');
13921409 Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 }
13931410 frameFix := pc; Put4(0);
1394- n := (AlignUp(frameMax, 8) - paramTop) div 8;
1411+ n := (AlignUp(zeroTop, 8) - paramTop) div 8;
13951412 if n > 0 then MovRI(RAX, 0);
13961413 if n <= 4 then
13971414 for i := 1 to n do Store(Mem(RBP, -paramTop - 8 * i), RAX, 8)

src/parse.pas modified +24 -15

@@ -1782,12 +1782,12 @@
17821782 its "begin" to its end, a use counts 1, in a loop 4, in two 16, and
17831783 a variable ADR is taken of can't have one. A register costs a save
17841784 and a restore, so a variable used less than three times keeps its
1785- place in memory. The registers are saved after the variables. }
1785+ place in memory. }
17861786 procedure ChooseRegs(params, before: PObj);
17871787 var
17881788 cand: array[0..63] of PObj; weight: array[0..63] of longint;
17891789 isLoop: array[0..255] of boolean;
1790- n, i, k, d, loops, mask, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean;
1790+ n, i, k, d, loops, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean;
17911791
17921792 function Find(name: longint): longint;
17931793 var j: longint;
@@ -1807,7 +1807,6 @@
18071807 while o <> nil do begin Add(o); o := o^.next end;
18081808 if before = nil then o := curModule^.locals else o := before^.link;
18091809 while o <> nil do begin Add(o); o := o^.link end;
1810- mask := 0;
18111810 if n > 0 then begin
18121811 SaveScan(st);
18131812 wasSkipping := skipping;
@@ -1846,17 +1845,12 @@
18461845 if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i;
18471846 if best < 0 then break;
18481847 cand[best]^.reg := VarRegs[k];
1849- mask := mask or (1 shl k);
18501848 end;
18511849 end;
1852- k := 0;
1853- for i := 0 to 4 do if mask and (1 shl i) <> 0 then inc(k);
1854- if k = 0 then UseRegs(0, 0)
1855- else UseRegs(mask, AllocPerm(8 * k, 8));
18561850 end;
18571851
1858-{ The registers the variables of the procedure are in, as ChooseRegs's
1859- mask }
1852+{ The registers the variables of the procedure are in, as bits of
1853+ VarRegs }
18601854 function RegsOf(params, before: PObj): longint;
18611855 var o: PObj; m, k: longint;
18621856 begin
@@ -1875,6 +1869,22 @@
18751869 RegsOf := m;
18761870 end;
18771871
1872+{ Gives each local not in a register its slot in the frame, the slots
1873+ the prologue zeroes, and then tells gen which registers the
1874+ variables are in. A variable in a register has no slot: it starts at
1875+ zero in its register, and the trap report finds it there. }
1876+procedure PlaceLocals(params, before: PObj);
1877+var o: PObj;
1878+begin
1879+ LocalsFromScratch;
1880+ if before = nil then o := curModule^.locals else o := before^.link;
1881+ while o <> nil do begin
1882+ if (o^.cls = oVar) and (o^.reg = 0) then o^.val := AllocPerm(o^.typ^.size, o^.typ^.align);
1883+ o := o^.link;
1884+ end;
1885+ UseRegs(RegsOf(params, before));
1886+end;
1887+
18781888 procedure Body(p: PObj; sig: PType; line: longint);
18791889 var
18801890 q, params, last, o, before: PObj; old: PType; st: TScanState;
@@ -1905,6 +1915,7 @@
19051915 before := curModule^.lastLocal;
19061916 Declarations(false);
19071917 ChooseRegs(params, before);
1918+ PlaceLocals(params, before);
19081919 { the body, made again if a variable in a register must be in memory }
19091920 SaveScan(st);
19101921 MarkCode(mark); drops0 := numDrops; errs := errors;
@@ -1922,7 +1933,7 @@
19221933 if not demoted or (errors <> errs) then break;
19231934 numDrops := drops0;
19241935 Redo(mark);
1925- UseRegs(RegsOf(params, before), chunks^[p^.chunk].saveAt);
1936+ PlaceLocals(params, before);
19261937 RestoreScan(st);
19271938 until false;
19281939 EndProc(p^.chunk);
@@ -2227,10 +2238,8 @@
22272238 o^.lev := 0;
22282239 o^.chunk := NewChunk(secBss, curModule^.sname, names[i]);
22292240 SetBss(o^.chunk, t^.size, t^.align);
2230- end else begin
2231- o^.lev := 1;
2232- o^.val := AllocPerm(t^.size, t^.align);
2233- end;
2241+ end else
2242+ o^.lev := 1; { its slot comes from PlaceLocals, if it needs one }
22342243 Declare(o);
22352244 end;
22362245 Expect(sSemicolon);

test/list/Listing.expect (history) modified +30 -33

@@ -28,39 +28,36 @@
2828 0 push rbp
2929 1 mov rbp, rsp
3030 4 sub rsp, 32
31- 11 xor eax, eax
32- 13 mov [rbp - 8], rax
33- 17 mov [rbp - 16], rax
34- 21 mov [rbp - 16], rbx
35- 25 xor ebx, ebx
31+ 11 mov [rbp - 8], rbx
32+ 15 xor ebx, ebx
3633 20 for i := 1 to 3 do
37- 27 mov rbx, 1
38- 32 mov qword [rbp - 24], 3
39- 40 mov r9, [rbp - 24]
40- 44 cmp rbx, r9
41- 47 jg 139
34+ 17 mov rbx, 1
35+ 22 mov qword [rbp - 16], 3
36+ 30 mov r9, [rbp - 16]
37+ 34 cmp rbx, r9
38+ 37 jg 129
4239 21 if i # 2 then total := add(total, twice(i)); end;
43- 53 cmp rbx, 2
44- 57 je 107
45- 63 mov r8, [Listing.total]
46- 70 mov [rbp - 32], r8
47- 74 mov rdi, rbx
48- 77 call Listing.twice
49- 82 mov r9, rax
50- 85 mov rdi, [rbp - 32]
51- 89 mov rsi, r9
52- 92 call Listing.add
53- 97 mov r8, rax
54- 100 mov [Listing.total], r8
55- 107 mov r8, [rbp - 24]
56- 111 sub r8, rbx
57- 114 cmp r8, 1
58- 118 jb 139
59- 124 mov r8, rbx
60- 127 add r8, 1
61- 131 mov rbx, r8
62- 134 jmp 40
40+ 43 cmp rbx, 2
41+ 47 je 97
42+ 53 mov r8, [Listing.total]
43+ 60 mov [rbp - 24], r8
44+ 64 mov rdi, rbx
45+ 67 call Listing.twice
46+ 72 mov r9, rax
47+ 75 mov rdi, [rbp - 24]
48+ 79 mov rsi, r9
49+ 82 call Listing.add
50+ 87 mov r8, rax
51+ 90 mov [Listing.total], r8
52+ 97 mov r8, [rbp - 16]
53+ 101 sub r8, rbx
54+ 104 cmp r8, 1
55+ 108 jb 129
56+ 114 mov r8, rbx
57+ 117 add r8, 1
58+ 121 mov rbx, r8
59+ 124 jmp 30
6360 23 end main;
64- 139 mov rbx, [rbp - 16]
65- 143 leave
66- 144 ret
61+ 129 mov rbx, [rbp - 8]
62+ 133 leave
63+ 134 ret