[clang] [CIR] Fix crashes in the vector constant folders (PR #245994)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 05:47:14 PST 2026


https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/245994

>From 38c8fbf8aa0e5fe20449be78581530330b2a8703 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Thu, 26 Nov 2026 13:46:53 +0000
Subject: [PATCH] [CIR] Fix crashes in the vector constant folders

A constant vector can hold elements that are not integer or
floating-point literals, such as undef and poison. Folding a
`cir.vec.shuffle` with a -1 index produces one. The folders of
`cir.vec.cmp`, `cir.vec.fcmp`, `cir.vec.ternary` and
`cir.vec.shuffle.dynamic` cast every element they evaluate to a literal
and asserted on such a vector. They now leave the operation alone. This
also covers a `cir.vec.ternary` whose condition is a constant vector of
bool, which is still not folded.

Two more assertions in the same folders are fixed the same way:

- The `true` and `false` shortcut of `cir.vec.fcmp` built a constant
  from the fixed size of the result type and asserted on a scalable
  vector.
- `cir.vec.shuffle.dynamic` indexed past the end of a vector whose
  element count is not a power of two.

`cir.vec.cmp` and `cir.vec.fcmp` did not verify their comparison kind or
their result type, and the folders asserted on the ones they did not
expect. Both operations now require a result vector of integers or bools
with the element count of the operands, and `cir.vec.cmp` rejects the
floating-point only kinds `one` and `uno`. A result vector of unsigned
integers stays valid, because comparing vectors of `char` produces one
where `char` is unsigned. The folders asserted while building its
all-ones element and now build it from an `APInt`. A comparison whose
result is a vector of bool is not folded.

Lowering a constant vector with an undef or poison element to LLVM
still aborts. That is a separate bug.

Assisted-by: Claude Code (Fable 5.2).
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  |  49 ++++--
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |  67 +++++++--
 .../CIR/CodeGen/vector-cmp-unsigned-char.cpp  |  29 ++++
 clang/test/CIR/IR/invalid-vector.cir          |  82 ++++++++++
 clang/test/CIR/Transforms/vector-cmp-fold.cir | 140 ++++++++++++++++++
 .../vector-shuffle-dynamic-fold.cir           |  25 ++++
 .../CIR/Transforms/vector-ternary-fold.cir    |  26 ++++
 7 files changed, 393 insertions(+), 25 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/vector-cmp-unsigned-char.cpp

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 01e4d9bfb6d202..bcfd7fc6c49c3a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -210,6 +210,13 @@ def SameOperandsPointeeType : PredOpTrait<
   }]>
 >;
 
+// Like `AllElementCountsMatch`, for `!cir.vector` operands and results, which
+// are not shaped types. A fixed and a scalable element count do not match.
+class CIR_AllElementCountsMatch<list<string> names>
+    : AllMatchSameOperatorTrait<names,
+        "::mlir::cast<::cir::VectorType>($_self.getType()).getSize()",
+        "element count">;
+
 //===----------------------------------------------------------------------===//
 // Floating-Point Environment Traits
 //===----------------------------------------------------------------------===//
