-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
@llvm/pr-subscribers-clangir @llvm/pr-subscribers-clang Author: Amr Hesham (AmrDeveloper) ChangesThis change adds support for floating point literal expressions Full diff: https://github.com/llvm/llvm-project/pull/129304.diff 2 Files Affected:
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: }
|
clang/test/CIR/func-simple.cpp
Outdated
|
||
float floatfunc() { return 42.42f; } | ||
// CHECK: cir.func @floatfunc() -> !cir.float { | ||
// CHECK: %0 = cir.const #cir.fp<4.242000e+01> : !cir.float |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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
This change adds support for floating point literal expressions
This change adds support for floating point literal expressions