SIMD optimization x64/x86: Better code for iNxM.all_true
Categories
(Core :: JavaScript: WebAssembly, enhancement, P3)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox101 | --- | fixed |
People
(Reporter: lth, Assigned: yury)
References
(Blocks 1 open bug)
Details
Attachments
(1 external file)
The code we generate for all_true is correct but has a fairly long dependency chain, it may be possible to shorten this. See discussion starting at https://github.com/WebAssembly/simd/pull/415#issuecomment-758884709, but look also to the end because there's more discussion there.
But there's another discussion in https://github.com/WebAssembly/simd/issues/189 that suggests that using PTEST is even better: it amounts to compare-to-zero-for-equality + PTEST (and then setcc or some boolean optimization to capture the result, as now).
Note that we have special boolean optimization for control of all_true and any_true, see CodeGenerator::visitWasmReduceAndBranchSimd128, and here we use cmp + ptest, so I'm not sure why the code for the result-capturing version does not do this. It could just be old code from SIMD.js.
| Reporter | ||
Updated•5 years ago
|
| Assignee | ||
Comment 1•5 years ago
|
||
But there's another discussion in https://github.com/WebAssembly/simd/issues/189 that suggests that using PTEST is even better: it amounts to compare-to-zero-for-equality + PTEST (and then setcc or some boolean optimization to capture the result, as now).
Was it done in bug 1640669? I see code at https://searchfox.org/mozilla-central/source/js/src/jit/x86-shared/CodeGenerator-x86-shared.cpp#3725 matches required recipe.
| Reporter | ||
Comment 2•5 years ago
|
||
It looks like maybe this got fixed for the control optimization but not for the value capture? Here's an example:
var mod = new WebAssembly.Module(wasmTextToBinary(
`(module
(func $f1 (param v128) (result i32)
(i8x16.all_true (local.get 0)))
(func $f2 (param v128) (result i32)
(block $B (br_if $B (i8x16.all_true (local.get 0)))
(return (i32.const 1)))
(return (i32.const 2)))
)`));
wasmDis(mod);
This produces:
Kind = Function, index = 0, name = f1:
00000000 55 push %rbp
00000001 48 8b ec mov %rsp, %rbp
00000004 41 81 fa 23 08 00 00 cmp $0x823, %r10d
0000000B 0f 84 13 00 00 00 jz 0x0000000000000024
00000011 0f 0b ud2
00000013 66 0f 1f 44 00 00 nopw %ax, (%rax,%rax,1)
00000019 0f 1f 80 00 00 00 00 nopl %eax, (%rax)
00000020 55 push %rbp
00000021 48 8b ec mov %rsp, %rbp
00000024 66 45 0f ef ff pxor %xmm15, %xmm15
00000029 66 44 0f 74 f8 pcmpeqb %xmm0, %xmm15
0000002E 66 41 0f d7 c7 pmovmskb %xmm15, %eax
00000033 85 c0 test %eax, %eax
00000035 0f 94 c0 setz %al
00000038 0f b6 c0 movzx %al, %eax
0000003B 5d pop %rbp
0000003C c3 ret
Kind = Function, index = 1, name = f2:
00000000 55 push %rbp
00000001 48 8b ec mov %rsp, %rbp
00000004 41 81 fa 23 08 00 00 cmp $0x823, %r10d
0000000B 0f 84 13 00 00 00 jz 0x0000000000000024
00000011 0f 0b ud2
00000013 66 0f 1f 44 00 00 nopw %ax, (%rax,%rax,1)
00000019 0f 1f 80 00 00 00 00 nopl %eax, (%rax)
00000020 55 push %rbp
00000021 48 8b ec mov %rsp, %rbp
00000024 66 45 0f ef ff pxor %xmm15, %xmm15
00000029 66 44 0f 74 f8 pcmpeqb %xmm0, %xmm15
0000002E 66 45 0f 38 17 ff ptest %xmm15, %xmm15
00000034 0f 84 0a 00 00 00 jz 0x0000000000000044
0000003A b8 01 00 00 00 mov $0x01, %eax
0000003F e9 05 00 00 00 jmp 0x0000000000000049
00000044 b8 02 00 00 00 mov $0x02, %eax
00000049 5d pop %rbp
0000004A c3 ret
We generate the desired code for the control case (where the capturing of the boolean is baked into the branch) but we may be able to do better for the value-capturing case. My guess is that the value-capturing case is not super common, but I have no actual data about that.
(Bonus points for adding codegen tests if/when a fix lands for this issue.)
| Assignee | ||
Comment 3•4 years ago
|
||
Updated•4 years ago
|
Comment 5•4 years ago
|
||
| bugherder | ||
Description
•