Skip to content

Improve test_js_optimizer. NFC #17407

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 1 commit into from
Jul 12, 2022
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ jobs:
other.test_bad_triple
other.test_closure_externs
other.test_binaryen_debug
other.test_js_optimizer_parse_error
other.test_js_optimizer*
other.test_output_to_nowhere
other.test_emcc_dev_null
other.test_cmake*
Expand Down
12 changes: 12 additions & 0 deletions tests/optimizer/AJSDCE-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,27 @@ function glue() {
}

glue();

null;

null;

null;

null;

null;

null;

null;

null;

null;

null;

new SomethingUnknownWithSideEffects("utf8");

new TextDecoder(Unknown());
43 changes: 43 additions & 0 deletions tests/optimizer/applyImportAndExportNameChanges2-output.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
var Module;

if (!Module) Module = "__EMSCRIPTEN_PRIVATE_MODULE_EXPORT_NAME_SUBSTITUTION__";

var ENVIRONMENT_IS_NODE = typeof process === "object";

if (ENVIRONMENT_IS_NODE) {
var fs = require("fs");
Module["wasm"] = fs.readFileSync(__dirname + "/a.wasm");
}

function out(text) {
console.log(text);
}

function err(text) {
console.error(text);
}

function ready() {
run();
}

function abort(what) {
throw what;
}

var UTF8Decoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf8") : undefined;

function UTF8ArrayToString(u8Array, idx, maxBytesToRead) {
var endIdx = idx + maxBytesToRead;
var endPtr = idx;
Expand Down Expand Up @@ -53,25 +62,40 @@ function UTF8ArrayToString(u8Array, idx, maxBytesToRead) {
}
return str;
}

function UTF8ToString(ptr, maxBytesToRead) {
return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead) : "";
}

var TOTAL_MEMORY = 16777216, STATIC_BASE = 1024, DYNAMICTOP_PTR = 6016;

var wasmMaximumMemory = TOTAL_MEMORY;

var wasmMemory = new WebAssembly.Memory({
"initial": TOTAL_MEMORY >> 16,
"maximum": wasmMaximumMemory >> 16
});

var buffer = wasmMemory.buffer;

var HEAP8 = new Int8Array(buffer);

var HEAP16 = new Int16Array(buffer);

var HEAP32 = new Int32Array(buffer);

var HEAPU8 = new Uint8Array(buffer);

var HEAPU16 = new Uint16Array(buffer);

var HEAPU32 = new Uint32Array(buffer);

var HEAPF32 = new Float32Array(buffer);

var HEAPF64 = new Float64Array(buffer);

HEAP32[DYNAMICTOP_PTR >> 2] = 5249152;

