Skip to content

Commit cb12c3a

Browse files
committed
[CIR][Bugfix] Fix cir.array getTypeSizeInBits method
Constant initialization of static local arrays would fail due to a mismatch between the variable and the initializer type size. This patch fixes the data layout interface implementation for the cir.array type. A complete array in C/C++ should have its type size in bits equal to the size of the array times the size of the element type. ghstack-source-id: 56f3f29 Pull Request resolved: #206
1 parent beb5bb5 commit cb12c3a

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ unsigned PointerType::getPreferredAlignment(
202202
unsigned
203203
ArrayType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
204204
::mlir::DataLayoutEntryListRef params) const {
205-
return dataLayout.getTypeSizeInBits(getEltType());
205+
return getSize() * dataLayout.getTypeSizeInBits(getEltType());
206206
}
207207

208208
unsigned

clang/test/CIR/CodeGen/static-vars.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ void func3(void) {
4242
static int *constAddr = &var;
4343
// CHECK-DAG: cir.global "private" internal @func3.constAddr = #cir.global_view<@func3.var> : !cir.ptr<!s32i>
4444
}
45+
46+
// Should match type size in bytes between var and initializer.
47+
void func4(void) {
48+
static char string[] = "Hello";
49+
// CHECK-DAG: cir.global "private" internal @func4.string = #cir.const_array<"Hello\00" : !cir.array<!s8i x 6>> : !cir.array<!s8i x 6>
50+
}

0 commit comments

Comments
 (0)