Skip to content

[clang][x86] Fix the return type of the cvtpd2dq builtin#189254

Merged
RKSimon merged 2 commits into
llvm:mainfrom
jmgorius:main
Mar 30, 2026
Merged

[clang][x86] Fix the return type of the cvtpd2dq builtin#189254
RKSimon merged 2 commits into
llvm:mainfrom
jmgorius:main

Conversation

@jmgorius
Copy link
Copy Markdown
Member

The CVTPD2DQ instruction converts packed 64-bit floating-point values to packed 32-bit signed integer values. This patch fixes the return type of the corresponding builtin, which previously returned a vector of two 64-bit signed integers. The new behavior is in line with the return type of the CVTTPD2DQ builtin.

@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:headers Headers provided by Clang, e.g. for intrinsics labels Mar 29, 2026
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Mar 29, 2026

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-backend-x86

Author: Jean-Michel Gorius (jmgorius)

Changes

The CVTPD2DQ instruction converts packed 64-bit floating-point values to packed 32-bit signed integer values. This patch fixes the return type of the corresponding builtin, which previously returned a vector of two 64-bit signed integers. The new behavior is in line with the return type of the CVTTPD2DQ builtin.


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/BuiltinsX86.td (+1-1)
  • (modified) clang/lib/Headers/emmintrin.h (+1-1)
  • (modified) clang/test/CodeGen/builtins-x86.c (+1-1)
diff --git a/clang/include/clang/Basic/BuiltinsX86.td b/clang/include/clang/Basic/BuiltinsX86.td
index f47532a63de04..0cab8c77d465d 100644
--- a/clang/include/clang/Basic/BuiltinsX86.td
+++ b/clang/include/clang/Basic/BuiltinsX86.td
@@ -180,7 +180,7 @@ let Features = "avx512f", Attributes = [NoThrow, Const, Constexpr, RequiredVecto
 
 let Features = "sse2", Attributes = [NoThrow, Const, RequiredVectorWidth<128>] in {
   def psadbw128 : X86Builtin<"_Vector<2, long long int>(_Vector<16, char>, _Vector<16, char>)">;
-  def cvtpd2dq : X86Builtin<"_Vector<2, long long int>(_Vector<2, double>)">;
+  def cvtpd2dq : X86Builtin<"_Vector<4, int>(_Vector<2, double>)">;
   def cvttpd2dq : X86Builtin<"_Vector<4, int>(_Vector<2, double>)">;
   def cvtsd2si : X86Builtin<"int(_Vector<2, double>)">;
   def cvttsd2si : X86Builtin<"int(_Vector<2, double>)">;
diff --git a/clang/lib/Headers/emmintrin.h b/clang/lib/Headers/emmintrin.h
index bbf366133c68a..43c93263f015a 100644
--- a/clang/lib/Headers/emmintrin.h
+++ b/clang/lib/Headers/emmintrin.h
@@ -1343,7 +1343,7 @@ _mm_cvtepi32_pd(__m128i __a) {
 /// \returns A 128-bit vector of [4 x i32] whose lower 64 bits contain the
 ///    converted values. The upper 64 bits are set to zero.
 static __inline__ __m128i __DEFAULT_FN_ATTRS _mm_cvtpd_epi32(__m128d __a) {
-  return __builtin_ia32_cvtpd2dq((__v2df)__a);
+  return (__m128i)__builtin_ia32_cvtpd2dq((__v2df)__a);
 }
 
 /// Converts the low-order element of a 128-bit vector of [2 x double]
diff --git a/clang/test/CodeGen/builtins-x86.c b/clang/test/CodeGen/builtins-x86.c
index a1e63d59e88e1..0f66d8c4e3120 100644
--- a/clang/test/CodeGen/builtins-x86.c
+++ b/clang/test/CodeGen/builtins-x86.c
@@ -290,7 +290,7 @@ void f0(void) {
   (void) __builtin_ia32_movnti64(tmp_LLip, tmp_LLi);
 #endif
   tmp_V2LLi = __builtin_ia32_psadbw128(tmp_V16c, tmp_V16c);
-  tmp_V2LLi = __builtin_ia32_cvtpd2dq(tmp_V2d);
+  tmp_V4i = __builtin_ia32_cvtpd2dq(tmp_V2d);
   tmp_V4f = __builtin_ia32_cvtpd2ps(tmp_V2d);
   tmp_V4i = __builtin_ia32_cvttpd2dq(tmp_V2d);
   tmp_i = __builtin_ia32_cvtsd2si(tmp_V2d);

@RKSimon RKSimon self-requested a review March 29, 2026 16:14
Copy link
Copy Markdown
Contributor

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

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

LGTM - will correct match int_x86_sse2_cvtpd2dq as well

@RKSimon RKSimon enabled auto-merge (squash) March 30, 2026 10:10
@RKSimon RKSimon merged commit 65cb5c3 into llvm:main Mar 30, 2026
9 of 10 checks passed
@jmgorius
Copy link
Copy Markdown
Member Author

Thanks @RKSimon !

Would it make sense to open more PRs if I come across inconsistent types in the x86 builtins? I found a few that, e.g., should semantically operate on unsigned values but take vectors of signed values instead (packuswb128 comes to mind).

@RKSimon
Copy link
Copy Markdown
Contributor

RKSimon commented Mar 30, 2026

Sure - keep each patch focused on a particular instruction type though and expect some issues with constexpr evaluation which often uses the signedness from the types in the APSInt values

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

Labels

backend:X86 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants