[clang] [CIR] Upstream basic support for ArrayType (PR #130502)

Amr Hesham via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 14:35:15 PDT 2025


https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/130502

>From 0b00b1b477f7d81220350669ecb43f87d2667a6d Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Sun, 9 Mar 2025 19:14:34 +0100
Subject: [PATCH 1/6] [CIR] Upstream basic support for ArrayType

---
 clang/include/clang/CIR/Dialect/IR/CIRAttrs.h |  4 ++++
 .../include/clang/CIR/Dialect/IR/CIRTypes.td  | 21 ++++++++++++++++++-
 clang/lib/CIR/CodeGen/CIRGenBuilder.h         |  8 +++++++
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp         | 12 +++++++++++
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |  3 +++
 clang/lib/CIR/Dialect/IR/CIRTypes.cpp         | 16 ++++++++++++++
 clang/test/CIR/CodeGen/array.cpp              | 12 +++++++++++
 7 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CIR/CodeGen/array.cpp

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
index 438fb7d09608d..1451ea47c50c8 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
@@ -30,6 +30,10 @@ class VarDecl;
 class RecordDecl;
 } // namespace clang
 
+namespace cir {
+class ArrayType;
+} // namespace cir
+
 #define GET_ATTRDEF_CLASSES
 #include "clang/CIR/Dialect/IR/CIROpsAttributes.h.inc"
 
diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index a78e5eae08e33..b2bac643b62da 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -280,6 +280,25 @@ def CIR_BoolType :
   }];
 }
 
+//===----------------------------------------------------------------------===//
+// ArrayType
+//===----------------------------------------------------------------------===//
+
+def CIR_ArrayType : CIR_Type<"Array", "array",
+    [DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
+
+  let summary = "CIR array type";
+  let description = [{
+    `CIR.array` represents C/C++ constant arrays.
+  }];
+
+  let parameters = (ins "mlir::Type":$eltType, "uint64_t":$size);
+
+  let assemblyFormat = [{
+    `<` $eltType `x` $size `>`
+  }];
+}
+
 //===----------------------------------------------------------------------===//
 // FuncType
 //===----------------------------------------------------------------------===//
@@ -386,7 +405,7 @@ def VoidPtr : Type<
 //===----------------------------------------------------------------------===//
 
 def CIR_AnyType : AnyTypeOf<[
-  CIR_VoidType, CIR_BoolType, CIR_IntType, CIR_AnyFloat, CIR_PointerType,
+  CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_IntType, CIR_AnyFloat, CIR_PointerType,
   CIR_FuncType
 ]>;
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 01d56963883cc..250192ec254d1 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -33,6 +33,14 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
       llvm_unreachable("NYI: PPC double-double format for long double");
     llvm_unreachable("Unsupported format for long double");
   }
+
+  bool isSized(mlir::Type ty) {
+    if (mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
+                  cir::IntType>(ty))
+      return true;
+    assert(0 && "Unimplemented size for type");
+    return false;
+  }
 };
 
 } // namespace clang::CIRGen
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index dcfaaedc2ef57..78e894dee0071 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -202,6 +202,18 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
     break;
   }
 
+  case Type::ConstantArray: {
+    const ConstantArrayType *arrTy = cast<ConstantArrayType>(ty);
+    mlir::Type elemTy = convertTypeForMem(arrTy->getElementType());
+
+    // FIXME: In LLVM, "lower arrays of undefined struct type to arrays of
+    // i8 just to have a concrete type". Not sure this makes sense in CIR yet.
+    assert(builder.isSized(elemTy) && "not implemented");
+    resultType = cir::ArrayType::get(builder.getContext(), elemTy,
+                                     arrTy->getSize().getZExtValue());
+    break;
+  }
+
   case Type::FunctionNoProto:
   case Type::FunctionProto:
     resultType = convertFunctionTypeInternal(type);
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 5ad369b40cda1..1e078e2797270 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -166,6 +166,9 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
     return success();
   }
 
+  if (mlir::isa<cir::ArrayType>(opType))
+    return success();
+
   assert(isa<TypedAttr>(attrType) && "What else could we be looking at here?");
   return op->emitOpError("global with type ")
          << cast<TypedAttr>(attrType).getType() << " not yet supported";
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index 8bdde54ad41f6..6291297492227 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -369,6 +369,22 @@ BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
   return 1;
 }
 
