raz / commit 4d68ba95

commit
4d68ba95f3479811ca7bf70d076cd2c5b06eb6af
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
787085c3
An element of a module's array by its address, with no lea

x86 has no rip + index, so x[i] of an array in a chunk made the
array's address in a register first, for every element. The
executable is loaded below 2 GB where the linker placed it, so the
address fits a 32-bit displacement with no base: X64.memAbs, whose
fixup holds the address itself (Chunk.addAbsFixup), and indexArray
notes the element as it does one in the frame, whatever register its
index is in.

NBody 25% fewer instructions, 16% faster. Grep's loops are unchanged
but moved 96 bytes, and it runs 5% slower for it.

patch · log

5 files changed, +82 -23

doc/compiler.md (history) modified +32 -4

@@ -16,12 +16,12 @@
1616 | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts |
1717 | `src/Scan.raz` | 1,062 | 886 | Symbols, literals, comments, positions |
1818 | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout |
19-| `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes |
20-| `src/X64.raz` | 1,493 | 1,238 | The instruction encoder, jumps within a procedure, the assembler |
21-| `src/Gen.raz` | 2,509 | 1,797 | The stack machine, for x86-64: values, operations, checks, calls, frames |
19+| `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
20+| `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
21+| `src/Gen.raz` | 2,506 | 1,794 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2222 | `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls |
2323 | `src/Parse.raz` | 3,853 | 3,361 | Declarations, statements, expressions, built-ins, recovery, warnings |
24-| `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places |
24+| `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
2727
@@ -779,6 +779,34 @@
779779 ```
780780
781781 NBody 8.6% fewer instructions, 2.8% faster; the rest unchanged.
782+
783+2. **An element of a module's array by its address.** x86 has no
784+ `rip + index`, so `x[i]`, of an array in a chunk, made the array's
785+ address in a register first, for each element, twice in
786+ `x[i] - x[j]`:
787+
788+ ```
789+ lea r8, [NBody.x]
790+ lea r9, [NBody.x]
791+ movsd xmm8, [r8 + rbx * 8]
792+ subsd xmm8, [r9 + r12 * 8]
793+ ```
794+
795+ But the executable is loaded where the linker placed it, below 2 GB,
796+ so the array's address fits a 32-bit displacement with no base
797+ register at all (`X.memAbs`, a `RIP` base with an index; its fixup
798+ holds the address itself, `Chunk.addAbsFixup`, not the distance from
799+ the field). `indexArray` notes such an element as it does one in the
800+ frame, whatever register its index is in:
801+
802+ ```
803+ movsd xmm8, [NBody.x + rbx * 8]
804+ subsd xmm8, [NBody.x + r12 * 8]
805+ ```
806+
807+ NBody 25% fewer instructions, 16% faster. Grep is 5% slower with
808+ the same code in its loops, only moved 96 bytes: where a loop falls
809+ changes its speed, as v0.8.0 found.
782810
783811 ## Diag.raz: messages
784812

src/Chunk.raz (history) modified +9 -0

@@ -56,6 +56,7 @@
5656 dataFixups, the 64-bit one, from its chunk's start *)
5757 chunk*: INT; (* the chunk it refers to *)
5858 addend*: INT; (* added to the chunk's address *)
59+ abs*: BOOL; (* text: the address itself, not from the field's end *)
5960 end;
6061
6162 var
@@ -246,7 +247,15 @@
246247 fixups[numFixups].at := at;
247248 fixups[numFixups].chunk := chunk;
248249 fixups[numFixups].addend := addend;
250+ fixups[numFixups].abs := false;
249251 INC(numFixups);
252+end;
253+
254+(* The same, for X64.memAbs: the field holds the address itself *)
255+proc addAbsFixup*(at, chunk, addend: INT);
256+begin
257+ addFixup(at, chunk, addend);
258+ fixups[numFixups - 1].abs := true;
250259 end;
251260
252261 (* The address of chunk, plus addend, at offset at of rodata chunk c,

src/Gen.raz modified +9 -12

@@ -1640,23 +1640,20 @@
16401640 picks of the array at a. An element of 1, 2,
16411641 4 or 8 bytes is only noted, as the operand [base + index * size]
16421642 (lIdx): an array in the frame has rbp as its base, and one in a
1643- chunk, if the index is a variable's register, its address made in
1644- the value's own register. Any other is added up. *)
1643+ chunk its address as the displacement (X.memAbs), with no lea. Any
1644+ other is added up. *)
16451645 proc indexArray*(a: Adr; size: I64);
1646-var r, b: INT; o: I64;
1646+var r: INT; o: I64;
16471647 begin
1648- if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base = bFrame) then
1648+ if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and ((a.base = bFrame) or (a.base = bGlobal)) then
16491649 o := indexOff(size, a.off);
16501650 r := peek(depth - 1);
16511651 drop();
1652- newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false);
1653- elsif (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then
1654- o := indexOff(size, 0);
1655- r := peek(depth - 1);
1656- drop();
1657- b := target();
1658- X.lea(b, X.memRip(a.chunk, INT(a.off)));
1659- newMem(lIdx, X.memIdx(b, r, INT(size), o), kWord, 8, false);
1652+ if a.base = bFrame then
1653+ newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false);
1654+ else
1655+ newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false);
1656+ end;
16601657 else
16611658 mulI(top(), size);
16621659 pushAdr(a);

src/Link.raz (history) modified +4 -3

@@ -358,9 +358,10 @@
358358 COPY(textImg[p..p + Chunk.chunks[c].size], Chunk.text[Chunk.chunks[c].start..Chunk.chunks[c].start + Chunk.chunks[c].size]);
359359 for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do
360360 at := Chunk.fixups[f].at - Chunk.chunks[c].start;
361- (* rel32: from the end of the field to the target *)
362- v := Chunk.chunks[Chunk.fixups[f].chunk].adr + Chunk.fixups[f].addend
363- - (Chunk.chunks[c].adr + at + 4);
361+ (* rel32: from the end of the field to the target;
362+ or, X64.memAbs's, the target's address *)
363+ v := Chunk.chunks[Chunk.fixups[f].chunk].adr + Chunk.fixups[f].addend;
364+ if not Chunk.fixups[f].abs then v := v - (Chunk.chunks[c].adr + at + 4); end;
364365 patch4(textImg, p + at, v);
365366 end;
366367 elsif Chunk.chunks[c].sec = Chunk.secRodata then

src/X64.raz (history) modified +28 -4

@@ -35,7 +35,8 @@
3535
3636 type
3737 Mem* = record
38- base*: INT; (* a register, or RIP *)
38+ base*: INT; (* a register, or RIP; RIP with an index is the
39+ chunk's address itself, as below (memAbs) *)
3940 index*: INT; (* a register, or NoIndex *)
4041 scale*: INT; (* 1, 2, 4 or 8 *)
4142 disp*: INT;
@@ -80,6 +81,19 @@
8081 begin
8182 m.base := RIP; m.index := NoIndex; m.scale := 1;
8283 m.disp := disp; m.chunk := chunk;
84+ return m;
85+end;
86+
87+(* An element of the array in chunk, [chunk + index * scale + disp]. x86
88+ has no rip + index, but the executable is loaded where the linker put
89+ it, below 2 GB, so the chunk's address fits the 32 bits of a
90+ displacement with no base: one instruction, where rip-relative needs a
91+ lea of the chunk first. *)
92+proc memAbs*(chunk, index, scale, disp: INT): Mem;
93+var m: Mem;
94+begin
95+ m := memRip(chunk, disp);
96+ m.index := index; m.scale := scale;
8397 return m;
8498 end;
8599
@@ -130,7 +144,7 @@
130144 var md, b, sc: INT;
131145 begin
132146 byteReg := byteReg and (reg >= 4) and (reg <= 7);
133- if m.base = RIP then
147+ if (m.base = RIP) and (m.index = NoIndex) then
134148 prefix(pfx, w, reg, NoIndex, 0, byteReg);
135149 else
136150 prefix(pfx, w, reg, m.index, m.base, byteReg);
@@ -138,6 +152,16 @@
138152 opcode(op);
139153 reg := reg & 7;
140154 if m.base = RIP then
155+ if m.index # NoIndex then
156+ if m.scale = 1 then sc := 0; elsif m.scale = 2 then sc := 1; elsif m.scale = 4 then sc := 2; else sc := 3; end;
157+ (* memAbs: mod 00, r/m 100 for a SIB byte, whose base 101
158+ under mod 00 means none, but a disp32: the chunk's address *)
159+ Chunk.put1((reg shl 3) | 4);
160+ Chunk.put1((sc shl 6) | ((m.index & 7) shl 3) | 5);
161+ Chunk.addAbsFixup(Chunk.pc, m.chunk, m.disp);
162+ Chunk.put4(0);
163+ return;
164+ end;
141165 Chunk.put1((reg shl 3) | 5);
142166 (* mod 00 with r/m 101 is rip + disp32. The displacement counts
143167 from the end of the instruction, and the fixup takes it to end
@@ -345,7 +369,7 @@
345369 else
346370 n := 4;
347371 end;
348- if m.base = RIP then m.disp := m.disp - n; end;
372+ if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end;
349373 if size = 1 then
350374 opM(0, false, 80H, op, m, false);
351375 elsif n = 1 then
@@ -363,7 +387,7 @@
363387 if listing then say(text2("mov", mnSized(m, size), num(v))); end;
364388 n := size;
365389 if n > 4 then n := 4; end;
366- if m.base = RIP then m.disp := m.disp - n; end;
390+ if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end;
367391 if size = 1 then
368392 opM(0, false, 0C6H, 0, m, false);
369393 else