Skip to content

Commit b7df27c

Browse files
authored
Utilize lowMemoryUnused (#1103)
1 parent c771461 commit b7df27c

File tree

118 files changed

+14584
-16004
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+14584
-16004
lines changed

src/compiler.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,20 @@ export class Compiler extends DiagnosticEmitter {
355355
super(program.diagnostics);
356356
this.program = program;
357357
var options = program.options;
358-
this.memoryOffset = i64_new(
359-
// leave space for `null`. also functions as a sentinel for erroneous stores at offset 0.
360-
// note that Binaryen's asm.js output utilizes the first 8 bytes for reinterpretations (#1547)
361-
max(options.memoryBase, 8)
362-
);
363358
var module = Module.create();
364359
this.module = module;
360+
if (options.memoryBase) {
361+
this.memoryOffset = i64_new(options.memoryBase);
362+
module.setLowMemoryUnused(false);
363+
} else {
364+
if (options.optimizeLevelHint >= 2) {
365+
this.memoryOffset = i64_new(1024);
366+
module.setLowMemoryUnused(true);
367+
} else {
368+
this.memoryOffset = i64_new(8);
369+
module.setLowMemoryUnused(false);
370+
}
371+
}
365372
var featureFlags: FeatureFlags = 0;
366373
if (options.hasFeature(Feature.SIGN_EXTENSION)) featureFlags |= FeatureFlags.SignExt;
367374
if (options.hasFeature(Feature.MUTABLE_GLOBALS)) featureFlags |= FeatureFlags.MutableGloabls;

std/assembly/util/memory.ts

+25-24
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,17 @@ export function memset(dest: usize, c: u8, n: usize): void { // see: musl/src/st
201201
} else {
202202
// fill head and tail with minimal branching
203203
if (!n) return;
204+
let dend = dest + n - 4;
204205
store<u8>(dest, c);
205-
store<u8>(dest + n - 1, c);
206+
store<u8>(dend, c, 3);
206207
if (n <= 2) return;
207-
208-
store<u8>(dest + 1, c);
209-
store<u8>(dest + 2, c);
210-
store<u8>(dest + n - 2, c);
211-
store<u8>(dest + n - 3, c);
208+
store<u8>(dest, c, 1);
209+
store<u8>(dest, c, 2);
210+
store<u8>(dend, c, 2);
211+
store<u8>(dend, c, 1);
212212
if (n <= 6) return;
213-
store<u8>(dest + 3, c);
214-
store<u8>(dest + n - 4, c);
213+
store<u8>(dest, c, 3);
214+
store<u8>(dend, c);
215215
if (n <= 8) return;
216216

217217
// advance pointer to align it at 4-byte boundary
@@ -223,22 +223,23 @@ export function memset(dest: usize, c: u8, n: usize): void { // see: musl/src/st
223223
let c32: u32 = <u32>-1 / 255 * c;
224224

225225
// fill head/tail up to 28 bytes each in preparation
226+
dend = dest + n - 28;
226227
store<u32>(dest, c32);
227-
store<u32>(dest + n - 4, c32);
228+
store<u32>(dend, c32, 24);
228229
if (n <= 8) return;
229-
store<u32>(dest + 4, c32);
230-
store<u32>(dest + 8, c32);
231-
store<u32>(dest + n - 12, c32);
232-
store<u32>(dest + n - 8, c32);
230+
store<u32>(dest, c32, 4);
231+
store<u32>(dest, c32, 8);
232+
store<u32>(dend, c32, 16);
233+
store<u32>(dend, c32, 20);
233234
if (n <= 24) return;
234-
store<u32>(dest + 12, c32);
235-
store<u32>(dest + 16, c32);
236-
store<u32>(dest + 20, c32);
237-
store<u32>(dest + 24, c32);
238-
store<u32>(dest + n - 28, c32);
239-
store<u32>(dest + n - 24, c32);
240-
store<u32>(dest + n - 20, c32);
241-
store<u32>(dest + n - 16, c32);
235+
store<u32>(dest, c32, 12);
236+
store<u32>(dest, c32, 16);
237+
store<u32>(dest, c32, 20);
238+
store<u32>(dest, c32, 24);
239+
store<u32>(dend, c32);
240+
store<u32>(dend, c32, 4);
241+
store<u32>(dend, c32, 8);
242+
store<u32>(dend, c32, 12);
242243

243244
// align to a multiple of 8
244245
k = 24 + (dest & 4);
@@ -249,9 +250,9 @@ export function memset(dest: usize, c: u8, n: usize): void { // see: musl/src/st
249250
let c64: u64 = <u64>c32 | (<u64>c32 << 32);
250251
while (n >= 32) {
251252
store<u64>(dest, c64);
252-
store<u64>(dest + 8, c64);
253-
store<u64>(dest + 16, c64);
254-
store<u64>(dest + 24, c64);
253+
store<u64>(dest, c64, 8);
254+
store<u64>(dest, c64, 16);
255+
store<u64>(dest, c64, 24);
255256
n -= 32;
256257
dest += 32;
257258
}

tests/compiler/abi.optimized.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
(type $none_=>_none (func))
33
(type $none_=>_i32 (func (result i32)))
44
(memory $0 1)
5-
(data (i32.const 16) "\0c\00\00\00\01\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s")
5+
(data (i32.const 1024) "\0c\00\00\00\01\00\00\00\01\00\00\00\0c\00\00\00a\00b\00i\00.\00t\00s")
66
(global $abi/condition (mut i32) (i32.const 0))
77
(global $abi/y (mut i32) (i32.const 0))
88
(export "memory" (memory $0))

tests/compiler/assert-nonnull.optimized.wat

+20-20
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
66
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
77
(memory $0 1)
8-
(data (i32.const 16) "\"\00\00\00\01\00\00\00\01\00\00\00\"\00\00\00a\00s\00s\00e\00r\00t\00-\00n\00o\00n\00n\00u\00l\00l\00.\00t\00s")
9-
(data (i32.const 80) "$\00\00\00\01\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
10-
(data (i32.const 144) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
11-
(data (i32.const 192) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
8+
(data (i32.const 1024) "\"\00\00\00\01\00\00\00\01\00\00\00\"\00\00\00a\00s\00s\00e\00r\00t\00-\00n\00o\00n\00n\00u\00l\00l\00.\00t\00s")
9+
(data (i32.const 1088) "$\00\00\00\01\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e")
10+
(data (i32.const 1152) "\1a\00\00\00\01\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
11+
(data (i32.const 1200) "^\00\00\00\01\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
1212
(table $0 1 funcref)
1313
(global $~argumentsLength (mut i32) (i32.const 0))
1414
(export "__setArgumentsLength" (func $~setArgumentsLength))
@@ -30,7 +30,7 @@
3030
i32.eqz
3131
if
3232
i32.const 0
33-
i32.const 32
33+
i32.const 1040
3434
i32.const 2
3535
i32.const 9
3636
call $~lib/builtins/abort
@@ -43,7 +43,7 @@
4343
i32.eqz
4444
if
4545
i32.const 0
46-
i32.const 32
46+
i32.const 1040
4747
i32.const 11
4848
i32.const 9
4949
call $~lib/builtins/abort
@@ -59,7 +59,7 @@
5959
i32.eqz
6060
if
6161
i32.const 0
62-
i32.const 32
62+
i32.const 1040
6363
i32.const 15
6464
i32.const 9
6565
call $~lib/builtins/abort
@@ -72,7 +72,7 @@
7272
i32.eqz
7373
if
7474
i32.const 0
75-
i32.const 32
75+
i32.const 1040
7676
i32.const 19
7777
i32.const 9
7878
call $~lib/builtins/abort
@@ -83,8 +83,8 @@
8383
i32.load offset=12
8484
i32.ge_u
8585
if
86-
i32.const 96
87-
i32.const 160
86+
i32.const 1104
87+
i32.const 1168
8888
i32.const 93
8989
i32.const 41
9090
call $~lib/builtins/abort
@@ -96,8 +96,8 @@
9696
local.tee $0
9797
i32.eqz
9898
if
99-
i32.const 208
100-
i32.const 160
99+
i32.const 1216
100+
i32.const 1168
101101
i32.const 97
102102
i32.const 39
103103
call $~lib/builtins/abort
@@ -111,8 +111,8 @@
111111
i32.load offset=12
112112
i32.ge_u
113113
if
114-
i32.const 96
115-
i32.const 160
114+
i32.const 1104
115+
i32.const 1168
116116
i32.const 93
117117
i32.const 41
118118
call $~lib/builtins/abort
@@ -129,7 +129,7 @@
129129
i32.eqz
130130
if
131131
i32.const 0
132-
i32.const 32
132+
i32.const 1040
133133
i32.const 23
134134
i32.const 9
135135
call $~lib/builtins/abort
@@ -157,7 +157,7 @@
157157
return
158158
end
159159
i32.const 0
160-
i32.const 32
160+
i32.const 1040
161161
i32.const 27
162162
i32.const 9
163163
call $~lib/builtins/abort
@@ -183,7 +183,7 @@
183183
return
184184
end
185185
i32.const 0
186-
i32.const 32
186+
i32.const 1040
187187
i32.const 31
188188
i32.const 9
189189
call $~lib/builtins/abort
@@ -204,7 +204,7 @@
204204
i32.eqz
205205
if
206206
i32.const 0
207-
i32.const 32
207+
i32.const 1040
208208
i32.const 39
209209
i32.const 12
210210
call $~lib/builtins/abort
@@ -226,7 +226,7 @@
226226
i32.eqz
227227
if
228228
i32.const 0
229-
i32.const 32
229+
i32.const 1040
230230
i32.const 44
231231
i32.const 9
232232
call $~lib/builtins/abort
@@ -252,7 +252,7 @@
252252
i32.eqz
253253
if
254254
i32.const 0
255-
i32.const 32
255+
i32.const 1040
256256
i32.const 52
257257
i32.const 9
258258
call $~lib/builtins/abort

tests/compiler/binary.optimized.wat

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
(type $f32_=>_f32 (func (param f32) (result f32)))
44
(type $f64_=>_f64 (func (param f64) (result f64)))
55
(memory $0 1)
6-
(data (i32.const 17) "\01\00\00\01\00\00\00\03\00\00\00\00\01\00\00\be\f3\f8y\eca\f6?\190\96[\c6\fe\de\bf=\88\afJ\edq\f5?\a4\fc\d42h\0b\db\bf\b0\10\f0\f09\95\f4?{\b7\1f\n\8bA\d7\bf\85\03\b8\b0\95\c9\f3?{\cfm\1a\e9\9d\d3\bf\a5d\88\0c\19\0d\f3?1\b6\f2\f3\9b\1d\d0\bf\a0\8e\0b{\"^\f2?\f0z;\1b\1d|\c9\bf?4\1aJJ\bb\f1?\9f<\af\93\e3\f9\c2\bf\ba\e5\8a\f0X#\f1?\\\8dx\bf\cb`\b9\bf\a7\00\99A?\95\f0?\ce_G\b6\9do\aa\bf\00\00\00\00\00\00\f0?\00\00\00\00\00\00\00\00\acG\9a\fd\8c`\ee?=\f5$\9f\ca8\b3?\a0j\02\1f\b3\a4\ec?\ba\918T\a9v\c4?\e6\fcjW6 \eb?\d2\e4\c4J\0b\84\ce?-\aa\a1c\d1\c2\e9?\1ce\c6\f0E\06\d4?\edAx\03\e6\86\e8?\f8\9f\1b,\9c\8e\d8?bHS\f5\dcg\e7?\cc{\b1N\a4\e0\dc?")
7-
(data (i32.const 289) "\01\00\00\01\00\00\00\04\00\00\00\00\01\00\00\00\00\00\00\00\00\f0?t\85\15\d3\b0\d9\ef?\0f\89\f9lX\b5\ef?Q[\12\d0\01\93\ef?{Q}<\b8r\ef?\aa\b9h1\87T\ef?8bunz8\ef?\e1\de\1f\f5\9d\1e\ef?\15\b71\n\fe\06\ef?\cb\a9:7\a7\f1\ee?\"4\12L\a6\de\ee?-\89a`\08\ce\ee?\'*6\d5\da\bf\ee?\82O\9dV+\b4\ee?)TH\dd\07\ab\ee?\85U:\b0~\a4\ee?\cd;\7ff\9e\a0\ee?t_\ec\e8u\9f\ee?\87\01\ebs\14\a1\ee?\13\ceL\99\89\a5\ee?\db\a0*B\e5\ac\ee?\e5\c5\cd\b07\b7\ee?\90\f0\a3\82\91\c4\ee?]%>\b2\03\d5\ee?\ad\d3Z\99\9f\e8\ee?G^\fb\f2v\ff\ee?\9cR\85\dd\9b\19\ef?i\90\ef\dc 7\ef?\87\a4\fb\dc\18X\ef?_\9b{3\97|\ef?\da\90\a4\a2\af\a4\ef?@En[v\d0\ef?")
6+
(data (i32.const 1025) "\01\00\00\01\00\00\00\03\00\00\00\00\01\00\00\be\f3\f8y\eca\f6?\190\96[\c6\fe\de\bf=\88\afJ\edq\f5?\a4\fc\d42h\0b\db\bf\b0\10\f0\f09\95\f4?{\b7\1f\n\8bA\d7\bf\85\03\b8\b0\95\c9\f3?{\cfm\1a\e9\9d\d3\bf\a5d\88\0c\19\0d\f3?1\b6\f2\f3\9b\1d\d0\bf\a0\8e\0b{\"^\f2?\f0z;\1b\1d|\c9\bf?4\1aJJ\bb\f1?\9f<\af\93\e3\f9\c2\bf\ba\e5\8a\f0X#\f1?\\\8dx\bf\cb`\b9\bf\a7\00\99A?\95\f0?\ce_G\b6\9do\aa\bf\00\00\00\00\00\00\f0?\00\00\00\00\00\00\00\00\acG\9a\fd\8c`\ee?=\f5$\9f\ca8\b3?\a0j\02\1f\b3\a4\ec?\ba\918T\a9v\c4?\e6\fcjW6 \eb?\d2\e4\c4J\0b\84\ce?-\aa\a1c\d1\c2\e9?\1ce\c6\f0E\06\d4?\edAx\03\e6\86\e8?\f8\9f\1b,\9c\8e\d8?bHS\f5\dcg\e7?\cc{\b1N\a4\e0\dc?")
7+
(data (i32.const 1297) "\01\00\00\01\00\00\00\04\00\00\00\00\01\00\00\00\00\00\00\00\00\f0?t\85\15\d3\b0\d9\ef?\0f\89\f9lX\b5\ef?Q[\12\d0\01\93\ef?{Q}<\b8r\ef?\aa\b9h1\87T\ef?8bunz8\ef?\e1\de\1f\f5\9d\1e\ef?\15\b71\n\fe\06\ef?\cb\a9:7\a7\f1\ee?\"4\12L\a6\de\ee?-\89a`\08\ce\ee?\'*6\d5\da\bf\ee?\82O\9dV+\b4\ee?)TH\dd\07\ab\ee?\85U:\b0~\a4\ee?\cd;\7ff\9e\a0\ee?t_\ec\e8u\9f\ee?\87\01\ebs\14\a1\ee?\13\ceL\99\89\a5\ee?\db\a0*B\e5\ac\ee?\e5\c5\cd\b07\b7\ee?\90\f0\a3\82\91\c4\ee?]%>\b2\03\d5\ee?\ad\d3Z\99\9f\e8\ee?G^\fb\f2v\ff\ee?\9cR\85\dd\9b\19\ef?i\90\ef\dc 7\ef?\87\a4\fb\dc\18X\ef?_\9b{3\97|\ef?\da\90\a4\a2\af\a4\ef?@En[v\d0\ef?")
88
(global $binary/b (mut i32) (i32.const 0))
99
(global $binary/i (mut i32) (i32.const 0))
1010
(global $binary/I (mut i64) (i64.const 0))

tests/compiler/bool.optimized.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(module
22
(memory $0 1)
3-
(data (i32.const 16) "\0e\00\00\00\01\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s")
3+
(data (i32.const 1024) "\0e\00\00\00\01\00\00\00\01\00\00\00\0e\00\00\00b\00o\00o\00l\00.\00t\00s")
44
(export "memory" (memory $0))
55
)

0 commit comments

Comments
 (0)