[clang] [CIR][X86]Implement handling for Select/Selectsh builtins in CIR (PR #174003)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 5 10:24:51 PST 2026


================
@@ -184,6 +185,53 @@ static mlir::Value emitX86Select(CIRGenBuilderTy &builder, mlir::Location loc,
   return cir::VecTernaryOp::create(builder, loc, mask, op0, op1);
 }
 
+static mlir::Value emitX86ScalarSelect(CIRGenBuilderTy &builder,
+                                       mlir::Location loc, mlir::Value mask,
+                                       mlir::Value op0, mlir::Value op1) {
+
+  // If the mask is all ones just return first argument.
+  if (auto c = mlir::dyn_cast_or_null<cir::ConstantOp>(mask.getDefiningOp()))
+    if (c.isAllOnesValue())
+      return op0;
+
+  // Extract the scalar values from the vector operands
+  auto vecTy0 = mlir::dyn_cast<cir::VectorType>(op0.getType());
+  auto vecTy1 = mlir::dyn_cast<cir::VectorType>(op1.getType());
+
+  mlir::Value scalar0 = op0;
+  mlir::Value scalar1 = op1;
+
+  if (vecTy0)
+    scalar0 = builder.createExtractElement(loc, op0, uint64_t(0));
+
+  if (vecTy1)
+    scalar1 = builder.createExtractElement(loc, op1, uint64_t(0));
+
+  // Get the mask as a vector of i1 and extract bit 0
+  auto intTy = mlir::dyn_cast<cir::IntType>(mask.getType());
+  assert(intTy && "mask must be an integer type");
+  unsigned width = intTy.getWidth();
+
+  auto i1Ty = builder.getUIntNTy(1);
+  auto maskVecTy = cir::VectorType::get(i1Ty, width);
+  mlir::Value maskVec = builder.createBitcast(mask, maskVecTy);
+
+  // Extract bit 0 from the mask vector
+  mlir::Value bit0 = builder.createExtractElement(loc, maskVec, uint64_t(0));
----------------
andykaylor wrote:

Can you bundle all of this logic to extract the zero-bit of the mask as a boolean into a separate helper function? It seems like a good candidate for future simplification. The optimizer is going to reduce this to `trunc i8 %mask to i1`. We don't currently have an integer `trunc` operation, and we probably want to keep the nature of what we're doing here visible in CIR, but all of this casting and extraction feels quite verbose.

https://github.com/llvm/llvm-project/pull/174003


More information about the cfe-commits mailing list