Skip to content

[CIR] Update isSized with upstreamed types #143960

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

Merged
merged 1 commit into from
Jun 13, 2025
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
5 changes: 3 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
}

bool isSized(mlir::Type ty) {
if (mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType,
cir::IntType>(ty))
if (mlir::isa<cir::PointerType, cir::ArrayType, cir::BoolType, cir::IntType,
cir::CIRFPTypeInterface, cir::ComplexType, cir::RecordType>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we figured out a way to make patches that add new types 'check' here. a switch on the TypeID is typically what we'd suggest in the FE, but of course that doesn't work here for a number of reasons. Would be nice if someone thought of a way to make sure we didn't forget this again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about creating IsSized type trait that is attached to type definition, so one does not need to track uses like this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a lovely idea!

ty))
return true;

if (const auto vt = mlir::dyn_cast<cir::VectorType>(ty))
Expand Down
13 changes: 11 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
case Type::ConstantArray: {
const ConstantArrayType *arrTy = cast<ConstantArrayType>(ty);
mlir::Type elemTy = convertTypeForMem(arrTy->getElementType());

// TODO(CIR): In LLVM, "lower arrays of undefined struct type to arrays of
// i8 just to have a concrete type"
if (!builder.isSized(elemTy)) {
cgm.errorNYI(SourceLocation(), "arrays of undefined struct type", type);
resultType = cgm.UInt32Ty;
break;
}

resultType = cir::ArrayType::get(elemTy, arrTy->getSize().getZExtValue());
break;
}
Expand All @@ -432,8 +441,8 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
}

case Type::Enum: {
const EnumDecl *ED = cast<EnumType>(ty)->getDecl();
if (auto integerType = ED->getIntegerType(); !integerType.isNull())
const EnumDecl *ed = cast<EnumType>(ty)->getDecl();
if (auto integerType = ed->getIntegerType(); !integerType.isNull())
return convertType(integerType);
// Return a placeholder 'i32' type. This can be changed later when the
// type is defined (see UpdateCompletedType), but is likely to be the
Expand Down
23 changes: 23 additions & 0 deletions clang/test/CIR/CodeGen/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,26 @@ void func10(int *a) {
// OGCG: %[[ELE:.*]] = getelementptr inbounds i32, ptr %[[TMP_1]], i64 5
// OGCG: %[[TMP_2:.*]] = load i32, ptr %[[ELE]], align 4
// OGCG: store i32 %[[TMP_2]], ptr %[[INIT]], align 4

void func11() { int _Complex a[4]; }

// CIR: %[[ARR:.*]] = cir.alloca !cir.array<!cir.complex<!s32i> x 4>, !cir.ptr<!cir.array<!cir.complex<!s32i> x 4>>, ["a"]

// LLVM: %[[ARR:.*]] = alloca [4 x { i32, i32 }], i64 1, align 16

// OGCG: %[[ARR:.*]] = alloca [4 x { i32, i32 }], align 16

void func12() {
struct Point {
int x;
int y;
};

Point a[4];
}

// CIR: %[[ARR:.*]] = cir.alloca !cir.array<!rec_Point x 4>, !cir.ptr<!cir.array<!rec_Point x 4>>, ["a"]

// LLVM: %[[ARR:.*]] = alloca [4 x %struct.Point], i64 1, align 16

// OGCG: %[[ARR:.*]] = alloca [4 x %struct.Point], align 16
Loading