Skip to content

Commit d0a1d1e

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-poisoned]
1 parent 368e7d9 commit d0a1d1e

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
@@ -35,3 +35,9 @@ void func2(void) {
3535
static float j;
3636
// CHECK-DAG: cir.global "private" internal @func2.j = 0.000000e+00 : f32
3737
}
38+
39+
// Should match type size in bytes between var and initializer.
40+
void func4(void) {
41+
static char string[] = "Hello";
42+
// CHECK-DAG: cir.global "private" internal @func4.string = #cir.const_array<"Hello\00" : !cir.array<!s8i x 6>> : !cir.array<!s8i x 6>
43+
}

0 commit comments

Comments
 (0)