llvm-project/clang/test/Sema/vector-bool-assign.cpp
Joseph Huber 134a58adff
[Clang] Permit implicit conversion from integral to boolean vectors (#158369)
Summary:
Clang supports boolean vectors as an extension to the vector model.
These are commonly used to represent mask vectors in many vector ISAs.
Currently, using these is quite difficult because all of the vector
comparison operations use integral bitmasks. C / C++ has a long history
of allowing implicit conversions to bool, and I think that we should be
able to do the same here, especially because boolean vectors often work
as wrappers around a bitfield.

This patch adds the minimal changes to enable integral to boolean
conversions for vectors. Because LLVM already handles comparison to zero
for vectors natively, minimal changes are required. We are not bound to
the OpenCL standard at all here because it explicitly forbids boolean
vectors anyway, so these are simply clang extensions.
2025-09-15 10:09:10 -05:00

28 lines
1.1 KiB
C++

// RUN: %clang_cc1 -triple x86_64 -fsyntax-only -verify %s
using v8i = int [[clang::ext_vector_type(8)]];
using v8b = bool [[clang::ext_vector_type(8)]];
using v4f = float [[clang::ext_vector_type(4)]];
using v4b = bool [[clang::ext_vector_type(4)]];
void foo(v8b);
v8b integral(v8i v) {
v8b m1 = __builtin_convertvector(v, int [[clang::ext_vector_type(8)]]);
v8b m2 = __builtin_convertvector(v, unsigned [[clang::ext_vector_type(8)]]);
v8b m3 = __builtin_convertvector(v, long [[clang::ext_vector_type(8)]]);
v8b m4 = __builtin_convertvector(v, unsigned long [[clang::ext_vector_type(8)]]);
v8b m5 = __builtin_convertvector(v, char [[clang::ext_vector_type(8)]]);
v8b m6 = __builtin_convertvector(v, unsigned char [[clang::ext_vector_type(8)]]);
foo(v);
return v;
}
v4b non_integral(v4f vf) {
return vf; // expected-error{{cannot initialize return object of type 'v4b' (vector of 4 'bool' values) with an lvalue of type 'v4f' (vector of 4 'float' values)}}
}
v4b size_mismatch(v8i v) {
return v; // expected-error{{cannot initialize return object of type 'v4b' (vector of 4 'bool' values) with an lvalue of type 'v8i' (vector of 8 'int' values)}}
}