Skip to content

Commit 3c27bc8

Browse files
authored
Add feature flags and validation to wasm-metadce (#2364)
Sometimes wasm-metadce is the last tool to run over a binary in Emscripten, and in that case it needs to know what features are enabled in order to emit a valid binary. For example it needs to know whether to emit a data count section.
1 parent 3bba195 commit 3c27bc8

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

auto_update_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def update_metadce_tests():
305305
print('..', t)
306306
t = os.path.join(options.binaryen_test, 'metadce', t)
307307
graph = t + '.graph.txt'
308-
cmd = WASM_METADCE + [t, '--graph-file=' + graph, '-o', 'a.wast', '-S']
308+
cmd = WASM_METADCE + [t, '--graph-file=' + graph, '-o', 'a.wast', '-S', '-all']
309309
stdout = run_command(cmd)
310310
actual = open('a.wast').read()
311311
out = t + '.dced'

check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def run_wasm_metadce_tests():
264264
print('..', t)
265265
t = os.path.join(test_dir, t)
266266
graph = t + '.graph.txt'
267-
cmd = WASM_METADCE + [t, '--graph-file=' + graph, '-o', 'a.wast', '-S']
267+
cmd = WASM_METADCE + [t, '--graph-file=' + graph, '-o', 'a.wast', '-S', '-all']
268268
stdout = run_command(cmd)
269269
expected = t + '.dced'
270270
with open('a.wast') as seen:

src/tools/wasm-metadce.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
#include "ir/module-utils.h"
3030
#include "pass.h"
3131
#include "support/colors.h"
32-
#include "support/command-line.h"
3332
#include "support/file.h"
3433
#include "support/json.h"
34+
#include "tool-options.h"
3535
#include "wasm-builder.h"
3636
#include "wasm-io.h"
37+
#include "wasm-validator.h"
3738

3839
using namespace wasm;
3940

@@ -403,7 +404,7 @@ int main(int argc, const char* argv[]) {
403404
std::string graphFile;
404405
bool dump = false;
405406

406-
Options options(
407+
ToolOptions options(
407408
"wasm-metadce",
408409
"This tool performs dead code elimination (DCE) on a larger space "
409410
"that the wasm module is just a part of. For example, if you have "
@@ -508,6 +509,15 @@ int main(int argc, const char* argv[]) {
508509
}
509510
}
510511

512+
options.applyFeatures(wasm);
513+
514+
if (options.passOptions.validate) {
515+
if (!WasmValidator().validate(wasm)) {
516+
WasmPrinter::printModule(&wasm);
517+
Fatal() << "error in validating input";
518+
}
519+
}
520+
511521
auto graphInput(
512522
read_file<std::string>(graphFile, Flags::Text, Flags::Release));
513523
auto* copy = strdup(graphInput.c_str());

test/metadce/all-outside.wast

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
(module)
2-

test/metadce/spanning_cycle.wast

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
(module
2+
(memory 1 1)
3+
(data passive "Hello, datacount section!")
4+
25
(import "env" "js_func" (func $a_js_func))
36

47
(export "wasm_func_a" (func $a_wasm_func))
58

69
(func $a_wasm_func
10+
;; refer to the data segment to keep it around
11+
(memory.init 0
12+
(i32.const 0)
13+
(i32.const 0)
14+
(i32.const 25)
15+
)
716
(call $a_js_func)
817
)
918
)
10-
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
(module
22
(type $FUNCSIG$v (func))
33
(import "env" "js_func" (func $a_js_func))
4+
(memory $0 1 1)
5+
(data passive "Hello, datacount section!")
46
(export "wasm_func_a" (func $a_wasm_func))
57
(func $a_wasm_func (; 1 ;) (type $FUNCSIG$v)
8+
(memory.init 0
9+
(i32.const 0)
10+
(i32.const 0)
11+
(i32.const 25)
12+
)
613
(call $a_js_func)
714
)
815
)

0 commit comments

Comments
 (0)