Skip to content

Commit 134a58a

Browse files
authored
[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.
1 parent c88f3c5 commit 134a58a

File tree

5 files changed

+152
-7
lines changed

5 files changed

+152
-7
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9385,11 +9385,21 @@ AssignConvertType Sema::CheckAssignmentConstraints(QualType LHSType,
93859385
return AssignConvertType::Incompatible;
93869386
}
93879387

9388-
// Allow scalar to ExtVector assignments, and assignments of an ExtVector type
9389-
// to the same ExtVector type.
9390-
if (LHSType->isExtVectorType()) {
9391-
if (RHSType->isExtVectorType())
9388+
// Allow scalar to ExtVector assignments, assignment to bool, and assignments
9389+
// of an ExtVector type to the same ExtVector type.
9390+
if (auto *LHSExtType = LHSType->getAs<ExtVectorType>()) {
9391+
if (auto *RHSExtType = RHSType->getAs<ExtVectorType>()) {
9392+
// Implicit conversions require the same number of elements.
9393+
if (LHSExtType->getNumElements() != RHSExtType->getNumElements())
9394+
return AssignConvertType::Incompatible;
9395+
9396+
if (LHSType->isExtVectorBoolType() &&
9397+
RHSExtType->getElementType()->isIntegerType()) {
9398+
Kind = CK_IntegralToBoolean;
9399+
return AssignConvertType::Compatible;
9400+
}
93929401
return AssignConvertType::Incompatible;
9402+
}
93939403
if (RHSType->isArithmeticType()) {
93949404
// CK_VectorSplat does T -> vector T, so first cast to the element type.
93959405
if (ConvertRHS)

clang/lib/Sema/SemaOverload.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,9 +2162,18 @@ static bool IsVectorConversion(Sema &S, QualType FromType, QualType ToType,
21622162

21632163
// There are no conversions between extended vector types, only identity.
21642164
if (auto *ToExtType = ToType->getAs<ExtVectorType>()) {
2165-
if (FromType->getAs<ExtVectorType>()) {
2166-
// There are no conversions between extended vector types other than the
2167-
// identity conversion.
2165+
if (auto *FromExtType = FromType->getAs<ExtVectorType>()) {
2166+
// Implicit conversions require the same number of elements.
2167+
if (ToExtType->getNumElements() != FromExtType->getNumElements())
2168+
return false;
2169+
2170+
// Permit implicit conversions from integral values to boolean vectors.
2171+
if (ToType->isExtVectorBoolType() &&
2172+
FromExtType->getElementType()->isIntegerType()) {
2173+
ICK = ICK_Boolean_Conversion;
2174+
return true;
2175+
}
2176+
// There are no other conversions between extended vector types.
21682177
return false;
21692178
}
21702179

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu %s -emit-llvm -o - | FileCheck %s
3+
4+
using v1i = int [[clang::ext_vector_type(1)]];
5+
using v1b = bool [[clang::ext_vector_type(1)]];
6+
using v8i = int [[clang::ext_vector_type(8)]];
7+
using v8b = bool [[clang::ext_vector_type(8)]];
8+
using v16i = short [[clang::ext_vector_type(16)]];
9+
using v16b = bool [[clang::ext_vector_type(16)]];
10+
using v32i = char [[clang::ext_vector_type(32)]];
11+
using v32b = bool [[clang::ext_vector_type(32)]];
12+
13+
// CHECK-LABEL: define dso_local noundef i8 @_Z3fooDv1_i(
14+
// CHECK-SAME: i32 noundef [[V_COERCE:%.*]]) #[[ATTR0:[0-9]+]] {
15+
// CHECK-NEXT: [[ENTRY:.*:]]
16+
// CHECK-NEXT: [[RETVAL:%.*]] = alloca <1 x i1>, align 1
17+
// CHECK-NEXT: [[V:%.*]] = alloca <1 x i32>, align 4
18+
// CHECK-NEXT: [[V_ADDR:%.*]] = alloca <1 x i32>, align 4
19+
// CHECK-NEXT: store i32 [[V_COERCE]], ptr [[V]], align 4
20+
// CHECK-NEXT: [[V1:%.*]] = load <1 x i32>, ptr [[V]], align 4
21+
// CHECK-NEXT: store <1 x i32> [[V1]], ptr [[V_ADDR]], align 4
22+
// CHECK-NEXT: [[TMP0:%.*]] = load <1 x i32>, ptr [[V_ADDR]], align 4
23+
// CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne <1 x i32> [[TMP0]], zeroinitializer
24+
// CHECK-NEXT: store <1 x i1> [[TOBOOL]], ptr [[RETVAL]], align 1
25+
// CHECK-NEXT: [[TMP1:%.*]] = load i8, ptr [[RETVAL]], align 1
26+
// CHECK-NEXT: ret i8 [[TMP1]]
27+
//
28+
v1b foo(v1i v) { return v; }
29+
// CHECK-LABEL: define dso_local noundef i8 @_Z3fooDv8_i(
30+
// CHECK-SAME: ptr noundef byval(<8 x i32>) align 32 [[TMP0:%.*]]) #[[ATTR0]] {
31+
// CHECK-NEXT: [[ENTRY:.*:]]
32+
// CHECK-NEXT: [[RETVAL:%.*]] = alloca <8 x i1>, align 1
33+
// CHECK-NEXT: [[V_ADDR:%.*]] = alloca <8 x i32>, align 32
34+
// CHECK-NEXT: [[V:%.*]] = load <8 x i32>, ptr [[TMP0]], align 32
35+
// CHECK-NEXT: store <8 x i32> [[V]], ptr [[V_ADDR]], align 32
36+
// CHECK-NEXT: [[TMP1:%.*]] = load <8 x i32>, ptr [[V_ADDR]], align 32
37+
// CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne <8 x i32> [[TMP1]], zeroinitializer
38+
// CHECK-NEXT: store <8 x i1> [[TOBOOL]], ptr [[RETVAL]], align 1
39+
// CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[RETVAL]], align 1
40+
// CHECK-NEXT: ret i8 [[TMP2]]
41+
//
42+
v8b foo(v8i v) { return v; }
43+
// CHECK-LABEL: define dso_local noundef i16 @_Z3fooDv16_s(
44+
// CHECK-SAME: ptr noundef byval(<16 x i16>) align 32 [[TMP0:%.*]]) #[[ATTR0]] {
45+
// CHECK-NEXT: [[ENTRY:.*:]]
46+
// CHECK-NEXT: [[RETVAL:%.*]] = alloca <16 x i1>, align 2
47+
// CHECK-NEXT: [[V_ADDR:%.*]] = alloca <16 x i16>, align 32
48+
// CHECK-NEXT: [[V:%.*]] = load <16 x i16>, ptr [[TMP0]], align 32
49+
// CHECK-NEXT: store <16 x i16> [[V]], ptr [[V_ADDR]], align 32
50+
// CHECK-NEXT: [[TMP1:%.*]] = load <16 x i16>, ptr [[V_ADDR]], align 32
51+
// CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne <16 x i16> [[TMP1]], zeroinitializer
52+
// CHECK-NEXT: store <16 x i1> [[TOBOOL]], ptr [[RETVAL]], align 2
53+
// CHECK-NEXT: [[TMP2:%.*]] = load i16, ptr [[RETVAL]], align 2
54+
// CHECK-NEXT: ret i16 [[TMP2]]
55+
//
56+
v16b foo(v16i v) { return v; }
57+
// CHECK-LABEL: define dso_local noundef i32 @_Z3fooDv32_c(
58+
// CHECK-SAME: ptr noundef byval(<32 x i8>) align 32 [[TMP0:%.*]]) #[[ATTR0]] {
59+
// CHECK-NEXT: [[ENTRY:.*:]]
60+
// CHECK-NEXT: [[RETVAL:%.*]] = alloca <32 x i1>, align 4
61+
// CHECK-NEXT: [[V_ADDR:%.*]] = alloca <32 x i8>, align 32
62+
// CHECK-NEXT: [[V:%.*]] = load <32 x i8>, ptr [[TMP0]], align 32
63+
// CHECK-NEXT: store <32 x i8> [[V]], ptr [[V_ADDR]], align 32
64+
// CHECK-NEXT: [[TMP1:%.*]] = load <32 x i8>, ptr [[V_ADDR]], align 32
65+
// CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne <32 x i8> [[TMP1]], zeroinitializer
66+
// CHECK-NEXT: store <32 x i1> [[TOBOOL]], ptr [[RETVAL]], align 4
67+
// CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[RETVAL]], align 4
68+
// CHECK-NEXT: ret i32 [[TMP2]]
69+
//
70+
v32b foo(v32i v) { return v; }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -triple x86_64 -fsyntax-only -verify %s
2+
3+
typedef _Bool bool;
4+
5+
typedef __attribute__((ext_vector_type(8))) int v8i;
6+
typedef __attribute__((ext_vector_type(8))) bool v8b;
7+
typedef __attribute__((ext_vector_type(4))) float v4f;
8+
typedef __attribute__((ext_vector_type(4))) bool v4b;
9+
10+
void foo(v8b);
11+
12+
v8b integral(v8i v) {
13+
v8b m1 = __builtin_convertvector(v, __attribute__((ext_vector_type(8))) int);
14+
v8b m2 = __builtin_convertvector(v, __attribute__((ext_vector_type(8))) unsigned);
15+
v8b m3 = __builtin_convertvector(v, __attribute__((ext_vector_type(8))) long);
16+
v8b m4 = __builtin_convertvector(v, __attribute__((ext_vector_type(8))) unsigned long);
17+
v8b m5 = __builtin_convertvector(v, __attribute__((ext_vector_type(8))) char);
18+
v8b m6 = __builtin_convertvector(v, __attribute__((ext_vector_type(8))) unsigned char);
19+
foo(v);
20+
return v;
21+
}
22+
23+
v4b non_integral(v4f vf) {
24+
return vf; // expected-error{{returning 'v4f' (vector of 4 'float' values) from a function with incompatible result type 'v4b' (vector of 4 'bool' values}}
25+
}
26+
27+
v4b size_mismatch(v8i v) {
28+
return v; // expected-error{{returning 'v8i' (vector of 8 'int' values) from a function with incompatible result type 'v4b' (vector of 4 'bool' values)}}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -triple x86_64 -fsyntax-only -verify %s
2+
3+
using v8i = int [[clang::ext_vector_type(8)]];
4+
using v8b = bool [[clang::ext_vector_type(8)]];
5+
using v4f = float [[clang::ext_vector_type(4)]];
6+
using v4b = bool [[clang::ext_vector_type(4)]];
7+
8+
void foo(v8b);
9+
10+
v8b integral(v8i v) {
11+
v8b m1 = __builtin_convertvector(v, int [[clang::ext_vector_type(8)]]);
12+
v8b m2 = __builtin_convertvector(v, unsigned [[clang::ext_vector_type(8)]]);
13+
v8b m3 = __builtin_convertvector(v, long [[clang::ext_vector_type(8)]]);
14+
v8b m4 = __builtin_convertvector(v, unsigned long [[clang::ext_vector_type(8)]]);
15+
v8b m5 = __builtin_convertvector(v, char [[clang::ext_vector_type(8)]]);
16+
v8b m6 = __builtin_convertvector(v, unsigned char [[clang::ext_vector_type(8)]]);
17+
foo(v);
18+
return v;
19+
}
20+
21+
v4b non_integral(v4f vf) {
22+
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)}}
23+
}
24+
25+
v4b size_mismatch(v8i v) {
26+
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)}}
27+
}

0 commit comments

Comments
 (0)