From a9f91b9774d117a13c231ef0f40861372456878f Mon Sep 17 00:00:00 2001 From: jgravelle-google Date: Mon, 2 Oct 2017 13:51:55 -0700 Subject: Share trap mode between asm2wasm and s2wasm (#1168) * Extract Asm2WasmBuilder::TrapMode to shared FloatTrapMode * Extract makeTrappingI32Binary * Extract makeTrappingI64Binary * Extract asm2wasm test script into scripts/test/asm2wasm.py This matches s2wasm.py, and makes iterating on asm2wasm slightly faster. * Simplify callsites with an arg struct * Combine func adding across i32 and i64 * Support f32-to-int in asm2wasm * Add BinaryenTrapMode pass, run pass from s2wasm * BinaryenTrapMode pass takes trap context as a parameter * Pass fully supports non-trapping binary ops * Defer adding functions until after iteration (hackily) * Update asm2wasm to work with deferred function adding, rebuild tests * Extract makeTrappingFloatToInt32 * Extract makeTrappingFloatToInt64 * Add unary conversions to trap pass * Add functions in the pass itself * Set s2wasm trap mode with command-line arguments * Print BINARYEN_PASS_DEBUG state when testing * Get asm2wasm using the BinaryenTrapMode pass instead of handling it inline * Also handle f32 to int in asm2wasm * Make BinaryenTrapMode only need a FloatTrapMode from the caller * Just pass the current binary Expression directly * Combine makeTrappingI32Binary with makeTrappingI64Binary * Pass Unary expr to makeTrappingFloatToInt32 * Unify makeTrappingFloatToInt32 & 64 * Move makeTrapping* functions inside BinaryenTrapMode, make addedFunctions non-static * Remove FloatTrapContext * Minor cleanups * Extract some smaller subfunctions * Emit name switch/casing, rename is32Bit to isI64 for consistency * Rename BinaryenTrapMode to FloatTrap, make trap mode a nested enum * Add some comments explaining why FloatTrap is non-parallel * Rename addedFunctions to generatedFunctions for precision * Rename move and split float-clamp.h to passes/FloatTrap.(h|cpp) * Use builder instead of allocator * Instantiate trap handling passes via the pass manager * Move passes/FloatTrap.h to ast/trapping.h * Add helper function to add trap-handling passes * Add trap mode pass tests * Rename FloatTrap.cpp to TrapMode.cpp * Add s2wasm trap mode tests. Force float->int conversion to be signed * Add trapping_sint_div_s test to unit.asm.js * Fix flake8 issues with test scripts * Update pass description comment * Extract building functions methods * Make generate functions into top-level functions * Add GeneratedTrappingFunctions class to manage function/import additions * Move ensure/makeTrapping functions outside class scope * Use GeneratedTrappingFunctions to add immediately in asm2wasm mode * Remove trapping_sint_div_s test We only added it to test that trapping divisions would get constant-folded at the correct time. Now that we're not changing the timing of trapping modes, the test is unneeded (and problematic). * Review feedback, add validator/*.wasm to .gitignore * Add support for unsigned float-to-int conversion * Use opcode directly instead of bools * Update s2wasm clamp test for unsigned ftoi --- scripts/test/s2wasm.py | 62 +++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 26 deletions(-) (limited to 'scripts/test/s2wasm.py') diff --git a/scripts/test/s2wasm.py b/scripts/test/s2wasm.py index 3f38acbac..fcba17d18 100755 --- a/scripts/test/s2wasm.py +++ b/scripts/test/s2wasm.py @@ -32,38 +32,48 @@ def test_s2wasm(): fail_if_not_contained( output, '(import "env" "memory" (memory $0 1))') + extension_arg_map = { + '.wast': [], + '.clamp.wast': ['--emit-clamped-potential-traps'], + '.js.wast': ['--emit-jsified-potential-traps'], + } for dot_s_dir in ['dot_s', 'llvm_autogenerated']: dot_s_path = os.path.join(options.binaryen_test, dot_s_dir) for s in sorted(os.listdir(dot_s_path)): if not s.endswith('.s'): continue print '..', s - wasm = s.replace('.s', '.wast') - full = os.path.join(options.binaryen_test, dot_s_dir, s) - stack_alloc = (['--allocate-stack=1024'] - if dot_s_dir == 'llvm_autogenerated' - else []) - cmd = S2WASM + [full, '--emscripten-glue'] + stack_alloc - if s.startswith('start_'): - cmd.append('--start') - actual = run_command(cmd) - - # verify output - expected_file = os.path.join(options.binaryen_test, dot_s_dir, wasm) - if not os.path.exists(expected_file): - print actual - fail_with_error('output ' + expected_file + ' does not exist') - expected = open(expected_file, 'rb').read() - if actual != expected: - fail(actual, expected) - - # verify with options - cmd = S2WASM + [full, '--global-base=1024'] + stack_alloc - run_command(cmd) - - # run wasm-shell on the .wast to verify that it parses - cmd = WASM_SHELL + [expected_file] - run_command(cmd) + for ext, ext_args in extension_arg_map.iteritems(): + wasm = s.replace('.s', ext) + expected_file = os.path.join(options.binaryen_test, dot_s_dir, wasm) + expected_exists = os.path.exists(expected_file) + if ext != '.wast' and not expected_exists: + continue + + full = os.path.join(options.binaryen_test, dot_s_dir, s) + stack_alloc = (['--allocate-stack=1024'] + if dot_s_dir == 'llvm_autogenerated' + else []) + cmd = S2WASM + [full, '--emscripten-glue'] + stack_alloc + ext_args + if s.startswith('start_'): + cmd.append('--start') + actual = run_command(cmd) + + # verify output + if not expected_exists: + print actual + fail_with_error('output ' + expected_file + ' does not exist') + expected = open(expected_file, 'rb').read() + if actual != expected: + fail(actual, expected) + + # verify with options + cmd = S2WASM + [full, '--global-base=1024'] + stack_alloc + run_command(cmd) + + # run wasm-shell on the .wast to verify that it parses + cmd = WASM_SHELL + [expected_file] + run_command(cmd) def test_linker(): -- cgit v1.2.3