Skip to content

[CIR] Upstream basic support for ArrayType #130502

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 8 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIRAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
23 changes: 21 additions & 2 deletions clang/include/clang/CIR/Dialect/IR/CIRTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -386,8 +405,8 @@ def VoidPtr : Type<
//===----------------------------------------------------------------------===//

def CIR_AnyType : AnyTypeOf<[
CIR_VoidType, CIR_BoolType, 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
2 changes: 2 additions & 0 deletions clang/include/clang/CIR/MissingFeatures.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ struct MissingFeatures {
static bool astVarDeclInterface() { return false; }
static bool stackSaveOp() { return false; }
static bool aggValueSlot() { return false; }

static bool unsizedTypes() { return false; }
};

} // namespace cir
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "CIRGenTypeCache.h"

#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
#include "clang/CIR/MissingFeatures.h"

namespace clang::CIRGen {

Expand All @@ -33,6 +34,15 @@ 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(!cir::MissingFeatures::unsizedTypes());
return false;
}
};

} // namespace clang::CIRGen
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
break;
}

case Type::ConstantArray: {
const ConstantArrayType *arrTy = cast<ConstantArrayType>(ty);
mlir::Type elemTy = convertTypeForMem(arrTy->getElementType());
resultType = cir::ArrayType::get(builder.getContext(), elemTy,
arrTy->getSize().getZExtValue());
break;
}

case Type::FunctionNoProto:
case Type::FunctionProto:
resultType = convertFunctionTypeInternal(type);
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,22 @@ BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
return 1;
}

//===----------------------------------------------------------------------===//
// Definitions
//===----------------------------------------------------------------------===//

llvm::TypeSize
ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these functions being called with any of the test cases? I don't see any checks that would be related to this.

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 was thinking of sizeof operator but it's not implemented yet, is there is another way to test the size in lit test

Copy link
Contributor

Choose a reason for hiding this comment

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

My point was that if the code isn't being called, you should leave it out until it is needed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but it's part of the DataLayoutTypeInterface interface so we must implement it, I can test it after merging the sizeof PR #130847

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks for the clarification.

::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
//===----------------------------------------------------------------------===//
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter &converter,

return mlir::LLVM::LLVMPointerType::get(type.getContext(), targetAS);
});
converter.addConversion([&](cir::ArrayType type) -> mlir::Type {
mlir::Type 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);
Expand Down
26 changes: 26 additions & 0 deletions clang/test/CIR/CodeGen/array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

Will multidimensional arrays and array function arguments work at this point? If so, can you add tests for those?


int a[10];
// CHECK: cir.global external @a : !cir.array<!cir.int<s, 32> 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][5];
// CHECK: cir.global external @bb : !cir.array<!cir.array<!cir.int<s, 32> x 5> x 10>

void f() {
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>>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also check the cir.alloca generated in this case?

// CHECK: cir.alloca !cir.ptr<!cir.int<s, 32>>, !cir.ptr<!cir.ptr<!cir.int<s, 32>>>, ["p", init]

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>>>
51 changes: 51 additions & 0 deletions clang/test/CIR/IR/array.cir
Original file line number Diff line number Diff line change
@@ -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: }

}
27 changes: 27 additions & 0 deletions clang/test/CIR/Lowering/array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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][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][5];
// CHECK: @bb = external dso_local global [10 x [5 x i32]]

void f() {
int l[10];
}
// CHECK: define void @f()
// CHECK-NEXT: alloca [10 x i32], i64 1, align 16

void f2(int p[10]) {}
// CHECK: define void @f2(ptr {{%.*}})
// CHECK-NEXT: alloca ptr, i64 1, align 8

void f3(int pp[10][5]) {}
// CHECK: define void @f3(ptr {{%.*}})
// CHECK-NEXT: alloca ptr, i64 1, align 8