var SYSCALLS = {
buffers: [ null, [], [] ],
printChar: function(stream, curr) {
Expand Down Expand Up @@ -101,6 +125,7 @@ var SYSCALLS = {
SYSCALLS.get();
}
};

function ___syscall140(which, varargs) {
SYSCALLS.varargs = varargs;
try {
Expand All @@ -115,6 +140,7 @@ function ___syscall140(which, varargs) {
return -e.errno;
}
}

function ___syscall146(which, varargs) {
SYSCALLS.varargs = varargs;
try {
Expand All @@ -134,6 +160,7 @@ function ___syscall146(which, varargs) {
return -e.errno;
}
}

function ___syscall54(which, varargs) {
SYSCALLS.varargs = varargs;
try {
Expand All @@ -143,6 +170,7 @@ function ___syscall54(which, varargs) {
return -e.errno;
}
}

function ___syscall6(which, varargs) {
SYSCALLS.varargs = varargs;
try {
Expand All @@ -154,15 +182,19 @@ function ___syscall6(which, varargs) {
return -e.errno;
}
}

function _emscripten_get_now() {
abort();
}

function _emscripten_random() {
return Math.random();
}

function _emscripten_memcpy_big(dest, src, num) {
HEAPU8.set(HEAPU8.subarray(src, src + num), dest);
}

if (ENVIRONMENT_IS_NODE) {
_emscripten_get_now = function _emscripten_get_now_actual() {
var t = process["hrtime"]();
Expand All @@ -181,6 +213,7 @@ if (ENVIRONMENT_IS_NODE) {
} else {
_emscripten_get_now = Date.now;
}

var asmLibraryArg = {
"b": abort,
"h": ___syscall140,
Expand All @@ -191,21 +224,29 @@ var asmLibraryArg = {
"d": _emscripten_memcpy_big,
"c": _emscripten_random
};

function run() {
var ret = _main();
}

function initRuntime(asm) {
asm["i"]();
}

var env = asmLibraryArg;

env["memory"] = wasmMemory;

env["table"] = new WebAssembly.Table({
"initial": 6,
"maximum": 6,
"element": "anyfunc"
});

env["__memory_base"] = STATIC_BASE;

env["__table_base"] = 0;

var imports = {
"env": env,
"global": {
Expand All @@ -222,7 +263,9 @@ var imports = {
}
}
};

var ___errno_location, _llvm_bswap_i32, _main, _memcpy, _memset, dynCall_ii, dynCall_iiii;

WebAssembly.instantiate(Module["wasm"], imports).then(function(output) {
var asm = output.instance.exports;
___errno_location = asm["j"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ WebAssembly.instantiate(Module["wasm"], imports).then(function(output) {
expD1 = asm["expD1"];
expD2 = asm["expD2"];
expD3 = asm["expD3"];

initRuntime(asm);
ready();
});
Expand Down
1 change: 0 additions & 1 deletion tests/optimizer/standalone-emitDCEGraph-output.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[]

17 changes: 17 additions & 0 deletions tests/optimizer/test-LittleEndianHeap-output.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
a = HEAP8[x];

HEAP8[x] = a;

a = HEAPU8[x];

HEAPU8[x] = a;

a = LE_HEAP_LOAD_I16(x * 2);

LE_HEAP_STORE_I16(x * 2, a);

a = LE_HEAP_LOAD_U16(x * 2);

LE_HEAP_STORE_U16(x * 2, a);

a = LE_HEAP_LOAD_I32(x * 4);

LE_HEAP_STORE_I32(x * 4, a);

a = LE_HEAP_LOAD_U32(x * 4);

LE_HEAP_STORE_U32(x * 4, a);

a = LE_HEAP_LOAD_F32(x * 4);

LE_HEAP_STORE_F32(x * 4, a);

a = LE_HEAP_LOAD_F64(x * 8);

LE_HEAP_STORE_F64(x * 8, a);

HEAP[x];

HeAp[x];
13 changes: 8 additions & 5 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
from common import RunnerCore, path_from_root, is_slow_test, ensure_dir, disabled, make_executable
from common import env_modify, no_mac, no_windows, requires_native_clang, with_env_modify
from common import create_file, parameterized, NON_ZERO, node_pthreads, TEST_ROOT, test_file
from common import compiler_for, read_file, read_binary, EMBUILDER, requires_v8, requires_node
from common import compiler_for, EMBUILDER, requires_v8, requires_node
from common import also_with_minimal_runtime, also_with_wasm_bigint, EMTEST_BUILD_VERBOSE, PYTHON
from tools import shared, building, utils, deps_info, response_file
from tools.utils import read_file, write_file, read_binary
import common
import jsrun
import clang_native
Expand Down Expand Up @@ -2306,12 +2307,14 @@ def test_extern_prepost(self):
})
def test_js_optimizer(self, input, passes):
input = test_file(input)
expected = os.path.splitext(input)[0] + '-output.js'
expected = read_file(expected).replace('\n\n', '\n')

expected_file = os.path.splitext(input)[0] + '-output.js'
# test calling optimizer
js = self.run_process(config.NODE_JS + [path_from_root('tools/acorn-optimizer.js'), input] + passes, stdin=PIPE, stdout=PIPE).stdout
self.assertIdentical(expected, js.replace('\r\n', '\n').replace('\n\n', '\n').replace('\n\n', '\n'))
if common.EMTEST_REBASELINE:
write_file(expected_file, js)
else:
expected = read_file(expected_file)
self.assertIdentical(expected, js)

@parameterized({
'wasm2js': ('wasm2js', ['minifyNames', 'last']),
Expand Down