[clang] [CIR] Upstream floating point literal expressions (PR #129304)

Amr Hesham via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 12:46:07 PST 2025


https://github.com/AmrDeveloper created https://github.com/llvm/llvm-project/pull/129304

This change adds support for floating point literal expressions

>From d13f2d0d40987302243efad7304a1a006fdb2dde Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Fri, 28 Feb 2025 21:42:36 +0100
Subject: [PATCH] [CIR] Upstream floating point literal expressions

---
 clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp |  9 +++++++++
 clang/test/CIR/func-simple.cpp             | 12 ++++++++++++
 2 files changed, 21 insertions(+)

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: }



More information about the cfe-commits mailing list