[clang] [Clang] Support using boolean vectors in ternary operators (PR #154145)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 18 09:50:13 PDT 2025
================
@@ -219,3 +219,65 @@ void OneScalarOp() {
four_ll ? four_ll : some_ll;
}
+
+constexpr TwoBools constexpr_two_bools{false, true};
+// CHECK-LABEL: define internal noundef double @_ZL16constexpr_vectorDv2_bDv2_iS0_(
+// CHECK-SAME: i8 noundef [[C_COERCE:%.*]], double noundef [[A_COERCE:%.*]], double noundef [[B_COERCE:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[RETVAL:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[C:%.*]] = alloca i8, align 1
+// CHECK-NEXT: [[A:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[B:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[C_ADDR:%.*]] = alloca i8, align 1
+// CHECK-NEXT: [[A_ADDR:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[B_ADDR:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: store i8 [[C_COERCE]], ptr [[C]], align 1
+// CHECK-NEXT: [[LOAD_BITS:%.*]] = load i8, ptr [[C]], align 1
+// CHECK-NEXT: [[TMP0:%.*]] = bitcast i8 [[LOAD_BITS]] to <8 x i1>
+// CHECK-NEXT: [[C1:%.*]] = shufflevector <8 x i1> [[TMP0]], <8 x i1> poison, <2 x i32> <i32 0, i32 1>
+// CHECK-NEXT: store double [[A_COERCE]], ptr [[A]], align 8
+// CHECK-NEXT: [[A2:%.*]] = load <2 x i32>, ptr [[A]], align 8
+// CHECK-NEXT: store double [[B_COERCE]], ptr [[B]], align 8
+// CHECK-NEXT: [[B3:%.*]] = load <2 x i32>, ptr [[B]], align 8
+// CHECK-NEXT: [[INSERTVEC:%.*]] = shufflevector <2 x i1> [[C1]], <2 x i1> poison, <8 x i32> <i32 0, i32 1, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
+// CHECK-NEXT: [[TMP1:%.*]] = bitcast <8 x i1> [[INSERTVEC]] to i8
+// CHECK-NEXT: store i8 [[TMP1]], ptr [[C_ADDR]], align 1
+// CHECK-NEXT: store <2 x i32> [[A2]], ptr [[A_ADDR]], align 8
+// CHECK-NEXT: store <2 x i32> [[B3]], ptr [[B_ADDR]], align 8
+// CHECK-NEXT: [[LOAD_BITS4:%.*]] = load i8, ptr [[C_ADDR]], align 1
+// CHECK-NEXT: [[TMP2:%.*]] = bitcast i8 [[LOAD_BITS4]] to <8 x i1>
+// CHECK-NEXT: [[EXTRACTVEC:%.*]] = shufflevector <8 x i1> [[TMP2]], <8 x i1> poison, <2 x i32> <i32 0, i32 1>
+// CHECK-NEXT: [[TMP3:%.*]] = load <2 x i32>, ptr [[A_ADDR]], align 8
+// CHECK-NEXT: [[TMP4:%.*]] = load <2 x i32>, ptr [[B_ADDR]], align 8
+// CHECK-NEXT: [[VECTOR_SELECT:%.*]] = select <2 x i1> [[EXTRACTVEC]], <2 x i32> [[TMP3]], <2 x i32> [[TMP4]]
+// CHECK-NEXT: store <2 x i32> [[VECTOR_SELECT]], ptr [[RETVAL]], align 8
+// CHECK-NEXT: [[TMP5:%.*]] = load double, ptr [[RETVAL]], align 8
+// CHECK-NEXT: ret double [[TMP5]]
+//
+constexpr static TwoInts constexpr_vector(TwoBools c, TwoInts a, TwoInts b) {
+ return c ? a : b;
+}
+
+// CHECK-LABEL: define dso_local void @_Z14UseConstexprOpv(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[COERCE:%.*]] = alloca i8, align 1
+// CHECK-NEXT: [[COERCE1:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[COERCE2:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[COERCE3:%.*]] = alloca <2 x i32>, align 8
+// CHECK-NEXT: [[TMP0:%.*]] = load <2 x i32>, ptr @two_ints, align 8
+// CHECK-NEXT: [[TMP1:%.*]] = load <2 x i32>, ptr @two_ints, align 8
+// CHECK-NEXT: store i8 bitcast (<8 x i1> <i1 false, i1 true, i1 undef, i1 undef, i1 undef, i1 undef, i1 undef, i1 undef> to i8), ptr [[COERCE]], align 1
+// CHECK-NEXT: [[TMP2:%.*]] = load i8, ptr [[COERCE]], align 1
+// CHECK-NEXT: store <2 x i32> [[TMP0]], ptr [[COERCE1]], align 8
+// CHECK-NEXT: [[TMP3:%.*]] = load double, ptr [[COERCE1]], align 8
+// CHECK-NEXT: store <2 x i32> [[TMP1]], ptr [[COERCE2]], align 8
+// CHECK-NEXT: [[TMP4:%.*]] = load double, ptr [[COERCE2]], align 8
+// CHECK-NEXT: [[CALL:%.*]] = call noundef double @_ZL16constexpr_vectorDv2_bDv2_iS0_(i8 noundef [[TMP2]], double noundef [[TMP3]], double noundef [[TMP4]])
+// CHECK-NEXT: store double [[CALL]], ptr [[COERCE3]], align 8
+// CHECK-NEXT: [[TMP5:%.*]] = load <2 x i32>, ptr [[COERCE3]], align 8
+// CHECK-NEXT: ret void
+//
+void UseConstexprOp() {
+ constexpr_vector(constexpr_two_bools, two_ints, two_ints);
----------------
erichkeane wrote:
you'd want to assign this return to a constexpr variable to force this to do constexpr eval.
https://github.com/llvm/llvm-project/pull/154145
More information about the cfe-commits
mailing list