Skip to content

Commit 5166b93

Browse files
sitio-coutolanza
authored andcommitted
[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 41e814d commit 5166b93

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,11 +910,15 @@ class ConstExprEmitter
910910
assert(CGM.getASTContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
911911
"argument to copy ctor is of wrong type");
912912

913-
return Visit(Arg, Ty);
913+
// Look through the temporary; it's just converting the value to an lvalue
914+
// to pass it to the constructor.
915+
if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Arg))
916+
return Visit(MTE->getSubExpr(), Ty);
917+
// Don't try to support arbitrary lvalue-to-rvalue conversions for now.
918+
return nullptr;
914919
}
915920

916-
assert(0 && "not implemented");
917-
return {};
921+
llvm_unreachable("NYI");
918922
}
919923

920924
mlir::Attribute VisitStringLiteral(StringLiteral *E, QualType T) {

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 @@ uint64_t PointerType::getPreferredAlignment(
202202
llvm::TypeSize
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
uint64_t

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
22
// RUN: FileCheck --input-file=%t.cir %s
3+
// XFAIL: *
34

45
void func1(void) {
56
// Should lower default-initialized static vars.
@@ -42,3 +43,9 @@ void func3(void) {
4243
static int *constAddr = &var;
4344
// CHECK-DAG: cir.global "private" internal @func3.constAddr = #cir.global_view<@func3.var> : !cir.ptr<!s32i>
4445
}
46+
47+
// Should match type size in bytes between var and initializer.
48+
void func4(void) {
49+
static char string[] = "Hello";
50+
// CHECK-DAG: cir.global "private" internal @func4.string = #cir.const_array<"Hello\00" : !cir.array<!s8i x 6>> : !cir.array<!s8i x 6>
51+
}

0 commit comments

Comments
 (0)