Skip to content

Utilize lowMemoryUnused #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,20 @@ export class Compiler extends DiagnosticEmitter {
super(program.diagnostics);
this.program = program;
var options = program.options;
this.memoryOffset = i64_new(
// leave space for `null`. also functions as a sentinel for erroneous stores at offset 0.
// note that Binaryen's asm.js output utilizes the first 8 bytes for reinterpretations (#1547)
max(options.memoryBase, 8)
);
var module = Module.create();
this.module = module;
if (options.memoryBase) {
this.memoryOffset = i64_new(options.memoryBase);
module.setLowMemoryUnused(false);
} else {
if (options.optimizeLevelHint >= 2) {
this.memoryOffset = i64_new(1024);
module.setLowMemoryUnused(true);
} else {
this.memoryOffset = i64_new(8);
module.setLowMemoryUnused(false);
}
}
var featureFlags: FeatureFlags = 0;
if (options.hasFeature(Feature.SIGN_EXTENSION)) featureFlags |= FeatureFlags.SignExt;
if (options.hasFeature(Feature.MUTABLE_GLOBALS)) featureFlags |= FeatureFlags.MutableGloabls;
Expand Down
49 changes: 25 additions & 24 deletions std/assembly/util/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ export function memset(dest: usize, c: u8, n: usize): void { // see: musl/src/st
} else {
// fill head and tail with minimal branching
if (!n) return;
let dend = dest + n - 4;
store<u8>(dest, c);
store<u8>(dest + n - 1, c);
store<u8>(dend, c, 3);
if (n <= 2) return;

store<u8>(dest + 1, c);
store<u8>(dest + 2, c);
store<u8>(dest + n - 2, c);
store<u8>(dest + n - 3, c);
store<u8>(dest, c, 1);
store<u8>(dest, c, 2);
store<u8>(dend, c, 2);
store<u8>(dend, c, 1);
if (n <= 6) return;
store<u8>(dest + 3, c);
store<u8>(dest + n - 4, c);
store<u8>(dest, c, 3);
store<u8>(dend, c);
if (n <= 8) return;

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

// fill head/tail up to 28 bytes each in preparation
dend = dest + n - 28;
store<u32>(dest, c32);
store<u32>(dest + n - 4, c32);
store<u32>(dend, c32, 24);
if (n <= 8) return;
store<u32>(dest + 4, c32);
store<u32>(dest + 8, c32);
store<u32>(dest + n - 12, c32);
store<u32>(dest + n - 8, c32);
store<u32>(dest, c32, 4);
store<u32>(dest, c32, 8);
store<u32>(dend, c32, 16);
store<u32>(dend, c32, 20);
if (n <= 24) return;
store<u32>(dest + 12, c32);
store<u32>(dest + 16, c32);
store<u32>(dest + 20, c32);
store<u32>(dest + 24, c32);
store<u32>(dest + n - 28, c32);
store<u32>(dest + n - 24, c32);
store<u32>(dest + n - 20, c32);
store<u32>(dest + n - 16, c32);
store<u32>(dest, c32, 12);
store<u32>(dest, c32, 16);
store<u32>(dest, c32, 20);
store<u32>(dest, c32, 24);
store<u32>(dend, c32);
store<u32>(dend, c32, 4);
store<u32>(dend, c32, 8);
store<u32>(dend, c32, 12);

// align to a multiple of 8
k = 24 + (dest & 4);
Expand All @@ -249,9 +250,9 @@ export function memset(dest: usize, c: u8, n: usize): void { // see: musl/src/st
let c64: u64 = <u64>c32 | (<u64>c32 << 32);
while (n >= 32) {
store<u64>(dest, c64);
store<u64>(dest + 8, c64);
store<u64>(dest + 16, c64);
store<u64>(dest + 24, c64);
store<u64>(dest, c64, 8);
store<u64>(dest, c64, 16);
store<u64>(dest, c64, 24);
n -= 32;
dest += 32;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/abi.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(memory $0 1)
(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")
(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")
(global $abi/condition (mut i32) (i32.const 0))
(global $abi/y (mut i32) (i32.const 0))
(export "memory" (memory $0))
Expand Down
40 changes: 20 additions & 20 deletions tests/compiler/assert-nonnull.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(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")
(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")
(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")
(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")
(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")
(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")
(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")
(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")
(table $0 1 funcref)
(global $~argumentsLength (mut i32) (i32.const 0))
(export "__setArgumentsLength" (func $~setArgumentsLength))
Expand All @@ -30,7 +30,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 2
i32.const 9
call $~lib/builtins/abort
Expand All @@ -43,7 +43,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 11
i32.const 9
call $~lib/builtins/abort
Expand All @@ -59,7 +59,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 15
i32.const 9
call $~lib/builtins/abort
Expand All @@ -72,7 +72,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 19
i32.const 9
call $~lib/builtins/abort
Expand All @@ -83,8 +83,8 @@
i32.load offset=12
i32.ge_u
if
i32.const 96
i32.const 160
i32.const 1104
i32.const 1168
i32.const 93
i32.const 41
call $~lib/builtins/abort
Expand All @@ -96,8 +96,8 @@
local.tee $0
i32.eqz
if
i32.const 208
i32.const 160
i32.const 1216
i32.const 1168
i32.const 97
i32.const 39
call $~lib/builtins/abort
Expand All @@ -111,8 +111,8 @@
i32.load offset=12
i32.ge_u
if
i32.const 96
i32.const 160
i32.const 1104
i32.const 1168
i32.const 93
i32.const 41
call $~lib/builtins/abort
Expand All @@ -129,7 +129,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 23
i32.const 9
call $~lib/builtins/abort
Expand Down Expand Up @@ -157,7 +157,7 @@
return
end
i32.const 0
i32.const 32
i32.const 1040
i32.const 27
i32.const 9
call $~lib/builtins/abort
Expand All @@ -183,7 +183,7 @@
return
end
i32.const 0
i32.const 32
i32.const 1040
i32.const 31
i32.const 9
call $~lib/builtins/abort
Expand All @@ -204,7 +204,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 39
i32.const 12
call $~lib/builtins/abort
Expand All @@ -226,7 +226,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 44
i32.const 9
call $~lib/builtins/abort
Expand All @@ -252,7 +252,7 @@
i32.eqz
if
i32.const 0
i32.const 32
i32.const 1040
i32.const 52
i32.const 9
call $~lib/builtins/abort
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/binary.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
(type $f32_=>_f32 (func (param f32) (result f32)))
(type $f64_=>_f64 (func (param f64) (result f64)))
(memory $0 1)
(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?")
(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?")
(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?")
(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?")
(global $binary/b (mut i32) (i32.const 0))
(global $binary/i (mut i32) (i32.const 0))
(global $binary/I (mut i64) (i64.const 0))
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/bool.optimized.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(memory $0 1)
(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")
(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")
(export "memory" (memory $0))
)
Loading