Skip to content

Conversation

@Priyanshu3820
Copy link
Contributor

@Priyanshu3820 Priyanshu3820 commented Dec 15, 2025

Related to: #167765

@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Dec 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 15, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clangir

Author: Priyanshu Kumar (Priyanshu3820)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/172299.diff

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp (+20)
  • (added) clang/test/CIR/CodeGenBuiltins/X86/avx512-select-builtins.c (+292)
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
index fb17e31bf36d6..b3182fc83776e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
@@ -370,6 +370,22 @@ static mlir::Value emitX86vpcom(CIRGenBuilderTy &builder, mlir::Location loc,
   return builder.createVecCompare(loc, pred, op0, op1);
 }
 
+static mlir::Value emitX86Select(CIRGenBuilderTy &builder, mlir::Location loc,
+                                 mlir::Value mask, mlir::Value Op0,
+                                 mlir::Value Op1) {
+  return builder.create<cir::VecTernaryOp>(loc, Op0.getType(), mask, Op0, Op1);
+}
+
+static mlir::Value emitX86ScalarSelect(CIRGenBuilderTy &builder,
+                                       mlir::Location loc, mlir::Value mask,
+                                       mlir::Value Op0, mlir::Value Op1) {
+
+  mlir::Value zero = builder.getZero(mask.getType(), loc);
+  mlir::Value cond = builder.createCompare(loc, CmpOpKind::ne, mask, zero);
+
+  return builder.createSelect(loc, Op0.getType(), cond, Op0, Op1);
+}
+
 mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
                                                const CallExpr *expr) {
   if (builtinID == Builtin::BI__builtin_cpu_is) {
@@ -1186,10 +1202,14 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
   case X86::BI__builtin_ia32_selectpd_128:
   case X86::BI__builtin_ia32_selectpd_256:
   case X86::BI__builtin_ia32_selectpd_512:
+    return emitX86Select(builder, getLoc(expr->getExprLoc()), ops[0], ops[1],
+                         ops[2]);
   case X86::BI__builtin_ia32_selectsh_128:
   case X86::BI__builtin_ia32_selectsbf_128:
   case X86::BI__builtin_ia32_selectss_128:
   case X86::BI__builtin_ia32_selectsd_128:
+    return emitX86ScalarSelect(builder, getLoc(expr->getExprLoc()), ops[0],
+                               ops[1], ops[2]);
   case X86::BI__builtin_ia32_cmpb128_mask:
   case X86::BI__builtin_ia32_cmpb256_mask:
   case X86::BI__builtin_ia32_cmpb512_mask:
diff --git a/clang/test/CIR/CodeGenBuiltins/X86/avx512-select-builtins.c b/clang/test/CIR/CodeGenBuiltins/X86/avx512-select-builtins.c
new file mode 100644
index 0000000000000..68edcbee8fcb6
--- /dev/null
+++ b/clang/test/CIR/CodeGenBuiltins/X86/avx512-select-builtins.c
@@ -0,0 +1,292 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +avx512bw -target-feature +avx512vl -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +avx512bw -target-feature +avx512vl -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -target-feature +avx512bw -target-feature +avx512vl -emit-llvm %s -o - | FileCheck %s --check-prefix=OGCG
+
+// REQUIRES: avx512bw
+// REQUIRES: avx512vl
+
+#include <immintrin.h>
+
+// CIR-LABEL: test_selectb_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectb_128
+// LLVM: select <16 x i8>
+// OGCG-LABEL: test_selectb_128
+// OGCG: select <16 x i8>
+__m128i test_selectb_128(__mmask16 k, __m128i a, __m128i b) {
+  return _mm_selectb_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectb_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectb_256
+// LLVM: select <32 x i8>
+// OGCG-LABEL: test_selectb_256
+// OGCG: select <32 x i8>
+__m256i test_selectb_256(__mmask32 k, __m256i a, __m256i b) {
+  return _mm256_selectb_epi8(k, a, b);
+}
+
+// CIR-LABEL: test_selectb_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectb_512
+// LLVM: select <64 x i8>
+// OGCG-LABEL: test_selectb_512
+// OGCG: select <64 x i8>
+__m512i test_selectb_512(__mmask64 k, __m512i a, __m512i b) {
+  return _mm512_selectb_epi8(k, a, b);
+}
+
+// CIR-LABEL: test_selectw_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectw_128
+// LLVM: select <8 x i16>
+// OGCG-LABEL: test_selectw_128
+// OGCG: select <8 x i16>
+__m128i test_selectw_128(__mmask8 k, __m128i a, __m128i b) {
+  return _mm_selectw_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectw_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectw_256
+// LLVM: select <16 x i16>
+// OGCG-LABEL: test_selectw_256
+// OGCG: select <16 x i16>
+__m256i test_selectw_256(__mmask16 k, __m256i a, __m256i b) {
+  return _mm256_selectw_epi16(k, a, b);
+}
+
+// CIR-LABEL: test_selectw_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectw_512
+// LLVM: select <32 x i16>
+// OGCG-LABEL: test_selectw_512
+// OGCG: select <32 x i16>
+__m512i test_selectw_512(__mmask32 k, __m512i a, __m512i b) {
+  return _mm512_selectw_epi16(k, a, b);
+}
+
+// CIR-LABEL: test_selectd_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectd_128
+// LLVM: select <4 x i32>
+// OGCG-LABEL: test_selectd_128
+// OGCG: select <4 x i32>
+__m128i test_selectd_128(__mmask4 k, __m128i a, __m128i b) {
+  return _mm_selectd_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectd_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectd_256
+// LLVM: select <8 x i32>
+// OGCG-LABEL: test_selectd_256
+// OGCG: select <8 x i32>
+__m256i test_selectd_256(__mmask8 k, __m256i a, __m256i b) {
+  return _mm256_selectd_epi32(k, a, b);
+}
+
+// CIR-LABEL: test_selectd_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectd_512
+// LLVM: select <16 x i32>
+// OGCG-LABEL: test_selectd_512
+// OGCG: select <16 x i32>
+__m512i test_selectd_512(__mmask16 k, __m512i a, __m512i b) {
+  return _mm512_selectd_epi32(k, a, b);
+}
+
+// CIR-LABEL: test_selectq_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectq_128
+// LLVM: select <2 x i64>
+// OGCG-LABEL: test_selectq_128
+// OGCG: select <2 x i64>
+__m128i test_selectq_128(__mmask2 k, __m128i a, __m128i b) {
+  return _mm_selectq_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectq_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectq_256
+// LLVM: select <4 x i64>
+// OGCG-LABEL: test_selectq_256
+// OGCG: select <4 x i64>
+__m256i test_selectq_256(__mmask4 k, __m256i a, __m256i b) {
+  return _mm256_selectq_epi64(k, a, b);
+}
+
+// CIR-LABEL: test_selectq_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectq_512
+// LLVM: select <8 x i64>
+// OGCG-LABEL: test_selectq_512
+// OGCG: select <8 x i64>
+__m512i test_selectq_512(__mmask8 k, __m512i a, __m512i b) {
+  return _mm512_selectq_epi64(k, a, b);
+}
+
+// CIR-LABEL: test_selectph_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectph_128
+// LLVM: select
+// OGCG-LABEL: test_selectph_128
+// OGCG: select
+__m128i test_selectph_128(__mmask8 k, __m128i a, __m128i b) {
+  return _mm_selectph_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectph_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectph_256
+// LLVM: select
+// OGCG-LABEL: test_selectph_256
+// OGCG: select
+__m256i test_selectph_256(__mmask16 k, __m256i a, __m256i b) {
+  return _mm256_selectph_epi16(k, a, b);
+}
+
+// CIR-LABEL: test_selectph_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectph_512
+// LLVM: select
+// OGCG-LABEL: test_selectph_512
+// OGCG: select
+__m512i test_selectph_512(__mmask32 k, __m512i a, __m512i b) {
+  return _mm512_selectph_epi16(k, a, b);
+}
+
+// CIR-LABEL: test_selectpbf_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectpbf_128
+// LLVM: select
+// OGCG-LABEL: test_selectpbf_128
+// OGCG: select
+__m128i test_selectpbf_128(__mmask8 k, __m128i a, __m128i b) {
+  return _mm_selectpbf_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectpbf_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectpbf_256
+// LLVM: select
+// OGCG-LABEL: test_selectpbf_256
+// OGCG: select
+__m256i test_selectpbf_256(__mmask16 k, __m256i a, __m256i b) {
+  return _mm256_selectpbf_epi16(k, a, b);
+}
+
+// CIR-LABEL: test_selectpbf_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectpbf_512
+// LLVM: select
+// OGCG-LABEL: test_selectpbf_512
+// OGCG: select
+__m512i test_selectpbf_512(__mmask32 k, __m512i a, __m512i b) {
+  return _mm512_selectpbf_epi16(k, a, b);
+}
+
+// CIR-LABEL: test_selectps_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectps_128
+// LLVM: select
+// OGCG-LABEL: test_selectps_128
+// OGCG: select
+__m128 test_selectps_128(__mmask8 k, __m128 a, __m128 b) {
+  return _mm_selectps_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectps_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectps_256
+// LLVM: select
+// OGCG-LABEL: test_selectps_256
+// OGCG: select
+__m256 test_selectps_256(__mmask8 k, __m256 a, __m256 b) {
+  return _mm256_selectps(k, a, b);
+}
+
+// CIR-LABEL: test_selectps_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectps_512
+// LLVM: select
+// OGCG-LABEL: test_selectps_512
+// OGCG: select
+__m512 test_selectps_512(__mmask16 k, __m512 a, __m512 b) {
+  return _mm512_selectps(k, a, b);
+}
+
+// CIR-LABEL: test_selectpd_128
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectpd_128
+// LLVM: select
+// OGCG-LABEL: test_selectpd_128
+// OGCG: select
+__m128d test_selectpd_128(__mmask8 k, __m128d a, __m128d b) {
+  return _mm_selectpd_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectpd_256
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectpd_256
+// LLVM: select
+// OGCG-LABEL: test_selectpd_256
+// OGCG: select
+__m256d test_selectpd_256(__mmask8 k, __m256d a, __m256d b) {
+  return _mm256_selectpd(k, a, b);
+}
+
+// CIR-LABEL: test_selectpd_512
+// CIR: cir.vec.ternary
+// LLVM-LABEL: test_selectpd_512
+// LLVM: select
+// OGCG-LABEL: test_selectpd_512
+// OGCG: select
+__m512d test_selectpd_512(__mmask8 k, __m512d a, __m512d b) {
+  return _mm512_selectpd(k, a, b);
+}
+
+// CIR-LABEL: test_selectsh_128
+// CIR: cir.cmp {{.*}} ne
+// CIR: cir.select
+// LLVM-LABEL: test_selectsh_128
+// LLVM: select
+// OGCG-LABEL: test_selectsh_128
+// OGCG: select
+__m128i test_selectsh_128(unsigned short k, __m128i a, __m128i b) {
+  return _mm_selectsh_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectsbf_128
+// CIR: cir.cmp {{.*}} ne
+// CIR: cir.select
+// LLVM-LABEL: test_selectsbf_128
+// LLVM: select
+// OGCG-LABEL: test_selectsbf_128
+// OGCG: select
+__m128i test_selectsbf_128(unsigned short k, __m128i a, __m128i b) {
+  return _mm_selectsbf_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectss_128
+// CIR: cir.cmp {{.*}} ne
+// CIR: cir.select
+// LLVM-LABEL: test_selectss_128
+// LLVM: select
+// OGCG-LABEL: test_selectss_128
+// OGCG: select
+__m128 test_selectss_128(unsigned short k, __m128 a, __m128 b) {
+  return _mm_selectss_128(k, a, b);
+}
+
+// CIR-LABEL: test_selectsd_128
+// CIR: cir.cmp {{.*}} ne
+// CIR: cir.select
+// LLVM-LABEL: test_selectsd_128
+// LLVM: select
+// OGCG-LABEL: test_selectsd_128
+// OGCG: select
+__m128d test_selectsd_128(unsigned short k, __m128d a, __m128d b) {
+  return _mm_selectsd_128(k, a, b);
+}

@github-actions
Copy link

github-actions bot commented Dec 15, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@github-actions
Copy link

github-actions bot commented Dec 15, 2025

🐧 Linux x64 Test Results

  • 3057 tests passed
  • 7 tests skipped

All executed tests passed, but another part of the build failed. Click on a failure below to see the details.

tools/clang/lib/CIR/CodeGen/CMakeFiles/obj.clangCIR.dir/CIRGenBuiltinX86.cpp.o
FAILED: tools/clang/lib/CIR/CodeGen/CMakeFiles/obj.clangCIR.dir/CIRGenBuiltinX86.cpp.o
sccache /opt/llvm/bin/clang++ -DCLANG_EXPORTS -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GLIBCXX_USE_CXX11_ABI=1 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/lib/CIR/CodeGen -I/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen -I/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/include -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/include -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/include -I/home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/include -I/home/gha/actions-runner/_work/llvm-project/llvm-project/llvm/../mlir/include -I/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/mlir/include -gmlt -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wc++98-compat-extra-semi -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wsuggest-override -Wstring-conversion -Wno-pass-failed -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual -Wno-nested-anon-types -O3 -DNDEBUG -std=c++17  -fno-exceptions -funwind-tables -fno-rtti -UNDEBUG -MD -MT tools/clang/lib/CIR/CodeGen/CMakeFiles/obj.clangCIR.dir/CIRGenBuiltinX86.cpp.o -MF tools/clang/lib/CIR/CodeGen/CMakeFiles/obj.clangCIR.dir/CIRGenBuiltinX86.cpp.o.d -o tools/clang/lib/CIR/CodeGen/CMakeFiles/obj.clangCIR.dir/CIRGenBuiltinX86.cpp.o -c /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:433:24: error: no member named 'ExtractElementOp' in namespace 'cir'
433 |   mlir::Value a = cir::ExtractElementOp::create(builder, loc, vecA, index);
|                   ~~~~~^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:434:24: error: no member named 'ExtractElementOp' in namespace 'cir'
434 |   mlir::Value b = cir::ExtractElementOp::create(builder, loc, vecB, index);
|                   ~~~~~^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:440:15: error: no member named 'createBinaryOp' in 'clang::CIRGen::CIRGenBuilderTy'
440 |       builder.createBinaryOp(loc, cir::BinaryOpKind::And, mask, one);
|       ~~~~~~~ ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:440:40: error: no member named 'BinaryOpKind' in namespace 'cir'; did you mean 'UnaryOpKind'?
440 |       builder.createBinaryOp(loc, cir::BinaryOpKind::And, mask, one);
|                                   ~~~~~^~~~~~~~~~~~
|                                        UnaryOpKind
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/include/clang/CIR/Dialect/IR/CIROpsEnums.h.inc:2160:12: note: 'UnaryOpKind' declared here
2160 | enum class UnaryOpKind : uint32_t {
|            ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:440:54: error: no member named 'And' in 'cir::UnaryOpKind'
440 |       builder.createBinaryOp(loc, cir::BinaryOpKind::And, mask, one);
|                                                      ^~~
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:450:15: error: no member named 'InsertElementOp' in namespace 'cir'
450 |   return cir::InsertElementOp::create(builder, loc, vecA, selected, index);
|          ~~~~~^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1371:39: error: use of undeclared identifier 'loc'; did you mean 'log'?
1371 |     ops[0] = getMaskVecValue(builder, loc, ops[0], numElts);
|                                       ^~~
|                                       log
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:104:17: note: 'log' declared here
104 | __MATHCALL_VEC (log,, (_Mdouble_ __x));
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1371:52: error: use of undeclared identifier 'numElts'; did you mean 'VlaSizePair::numElts'?
1371 |     ops[0] = getMaskVecValue(builder, loc, ops[0], numElts);
|                                                    ^~~~~~~
|                                                    VlaSizePair::numElts
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenFunction.h:515:17: note: 'VlaSizePair::numElts' declared here
515 |     mlir::Value numElts;
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1371:52: error: invalid use of non-static data member 'numElts'
1371 |     ops[0] = getMaskVecValue(builder, loc, ops[0], numElts);
|                                                    ^~~~~~~
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1373:41: error: use of undeclared identifier 'loc'; did you mean 'log'?
1373 |     return emitX86VectorSelect(builder, loc, ops[0], ops[1], ops[2], resultTy);
|                                         ^~~
|                                         log
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:104:17: note: 'log' declared here
104 | __MATHCALL_VEC (log,, (_Mdouble_ __x));
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1373:12: error: no matching function for call to 'emitX86VectorSelect'
1373 |     return emitX86VectorSelect(builder, loc, ops[0], ops[1], ops[2], resultTy);
|            ^~~~~~~~~~~~~~~~~~~
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:420:20: note: candidate function not viable: requires 5 arguments, but 6 were provided
420 | static mlir::Value emitX86VectorSelect(CIRGenBuilderTy &builder,
|                    ^                   ~~~~~~~~~~~~~~~~~~~~~~~~~
421 |                                        mlir::Location loc, mlir::Value mask,
|                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
422 |                                        mlir::Value Op0, mlir::Value Op1) {
|                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1384:26: error: no member named 'ExtractElementOp' in namespace 'cir'
1384 |     mlir::Value a = cir::ExtractElementOp::create(builder, loc, ops[1], idx0);
|                     ~~~~~^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1385:26: error: no member named 'ExtractElementOp' in namespace 'cir'
1385 |     mlir::Value b = cir::ExtractElementOp::create(builder, loc, ops[2], idx0);
|                     ~~~~~^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1390:14: error: no member named 'InsertElementOp' in namespace 'cir'
1390 |         cir::InsertElementOp::create(builder, loc, ops[1], selected, idx0);
|         ~~~~~^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1816:3: error: cannot jump from switch statement to this case label
1816 |   case X86::BI__builtin_ia32_prefetchi:
|   ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1387:17: note: jump bypasses variable initialization
1387 |     mlir::Value selected = emitX86ScalarSelect(builder, loc, ops[0], a, b);
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1380:17: note: jump bypasses variable initialization
1380 |     mlir::Value idx0 = cir::ConstantOp::create(
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1378:20: note: jump bypasses variable initialization
1378 |     mlir::Location loc = getLoc(expr->getExprLoc());
|                    ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1372:16: note: jump bypasses variable initialization
1372 |     mlir::Type resultTy = ops[1].getType();
|                ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1815:3: error: cannot jump from switch statement to this case label
1815 |   case X86::BI__builtin_ia32_vfmaddcsh_round_mask3:
|   ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1387:17: note: jump bypasses variable initialization
1387 |     mlir::Value selected = emitX86ScalarSelect(builder, loc, ops[0], a, b);
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1380:17: note: jump bypasses variable initialization
1380 |     mlir::Value idx0 = cir::ConstantOp::create(
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1378:20: note: jump bypasses variable initialization
1378 |     mlir::Location loc = getLoc(expr->getExprLoc());
|                    ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1372:16: note: jump bypasses variable initialization
1372 |     mlir::Type resultTy = ops[1].getType();
|                ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1814:3: error: cannot jump from switch statement to this case label
1814 |   case X86::BI__builtin_ia32_vfcmaddcsh_round_mask3:
|   ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1387:17: note: jump bypasses variable initialization
1387 |     mlir::Value selected = emitX86ScalarSelect(builder, loc, ops[0], a, b);
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1380:17: note: jump bypasses variable initialization
1380 |     mlir::Value idx0 = cir::ConstantOp::create(
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1378:20: note: jump bypasses variable initialization
1378 |     mlir::Location loc = getLoc(expr->getExprLoc());
|                    ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1372:16: note: jump bypasses variable initialization
1372 |     mlir::Type resultTy = ops[1].getType();
|                ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1813:3: error: cannot jump from switch statement to this case label
1813 |   case X86::BI__builtin_ia32_vfmaddcsh_round_mask:
|   ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1387:17: note: jump bypasses variable initialization
1387 |     mlir::Value selected = emitX86ScalarSelect(builder, loc, ops[0], a, b);
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1380:17: note: jump bypasses variable initialization
1380 |     mlir::Value idx0 = cir::ConstantOp::create(
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1378:20: note: jump bypasses variable initialization
1378 |     mlir::Location loc = getLoc(expr->getExprLoc());
|                    ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1372:16: note: jump bypasses variable initialization
1372 |     mlir::Type resultTy = ops[1].getType();
|                ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1812:3: error: cannot jump from switch statement to this case label
1812 |   case X86::BI__builtin_ia32_vfcmaddcsh_round_mask:
|   ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1387:17: note: jump bypasses variable initialization
1387 |     mlir::Value selected = emitX86ScalarSelect(builder, loc, ops[0], a, b);
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1380:17: note: jump bypasses variable initialization
1380 |     mlir::Value idx0 = cir::ConstantOp::create(
|                 ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1378:20: note: jump bypasses variable initialization
1378 |     mlir::Location loc = getLoc(expr->getExprLoc());
|                    ^
/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp:1372:16: note: jump bypasses variable initialization
1372 |     mlir::Type resultTy = ops[1].getType();
|                ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

@Priyanshu3820 Priyanshu3820 marked this pull request as draft December 15, 2025 19:30
case X86::BI__builtin_ia32_selectpd_128:
case X86::BI__builtin_ia32_selectpd_256:
case X86::BI__builtin_ia32_selectpd_512:
return emitX86VectorSelect(builder, getLoc(expr->getExprLoc()), ops[0],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return emitX86VectorSelect(builder, getLoc(expr->getExprLoc()), ops[0],
return emitX86Select(builder, getLoc(expr->getExprLoc()), ops[0],

This function has already been upstreamed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but it emits selectop while we need to emit vecTernaryOp to match the semantics of vector select.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you pass vector operands to cir.select it performs a vector select. I just tested it and the existing function works as required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok then I'll update it

auto zeroAttr = builder.getZeroAttr(mask.getType());
mlir::Value zero =
cir::ConstantOp::create(builder, loc, mask.getType(), zeroAttr);
mlir::Value cond = builder.createCompare(loc, cir::CmpOpKind::ne, mask, zero);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The classic codegen implementation of this function is casting the mask to a vector of i1 and extracting element zero. You need to do something equivalent to that. Effectively, it's this (assuming mask is an integer):

cond = (bool)(mask & 0x1);

So, for example, if the value of mask is 2, cond should be zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

case X86::BI__builtin_ia32_selectsbf_128:
case X86::BI__builtin_ia32_selectss_128:
case X86::BI__builtin_ia32_selectsd_128:
return emitX86ScalarSelect(builder, getLoc(expr->getExprLoc()), ops[0],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. Compare the classic codegen implementation:

    Value *A = Builder.CreateExtractElement(Ops[1], (uint64_t)0);
    Value *B = Builder.CreateExtractElement(Ops[2], (uint64_t)0);
    A = EmitX86ScalarSelect(*this, Ops[0], A, B);
    return Builder.CreateInsertElement(Ops[1], A, (uint64_t)0);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

// LLVM-LABEL: test_selectb_128
// LLVM: select <16 x i8>
// OGCG-LABEL: test_selectb_128
// OGCG: select <16 x i8>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more checks so that we are verifying all the operations that are generated and the correct parameter usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants