Skip to content

Commit 8283229

Browse files
Add --trap-mode=allow/clamp/js argument to asm2wasm and s2wasm (#1210)
* 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
1 parent 1f8d8a5 commit 8283229

File tree

6 files changed

+49
-39
lines changed

6 files changed

+49
-39
lines changed

auto_update_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
cmd = ASM2WASM + [os.path.join('test', asm)]
2020
wasm = asm.replace('.asm.js', '.fromasm')
2121
if not precise:
22-
cmd += ['--emit-potential-traps', '--ignore-implicit-traps']
22+
cmd += ['--trap-mode=allow', '--ignore-implicit-traps']
2323
wasm += '.imprecise'
2424
elif precise == 2:
25-
cmd += ['--emit-clamped-potential-traps']
25+
cmd += ['--trap-mode=clamp']
2626
wasm += '.clamp'
2727
if not opts:
2828
wasm += '.no-opts'

scripts/test/asm2wasm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def test_asm2wasm():
3535
cmd = ASM2WASM + [os.path.join(options.binaryen_test, asm)]
3636
wasm = asm.replace('.asm.js', '.fromasm')
3737
if not precise:
38-
cmd += ['--emit-potential-traps', '--ignore-implicit-traps']
38+
cmd += ['--trap-mode=allow', '--ignore-implicit-traps']
3939
wasm += '.imprecise'
4040
elif precise == 2:
41-
cmd += ['--emit-clamped-potential-traps']
41+
cmd += ['--trap-mode=clamp']
4242
wasm += '.clamp'
4343
if not opts:
4444
wasm += '.no-opts'

scripts/test/s2wasm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def test_s2wasm():
3434

3535
extension_arg_map = {
3636
'.wast': [],
37-
'.clamp.wast': ['--emit-clamped-potential-traps'],
38-
'.js.wast': ['--emit-jsified-potential-traps'],
37+
'.clamp.wast': ['--trap-mode=clamp'],
38+
'.js.wast': ['--trap-mode=js'],
3939
}
4040
for dot_s_dir in ['dot_s', 'llvm_autogenerated']:
4141
dot_s_path = os.path.join(options.binaryen_test, dot_s_dir)

src/ast/trapping.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef wasm_ast_trapping_h
1818
#define wasm_ast_trapping_h
1919

20+
#include <exception>
21+
2022
#include "pass.h"
2123

2224
namespace wasm {
@@ -95,6 +97,20 @@ class TrappingFunctionContainer {
9597
Expression* makeTrappingBinary(Binary* curr, TrappingFunctionContainer &trappingFunctions);
9698
Expression* makeTrappingUnary(Unary* curr, TrappingFunctionContainer &trappingFunctions);
9799

100+
inline TrapMode trapModeFromString(std::string const& str) {
101+
if (str == "allow") {
102+
return TrapMode::Allow;
103+
} else if (str == "clamp") {
104+
return TrapMode::Clamp;
105+
} else if (str == "js") {
106+
return TrapMode::JS;
107+
} else {
108+
throw std::invalid_argument(
109+
"Unsupported trap mode \"" + str + "\". "
110+
"Valid modes are \"allow\", \"js\", and \"clamp\"");
111+
}
112+
}
113+
98114
} // wasm
99115

100116
#endif // wasm_ast_trapping_h

src/tools/asm2wasm.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
// asm2wasm console tool
1919
//
2020

21+
#include <exception>
22+
2123
#include "ast/trapping.h"
2224
#include "support/colors.h"
2325
#include "support/command-line.h"
@@ -78,21 +80,17 @@ int main(int argc, const char *argv[]) {
7880
[](Options *o, const std::string &) {
7981
std::cerr << "--no-opts is deprecated (use -O0, etc.)\n";
8082
})
81-
.add("--emit-potential-traps", "-i", "Emit instructions that might trap, like div/rem of 0", Options::Arguments::Zero,
82-
[&trapMode](Options *o, const std::string &) {
83-
trapMode = TrapMode::Allow;
84-
})
85-
.add("--emit-clamped-potential-traps", "-i", "Clamp instructions that might trap, like float => int", Options::Arguments::Zero,
86-
[&trapMode](Options *o, const std::string &) {
87-
trapMode = TrapMode::Clamp;
88-
})
89-
.add("--emit-jsified-potential-traps", "-i", "Avoid instructions that might trap, handling them exactly like JS would", Options::Arguments::Zero,
90-
[&trapMode](Options *o, const std::string &) {
91-
trapMode = TrapMode::JS;
92-
})
93-
.add("--imprecise", "-i", "Imprecise optimizations (old name for --emit-potential-traps)", Options::Arguments::Zero,
94-
[&trapMode](Options *o, const std::string &) {
95-
trapMode = TrapMode::Allow;
83+
.add("--trap-mode", "",
84+
"Strategy for handling potentially trapping instructions. Valid "
85+
"values are \"allow\", \"js\", and \"clamp\"",
86+
Options::Arguments::One,
87+
[&trapMode](Options *o, const std::string &argument) {
88+
try {
89+
trapMode = trapModeFromString(argument);
90+
} catch (std::invalid_argument e) {
91+
std::cerr << "Error: " << e.what() << "\n";
92+
exit(EXIT_FAILURE);
93+
}
9694
})
9795
.add("--wasm-only", "-w", "Input is in WebAssembly-only format, and not actually valid asm.js", Options::Arguments::Zero,
9896
[&wasmOnly](Options *o, const std::string &) {

src/tools/s2wasm.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616

1717
//
18-
// wasm2asm console tool
18+
// s2wasm console tool
1919
//
2020

21+
#include <exception>
22+
2123
#include "ast/trapping.h"
2224
#include "support/colors.h"
2325
#include "support/command-line.h"
@@ -83,23 +85,17 @@ int main(int argc, const char *argv[]) {
8385
[&allowMemoryGrowth](Options *, const std::string &) {
8486
allowMemoryGrowth = true;
8587
})
86-
.add("--emit-potential-traps", "",
87-
"Emit instructions that might trap, like div/rem of 0",
88-
Options::Arguments::Zero,
89-
[&trapMode](Options *o, const std::string &) {
90-
trapMode = TrapMode::Allow;
91-
})
92-
.add("--emit-clamped-potential-traps", "",
93-
"Clamp instructions that might trap, like float => int",
94-
Options::Arguments::Zero,
95-
[&trapMode](Options *o, const std::string &) {
96-
trapMode = TrapMode::Clamp;
97-
})
98-
.add("--emit-jsified-potential-traps", "",
99-
"Avoid instructions that might trap, handling them exactly like JS would",
100-
Options::Arguments::Zero,
101-
[&trapMode](Options *o, const std::string &) {
102-
trapMode = TrapMode::JS;
88+
.add("--trap-mode", "",
89+
"Strategy for handling potentially trapping instructions. Valid "
90+
"values are \"allow\", \"js\", and \"clamp\"",
91+
Options::Arguments::One,
92+
[&trapMode](Options *o, const std::string &argument) {
93+
try {
94+
trapMode = trapModeFromString(argument);
95+
} catch (std::invalid_argument e) {
96+
std::cerr << "Error: " << e.what() << "\n";
97+
exit(EXIT_FAILURE);
98+
}
10399
})
104100
.add("--emscripten-glue", "-e", "Generate emscripten glue",
105101
Options::Arguments::Zero,

0 commit comments

Comments
 (0)