Skip to content

Conversation

@AmrDeveloper
Copy link
Member

Add support for the ArrayTypeTraitExpr

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

llvmbot commented Dec 10, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clangir

Author: Amr Hesham (AmrDeveloper)

Changes

Add support for the ArrayTypeTraitExpr


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

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+3-3)
  • (modified) clang/test/CIR/CodeGen/cxx-traits.cpp (+22)
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 3887433e5e181..fb00172d8848a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -854,9 +854,9 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
     return {};
   }
   mlir::Value VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *e) {
-    cgf.cgm.errorNYI(e->getSourceRange(),
-                     "ScalarExprEmitter: array type trait");
-    return {};
+    mlir::Type type = cgf.convertType(e->getType());
+    mlir::Location loc = cgf.getLoc(e->getExprLoc());
+    return builder.getConstInt(loc, type, e->getValue());
   }
   mlir::Value VisitExpressionTraitExpr(const ExpressionTraitExpr *e) {
     return builder.getBool(e->getValue(), cgf.getLoc(e->getExprLoc()));
diff --git a/clang/test/CIR/CodeGen/cxx-traits.cpp b/clang/test/CIR/CodeGen/cxx-traits.cpp
index cf44f775d0842..5662d07957761 100644
--- a/clang/test/CIR/CodeGen/cxx-traits.cpp
+++ b/clang/test/CIR/CodeGen/cxx-traits.cpp
@@ -18,3 +18,25 @@ void expression_trait_expr() {
 
 // OGCG: %[[A_ADDR:.*]] = alloca i8, align 1
 // OGCG: store i8 0, ptr %[[A_ADDR]], align 1
+
+void array_type_trait_expr() {
+  unsigned long a = __array_rank(int[10][20]);
+  unsigned long b = __array_extent(int[10][20], 1);
+}
+
+// CIR: %[[A_ADDR:.*]] = cir.alloca !u64i, !cir.ptr<!u64i>, ["a", init]
+// CIR: %[[B_ADDR:.*]] = cir.alloca !u64i, !cir.ptr<!u64i>, ["b", init]
+// CIR: %[[CONST_2:.*]] = cir.const #cir.int<2> : !u64i
+// CIR: cir.store {{.*}} %[[CONST_2]], %[[A_ADDR]] : !u64i, !cir.ptr<!u64i>
+// CIR: %[[CONST_20:.*]] = cir.const #cir.int<20> : !u64i
+// CIR: cir.store {{.*}} %[[CONST_20]], %[[B_ADDR]] : !u64i, !cir.ptr<!u64i>
+
+// LLVM: %[[A_ADDR:.*]] = alloca i64, i64 1, align 8
+// LLVM: %[[B_ADDR:.*]] = alloca i64, i64 1, align 8
+// LLVM: store i64 2, ptr %[[A_ADDR]], align 8
+// LLVM: store i64 20, ptr %[[B_ADDR]], align 8
+
+// OGCG: %[[A_ADDR:.*]] = alloca i64, align 8
+// OGCG: %[[B_ADDR:.*]] = alloca i64, align 8
+// OGCG: store i64 2, ptr %[[A_ADDR]], align 8
+// OGCG: store i64 20, ptr %[[B_ADDR]], align 8

Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

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

lgtm

@AmrDeveloper AmrDeveloper force-pushed the cir_array_type_trait_expr branch from b405507 to f400cd5 Compare December 13, 2025 12:22
@AmrDeveloper AmrDeveloper enabled auto-merge (squash) December 13, 2025 12:22
@AmrDeveloper AmrDeveloper merged commit c3a0849 into llvm:main Dec 13, 2025
9 of 10 checks passed
anonymouspc pushed a commit to anonymouspc/llvm that referenced this pull request Dec 15, 2025
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