Skip to content
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

[CIR][IR] Relax get_member verifier for incomplete types #269

Merged
merged 8 commits into from
Oct 18, 2023
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
18 changes: 15 additions & 3 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,14 @@ LogicalResult MemCpyOp::verify() {
return mlir::success();
}

static bool isIncompleteType(mlir::Type typ) {
if (auto ptr = typ.dyn_cast<PointerType>())
return isIncompleteType(ptr.getPointee());
else if (auto rec = typ.dyn_cast<StructType>())
return !rec.getBody();
return false;
}

//===----------------------------------------------------------------------===//
// GetMemberOp Definitions
//===----------------------------------------------------------------------===//
Expand All @@ -2410,16 +2418,20 @@ LogicalResult GetMemberOp::verify() {

// FIXME: currently we bypass typechecking of incomplete types due to errors
// in the codegen process. This should be removed once the codegen is fixed.
if (!recordTy.getBody())
if (isIncompleteType(recordTy))
return mlir::success();

if (recordTy.getMembers().size() <= getIndex())
return emitError() << "member index out of bounds";

// FIXME(cir): member type check is disabled for classes as the codegen for
// these still need to be patched.
gitoleg marked this conversation as resolved.
Show resolved Hide resolved
if (!recordTy.isClass() &&
recordTy.getMembers()[getIndex()] != getResultTy().getPointee())
// Also we bypass the typechecking for the fields of incomplete types.
bool shouldSkipMemberTypeMismatch =
recordTy.isClass() || isIncompleteType(recordTy.getMembers()[getIndex()]);

if (!shouldSkipMemberTypeMismatch
&& recordTy.getMembers()[getIndex()] != getResultTy().getPointee())
return emitError() << "member type mismatch";

return mlir::success();
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CIR/CodeGen/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ struct Foo {
struct Bar z;
};

// Recursive type
typedef struct Node {
struct Node* next;
} NodeStru;

void baz(void) {
struct Bar b;
struct Foo f;
}

// CHECK-DAG: !ty_22Node22 = !cir.struct<struct "Node" incomplete #cir.record.decl.ast>
// CHECK-DAG: !ty_22Node221 = !cir.struct<struct "Node" {!cir.ptr<!ty_22Node22>} #cir.record.decl.ast>
// CHECK-DAG: !ty_22Bar22 = !cir.struct<struct "Bar" {!s32i, !s8i}>
// CHECK-DAG: !ty_22Foo22 = !cir.struct<struct "Foo" {!s32i, !s8i, !ty_22Bar22}>
// CHECK-DAG: module {{.*}} {
Expand Down Expand Up @@ -78,3 +85,9 @@ struct Bar shouldGenerateAndAccessStructArrays(void) {
// CHECK-DAG: %[[#DARR:]] = cir.cast(array_to_ptrdecay, %{{.+}} : !cir.ptr<!cir.array<!ty_22Bar22 x 1>>), !cir.ptr<!ty_22Bar22>
// CHECK-DAG: %[[#ELT:]] = cir.ptr_stride(%[[#DARR]] : !cir.ptr<!ty_22Bar22>, %[[#STRIDE]] : !s32i), !cir.ptr<!ty_22Bar22>
// CHECK-DAG: cir.copy %[[#ELT]] to %{{.+}} : !cir.ptr<!ty_22Bar22>

// CHECK-DAG: cir.func @useRecursiveType
// CHECK-DAG: cir.get_member {{%.}}[0] {name = "next"} : !cir.ptr<!ty_22Node221> -> !cir.ptr<!cir.ptr<!ty_22Node221>>
void useRecursiveType(NodeStru* a) {
a->next = 0;
}