Repository navigation
Update proposal to include Integer Min/Max Ops - #27
Conversation
stoklund
left a comment
There was a problem hiding this comment.
LGTM.
Perhaps you could include a rationale for omitting the 64-bit versions?
|
Would be good to also get information on codegen for different ISAs, as well as perf numbers. |
@dtig these can all be implemented by using the relational operations returning mask and bitselect, such that: The meeting notes mention:
Maybe someone can ping DG so that they can chime in. Instead of adding more instructions we could, instead, add a "recommended encoding" for these to some of the other documents, but IMO any code generator worth its salt needs to lower The main advantage I see here is that it reduces binary size and encoding/decoding time, but I'd rather add these if this actually becomes an issue in practice, and not as a preventive measure. nitpick: This is missing the operations for |
Some compilers translate from wasm to machine code in a single pass, for fast application startup. Such compilers aren't worth their salt by typical measures, but they're important for WebAssembly. It's possible to implement pattern matching in such compilers, but it adds complexity to what otherwise can be a very simple compiler. There will always be ambiguity about what things to add as opcodes and what to leave to pattern matching, however when we find ourselves considering documenting "recommended" patterns, and we expect optimizing engines to recognize them, that's a sign that they may be important enough to add as opcodes. I agree that |
FWIW I think that in this case there isn't that much ambiguity because the consensus seems to be that these (or something like these) should be added at some point, but that it is not worth delaying the MVP for these. I also don't like the "recommended" patterns situation much. Maybe we should just start a design document for the v2 of the SIMD spec already, and move these there. That way compilers and code generators that want to support these can do so already by targeting that spec. |
I don't think adding these would appreciably delay MVP. They are already implemented in V8 and it would not be much effort to add tools support. I believe the only things we need to do before rebasing and merging this are:
|
The 64-bit element versions are probably omitted due to lack of platform support: on x86_64 they require AVX-512 and are not part of NEON.
I think that these are useful (*) and supported by most architectures. I think those two evaluation criteria are enough, and I've argued why in the issue #37 . (*) e.g., to write the min/max elements of two arrays into a third one, to do a min/max horizontal reduction of an array (implementing C++ min_element, max_element, and minmax_element |
|
Emulating unsigned min/max on pre-AVX512 x86 is notoriously inefficient:
Thus, without the integer minimum/maximum instructions in WebAssembly SIMD spec we would have to simulate unsigned minimum/maximum operations at the cost of 5 (!) instructions, including unsigned minimum/maximum instruction (!). Emulation of signed minimum/maximum is only marginally cheaper: due to the need to lower |
Summary: Introduces a clang builtins and LLVM intrinsics representing integer min/max instructions. These instructions have not been merged to the SIMD spec proposal yet, so they are currently opt-in only via builtins and not produced by general pattern matching. If these instructions are accepted into the spec proposal the builtins and intrinsics will be replaced with normal pattern matching. Defined in WebAssembly/simd#27. Reviewers: aheejin Reviewed By: aheejin Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D69696
|
Can't |
|
No, in |
As proposed in WebAssembly/simd#27.
|
Rebasing + updating this PR after renewed interest. Updated the PR text to include mapping to ISAs. These are not included for the 64x2 variants because they are not widely used, as well as the unsigned variants have suboptimal mappings on Intel platforms. Going back to the discussion about recommended patterns, I think there were a couple of conversations there as the notes don't differentiate between two speakers both with initials DG. I want to clarify the performance example for pattern matching being fragile. The particular example here was about LLVM's current optimizations where in certain cases integer min/max don't lower down exactly to expected nodes even when Min/Max are assumed to be available. So having these operations be explicitly part of the operation set gives applications the flexibility of using them, and not relying on pattern matching when possible. This specific example was - With saturating add/subtract enabled, the codegen will be lowered to -
And without,
So, in that context when we were talking about LLVM being able to pattern match accurately, I was using this particular case as an example of why that may not always work. Though I agree with the replies above of not documenting usage patterns, and that being a good indicator of when to include an operation as a part of the proposal. |
|
As this has had some discussion already, leaving a poll here to reach consensus. In favor of including integer min/max operations in the current proposal, please respond with 👍 |
Summary: Introduces a clang builtins and LLVM intrinsics representing integer min/max instructions. These instructions have not been merged to the SIMD spec proposal yet, so they are currently opt-in only via builtins and not produced by general pattern matching. If these instructions are accepted into the spec proposal the builtins and intrinsics will be replaced with normal pattern matching. Defined in WebAssembly/simd#27. Reviewers: aheejin Reviewed By: aheejin Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, cfe-commits, llvm-commits Tags: #clang, #llvm Differential Revision: https://reviews.llvm.org/D69696
As discussed in a previous CG meeting, it would be good to discuss what Integer Min/Max operations should look like. This pull request adds lane-wise Integer Min/Max ops similar to the Floating point Min/Max ops. Linked issue to follow up on AIs from CG meetings.
These have native support on most modern architectures.
On Intel:
i32x4.min_s, i16x8.min_s, i8x16.min_smap directly topmins(d/w/b)i32x4.max_s, i16x8.max_s, i8x16.max_smap directly topmaxs(d/w/b)i32x4.min_u, i16x8.min_u, i8x16.min_umap directly topminu(d/w/b)i32x4.max_u, i16x8.max_u, i8x16.max_umap directly topmaxu(d/w/b)On ARM Neon:
i32x4.min_s, i16x8.min_s, i8x16.min_smap directly tosmini32x4.max_s, i16x8.max_s, i8x16.max_smap directly tosmaxi32x4.min_u, i16x8.min_u, i8x16.min_umap directly toumini32x4.max_u, i16x8.max_u, i8x16.max_umap directly toumaxOn ARMv7, these map directly to
vmin/vmaxinstructions.