Open philnik777 opened 5 months ago
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
@llvm/pr-subscribers-clang @llvm/pr-subscribers-backend-x86
@llvm/pr-subscribers-clang-codegen
Author: Nikolas Klauser (philnik777)
I don't understand why the order of emitted instructions changes based on how exactly Clang is compiled, but other than that this should be ready. Hopefully someone spots what the problem could be.
clang already supports ?:
with a vector condition; does this add anything new on top of that?
clang already supports
?:
with a vector condition; does this add anything new on top of that?
This works with bool vectors. I didn't realize you could use the ternary operator, since I only tested with them. I guess we could extend the ternary operator to accept bool vectors as well. Any thoughts?
You mean, if all three operands are boolean vectors? I'm surprised that doesn't already work.
You mean, if all three operands are boolean vectors? I'm surprised that doesn't already work.
No, I mean I have a vector of bools and want to select a value based on that. e.g. declval<simd_vector<bool, 16>>() ? declval<simd_vector<int, 16>>() : declval<simd_vector<int, 16>>()
.
The relevant bit of code is:
// The OpenCL operator with a vector condition is sufficiently
// different to merit its own checker.
if ((getLangOpts().OpenCL && Cond.get()->getType()->isVectorType()) ||
Cond.get()->getType()->isExtVectorType())
return OpenCLCheckVectorConditional(*this, Cond, LHS, RHS, QuestionLoc);
Maybe makes sense to relax it.
This is also very useful for generic code. For example this would allow libc++ to vectorize
{min,max,minmax}_element
without having to use platform-specific intrinsics. I've done some testing and even at-O0
Clang compiles the code to the expected instructions for architectures where the mask vector has the same bit count as the element vector (i.e. every SIMD ISA except AVX512 that I'm aware of) as long as the comparison operation is visible.