Skip to content

[CIR] Enable floating point casts #144142

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 1 commit into from
Jun 13, 2025
Merged

Conversation

andykaylor
Copy link
Contributor

We already had the code in place to emit CIR floating point cast ops that get lowered to fpext or fptrunc, but we weren't calling the function to emit that cast from ScalarExprEmitter::emitScalarCast. This change adds that call.

We already had the code in place to emit CIR floating point cast ops
that get lowered to fpext or fptrunc, but we weren't calling the
function to emit that cast from ScalarExprEmitter::emitScalarCast.
This change adds that call.
@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Jun 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 13, 2025

@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Andy Kaylor (andykaylor)

Changes

We already had the code in place to emit CIR floating point cast ops that get lowered to fpext or fptrunc, but we weren't calling the function to emit that cast from ScalarExprEmitter::emitScalarCast. This change adds that call.


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

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+2-2)
  • (modified) clang/test/CIR/CodeGen/cast.cpp (+8)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 30d231e2c61de..baaef022ccc68 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -346,8 +346,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
         assert(!cir::MissingFeatures::fpConstraints());
         castKind = cir::CastKind::float_to_int;
       } else if (mlir::isa<cir::CIRFPTypeInterface>(dstTy)) {
-        cgf.getCIRGenModule().errorNYI("floating point casts");
-        return cgf.createDummyValue(src.getLoc(), dstType);
+        // TODO: split this to createFPExt/createFPTrunc
+        return builder.createFloatingCast(src, fullDstTy);
       } else {
         llvm_unreachable("Internal error: Cast to unexpected type");
       }
diff --git a/clang/test/CIR/CodeGen/cast.cpp b/clang/test/CIR/CodeGen/cast.cpp
index a7c11b1939ba5..84f55242a6118 100644
--- a/clang/test/CIR/CodeGen/cast.cpp
+++ b/clang/test/CIR/CodeGen/cast.cpp
@@ -73,6 +73,14 @@ int cStyleCasts_0(unsigned x1, int x2, float x3, short x4, double x5) {
   // LLVM: %{{[0-9]+}} = fcmp une float %{{[0-9]+}}, 0.000000e+00
   // LLVM: %{{[0-9]+}} = zext i1 %{{[0-9]+}} to i8
 
+  double d2 = f; // float to double
+  // CIR: %{{[0-9]+}} = cir.cast(floating, %{{[0-9]+}} : !cir.float), !cir.double
+  // LLVM: %{{[0-9]+}} = fpext float %{{[0-9]+}} to double
+
+  f = d2; // double to float
+  // CIR: %{{[0-9]+}} = cir.cast(floating, %{{[0-9]+}} : !cir.double), !cir.float
+  // LLVM: %{{[0-9]+}} = fptrunc double %{{[0-9]+}} to float
+
   return 0;
 }
 

Copy link
Member

@AmrDeveloper AmrDeveloper left a comment

Choose a reason for hiding this comment

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

LGTM

@andykaylor andykaylor merged commit b7cb348 into llvm:main Jun 13, 2025
10 checks passed
@andykaylor andykaylor deleted the cir-floating-cast branch June 13, 2025 19:41
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
We already had the code in place to emit CIR floating point cast ops
that get lowered to fpext or fptrunc, but we weren't calling the
function to emit that cast from ScalarExprEmitter::emitScalarCast. This
change adds that call.
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
We already had the code in place to emit CIR floating point cast ops
that get lowered to fpext or fptrunc, but we weren't calling the
function to emit that cast from ScalarExprEmitter::emitScalarCast. This
change adds that call.
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