Skip to content

Commit fe5b59f

Browse files
bruteforceboylanza
authored andcommitted
[CIR][Lowering] Fix static array lowering (#838)
Consider the following code snippet `test.c`: ``` int test(int x) { static int arr[10] = {0, 1, 0, 0}; return arr[x]; } ``` When lowering from CIR to LLVM using `bin/clang test.c -Xclang -fclangir -Xclang -emit-llvm -S -o -` It produces: ``` clangir/mlir/lib/IR/BuiltinAttributes.cpp:1015: static mlir::DenseElementsAttr mlir::DenseElementsAttr::get(mlir::ShapedType, llvm::ArrayRef<llvm::APInt>): Assertion `hasSameElementsOrSplat(type, values)' failed. ``` I traced the bug back to `Lowering/LoweringHelpers.cpp` where we fill trailing zeros, and I believe this PR does it the right way. I have also added a very simple test for verification.
1 parent dcc7cfb commit fe5b59f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

clang/lib/CIR/Lowering/LoweringHelpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void convertToDenseElementsAttrImpl(mlir::cir::ConstArrayAttr attr,
9191

9292
auto nestTy = localArrayTy.getEltType();
9393
if (!mlir::isa<mlir::cir::ArrayType>(nestTy))
94-
values.insert(values.end(), localArrayTy.getSize() - numTrailingZeros,
94+
values.insert(values.end(), numTrailingZeros,
9595
getZeroInitFromType<StorageTy>(nestTy));
9696
}
9797
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t.ll
2+
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
3+
4+
int test(int x) {
5+
static int arr[10] = {0, 1, 0, 0};
6+
return arr[x];
7+
}
8+
// LLVM: internal global [10 x i32] [i32 0, i32 1, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0, i32 0]

0 commit comments

Comments
 (0)