Skip to content
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

[CIR][Lowering] Fix lowering for multi dimensional array #851

Merged
merged 17 commits into from
Sep 18, 2024
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
38 changes: 28 additions & 10 deletions clang/lib/CIR/Lowering/LoweringHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,43 @@ mlir::Type getNestedTypeAndElemQuantity(mlir::Type Ty, unsigned &elemQuantity) {
return nestTy;
}

template <typename StorageTy>
void fillTrailingZeros(mlir::cir::ConstArrayAttr attr,
llvm::SmallVectorImpl<StorageTy> &values) {
auto numTrailingZeros = attr.getTrailingZerosNum();
if (numTrailingZeros) {
auto localArrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType());
assert(localArrayTy && "expected !cir.array");

auto nestTy = localArrayTy.getEltType();
if (!mlir::isa<mlir::cir::ArrayType>(nestTy))
values.insert(values.end(), numTrailingZeros,
getZeroInitFromType<StorageTy>(nestTy));
}
}

template <typename AttrTy, typename StorageTy>
void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,
llvm::SmallVectorImpl<StorageTy> &values) {
if (auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) {
if (auto arrayType = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType())) {
for (auto element : stringAttr) {
auto intAttr = mlir::cir::IntAttr::get(arrayType.getEltType(), element);
values.push_back(mlir::dyn_cast<AttrTy>(intAttr).getValue());
}
return;
}
}

auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts());
for (auto eltAttr : arrayAttr) {
if (auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) {
values.push_back(valueAttr.getValue());
} else if (auto subArrayAttr =
mlir::dyn_cast<mlir::cir::ConstArrayAttr>(eltAttr)) {
convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values);
if (mlir::dyn_cast<mlir::StringAttr>(subArrayAttr.getElts()))
fillTrailingZeros(subArrayAttr, values);
} else if (auto zeroAttr = mlir::dyn_cast<mlir::cir::ZeroAttr>(eltAttr)) {
unsigned numStoredZeros = 0;
auto nestTy =
Expand All @@ -84,16 +111,7 @@ void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,

// Only fill in trailing zeros at the local cir.array level where the element
// type isn't another array (for the mult-dim case).
auto numTrailingZeros = attr.getTrailingZerosNum();
if (numTrailingZeros) {
auto localArrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType());
assert(localArrayTy && "expected !cir.array");

auto nestTy = localArrayTy.getEltType();
if (!mlir::isa<mlir::cir::ArrayType>(nestTy))
values.insert(values.end(), numTrailingZeros,
getZeroInitFromType<StorageTy>(nestTy));
}
fillTrailingZeros(attr, values);
}

template <typename AttrTy, typename StorageTy>
Expand Down
17 changes: 17 additions & 0 deletions clang/test/CIR/Lowering/array-init.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM

// LLVM: charInit1.ar = internal global [4 x [4 x i8]] {{.*}}4 x i8] c"aa\00\00", [4 x i8] c"aa\00\00", [4 x i8] c"aa\00\00", [4 x i8] c"aa\00\00"], align 16
char charInit1() {
static char ar[][4] = {"aa", "aa", "aa", "aa"};
return ar[0][0];
}

// LLVM: define dso_local void @zeroInit
// LLVM: [[RES:%.*]] = alloca [3 x i32], i64 1
// LLVM: store [3 x i32] zeroinitializer, ptr [[RES]]
void zeroInit() {
int a[3] = {0, 0, 0};
}

// LLVM: %1 = alloca [4 x [1 x i8]], i64 1, align 1
// LLVM: store [4 x [1 x i8]] {{.*}}1 x i8] c"a", [1 x i8] c"b", [1 x i8] c"c", [1 x i8] c"d"], ptr %1, align 1
void charInit2() {
char arr[4][1] = {"a", "b", "c", "d"};
}

// LLVM: %1 = alloca [4 x [2 x i8]], i64 1, align 1
// LLVM: store [4 x [2 x i8]] {{.*}}2 x i8] c"ab", [2 x i8] c"cd", [2 x i8] c"ef", [2 x i8] c"gh"], ptr %1, align 1
void charInit3() {
char arr[4][2] = {"ab", "cd", "ef", "gh"};
}
Loading