+//===----------------------------------------------------------------------===//
+//  Definitions
+//===----------------------------------------------------------------------===//
+
+llvm::TypeSize
+ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
+                             ::mlir::DataLayoutEntryListRef params) const {
+  return getSize() * dataLayout.getTypeSizeInBits(getEltType());
+}
+
+uint64_t
+ArrayType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
+                           ::mlir::DataLayoutEntryListRef params) const {
+  return dataLayout.getTypeABIAlignment(getEltType());
+}
+
 //===----------------------------------------------------------------------===//
 // PointerType Definitions
 //===----------------------------------------------------------------------===//
diff --git a/clang/test/CIR/CodeGen/array.cpp b/clang/test/CIR/CodeGen/array.cpp
new file mode 100644
index 0000000000000..3a5950ffe3d87
--- /dev/null
+++ b/clang/test/CIR/CodeGen/array.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s
+
+int a[10];
+// CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
+
+extern int b[10];
+// CHECK: cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
+
+void f() {
+  int c[10];
+  // CHECK: %[[ARR:.*]] = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["c"]
+}

>From 541da710d6b74e2ab4fb082878f5d5bcf01e99d4 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Mon, 10 Mar 2025 19:39:37 +0100
Subject: [PATCH 2/6] Add test for LLVM lowering, CIR test and address comments

---
 .../include/clang/CIR/Dialect/IR/CIRTypes.td  |  4 +-
 clang/lib/CIR/CodeGen/CIRGenBuilder.h         |  7 +--
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp         |  9 ++--
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |  3 --
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp |  4 ++
 clang/test/CIR/CodeGen/array.cpp              | 16 +++++-
 clang/test/CIR/IR/array.cir                   | 51 +++++++++++++++++++
 clang/test/CIR/Lowering/array.cpp             | 24 +++++++++
 8 files changed, 103 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CIR/IR/array.cir
 create mode 100644 clang/test/CIR/Lowering/array.cpp

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
index b2bac643b62da..e285c0f28f113 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td
@@ -405,8 +405,8 @@ def VoidPtr : Type<
 //===----------------------------------------------------------------------===//
 
 def CIR_AnyType : AnyTypeOf<[
-  CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_IntType, CIR_AnyFloat, CIR_PointerType,
-  CIR_FuncType
+  CIR_VoidType, CIR_BoolType, CIR_ArrayType, CIR_IntType, CIR_AnyFloat,
+  CIR_PointerType, CIR_FuncType
 ]>;
 
 #endif // MLIR_CIR_DIALECT_CIR_TYPES
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 250192ec254d1..cb9c049b94396 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -35,11 +35,8 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
   }
 
   bool isSized(mlir::Type ty) {
-    if (mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
-                  cir::IntType>(ty))
-      return true;
-    assert(0 && "Unimplemented size for type");
-    return false;
+    return mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
+                     cir::IntType>(ty);
   }
 };
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index 78e894dee0071..0a22a82839e59 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -206,9 +206,12 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
     const ConstantArrayType *arrTy = cast<ConstantArrayType>(ty);
     mlir::Type elemTy = convertTypeForMem(arrTy->getElementType());
 
-    // FIXME: In LLVM, "lower arrays of undefined struct type to arrays of
-    // i8 just to have a concrete type". Not sure this makes sense in CIR yet.
-    assert(builder.isSized(elemTy) && "not implemented");
+    if (!builder.isSized(elemTy)) {
+      cgm.errorNYI(SourceLocation(), "unimplemented size for type", type);
+      resultType = cir::ArrayType::get(builder.getContext(), cgm.SInt32Ty, 0);
+      break;
+    }
+
     resultType = cir::ArrayType::get(builder.getContext(), elemTy,
                                      arrTy->getSize().getZExtValue());
     break;
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 1e078e2797270..5ad369b40cda1 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -166,9 +166,6 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
     return success();
   }
 
-  if (mlir::isa<cir::ArrayType>(opType))
-    return success();
-
   assert(isa<TypedAttr>(attrType) && "What else could we be looking at here?");
   return op->emitOpError("global with type ")
          << cast<TypedAttr>(attrType).getType() << " not yet supported";
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 3200527bd03af..9851af1c996a2 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -572,6 +572,10 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
 
     return mlir::LLVM::LLVMPointerType::get(type.getContext(), targetAS);
   });
