Skip to content

Commit 2223527

Browse files
committed
Address review feedback
1 parent 72b4b2d commit 2223527

File tree

6 files changed

+41
-36
lines changed

6 files changed

+41
-36
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTypesDetails.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ struct StructTypeStorage : public mlir::TypeStorage {
5353
bool incomplete, bool packed, bool padded,
5454
StructType::RecordKind kind)
5555
: members(members), name(name), incomplete(incomplete), packed(packed),
56-
padded(padded), kind(kind) {}
56+
padded(padded), kind(kind) {
57+
assert(name || !incomplete && "Incomplete structs must have a name");
58+
}
5759

5860
KeyTy getAsKey() const {
5961
return KeyTy(members, name, incomplete, packed, padded, kind);
@@ -62,9 +64,9 @@ struct StructTypeStorage : public mlir::TypeStorage {
6264
bool operator==(const KeyTy &key) const {
6365
if (name)
6466
return (name == key.name) && (kind == key.kind);
65-
return (members == key.members) && (name == key.name) &&
66-
(incomplete == key.incomplete) && (packed == key.packed) &&
67-
(padded == key.padded) && (kind == key.kind);
67+
return std::tie(members, name, incomplete, packed, padded, kind) ==
68+
std::tie(key.members, key.name, key.incomplete, key.packed,
69+
key.padded, key.kind);
6870
}
6971

7072
static llvm::hash_code hashKey(const KeyTy &key) {

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
6262
case clang::TagTypeKind::Interface:
6363
llvm_unreachable("interface records are NYI");
6464
case clang::TagTypeKind::Enum:
65-
llvm_unreachable("enum records are NYI");
65+
llvm_unreachable("enums are not records");
6666
}
6767
}
6868

69-
/// Get an incomplete CIR struct type.
69+
/// Get an incomplete CIR struct type. If we have a complete record
70+
/// declaration, we may create an incomplete type and then add the
71+
/// members, so \p rd here may be complete.
7072
cir::StructType getIncompleteStructTy(llvm::StringRef name,
7173
const clang::RecordDecl *rd) {
7274
const mlir::StringAttr nameAttr = getStringAttr(name);

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ void CIRGenModule::emitTopLevelDecl(Decl *decl) {
591591
break;
592592

593593
case Decl::Record:
594+
case Decl::CXXRecord:
594595
assert(!cir::MissingFeatures::generateDebugInfo());
595596
break;
596597
}

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,16 @@ std::string CIRGenTypes::getRecordTypeName(const clang::RecordDecl *recordDecl,
9595

9696
PrintingPolicy policy = recordDecl->getASTContext().getPrintingPolicy();
9797
policy.SuppressInlineNamespace = false;
98+
policy.AlwaysIncludeTypeForTemplateArgument = true;
99+
policy.PrintCanonicalTypes = true;
100+
policy.SuppressTagKeyword = true;
98101

99-
if (recordDecl->getIdentifier()) {
100-
recordDecl->printQualifiedName(outStream, policy);
101-
102-
// Ensure each template specialization has a unique name.
103-
if (auto *templateSpecialization =
104-
llvm::dyn_cast<ClassTemplateSpecializationDecl>(recordDecl)) {
105-
outStream << '<';
106-
const ArrayRef<TemplateArgument> args =
107-
templateSpecialization->getTemplateArgs().asArray();
108-
const auto printer = [&policy, &outStream](const TemplateArgument &arg) {
109-
/// Print this template argument to the given output stream.
110-
arg.print(policy, outStream, /*IncludeType=*/true);
111-
};
112-
llvm::interleaveComma(args, outStream, printer);
113-
outStream << '>';
114-
}
115-
} else if (auto *typedefNameDecl = recordDecl->getTypedefNameForAnonDecl()) {
102+
if (recordDecl->getIdentifier())
103+
astContext.getRecordType(recordDecl).print(outStream, policy);
104+
else if (auto *typedefNameDecl = recordDecl->getTypedefNameForAnonDecl())
116105
typedefNameDecl->printQualifiedName(outStream, policy);
117-
} else {
106+
else
118107
outStream << builder.getUniqueAnonRecordName();
119-
}
120108

121109
if (!suffix.empty())
122110
outStream << suffix;
@@ -131,7 +119,9 @@ mlir::Type CIRGenTypes::convertRecordDeclType(const clang::RecordDecl *rd) {
131119
const Type *key = astContext.getTagDeclType(rd).getTypePtr();
132120
cir::StructType entry = recordDeclTypes[key];
133121

134-
// Handle forward decl / incomplete types.
122+
// If we don't have an entry for this record yet, create one.
123+
// We create an incomplete type initially. If `rd` is complete, we will
124+
// add the members below.
135125
if (!entry) {
136126
auto name = getRecordTypeName(rd, "");
137127
entry = builder.getIncompleteStructTy(name, rd);

clang/test/CIR/CodeGen/struct.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@
55
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
66
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
77

8-
// Declaration with an incomplete struct type.
9-
struct U *p;
8+
struct IncompleteS *p;
109

11-
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!cir.struct<struct "U" incomplete>>
10+
// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!cir.struct<struct "IncompleteS" incomplete>>
1211
// LLVM: @p = dso_local global ptr null
1312
// OGCG: @p = global ptr null, align 8
1413

1514
void f(void) {
16-
struct U2 *p;
15+
struct IncompleteS *p;
1716
}
1817

19-
// CIR: cir.func @f()
20-
// CIR-NEXT: cir.alloca !cir.ptr<!cir.struct<struct "U2" incomplete>>,
21-
// CIR-SAME: !cir.ptr<!cir.ptr<!cir.struct<struct "U2" incomplete>>>, ["p"]
22-
// CIR-NEXT: cir.return
18+
// CIR: cir.func @f()
19+
// CIR-NEXT: cir.alloca !cir.ptr<!cir.struct<struct "IncompleteS" incomplete>>,
20+
// CIR-SAME: !cir.ptr<!cir.ptr<!cir.struct<struct "IncompleteS" incomplete>>>, ["p"]
21+
// CIR-NEXT: cir.return
22+
23+
// LLVM: define void @f()
24+
// LLVM-NEXT: %[[P:.*]] = alloca ptr, i64 1, align 8
25+
// LLVM-NEXT: ret void
26+
27+
// OGCG: define{{.*}} void @f()
28+
// OGCG-NEXT: entry:
29+
// OGCG-NEXT: %[[P:.*]] = alloca ptr, align 8
30+
// OGCG-NEXT: ret void

clang/test/CIR/IR/struct.cir

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: cir-opt %s | FileCheck %s
22

33
module {
4-
cir.global external @p = #cir.ptr<null> : !cir.ptr<!cir.struct<struct "U" incomplete>>
4+
cir.global external @p1 = #cir.ptr<null> : !cir.ptr<!cir.struct<struct "S" incomplete>>
5+
cir.global external @p2 = #cir.ptr<null> : !cir.ptr<!cir.struct<union "U" incomplete>>
56
}
67

7-
// CHECK: cir.global external @p = #cir.ptr<null> : !cir.ptr<!cir.struct<struct "U" incomplete>>
8+
// CHECK: cir.global external @p1 = #cir.ptr<null> : !cir.ptr<!cir.struct<struct "S" incomplete>>
9+
// CHECK: cir.global external @p2 = #cir.ptr<null> : !cir.ptr<!cir.struct<union "U" incomplete>>

0 commit comments

Comments
 (0)