Skip to content

Commit

Permalink
Add --trap-mode=allow/clamp/js argument to asm2wasm and s2wasm (#1210)
Browse files Browse the repository at this point in the history
* Add --trap-mode=allow/clamp/js argument to asm2wasm and s2wasm

* Update asm2wasm and auto_update_tests scripts to use --trap-mode

* Throw std::invalid_argument instead of adding a new Invalid TrapMode type

* Remove legacy asm2wasm trap mode arguments
  • Loading branch information
jgravelle-google authored Oct 3, 2017
1 parent 1f8d8a5 commit 8283229
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions auto_update_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
cmd = ASM2WASM + [os.path.join('test', asm)]
wasm = asm.replace('.asm.js', '.fromasm')
if not precise:
cmd += ['--emit-potential-traps', '--ignore-implicit-traps']
cmd += ['--trap-mode=allow', '--ignore-implicit-traps']
wasm += '.imprecise'
elif precise == 2:
cmd += ['--emit-clamped-potential-traps']
cmd += ['--trap-mode=clamp']
wasm += '.clamp'
if not opts:
wasm += '.no-opts'
Expand Down
4 changes: 2 additions & 2 deletions scripts/test/asm2wasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def test_asm2wasm():
cmd = ASM2WASM + [os.path.join(options.binaryen_test, asm)]
wasm = asm.replace('.asm.js', '.fromasm')
if not precise:
cmd += ['--emit-potential-traps', '--ignore-implicit-traps']
cmd += ['--trap-mode=allow', '--ignore-implicit-traps']
wasm += '.imprecise'
elif precise == 2:
cmd += ['--emit-clamped-potential-traps']
cmd += ['--trap-mode=clamp']
wasm += '.clamp'
if not opts:
wasm += '.no-opts'
Expand Down
4 changes: 2 additions & 2 deletions scripts/test/s2wasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_s2wasm():

extension_arg_map = {
'.wast': [],
'.clamp.wast': ['--emit-clamped-potential-traps'],
'.js.wast': ['--emit-jsified-potential-traps'],
'.clamp.wast': ['--trap-mode=clamp'],
'.js.wast': ['--trap-mode=js'],
}
for dot_s_dir in ['dot_s', 'llvm_autogenerated']:
dot_s_path = os.path.join(options.binaryen_test, dot_s_dir)
Expand Down
16 changes: 16 additions & 0 deletions src/ast/trapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef wasm_ast_trapping_h
#define wasm_ast_trapping_h

#include <exception>

#include "pass.h"

namespace wasm {
Expand Down Expand Up @@ -95,6 +97,20 @@ class TrappingFunctionContainer {
Expression* makeTrappingBinary(Binary* curr, TrappingFunctionContainer &trappingFunctions);
Expression* makeTrappingUnary(Unary* curr, TrappingFunctionContainer &trappingFunctions);

inline TrapMode trapModeFromString(std::string const& str) {
if (str == "allow") {
return TrapMode::Allow;
} else if (str == "clamp") {
return TrapMode::Clamp;
} else if (str == "js") {
return TrapMode::JS;
} else {
throw std::invalid_argument(
"Unsupported trap mode \"" + str + "\". "
"Valid modes are \"allow\", \"js\", and \"clamp\"");
}
}

} // wasm

#endif // wasm_ast_trapping_h
28 changes: 13 additions & 15 deletions src/tools/asm2wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// asm2wasm console tool
//

#include <exception>

#include "ast/trapping.h"
#include "support/colors.h"
#include "support/command-line.h"
Expand Down Expand Up @@ -78,21 +80,17 @@ int main(int argc, const char *argv[]) {
[](Options *o, const std::string &) {
std::cerr << "--no-opts is deprecated (use -O0, etc.)\n";
})
.add("--emit-potential-traps", "-i", "Emit instructions that might trap, like div/rem of 0", Options::Arguments::Zero,
[&trapMode](Options *o, const std::string &) {
trapMode = TrapMode::Allow;
})
.add("--emit-clamped-potential-traps", "-i", "Clamp instructions that might trap, like float => int", Options::Arguments::Zero,
[&trapMode](Options *o, const std::string &) {
trapMode = TrapMode::Clamp;
})
.add("--emit-jsified-potential-traps", "-i", "Avoid instructions that might trap, handling them exactly like JS would", Options::Arguments::Zero,
[&trapMode](Options *o, const std::string &) {
trapMode = TrapMode::JS;
})
.add("--imprecise", "-i", "Imprecise optimizations (old name for --emit-potential-traps)", Options::Arguments::Zero,
[&trapMode](Options *o, const std::string &) {
trapMode = TrapMode::Allow;
.add("--trap-mode", "",
"Strategy for handling potentially trapping instructions. Valid "
"values are \"allow\", \"js\", and \"clamp\"",
Options::Arguments::One,
[&trapMode](Options *o, const std::string &argument) {
try {
trapMode = trapModeFromString(argument);
} catch (std::invalid_argument e) {
std::cerr << "Error: " << e.what() << "\n";
exit(EXIT_FAILURE);
}
})
.add("--wasm-only", "-w", "Input is in WebAssembly-only format, and not actually valid asm.js", Options::Arguments::Zero,
[&wasmOnly](Options *o, const std::string &) {
Expand Down
32 changes: 14 additions & 18 deletions src/tools/s2wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/

//
// wasm2asm console tool
// s2wasm console tool
//

#include <exception>

#include "ast/trapping.h"
#include "support/colors.h"
#include "support/command-line.h"
Expand Down Expand Up @@ -83,23 +85,17 @@ int main(int argc, const char *argv[]) {
[&allowMemoryGrowth](Options *, const std::string &) {
allowMemoryGrowth = true;
})
.add("--emit-potential-traps", "",
"Emit instructions that might trap, like div/rem of 0",
Options::Arguments::Zero,
[&trapMode](Options *o, const std::string &) {
trapMode = TrapMode::Allow;
})
.add("--emit-clamped-potential-traps", "",
"Clamp instructions that might trap, like float => int",
Options::Arguments::Zero,
[&trapMode](Options *o, const std::string &) {
trapMode = TrapMode::Clamp;
})
.add("--emit-jsified-potential-traps", "",
"Avoid instructions that might trap, handling them exactly like JS would",
Options::Arguments::Zero,
[&trapMode](Options *o, const std::string &) {
trapMode = TrapMode::JS;
.add("--trap-mode", "",
"Strategy for handling potentially trapping instructions. Valid "
"values are \"allow\", \"js\", and \"clamp\"",
Options::Arguments::One,
[&trapMode](Options *o, const std::string &argument) {
try {
trapMode = trapModeFromString(argument);
} catch (std::invalid_argument e) {
std::cerr << "Error: " << e.what() << "\n";
exit(EXIT_FAILURE);
}
})
.add("--emscripten-glue", "-e", "Generate emscripten glue",
Options::Arguments::Zero,
Expand Down

0 comments on commit 8283229

Please sign in to comment.