@@ -2684,6 +2691,10 @@ def CIR_ForOp : CIR_LoopOpBase<"for"> {
 // CmpOp
 //===----------------------------------------------------------------------===//
 
+// The comparison kinds that apply to floating-point operands only.
+def CIR_CmpOpKindOne : I32EnumCase<"one", 6>;
+def CIR_CmpOpKindUno : I32EnumCase<"uno", 7>;
+
 def CIR_CmpOpKind : CIR_I32Enum<"CmpOpKind", "compare operation kind", [
   I32EnumCase<"lt", 0>,
   I32EnumCase<"le", 1>,
@@ -2691,12 +2702,17 @@ def CIR_CmpOpKind : CIR_I32Enum<"CmpOpKind", "compare operation kind", [
   I32EnumCase<"ge", 3>,
   I32EnumCase<"eq", 4>,
   I32EnumCase<"ne", 5>,
-  I32EnumCase<"one", 6>,
-  I32EnumCase<"uno", 7>
+  CIR_CmpOpKindOne,
+  CIR_CmpOpKindUno
 ]>;
 
 def CIR_CmpOpKindAttr : CIR_EnumAttr<CIR_CmpOpKind, "cmp">;
 
+// A comparison kind of integer or boolean operands.
+def CIR_IntCmpOpKindAttr : ConfinedAttr<CIR_CmpOpKindAttr, [
+  EnumAttrIsNoneOf<CIR_CmpOpKindAttr, [CIR_CmpOpKindOne, CIR_CmpOpKindUno]>
+]>;
+
 def CIR_CmpOp : CIR_Op<"cmp",
     !listconcat([SameTypeOperands], CIR_FenvOpTraits)> {
   let summary = "Compare two values and produce a boolean result";
@@ -6969,13 +6985,18 @@ def CIR_VecReduceFMulOp : CIR_VecReduceAccFPOp<"fmul", "vector_reduce_fmul">;
 // VecCmpOp
 //===----------------------------------------------------------------------===//
 
-def CIR_VecCmpOp : CIR_Op<"vec.cmp", [Pure, SameTypeOperands]> {
+def CIR_VecCmpOp : CIR_Op<"vec.cmp", [
+  Pure, SameTypeOperands, CIR_AllElementCountsMatch<["lhs", "result"]>
+]> {
   let summary = "Compare two vectors";
   let description = [{
     The `cir.vec.cmp` operation does an element-wise comparison of two vectors
-    of the same non-fp type. The result is a vector of the same size as the operands
-    whose element type is the signed integral type that is the same size as the
-    element type of the operands. The values in the result are 0 or -1.
+    of the same non-fp type. The comparison kind is any but `one` and `uno`,
+    which apply to floating-point operands only. The result is a vector with as
+    many elements as the operands, whose element type is an integral or boolean
+    type. It is usually the signed integral type that is the same size as the
+    element type of the operands. Each element of an integral result has all
+    bits clear or all bits set.
 
     ```
     %eq = cir.vec.cmp(eq, %vec_a, %vec_b) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
@@ -6984,12 +7005,12 @@ def CIR_VecCmpOp : CIR_Op<"vec.cmp", [Pure, SameTypeOperands]> {
   }];
 
   let arguments = (ins
-    CIR_CmpOpKindAttr:$kind,
+    CIR_IntCmpOpKindAttr:$kind,
     CIR_VectorOfIntOrBoolType:$lhs,
     CIR_VectorOfIntOrBoolType:$rhs
   );
 
-  let results = (outs CIR_VectorType:$result);
+  let results = (outs CIR_VectorOfIntOrBoolType:$result);
 
   let assemblyFormat = [{
     `(` enum($kind) `,` $lhs `,` $rhs `)` `:` qualified(type($lhs)) `,`
@@ -7004,13 +7025,15 @@ def CIR_VecCmpOp : CIR_Op<"vec.cmp", [Pure, SameTypeOperands]> {
 //===----------------------------------------------------------------------===//
 
 def CIR_VecFCmpOp : CIR_Op<"vec.fcmp",
-    !listconcat([SameTypeOperands], CIR_FenvOpTraits)> {
+    !listconcat([SameTypeOperands,
+                 CIR_AllElementCountsMatch<["lhs", "result"]>],
+                CIR_FenvOpTraits)> {
   let summary = "Compare two floating point vectors";
   let description = [{
     The `cir.vec.fcmp` operation does an element-wise comparison of two vectors
-    of the same floating type. The result is a vector of the same size as the
-    operands whose element type is signed integral type. The values in the
-    result are 0 or -1.
+    of the same floating type. The result is a vector with as many elements as
+    the operands, whose element type is an integral or boolean type. Each
+    element of an integral result has all bits clear or all bits set.
 
     The optional `fenv` attribute describes constraints on the floating-point
     handling of the operation.
@@ -7033,7 +7056,7 @@ def CIR_VecFCmpOp : CIR_Op<"vec.fcmp",
     OptionalAttr<CIR_FenvAttr>:$fenv
   );
 
-  let results = (outs CIR_VectorType:$result);
+  let results = (outs CIR_VectorOfIntOrBoolType:$result);
 
   let assemblyFormat = [{
     `(` enum($kind) `,` $lhs `,` $rhs `)` `:` qualified(type($lhs)) `,`
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index e81db4631eeef4..4e00ff013418c6 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -4403,6 +4403,27 @@ LogicalResult cir::InsertMemberOp::verify() {
   return mlir::success();
 }
 
+//===----------------------------------------------------------------------===//
+// Vector folding helpers
+//===----------------------------------------------------------------------===//
+
+/// Whether every element of \p vec is an \p AttrTy literal. A constant vector
+/// can hold any element of the vector's element type, such as undef or poison,
+/// which the folders do not evaluate.
+template <typename AttrTy>
+static bool hasOnlyLiteralElements(cir::ConstVectorAttr vec) {
+  return llvm::all_of(vec.getElts(), llvm::IsaPred<AttrTy>);
+}
+
+/// The element a vector comparison folds to. A true result is all bits set
+/// (-1 in two's complement), and a false result is all bits clear. For a 1-bit
+/// element type these are the same bit pattern as 1 and 0, respectively.
+static cir::IntAttr getVecCmpResultElement(cir::IntType ty, bool cmpResult) {
+  unsigned width = ty.getWidth();
+  return cir::IntAttr::get(ty, cmpResult ? llvm::APInt::getAllOnes(width)
+                                         : llvm::APInt::getZero(width));
+}
+
 //===----------------------------------------------------------------------===//
 // VecCreateOp
 //===----------------------------------------------------------------------===//
@@ -4496,6 +4517,15 @@ OpFoldResult cir::VecCmpOp::fold(FoldAdaptor adaptor) {
   if (!IntTy)
     return {};
 
+  // FIXME: Fold to a vector of bool too?
+  auto resultElemTy = mlir::dyn_cast<cir::IntType>(getType().getElementType());
+  if (!resultElemTy)
+    return {};
+
+  if (!hasOnlyLiteralElements<cir::IntAttr>(lhsVecAttr) ||
+      !hasOnlyLiteralElements<cir::IntAttr>(rhsVecAttr))
+    return {};
+
   cir::CmpOpKind opKind = adaptor.getKind();
   mlir::ArrayAttr lhsVecElhs = lhsVecAttr.getElts();
   mlir::ArrayAttr rhsVecElhs = rhsVecAttr.getElts();
@@ -4558,11 +4588,7 @@ OpFoldResult cir::VecCmpOp::fold(FoldAdaptor adaptor) {
     }
     }
 
-    // A true result is all bits set (-1 in two's complement), and a false
-    // result is all bits clear. For a 1-bit element type these are the same
-    // bit pattern as 1 and 0, respectively.
-    elements[i] =
-        cir::IntAttr::get(getType().getElementType(), cmpResult ? -1LL : 0LL);
+    elements[i] = getVecCmpResultElement(resultElemTy, cmpResult);
   }
 
   return cir::ConstVectorAttr::get(
@@ -4582,11 +4608,19 @@ OpFoldResult cir::VecFCmpOp::fold(FoldAdaptor adaptor) {
   if (getFenvAttr())
     return {};
 
+  // FIXME: Fold to a vector of bool too?
+  auto resultElemTy = mlir::dyn_cast<cir::IntType>(getType().getElementType());
+  if (!resultElemTy)
+    return {};
+
   cir::FCmpOpKind kind = getKind();
   if (kind == cir::FCmpOpKind::_true || kind == cir::FCmpOpKind::_false) {
+    // A scalable vector cannot be a constant vector.
+    if (getType().isScalable())
+      return {};
+
     mlir::Attribute elt =
-        cir::IntAttr::get(getType().getElementType(),
-                          (kind == cir::FCmpOpKind::_true) ? -1LL : 0LL);
+        getVecCmpResultElement(resultElemTy, kind == cir::FCmpOpKind::_true);
     SmallVector<mlir::Attribute, 16> elements(getType().getFixedSize(), elt);
     return cir::ConstVectorAttr::get(
         getType(), mlir::ArrayAttr::get(getContext(), elements));
@@ -4599,6 +4633,10 @@ OpFoldResult cir::VecFCmpOp::fold(FoldAdaptor adaptor) {
   if (!lhsVecAttr || !rhsVecAttr)
     return {};
 
+  if (!hasOnlyLiteralElements<cir::FPAttr>(lhsVecAttr) ||
+      !hasOnlyLiteralElements<cir::FPAttr>(rhsVecAttr))
+    return {};
+
   mlir::ArrayAttr lhsElts = lhsVecAttr.getElts();
   mlir::ArrayAttr rhsElts = rhsVecAttr.getElts();
   uint64_t vecSize = lhsElts.size();
@@ -4662,11 +4700,7 @@ OpFoldResult cir::VecFCmpOp::fold(FoldAdaptor adaptor) {
       llvm_unreachable("Op kind have been handled already");
     }
 
-    // A true result is all bits set (-1 in two's complement), and a false
-    // result is all bits clear. For a 1-bit element type these are the same
-    // bit pattern as 1 and 0, respectively.
-    elements.push_back(
-        cir::IntAttr::get(getType().getElementType(), cmpResult ? -1LL : 0LL));
+    elements.push_back(getVecCmpResultElement(resultElemTy, cmpResult));
   }
 
   return cir::ConstVectorAttr::get(
@@ -4759,6 +4793,8 @@ OpFoldResult cir::VecShuffleDynamicOp::fold(FoldAdaptor adaptor) {
       mlir::isa_and_nonnull<cir::ConstVectorAttr>(indices)) {
     auto vecAttr = mlir::cast<cir::ConstVectorAttr>(vec);
     auto indicesAttr = mlir::cast<cir::ConstVectorAttr>(indices);
+    if (!hasOnlyLiteralElements<cir::IntAttr>(indicesAttr))
+      return {};
 
     mlir::ArrayAttr vecElts = vecAttr.getElts();
     mlir::ArrayAttr indicesElts = indicesAttr.getElts();
@@ -4772,6 +4808,10 @@ OpFoldResult cir::VecShuffleDynamicOp::fold(FoldAdaptor adaptor) {
     for (const auto &idxAttr : indicesElts.getAsRange<cir::IntAttr>()) {
       uint64_t idxValue = idxAttr.getUInt();
       uint64_t newIdx = idxValue & maskBits;
+      // The mask rounds the element count up to a power of two, so a masked
+      // index can still be out of range.
+      if (newIdx >= numElements)
+        return {};
       elements.push_back(vecElts[newIdx]);
     }
 
@@ -4821,6 +4861,9 @@ OpFoldResult cir::VecTernaryOp::fold(FoldAdaptor adaptor) {
   auto condVec = mlir::cast<cir::ConstVectorAttr>(cond);
   auto lhsVec = mlir::cast<cir::ConstVectorAttr>(lhs);
   auto rhsVec = mlir::cast<cir::ConstVectorAttr>(rhs);
+  // FIXME: Fold a vector of bool condition too?
+  if (!hasOnlyLiteralElements<cir::IntAttr>(condVec))
+    return {};
 
   mlir::ArrayAttr condElts = condVec.getElts();
 
diff --git a/clang/test/CIR/CodeGen/vector-cmp-unsigned-char.cpp b/clang/test/CIR/CodeGen/vector-cmp-unsigned-char.cpp
new file mode 100644
index 00000000000000..df81d1d2aa5d48
--- /dev/null
+++ b/clang/test/CIR/CodeGen/vector-cmp-unsigned-char.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-signed-char -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-signed-char -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-signed-char -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
+
+// Comparing vectors of char gives a vector of char, which is unsigned here.
+
+typedef char vc4 __attribute__((ext_vector_type(4)));
+
+vc4 result;
+
+void cmp(vc4 a, vc4 b) { result = a < b; }
+
+// CIR-LABEL: cir.func{{.*}} @_Z3cmpDv4_cS_(
+// CIR: cir.vec.cmp(lt, %{{.*}}, %{{.*}}) : !cir.vector<4 x !u8i>, !cir.vector<4 x !u8i>
+
+void cmp_constant() {
+  constexpr vc4 a = {1, 3, 5, 200};
+  constexpr vc4 b = {2, 2, 6, 100};
+  result = a < b;
+}
+
+// CIR-LABEL: cir.func{{.*}} @_Z12cmp_constantv()
+// CIR: cir.const #cir.const_vector<[#cir.int<255> : !u8i, #cir.int<0> : !u8i, #cir.int<255> : !u8i, #cir.int<0> : !u8i]> : !cir.vector<4 x !u8i>
+
+// LLVM-LABEL: define{{.*}} @_Z12cmp_constantv()
+// LLVM: store <4 x i8> <i8 -1, i8 0, i8 -1, i8 0>, ptr @result
diff --git a/clang/test/CIR/IR/invalid-vector.cir b/clang/test/CIR/IR/invalid-vector.cir
index f025365573f955..264dc0be02a8ec 100644
--- a/clang/test/CIR/IR/invalid-vector.cir
+++ b/clang/test/CIR/IR/invalid-vector.cir
@@ -316,3 +316,85 @@ module {
     cir.return
   }
 }
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @vec_cmp_one(%a: !cir.vector<4 x !s32i>, %b: !cir.vector<4 x !s32i>) {
+    // expected-error @below {{'cir.vec.cmp' op attribute 'kind' failed to satisfy constraint: compare operation kind whose value is none of {one, uno}}}
+    %0 = cir.vec.cmp(one, %a, %b) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+    cir.return
+  }
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @vec_cmp_uno(%a: !cir.vector<4 x !s32i>, %b: !cir.vector<4 x !s32i>) {
+    // expected-error @below {{'cir.vec.cmp' op attribute 'kind' failed to satisfy constraint: compare operation kind whose value is none of {one, uno}}}
+    %0 = cir.vec.cmp(uno, %a, %b) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+    cir.return
+  }
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @vec_cmp_float_result(%a: !cir.vector<4 x !s32i>, %b: !cir.vector<4 x !s32i>) {
+    // expected-error @below {{'cir.vec.cmp' op result #0 must be vector of any of boolean type, integer type, but got '!cir.vector<4 x !cir.float>'}}
+    %0 = cir.vec.cmp(lt, %a, %b) : !cir.vector<4 x !s32i>, !cir.vector<4 x !cir.float>
+    cir.return
+  }
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @vec_cmp_result_length_mismatch(%a: !cir.vector<4 x !s32i>, %b: !cir.vector<4 x !s32i>) {
+    // expected-error @below {{'cir.vec.cmp' op failed to verify that all of {lhs, result} have same element count}}
+    %0 = cir.vec.cmp(lt, %a, %b) : !cir.vector<4 x !s32i>, !cir.vector<2 x !s32i>
+    cir.return
+  }
+}
+
+// -----
+
+module {
+  cir.func @vec_fcmp_float_result(%a: !cir.vector<4 x !cir.float>, %b: !cir.vector<4 x !cir.float>) {
+    // expected-error @below {{'cir.vec.fcmp' op result #0 must be vector of any of boolean type, integer type, but got '!cir.vector<4 x !cir.float>'}}
+    %0 = cir.vec.fcmp(olt, %a, %b) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !cir.float>
+    cir.return
+  }
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @vec_fcmp_result_length_mismatch(%a: !cir.vector<4 x !cir.float>, %b: !cir.vector<4 x !cir.float>) {
+    // expected-error @below {{'cir.vec.fcmp' op failed to verify that all of {lhs, result} have same element count}}
+    %0 = cir.vec.fcmp(olt, %a, %b) : !cir.vector<4 x !cir.float>, !cir.vector<8 x !s32i>
+    cir.return
+  }
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @vec_fcmp_scalable_result(%a: !cir.vector<4 x !cir.float>, %b: !cir.vector<4 x !cir.float>) {
+    // expected-error @below {{'cir.vec.fcmp' op failed to verify that all of {lhs, result} have same element count}}
+    %0 = cir.vec.fcmp(true, %a, %b) : !cir.vector<4 x !cir.float>, !cir.vector<[4] x !s32i, sve_data>
+    cir.return
+  }
+}
diff --git a/clang/test/CIR/Transforms/vector-cmp-fold.cir b/clang/test/CIR/Transforms/vector-cmp-fold.cir
index dc5f4118587a59..72c3f3a379528f 100644
--- a/clang/test/CIR/Transforms/vector-cmp-fold.cir
+++ b/clang/test/CIR/Transforms/vector-cmp-fold.cir
@@ -497,6 +497,146 @@ module  {
 
 !s32i = !cir.int<s, 32>
 
+module  {
+  cir.func @no_fold_cmp_vector_with_undef_element() -> !cir.vector<4 x !s32i> {
+    %vec_1 = cir.const #cir.const_vector<[#cir.undef : !s32i, #cir.int<2> : !s32i,
+      #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
+    %vec_2 = cir.const #cir.const_vector<[#cir.int<5> : !s32i, #cir.int<6> : !s32i,
+      #cir.int<7> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<4 x !s32i>
+    %new_vec = cir.vec.cmp(lt, %vec_1, %vec_2) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+    cir.return %new_vec : !cir.vector<4 x !s32i>
+  }
+
+  // An undef element is not evaluated, so the comparison stays.
+  // CHECK-LABEL: cir.func{{.*}} @no_fold_cmp_vector_with_undef_element
+  // CHECK: %[[RES:.*]] = cir.vec.cmp(lt, %{{.*}}, %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i>
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module  {
+  cir.func @no_fold_fcmp_vector_with_poison_element() -> !cir.vector<4 x !s32i> {
+    %vec_1 = cir.const #cir.const_vector<[#cir.fp<1.000000e+00> : !cir.float, #cir.fp<2.000000e+00>
+      : !cir.float, #cir.fp<3.000000e+00> : !cir.float, #cir.fp<4.000000e+00> : !cir.float]> : !cir.vector<4 x !cir.float>
+    %vec_2 = cir.const #cir.const_vector<[#cir.fp<2.000000e+00> : !cir.float, #cir.poison
+      : !cir.float, #cir.fp<3.000000e+00> : !cir.float, #cir.fp<1.000000e+00> : !cir.float]> : !cir.vector<4 x !cir.float>
+    %new_vec = cir.vec.fcmp(olt, %vec_1, %vec_2) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i>
+    cir.return %new_vec : !cir.vector<4 x !s32i>
+  }
+
+  // A poison element is not evaluated, so the comparison stays.
+  // CHECK-LABEL: cir.func{{.*}} @no_fold_fcmp_vector_with_poison_element
+  // CHECK: %[[RES:.*]] = cir.vec.fcmp(olt, %{{.*}}, %{{.*}}) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i>
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module  {
+  cir.func @no_fold_true_fcmp_scalable_vector(
+      %a: !cir.vector<[4] x !cir.float, sve_data>,
+      %b: !cir.vector<[4] x !cir.float, sve_data>)
+      -> !cir.vector<[4] x !s32i, sve_data> {
+    %new_vec = cir.vec.fcmp(true, %a, %b) :
+        !cir.vector<[4] x !cir.float, sve_data>, !cir.vector<[4] x !s32i, sve_data>
+    cir.return %new_vec : !cir.vector<[4] x !s32i, sve_data>
+  }
+
+  // A scalable vector cannot be a constant vector, so the comparison stays.
+  // CHECK-LABEL: cir.func{{.*}} @no_fold_true_fcmp_scalable_vector
+  // CHECK: %[[RES:.*]] = cir.vec.fcmp(true, %{{.*}}, %{{.*}}) :
+  // CHECK-SAME: !cir.vector<[4] x !cir.float, sve_data>, !cir.vector<[4] x !s32i, sve_data>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<[4] x !s32i, sve_data>
+}
+
+// -----
+
+!u8i = !cir.int<u, 8>
+!u32i = !cir.int<u, 32>
+
+module  {
+  cir.func @fold_cmp_vector_unsigned_result() -> !cir.vector<4 x !u8i> {
+    %vec_1 = cir.const #cir.const_vector<[#cir.int<1> : !u8i, #cir.int<3> : !u8i,
+      #cir.int<5> : !u8i, #cir.int<200> : !u8i]> : !cir.vector<4 x !u8i>
+    %vec_2 = cir.const #cir.const_vector<[#cir.int<2> : !u8i, #cir.int<2> : !u8i,
+      #cir.int<6> : !u8i, #cir.int<100> : !u8i]> : !cir.vector<4 x !u8i>
+    %new_vec = cir.vec.cmp(lt, %vec_1, %vec_2) : !cir.vector<4 x !u8i>, !cir.vector<4 x !u8i>
+    cir.return %new_vec : !cir.vector<4 x !u8i>
+  }
+
+  // A true element of an unsigned result has every bit set too.
+  // CHECK-LABEL: cir.func{{.*}} @fold_cmp_vector_unsigned_result
+  // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<255> : !u8i, #cir.int<0> : !u8i,
+  // CHECK-SAME: #cir.int<255> : !u8i, #cir.int<0> : !u8i]> : !cir.vector<4 x !u8i>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !u8i>
+
+  cir.func @fold_fcmp_vector_unsigned_result() -> !cir.vector<4 x !u32i> {
+    %vec_1 = cir.const #cir.const_vector<[#cir.fp<1.000000e+00> : !cir.float, #cir.fp<2.000000e+00>
+      : !cir.float, #cir.fp<3.000000e+00> : !cir.float, #cir.fp<4.000000e+00> : !cir.float]> : !cir.vector<4 x !cir.float>
+    %vec_2 = cir.const #cir.const_vector<[#cir.fp<2.000000e+00> : !cir.float, #cir.fp<1.000000e+00>
+      : !cir.float, #cir.fp<3.000000e+00> : !cir.float, #cir.fp<5.000000e+00> : !cir.float]> : !cir.vector<4 x !cir.float>
+    %new_vec = cir.vec.fcmp(olt, %vec_1, %vec_2) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !u32i>
+    cir.return %new_vec : !cir.vector<4 x !u32i>
+  }
+
+  // CHECK-LABEL: cir.func{{.*}} @fold_fcmp_vector_unsigned_result
+  // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<4294967295> : !u32i, #cir.int<0> : !u32i,
+  // CHECK-SAME: #cir.int<0> : !u32i, #cir.int<4294967295> : !u32i]> : !cir.vector<4 x !u32i>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !u32i>
+
+  cir.func @fold_true_fcmp_vector_unsigned_result(
+      %a: !cir.vector<2 x !cir.float>, %b: !cir.vector<2 x !cir.float>)
+      -> !cir.vector<2 x !u32i> {
+    %new_vec = cir.vec.fcmp(true, %a, %b) : !cir.vector<2 x !cir.float>, !cir.vector<2 x !u32i>
+    cir.return %new_vec : !cir.vector<2 x !u32i>
+  }
+
+  // CHECK-LABEL: cir.func{{.*}} @fold_true_fcmp_vector_unsigned_result
+  // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<4294967295> : !u32i,
+  // CHECK-SAME: #cir.int<4294967295> : !u32i]> : !cir.vector<2 x !u32i>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<2 x !u32i>
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
+module  {
+  cir.func @no_fold_cmp_vector_bool_result() -> !cir.vector<4 x !cir.bool> {
+    %vec_1 = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<3> : !s32i,
+      #cir.int<5> : !s32i, #cir.int<7> : !s32i]> : !cir.vector<4 x !s32i>
+    %vec_2 = cir.const #cir.const_vector<[#cir.int<2> : !s32i, #cir.int<2> : !s32i,
+      #cir.int<6> : !s32i, #cir.int<6> : !s32i]> : !cir.vector<4 x !s32i>
+    %new_vec = cir.vec.cmp(lt, %vec_1, %vec_2) : !cir.vector<4 x !s32i>, !cir.vector<4 x !cir.bool>
+    cir.return %new_vec : !cir.vector<4 x !cir.bool>
+  }
+
+  // FIXME: A comparison is not folded to a vector of bool yet.
+  // CHECK-LABEL: cir.func{{.*}} @no_fold_cmp_vector_bool_result
+  // CHECK: %[[RES:.*]] = cir.vec.cmp(lt, %{{.*}}, %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !cir.bool>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !cir.bool>
+
+  cir.func @no_fold_true_fcmp_vector_bool_result(
+      %a: !cir.vector<4 x !cir.float>, %b: !cir.vector<4 x !cir.float>)
+      -> !cir.vector<4 x !cir.bool> {
+    %new_vec = cir.vec.fcmp(true, %a, %b) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !cir.bool>
+    cir.return %new_vec : !cir.vector<4 x !cir.bool>
+  }
+
+  // CHECK-LABEL: cir.func{{.*}} @no_fold_true_fcmp_vector_bool_result
+  // CHECK: %[[RES:.*]] = cir.vec.fcmp(true, %{{.*}}, %{{.*}}) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !cir.bool>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !cir.bool>
+}
+
+// -----
+
+!s32i = !cir.int<s, 32>
+
 module  {
   cir.func @no_fold_fcmp_vector_with_fenv() -> !cir.vector<4 x !s32i> {
     %vec_1 = cir.const #cir.const_vector<[#cir.fp<1.000000e+00> : !cir.float, #cir.fp<0x7FC00000>
diff --git a/clang/test/CIR/Transforms/vector-shuffle-dynamic-fold.cir b/clang/test/CIR/Transforms/vector-shuffle-dynamic-fold.cir
index 6584df3d7050bd..bc01e2f5defa44 100644
--- a/clang/test/CIR/Transforms/vector-shuffle-dynamic-fold.cir
+++ b/clang/test/CIR/Transforms/vector-shuffle-dynamic-fold.cir
@@ -26,6 +26,31 @@ module {
   // CHECK: cir.func{{.*}} @fold_shuffle_dynamic_vector_op_test_2() -> !cir.vector<4 x !s32i> {
   // CHECK-NEXT: %[[NEW_VEC:.*]] = cir.const #cir.const_vector<[#cir.int<4> : !s32i, #cir.int<3> : !s32i, #cir.int<2> : !s32i, #cir.int<1> : !s32i]> : !cir.vector<4 x !s32i>
   // CHECK-NEXT: cir.return %[[NEW_VEC:.*]] : !cir.vector<4 x !s32i>
+
+  cir.func @no_fold_shuffle_dynamic_undef_index_test() -> !cir.vector<4 x !s32i> {
+    %vec = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
+    %indices = cir.const #cir.const_vector<[#cir.undef : !s32i, #cir.int<2> : !s32i, #cir.int<1> : !s32i, #cir.int<0> : !s32i]> : !cir.vector<4 x !s32i>
+    %new_vec = cir.vec.shuffle.dynamic %vec : !cir.vector<4 x !s32i>, %indices : !cir.vector<4 x !s32i>
+    cir.return %new_vec : !cir.vector<4 x !s32i>
+  }
+
+  // An undef index is not evaluated, so the shuffle stays.
+  // CHECK-LABEL: cir.func{{.*}} @no_fold_shuffle_dynamic_undef_index_test() -> !cir.vector<4 x !s32i> {
+  // CHECK: %[[NEW_VEC:.*]] = cir.vec.shuffle.dynamic %{{.*}} : !cir.vector<4 x !s32i>, %{{.*}} : !cir.vector<4 x !s32i>
+  // CHECK-NEXT: cir.return %[[NEW_VEC]] : !cir.vector<4 x !s32i>
+
+  cir.func @no_fold_shuffle_dynamic_out_of_range_index_test() -> !cir.vector<3 x !s32i> {
+    %vec = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i]> : !cir.vector<3 x !s32i>
+    %indices = cir.const #cir.const_vector<[#cir.int<3> : !s32i, #cir.int<0> : !s32i, #cir.int<1> : !s32i]> : !cir.vector<3 x !s32i>
+    %new_vec = cir.vec.shuffle.dynamic %vec : !cir.vector<3 x !s32i>, %indices : !cir.vector<3 x !s32i>
+    cir.return %new_vec : !cir.vector<3 x !s32i>
+  }
+
+  // The index mask for 3 elements is 3, so index 3 stays out of range and
+  // the shuffle is not folded.
+  // CHECK-LABEL: cir.func{{.*}} @no_fold_shuffle_dynamic_out_of_range_index_test() -> !cir.vector<3 x !s32i> {
+  // CHECK: %[[NEW_VEC:.*]] = cir.vec.shuffle.dynamic %{{.*}} : !cir.vector<3 x !s32i>, %{{.*}} : !cir.vector<3 x !s32i>
+  // CHECK-NEXT: cir.return %[[NEW_VEC]] : !cir.vector<3 x !s32i>
 }
 
 
diff --git a/clang/test/CIR/Transforms/vector-ternary-fold.cir b/clang/test/CIR/Transforms/vector-ternary-fold.cir
index 3de614a39264fe..8b8f0dba8e0a21 100644
--- a/clang/test/CIR/Transforms/vector-ternary-fold.cir
+++ b/clang/test/CIR/Transforms/vector-ternary-fold.cir
@@ -15,6 +15,32 @@ module {
   // CHECK: cir.func{{.*}} @vector_ternary_fold_test() -> !cir.vector<4 x !s32i> {
   // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<6> : !s32i, #cir.int<3> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<4 x !s32i>
   // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i>
+
+  cir.func @vector_ternary_undef_cond_test() -> !cir.vector<4 x !s32i> {
+    %cond = cir.const #cir.const_vector<[#cir.undef : !s32i, #cir.int<0> : !s32i, #cir.int<1> : !s32i, #cir.int<0> : !s32i]> : !cir.vector<4 x !s32i>
+    %lhs = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
+    %rhs = cir.const #cir.const_vector<[#cir.int<5> : !s32i, #cir.int<6> : !s32i, #cir.int<7> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<4 x !s32i>
+    %res = cir.vec.ternary(%cond, %lhs, %rhs) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+    cir.return %res : !cir.vector<4 x !s32i>
+  }
+
+  // An undef condition element is not evaluated, so the operation stays.
+  // CHECK-LABEL: cir.func{{.*}} @vector_ternary_undef_cond_test() -> !cir.vector<4 x !s32i> {
+  // CHECK: %[[RES:.*]] = cir.vec.ternary(%{{.*}}, %{{.*}}, %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i>
+
+  cir.func @vector_ternary_bool_cond_test() -> !cir.vector<4 x !s32i> {
+    %cond = cir.const #cir.const_vector<[#cir.bool<true> : !cir.bool, #cir.bool<false> : !cir.bool, #cir.bool<true> : !cir.bool, #cir.bool<false> : !cir.bool]> : !cir.vector<4 x !cir.bool>
+    %lhs = cir.const #cir.const_vector<[#cir.int<1> : !s32i, #cir.int<2> : !s32i, #cir.int<3> : !s32i, #cir.int<4> : !s32i]> : !cir.vector<4 x !s32i>
+    %rhs = cir.const #cir.const_vector<[#cir.int<5> : !s32i, #cir.int<6> : !s32i, #cir.int<7> : !s32i, #cir.int<8> : !s32i]> : !cir.vector<4 x !s32i>
+    %res = cir.vec.ternary(%cond, %lhs, %rhs) : !cir.vector<4 x !cir.bool>, !cir.vector<4 x !s32i>
+    cir.return %res : !cir.vector<4 x !s32i>
+  }
+
+  // FIXME: A vector of bool condition is not folded yet.
+  // CHECK-LABEL: cir.func{{.*}} @vector_ternary_bool_cond_test() -> !cir.vector<4 x !s32i> {
+  // CHECK: %[[RES:.*]] = cir.vec.ternary(%{{.*}}, %{{.*}}, %{{.*}}) : !cir.vector<4 x !cir.bool>, !cir.vector<4 x !s32i>
+  // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i>
 }
 
 



More information about the cfe-commits mailing list