<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi folks,<div class=""><br class=""></div><div class="">In clang’s vector extension, the ternary operator (?:) is able to select from two vector type values based on the boolean type predicate. For example:</div><div class="">```</div><div class="">typedef float float4 __attribute__((ext_vector_type(4)));</div><div class=""><br class=""></div><div class="">float4 foo(bool cond, float4 a, float4 b) {</div><div class="">  return cond? a : b;</div><div class="">}</div><div class="">```</div><div class="">However, clang currently doesn’t support vector type values as the predicates. So code shown below won’t compile:</div><div class="">```</div><div class=""><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class="">float4 foo(float4 a, float4 b) {</div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class="">  return a > b? a : b;</div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class="">}</div></div><div class="">```</div><div class="">Which is essentially just a max function that can be implemented by hardware instructions (vmaxps in Intel AVX for example) efficiently.</div><div class=""><br class=""></div><div class="">I’m proposing to support vector type values as predicates in the ternary operator: If the predicate value has vector type, each of its element would be converted to bool before being used to select corresponding elements from the second or third operands. Note that this is exactly what GCC is doing right now [1] in their vector extensions.</div><div class=""><br class=""></div><div class="">On the (IR) codegen side, since the IR `select`, `icmp`, and `fcmp` instruction all support vector type values as the operands, I don’t think there are much trouble in it.</div><div class=""><br class=""></div><div class="">Here are the reasons and some advantages:</div><div class="">1. It provides more flexibility for programmers to do conditional selections on vectors. Especially for cases that can be optimized by hardware instructions like the motivated example above.</div><div class=""><br class=""></div><div class="">2. It will not break the current usages of ternary operator: If the predicate is a scalar value, it’s still sticking to the current model.</div><div class=""><br class=""></div><div class="">3. If the goal of our extensions is aiming to “support a broad range of GCC extensions”. Then the behavior should also be as consistent as possible.</div><div class=""><br class=""></div><div class="">I’ll try to come up with a patch for preview as soon as possible.</div><div class="">Also a slightly off-topic thing: In our document [2], the ternary operator has always been mis-typed as ‘:?’ Maybe we should fix it some times.</div><div class=""><br class=""></div><div class="">Thanks for the feedbacks in advance.</div><div class="">-Min</div><div class=""><br class=""></div><div class="">[1]: <a href="https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html" class="">https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html</a></div><div class="">[2]: <a href="https://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors" class="">https://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors</a></div><div class=""><br class=""></div></body></html>