summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/branch-hinting
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/jit-test/tests/wasm/branch-hinting')
-rw-r--r--js/src/jit-test/tests/wasm/branch-hinting/complex_control_flow.js38
-rw-r--r--js/src/jit-test/tests/wasm/branch-hinting/directives.txt1
-rw-r--r--js/src/jit-test/tests/wasm/branch-hinting/parsing.js154
-rw-r--r--js/src/jit-test/tests/wasm/branch-hinting/simple_example.js41
4 files changed, 234 insertions, 0 deletions
diff --git a/js/src/jit-test/tests/wasm/branch-hinting/complex_control_flow.js b/js/src/jit-test/tests/wasm/branch-hinting/complex_control_flow.js
new file mode 100644
index 0000000000..727a3156fb
--- /dev/null
+++ b/js/src/jit-test/tests/wasm/branch-hinting/complex_control_flow.js
@@ -0,0 +1,38 @@
+// Test branch hinting with nested if.
+
+var imports = { "":{inc() { counter++ }} };
+counter = 0;
+
+let module = new WebAssembly.Module(wasmTextToBinary(`(module
+ (import "" "inc" (func (result i32)))
+ (func
+ (result i32)
+ (@metadata.code.branch_hint "\\00") (if (result i32)
+ (i32.const 1)
+ (then
+ (@metadata.code.branch_hint "\\00") (if (result i32)
+ (i32.const 2)
+ (then
+ (@metadata.code.branch_hint "\\00") (if (result i32)
+ (i32.const 3)
+ (then
+ (@metadata.code.branch_hint "\\00") (if (result i32)
+ (i32.const 0)
+ (then (call 0))
+ (else (i32.const 42))
+ )
+ )
+ (else (call 0))
+ )
+ )
+ (else (call 0))
+ )
+ )
+ (else (call 0))
+ )
+ )
+ (export "run" (func 1))
+)`, 42, imports));
+
+assertEq(counter, 0);
+assertEq(wasmParsedBranchHints(module), true);
diff --git a/js/src/jit-test/tests/wasm/branch-hinting/directives.txt b/js/src/jit-test/tests/wasm/branch-hinting/directives.txt
new file mode 100644
index 0000000000..8fe3d1e8e4
--- /dev/null
+++ b/js/src/jit-test/tests/wasm/branch-hinting/directives.txt
@@ -0,0 +1 @@
+|jit-test| --setpref=wasm_branch_hinting=true; --wasm-compiler=ion; test-also=--wasm-compiler=baseline;skip-if: !wasmBranchHintingEnabled(); include:wasm.js
diff --git a/js/src/jit-test/tests/wasm/branch-hinting/parsing.js b/js/src/jit-test/tests/wasm/branch-hinting/parsing.js
new file mode 100644
index 0000000000..0ff9b0f557
--- /dev/null
+++ b/js/src/jit-test/tests/wasm/branch-hinting/parsing.js
@@ -0,0 +1,154 @@
+// Make sure we are correctly parsing this custom section.
+var code =`
+(module
+ (func $$dummy)
+ (func $main (param i32) (result i32)
+ i32.const 0
+ local.get 0
+ i32.eq
+ ;; Only allowed on br_if and if
+ (@metadata.code.branch_hint "\\00") if
+ call $$dummy
+ i32.const 1
+ return
+ else
+ call $$dummy
+ i32.const 0
+ return
+ end
+ i32.const 3
+ return
+ )
+
+ (export "_main" (func $main))
+)`;
+
+let branchHintsModule = new WebAssembly.Module(wasmTextToBinary(code));
+assertEq(WebAssembly.Module.customSections(branchHintsModule, "metadata.code.branch_hint").length, 1);
+assertEq(wasmParsedBranchHints(branchHintsModule), true);
+
+let instance = new WebAssembly.Instance(branchHintsModule);
+assertEq(instance.exports._main(0), 1);
+
+// Testing branch hints parsing on `if` and `br_if`
+branchHintsModule = new WebAssembly.Module(wasmTextToBinary(`
+(module
+ (func $main
+ i32.const 0
+ (@metadata.code.branch_hint "\\00")
+ if
+ i32.const 0
+ (@metadata.code.branch_hint "\\01")
+ br_if 0
+ end
+ )
+ (export "_main" (func $main))
+)`));
+assertEq(wasmParsedBranchHints(branchHintsModule), true);
+instance = new WebAssembly.Instance(branchHintsModule);
+instance.exports._main();
+
+let m = new WebAssembly.Module(wasmTextToBinary(`
+(module
+ (type (;0;) (func))
+ (type (;1;) (func (param i32) (result i32)))
+ (type (;2;) (func (result i32)))
+ (func $__wasm_nullptr (type 0)
+ unreachable)
+ (func $main (type 2) (result i32)
+ (local i32 i32 i32 i32)
+ i32.const 0
+ local.tee 2
+ local.set 3
+ loop
+ local.get 2
+ i32.const 50000
+ i32.eq
+ (@metadata.code.branch_hint "\\00") if
+ i32.const 1
+ local.set 3
+ end
+ local.get 2
+ i32.const 1
+ i32.add
+ local.tee 2
+ i32.const 100000
+ i32.ne
+ (@metadata.code.branch_hint "\\01") br_if 0 (;@1;)
+ end
+ local.get 3)
+ (table (;0;) 1 1 funcref)
+ (memory (;0;) 17 128)
+ (global (;0;) (mut i32) (i32.const 42))
+ (export "memory" (memory 0))
+ (export "_main" (func $main))
+ (elem (;0;) (i32.const 0) func $__wasm_nullptr)
+ (type (;0;) (func (param i32)))
+)`));
+
+assertEq(wasmParsedBranchHints(m), true);
+instance = new WebAssembly.Instance(m);
+assertEq(instance.exports._main(0), 1);
+
+// Testing invalid values for branch hints
+assertErrorMessage(() => new WebAssembly.Module(wasmTextToBinary(`
+(module
+ (func $main (param i32) (result i32)
+ i32.const 0
+ (@metadata.code.branch_hint "\\0000000") if
+ i32.const 1
+ return
+ end
+ i32.const 42
+ return
+ )
+ )
+`)), SyntaxError, /invalid value for branch hint/);
+
+assertErrorMessage(() => new WebAssembly.Module(wasmTextToBinary(`
+(module
+ (func $main (param i32) (result i32)
+ i32.const 0
+ (@metadata.code.branch_hint "\\02") if
+ i32.const 1
+ return
+ end
+ i32.const 42
+ return
+ )
+ )
+`)), SyntaxError, /invalid value for branch hint/);
+
+assertErrorMessage(() => new WebAssembly.Module(wasmTextToBinary(`
+(module
+ (func $main (param i32) (result i32)
+ i32.const 0
+ (@metadata.code.branch_hint "\\aaaa") if
+ i32.const 1
+ return
+ end
+ i32.const 42
+ return
+ )
+ )
+`)), SyntaxError, /wasm text error/);
+
+assertErrorMessage(() => new WebAssembly.Module(wasmTextToBinary(`
+(module
+ (func $main (param i32) (result i32)
+ i32.const 0
+ (@metadata.code.branch_hint) if
+ i32.const 1
+ return
+ end
+ i32.const 42
+ return
+ )
+ )
+`)), SyntaxError, /wasm text error/);
+
+assertErrorMessage(() => new WebAssembly.Module(wasmTextToBinary(`
+(module
+ (@metadata.code.branch_hint)
+)
+`)), SyntaxError, /wasm text error/);
diff --git a/js/src/jit-test/tests/wasm/branch-hinting/simple_example.js b/js/src/jit-test/tests/wasm/branch-hinting/simple_example.js
new file mode 100644
index 0000000000..141ee02fef
--- /dev/null
+++ b/js/src/jit-test/tests/wasm/branch-hinting/simple_example.js
@@ -0,0 +1,41 @@
+// Branch Hinting proposal
+
+function runModule(hint) {
+ let code =`
+ (module
+ (func $$dummy)
+ (func $main (param i32) (result i32)
+ i32.const 0
+ local.get 0
+ i32.eq
+ ;; Only allowed on br_if and if
+ (@metadata.code.branch_hint "${hint}") if
+ call $$dummy
+ i32.const 1
+ return
+ else
+ call $$dummy
+ i32.const 0
+ return
+ end
+ i32.const 3
+ return
+ )
+ (export "_main" (func $main))
+ )`;
+ let branchHintsModule = new WebAssembly.Module(wasmTextToBinary(code));
+ assertEq(wasmParsedBranchHints(branchHintsModule), true);
+
+ let instance = new WebAssembly.Instance(branchHintsModule);
+ assertEq(instance.exports._main(0), 1);
+}
+
+// Ensure that we have the same result with different branch hints.
+runModule("\\00");
+runModule("\\01");
+
+let module = new WebAssembly.Module(wasmTextToBinary(`
+ (func i32.const 0 (@metadata.code.branch_hint "\\00") if end)
+`))
+
+assertEq(wasmParsedBranchHints(module), true);