Skip to content

[CIR] Upstream basic support for sizeof and alignof #130847

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 6 commits into from
Mar 14, 2025

Conversation

AmrDeveloper
Copy link
Member

This change adds the essential support for sizeof and alignof operators

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

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clangir

Author: Amr Hesham (AmrDeveloper)

Changes

This change adds the essential support for sizeof and alignof operators

  • Support for VariableArrayType can be added after closing #130197

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

3 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+26)
  • (added) clang/test/CIR/CodeGen/unary-expr-or-type-trait.cpp (+35)
  • (added) clang/test/CIR/Lowering/unary-expr-or-type-trait.cpp (+35)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index b9e56dc4123d6..19d57ed9cf6fa 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -92,6 +92,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
 
   mlir::Value VisitCastExpr(CastExpr *E);
 
+  mlir::Value VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *e);
+
   /// Emit a conversion from the specified type to the specified destination
   /// type, both of which are CIR scalar types.
   /// TODO: do we need ScalarConversionOpts here? Should be done in another
@@ -148,3 +150,27 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
   }
   return {};
 }
+
+/// Return the size or alignment of the type of argument of the sizeof
+/// expression as an integer.
+mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
+    const UnaryExprOrTypeTraitExpr *e) {
+  const QualType typeToSize = e->getTypeOfArgument();
+  if (e->getKind() == UETT_SizeOf) {
+    if (const VariableArrayType *variableArrTy =
+            cgf.getContext().getAsVariableArrayType(typeToSize)) {
+      cgf.getCIRGenModule().errorNYI(e->getSourceRange(),
+                                     "sizeof operator for VariableArrayType",
+                                     e->getStmtClassName());
+    }
+  } else if (e->getKind() == UETT_OpenMPRequiredSimdAlign) {
+    cgf.getCIRGenModule().errorNYI(
+        e->getSourceRange(),
+        "sizeof operator for Not yet implemented: ", e->getStmtClassName());
+  }
+
+  return builder.create<cir::ConstantOp>(
+      cgf.getLoc(e->getSourceRange()), cgf.cgm.UInt64Ty,
+      builder.getAttr<cir::IntAttr>(
+          cgf.cgm.UInt64Ty, e->EvaluateKnownConstInt(cgf.getContext())));
+}
diff --git a/clang/test/CIR/CodeGen/unary-expr-or-type-trait.cpp b/clang/test/CIR/CodeGen/unary-expr-or-type-trait.cpp
new file mode 100644
index 0000000000000..e1868d99c42b6
--- /dev/null
+++ b/clang/test/CIR/CodeGen/unary-expr-or-type-trait.cpp
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | filecheck %s
+
+void foo() {
+  unsigned long b = sizeof(bool);
+  // CHECK: cir.const #cir.int<1> : !cir.int<u, 64>
+
+  unsigned long i = sizeof(int);
+  // CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
+
+  unsigned long l =  sizeof(long);
+  // CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
+
+  unsigned long f =  sizeof(float);
+  // CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
+
+  unsigned long d =  sizeof(double);
+  // CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
+}
+
+void foo2() {
+  unsigned long b = alignof(bool);
+  // CHECK: cir.const #cir.int<1> : !cir.int<u, 64>
+
+  unsigned long i = alignof(int);
+  // CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
+
+  unsigned long l =  alignof(long);
+  // CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
+
+  unsigned long f =  alignof(float);
+  // CHECK: cir.const #cir.int<4> : !cir.int<u, 64>
+
+  unsigned long d =  alignof(double);
+  // CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
+}
diff --git a/clang/test/CIR/Lowering/unary-expr-or-type-trait.cpp b/clang/test/CIR/Lowering/unary-expr-or-type-trait.cpp
new file mode 100644
index 0000000000000..551a1c254216b
--- /dev/null
+++ b/clang/test/CIR/Lowering/unary-expr-or-type-trait.cpp
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - 2>&1 | FileCheck %s
+
+void foo() {
+  unsigned long b = sizeof(bool);
+  // CHECK: store i64 1, ptr {{%.*}}, align 4
+
+  unsigned long i = sizeof(int);
+  // CHECK: store i64 4, ptr {{%.*}}, align 4
+
+  unsigned long l =  sizeof(long);
+  // CHECK: store i64 8, ptr {{%.*}}, align 4
+
+  unsigned long f =  sizeof(float);
+  // CHECK: store i64 4, ptr {{%.*}}, align 4
+
+  unsigned long d =  sizeof(double);
+  // CHECK: store i64 8, ptr {{%.*}}, align 4
+}
+
+void foo2() {
+  unsigned long b = alignof(bool);
+  // CHECK: store i64 1, ptr {{%.*}}, align 4
+
+  unsigned long i = alignof(int);
+  // CHECK: store i64 4, ptr {{%.*}}, align 4
+
+  unsigned long l =  alignof(long);
+  // CHECK: store i64 8, ptr {{%.*}}, align 4
+
+  unsigned long f =  alignof(float);
+  // CHECK: store i64 4, ptr {{%.*}}, align 4
+
+  unsigned long d =  alignof(double);
+  // CHECK: store i64 8, ptr {{%.*}}, align 4
+}

mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
const UnaryExprOrTypeTraitExpr *e) {
const QualType typeToSize = e->getTypeOfArgument();
if (e->getKind() == UETT_SizeOf) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

See here:

ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(

This is missing A LOT of those things it needs. I think this function needs more work (even if it is just NYI).

if (e->getKind() == UETT_SizeOf) {
if (const VariableArrayType *variableArrTy =
cgf.getContext().getAsVariableArrayType(typeToSize)) {
cgf.getCIRGenModule().errorNYI(e->getSourceRange(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is this recovering? Will there be a known-const-int in this case to give a valid value? Same on 169.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will check EvaluateKnownConstInt and create a dummy value for recovering

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think instead that after each of these errorNYI diagnostics, we are better just returning a constant value that is reasonably sensible. A 1 or a ptr-size both seem reasonable to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice i used 1 as recovering value for unimplemented yet cases

@AmrDeveloper
Copy link
Member Author

I updated the test to include fixed size ArrayType

@AmrDeveloper AmrDeveloper merged commit e9fc768 into llvm:main Mar 14, 2025
11 checks passed
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.

4 participants