Skip to content

[clang] fp options fix for __builtin_convertvector #134102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025

Conversation

ficol
Copy link
Contributor

@ficol ficol commented Apr 2, 2025

Add missing CGFPOptionsRAII for fptoi and itofp cases

Add missing CGFPOptionsRAII for fptoi and itofp cases
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Apr 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 2, 2025

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Jakub Ficek (ficol)

Changes

Add missing CGFPOptionsRAII for fptoi and itofp cases


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

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGExprScalar.cpp (+8-4)
  • (modified) clang/test/CodeGen/pragma-fenv_access.c (+36)
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 140a12d384502..28ae56058a7b4 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1969,12 +1969,16 @@ Value *ScalarExprEmitter::VisitConvertVectorExpr(ConvertVectorExpr *E) {
     bool InputSigned = SrcEltType->isSignedIntegerOrEnumerationType();
     if (isa<llvm::IntegerType>(DstEltTy))
       Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
-    else if (InputSigned)
-      Res = Builder.CreateSIToFP(Src, DstTy, "conv");
-    else
-      Res = Builder.CreateUIToFP(Src, DstTy, "conv");
+    else {
+      CodeGenFunction::CGFPOptionsRAII FPOptions(CGF, E);
+      if (InputSigned)
+        Res = Builder.CreateSIToFP(Src, DstTy, "conv");
+      else
+        Res = Builder.CreateUIToFP(Src, DstTy, "conv");
+    }
   } else if (isa<llvm::IntegerType>(DstEltTy)) {
     assert(SrcEltTy->isFloatingPointTy() && "Unknown real conversion");
+    CodeGenFunction::CGFPOptionsRAII FPOptions(CGF, E);
     if (DstEltType->isSignedIntegerOrEnumerationType())
       Res = Builder.CreateFPToSI(Src, DstTy, "conv");
     else
diff --git a/clang/test/CodeGen/pragma-fenv_access.c b/clang/test/CodeGen/pragma-fenv_access.c
index 347e9670c4742..48e60d907e2cf 100644
--- a/clang/test/CodeGen/pragma-fenv_access.c
+++ b/clang/test/CodeGen/pragma-fenv_access.c
@@ -251,3 +251,39 @@ vector4float func_21(vector4double x) {
 }
 // CHECK-LABEL: @func_21
 // STRICT: call <4 x float> @llvm.experimental.constrained.fptrunc.v4f32.v4f64(<4 x double> {{.*}}, metadata !"round.upward", metadata !"fpexcept.strict")
+
+typedef short vector8short __attribute__((__vector_size__(16)));
+typedef double vector8double __attribute__((__vector_size__(64)));
+vector8double func_24(vector8short x) {
+  #pragma STDC FENV_ROUND FE_TOWARDZERO
+  return __builtin_convertvector(x, vector8double);
+}
+// CHECK-LABEL: @func_24
+// STRICT: call <8 x double> @llvm.experimental.constrained.sitofp.v8f64.v8i16(<8 x i16> {{.*}}, metadata !"round.towardzero", metadata !"fpexcept.strict")
+
+typedef unsigned int vector16uint __attribute__((__vector_size__(64)));
+typedef double vector16double __attribute__((__vector_size__(128)));
+vector16double func_25(vector16uint x) {
+  #pragma STDC FENV_ROUND FE_DOWNWARD
+  return __builtin_convertvector(x, vector16double);
+}
+// CHECK-LABEL: @func_25
+// STRICT: call <16 x double> @llvm.experimental.constrained.uitofp.v16f64.v16i32(<16 x i32> {{.*}}, metadata !"round.downward", metadata !"fpexcept.strict")
+
+typedef float vector2float __attribute__((__vector_size__(8)));
+typedef char vector2char __attribute__((__vector_size__(2)));
+vector2char func_22(vector2float x) {
+  #pragma STDC FENV_ACCESS ON
+  return __builtin_convertvector(x, vector2char);
+}
+// CHECK-LABEL: @func_22
+// STRICT: call <2 x i8> @llvm.experimental.constrained.fptosi.v2i8.v2f32(<2 x float> {{.*}}, metadata !"fpexcept.strict")
+
+typedef float vector3float __attribute__((__vector_size__(12)));
+typedef unsigned long long vector3ulong __attribute__((__vector_size__(24)));
+vector3ulong func_23(vector3float x) {
+  #pragma STDC FENV_ACCESS ON
+  return __builtin_convertvector(x, vector3ulong);
+}
+// CHECK-LABEL: @func_23
+// STRICT: call <3 x i64> @llvm.experimental.constrained.fptoui.v3i64.v3f32(<3 x float> {{.*}}, metadata !"fpexcept.strict")

@ficol
Copy link
Contributor Author

ficol commented Apr 3, 2025

Hi @shafik, could you please check this? It's a small fix that was missing in #125522

@shafik shafik requested a review from tbaederr April 8, 2025 03:13
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

This makes sense to me but I would like @tbaederr to also take a look and if he approves then we are all good.

Copy link
Contributor

@tbaederr tbaederr left a comment

Choose a reason for hiding this comment

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

This is all happening in codegen so not my area of expertise, but looks good.

@ficol
Copy link
Contributor Author

ficol commented Apr 8, 2025

@shafik @tbaederr thanks, can you help with merging this PR?

@tbaederr tbaederr merged commit a5509d6 into llvm:main Apr 8, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants