Skip to content

[CIR] Upstream floating point literal expressions #129304

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
Mar 3, 2025

Conversation

AmrDeveloper
Copy link
Member

This change adds support for floating point literal expressions

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

llvmbot commented Feb 28, 2025

@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Amr Hesham (AmrDeveloper)

Changes

This change adds support for floating point literal expressions


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

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+9)
  • (modified) clang/test/CIR/func-simple.cpp (+12)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 24a959108f73b..1b2861ddcedb5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -59,6 +59,15 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
         builder.getAttr<cir::IntAttr>(type, e->getValue()));
   }
 
+  mlir::Value VisitFloatingLiteral(const FloatingLiteral *e) {
+    mlir::Type type = cgf.convertType(e->getType());
+    assert(mlir::isa<cir::CIRFPTypeInterface>(type) &&
+           "expect floating-point type");
+    return builder.create<cir::ConstantOp>(
+        cgf.getLoc(e->getExprLoc()), type,
+        builder.getAttr<cir::FPAttr>(type, e->getValue()));
+  }
+
   mlir::Value VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *e) {
     mlir::Type type = cgf.convertType(e->getType());
     return builder.create<cir::ConstantOp>(
diff --git a/clang/test/CIR/func-simple.cpp b/clang/test/CIR/func-simple.cpp
index 3947055e300a0..5927d90a2e548 100644
--- a/clang/test/CIR/func-simple.cpp
+++ b/clang/test/CIR/func-simple.cpp
@@ -57,3 +57,15 @@ bool boolfunc() { return true; }
 // CHECK:   %0 = cir.const #true
 // CHECK:   cir.return %0 : !cir.bool
 // CHECK: }
+
+float floatfunc() { return 42.42f; }
+// CHECK: cir.func @floatfunc() -> !cir.float {
+// CHECK:   %0 = cir.const #cir.fp<4.242000e+01> : !cir.float
+// CHECK:   cir.return %0 : !cir.float
+// CHECK: }
+
+double doublefunc() { return 42.42; }
+// CHECK: cir.func @doublefunc() -> !cir.double {
+// CHECK:   %0 = cir.const #cir.fp<4.242000e+01> : !cir.double
+// CHECK:   cir.return %0 : !cir.double
+// CHECK: }


float floatfunc() { return 42.42f; }
// CHECK: cir.func @floatfunc() -> !cir.float {
// CHECK: %0 = cir.const #cir.fp<4.242000e+01> : !cir.float
Copy link
Collaborator

Choose a reason for hiding this comment

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

At least in LLVM-IR we are shockingly inconsistent with how we output floating point literals in IR. So I'm not sure if we need to make the format a little less 'strict' here? How consistent is CIR with literal representation across platforms?

Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't looked into how these values get printed, but I would expect the behavior to be similar to LLVM IR, which while it is as you note inconsistent is generally stable.

Copy link
Member

@bcardosolopes bcardosolopes Mar 1, 2025

Choose a reason for hiding this comment

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

How consistent is CIR with literal representation across platforms?

We use the same as MLIR LLVM dialect uses (which is MLIR's own parsing/printing), we haven't tested much past AArch64 and x86_64 so not a lot of data to say much more - we haven't had issues so far though.

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I understand both MLIR LLVM dialect and LLVM IR use the same format, I will update the test to just check this part %0 = cir.const #cir.fp<4.242

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

LGTM after any extra feedback on format

@AmrDeveloper AmrDeveloper merged commit ab30df4 into llvm:main Mar 3, 2025
11 checks passed
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
This change adds support for floating point literal expressions
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.

5 participants