+  converter.addConversion([&](cir::ArrayType type) -> mlir::Type {
+    auto ty = convertTypeForMemory(converter, dataLayout, type.getEltType());
+    return mlir::LLVM::LLVMArrayType::get(ty, type.getSize());
+  });
   converter.addConversion([&](cir::BoolType type) -> mlir::Type {
     return mlir::IntegerType::get(type.getContext(), 1,
                                   mlir::IntegerType::Signless);
diff --git a/clang/test/CIR/CodeGen/array.cpp b/clang/test/CIR/CodeGen/array.cpp
index 3a5950ffe3d87..41a22b2008e3c 100644
--- a/clang/test/CIR/CodeGen/array.cpp
+++ b/clang/test/CIR/CodeGen/array.cpp
@@ -3,10 +3,22 @@
 int a[10];
 // CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
 
+int aa[10][10];
+// CHECK: cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+
 extern int b[10];
 // CHECK: cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
 
+extern int bb[10][10];
+// CHECK: cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+
 void f() {
-  int c[10];
-  // CHECK: %[[ARR:.*]] = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["c"]
+  int l[10];
+  // CHECK: %[[ARR:.*]] = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["l"]
 }
+
+void f2(int p[10]) {}
+// CHECK: cir.func @f2(%arg0: !cir.ptr<!cir.int<s, 32>>
+
+void f3(int pp[10][10]) {}
+// CHECK: cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>
diff --git a/clang/test/CIR/IR/array.cir b/clang/test/CIR/IR/array.cir
new file mode 100644
index 0000000000000..293a202d18a92
--- /dev/null
+++ b/clang/test/CIR/IR/array.cir
@@ -0,0 +1,51 @@
+// RUN: cir-opt %s | FileCheck %s
+
+module  {
+
+cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
+// CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
+
+cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+// CHECK: cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+
+cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
+// CHECK: cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
+
+cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+// CHECK: cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+
+cir.func @f() {
+  %0 = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["l"] {alignment = 4 : i64}
+  cir.return
+}
+
+// CHECK: cir.func @f() {
+// CHECK:   %0 = cir.alloca !cir.array<!cir.int<s, 32> x 10>, !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, ["l"] {alignment = 4 : i64}
+// CHECK:   cir.return
+// CHECK: }
+
+cir.func @f2(%arg0: !cir.ptr<!cir.int<s, 32>>) {
+  %0 = cir.alloca !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>, ["p", init] {alignment = 8 : i64}
+  cir.store %arg0, %0 : !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>
+  cir.return
+}
+
+// CHECK: cir.func @f2(%arg0: !cir.ptr<!cir.int<s, 32>>) {
+// CHECK:   %0 = cir.alloca !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>, ["p", init] {alignment = 8 : i64}
+// CHECK:   cir.store %arg0, %0 : !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>
+// CHECK:   cir.return
+// CHECK: }
+
+cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>) {
+  %0 = cir.alloca !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>, ["pp", init] {alignment = 8 : i64}
+  cir.store %arg0, %0 : !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>
+  cir.return
+}
+
+// CHECK: cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>) {
+// CHECK:   %0 = cir.alloca !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>, ["pp", init] {alignment = 8 : i64}
+// CHECK:   cir.store %arg0, %0 : !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 10>>>
+// CHECK:   cir.return
+// CHECK: }
+
+}
diff --git a/clang/test/CIR/Lowering/array.cpp b/clang/test/CIR/Lowering/array.cpp
new file mode 100644
index 0000000000000..382b6b4dd9efb
--- /dev/null
+++ b/clang/test/CIR/Lowering/array.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - 2>&1 | FileCheck %s
+
+int a[10];
+// CHECK: @a = external dso_local global [10 x i32]
+
+int aa[10][10];
+// CHECK: @aa = external dso_local global [10 x [10 x i32]]
+
+extern int b[10];
+// CHECK: @b = external dso_local global [10 x i32]
+
+extern int bb[10][10];
+// CHECK: @bb = external dso_local global [10 x [10 x i32]]
+
+void f() {
+  int l[10];
+}
+// CHECK: alloca [10 x i32], i64 1, align 16
+
+void f2(int p[10]) {}
+// CHECK: alloca ptr, i64 1, align 8
+
+void f3(int pp[10][10]) {}
+// CHECK: alloca ptr, i64 1, align 8

>From b5cf42b67666d0a684b1b47d43ba329fed6f7d63 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Tue, 11 Mar 2025 23:17:25 +0100
Subject: [PATCH 3/6] Address code review comments

---
 clang/lib/CIR/CodeGen/CIRGenBuilder.h              |  8 ++++++--
 clang/lib/CIR/CodeGen/CIRGenTypes.cpp              |  7 -------
 .../lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp  |  3 ++-
 clang/test/CIR/CodeGen/array.cpp                   | 14 ++++++++------
 clang/test/CIR/Lowering/array.cpp                  | 10 +++++-----
 5 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index cb9c049b94396..1c63482ba5242 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -35,8 +35,12 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
   }
 
   bool isSized(mlir::Type ty) {
-    return mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
-                     cir::IntType>(ty);
+    if (mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
+                  cir::IntType>(ty))
+      return true;
+
+    assert(0 && "Unimplemented size for type");
+    return false;
   }
 };
 
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
index 0a22a82839e59..aaf3fe240f3c3 100644
--- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp
@@ -205,13 +205,6 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
   case Type::ConstantArray: {
     const ConstantArrayType *arrTy = cast<ConstantArrayType>(ty);
     mlir::Type elemTy = convertTypeForMem(arrTy->getElementType());
-
-    if (!builder.isSized(elemTy)) {
-      cgm.errorNYI(SourceLocation(), "unimplemented size for type", type);
-      resultType = cir::ArrayType::get(builder.getContext(), cgm.SInt32Ty, 0);
-      break;
-    }
-
     resultType = cir::ArrayType::get(builder.getContext(), elemTy,
                                      arrTy->getSize().getZExtValue());
     break;
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 9851af1c996a2..b9e0d3487b741 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -573,7 +573,8 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
     return mlir::LLVM::LLVMPointerType::get(type.getContext(), targetAS);
   });
   converter.addConversion([&](cir::ArrayType type) -> mlir::Type {
-    auto ty = convertTypeForMemory(converter, dataLayout, type.getEltType());
+    mlir::Type ty =
+        convertTypeForMemory(converter, dataLayout, type.getEltType());
     return mlir::LLVM::LLVMArrayType::get(ty, type.getSize());
   });
   converter.addConversion([&](cir::BoolType type) -> mlir::Type {
diff --git a/clang/test/CIR/CodeGen/array.cpp b/clang/test/CIR/CodeGen/array.cpp
index 41a22b2008e3c..02ecdc11e1d94 100644
--- a/clang/test/CIR/CodeGen/array.cpp
+++ b/clang/test/CIR/CodeGen/array.cpp
@@ -3,14 +3,14 @@
 int a[10];
 // CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> x 10>
 
-int aa[10][10];
-// CHECK: cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+int aa[10][5];
+// CHECK: cir.global external @aa : !cir.array<!cir.array<!cir.int<s, 32> x 5> x 10>
 
 extern int b[10];
 // CHECK: cir.global external @b : !cir.array<!cir.int<s, 32> x 10>
 
-extern int bb[10][10];
-// CHECK: cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 10> x 10>
+extern int bb[10][5];
+// CHECK: cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 5> x 10>
 
 void f() {
   int l[10];
@@ -19,6 +19,8 @@ void f() {
 
 void f2(int p[10]) {}
 // CHECK: cir.func @f2(%arg0: !cir.ptr<!cir.int<s, 32>>
+// CHECK: cir.alloca !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>, ["p", init]
 
-void f3(int pp[10][10]) {}
-// CHECK: cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 10>>
+void f3(int pp[10][5]) {}
+// CHECK: cir.func @f3(%arg0: !cir.ptr<!cir.array<!cir.int<s, 32> x 5>>
+// CHECK: cir.alloca !cir.ptr<!cir.array<!cir.int<s, 32> x 5>>, !cir.ptr<!cir.ptr<!cir.array<!cir.int<s, 32> x 5>>>
diff --git a/clang/test/CIR/Lowering/array.cpp b/clang/test/CIR/Lowering/array.cpp
index 382b6b4dd9efb..f0d5d2584866a 100644
--- a/clang/test/CIR/Lowering/array.cpp
+++ b/clang/test/CIR/Lowering/array.cpp
@@ -3,14 +3,14 @@
 int a[10];
 // CHECK: @a = external dso_local global [10 x i32]
 
-int aa[10][10];
-// CHECK: @aa = external dso_local global [10 x [10 x i32]]
+int aa[10][5];
+// CHECK: @aa = external dso_local global [10 x [5 x i32]]
 
 extern int b[10];
 // CHECK: @b = external dso_local global [10 x i32]
 
-extern int bb[10][10];
-// CHECK: @bb = external dso_local global [10 x [10 x i32]]
+extern int bb[10][5];
+// CHECK: @bb = external dso_local global [10 x [5 x i32]]
 
 void f() {
   int l[10];
@@ -20,5 +20,5 @@ void f() {
 void f2(int p[10]) {}
 // CHECK: alloca ptr, i64 1, align 8
 
-void f3(int pp[10][10]) {}
+void f3(int pp[10][5]) {}
 // CHECK: alloca ptr, i64 1, align 8

>From 6ceabd28e26f34c75b949a86a38a53b14b5326f8 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Tue, 11 Mar 2025 23:55:19 +0100
Subject: [PATCH 4/6] Update the test to check function name of alloca

---
 clang/test/CIR/Lowering/array.cpp | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/test/CIR/Lowering/array.cpp b/clang/test/CIR/Lowering/array.cpp
index f0d5d2584866a..42208a81caea2 100644
--- a/clang/test/CIR/Lowering/array.cpp
+++ b/clang/test/CIR/Lowering/array.cpp
@@ -15,10 +15,13 @@ extern int bb[10][5];
 void f() {
   int l[10];
 }
-// CHECK: alloca [10 x i32], i64 1, align 16
+// CHECK: define void @f()
+// CHECK-NEXT: alloca [10 x i32], i64 1, align 16
 
 void f2(int p[10]) {}
-// CHECK: alloca ptr, i64 1, align 8
+// CHECK: define void @f2(ptr {{%.*}})
+// CHECK-NEXT: alloca ptr, i64 1, align 8
 
 void f3(int pp[10][5]) {}
-// CHECK: alloca ptr, i64 1, align 8
+// CHECK: define void @f3(ptr {{%.*}})
+// CHECK-NEXT: alloca ptr, i64 1, align 8

>From 101f7a0ba5091ae6ff6197fad5462c7576769e13 Mon Sep 17 00:00:00 2001
From: Amr Hesham <amr96 at programmer.net>
Date: Wed, 12 Mar 2025 22:22:01 +0100
Subject: [PATCH 5/6] Update assert message

Co-authored-by: Andy Kaylor <akaylor at nvidia.com>
---
 clang/lib/CIR/CodeGen/CIRGenBuilder.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 1c63482ba5242..e119570a03258 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -39,7 +39,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
                   cir::IntType>(ty))
       return true;
 
-    assert(0 && "Unimplemented size for type");
+    assert(0 && "Unexpected MLIR type");
     return false;
   }
 };

>From b386dc1ad4ecc039237fd0bb78f83d6130ec825e Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Wed, 12 Mar 2025 22:33:39 +0100
Subject: [PATCH 6/6] Add MissingFeatures for unsized types

---
 clang/include/clang/CIR/MissingFeatures.h | 2 ++
 clang/lib/CIR/CodeGen/CIRGenBuilder.h     | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h
index 6f845b7689e51..43f46ca89df36 100644
--- a/clang/include/clang/CIR/MissingFeatures.h
+++ b/clang/include/clang/CIR/MissingFeatures.h
@@ -76,6 +76,8 @@ struct MissingFeatures {
   static bool constructABIArgDirectExtend() { return false; }
   static bool opGlobalViewAttr() { return false; }
   static bool lowerModeOptLevel() { return false; }
+
+  static bool unsizedTypes() { return false; }
 };
 
 } // namespace cir
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index e119570a03258..260ee25719be1 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -12,6 +12,7 @@
 #include "CIRGenTypeCache.h"
 
 #include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
+#include "clang/CIR/MissingFeatures.h"
 
 namespace clang::CIRGen {
 
@@ -39,7 +40,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
                   cir::IntType>(ty))
       return true;
 
-    assert(0 && "Unexpected MLIR type");
+    assert(!cir::MissingFeatures::unsizedTypes());
     return false;
   }
 };



More information about the cfe-commits mailing list