Skip to content

[CIR] Add support for DumpRecordLayouts #1667

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 4 commits into from
Jun 9, 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
4 changes: 3 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenRecordLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct CIRGenBitFieldInfo {
StorageSize(StorageSize), StorageOffset(StorageOffset) {}

void print(llvm::raw_ostream &OS) const;
void dump() const;
LLVM_DUMP_METHOD void dump() const;

/// Given a bit-field decl, build an appropriate helper object for
/// accessing that field (which is expected to have the given offset and
Expand Down Expand Up @@ -201,6 +201,8 @@ class CIRGenRecordLayout {
assert(it != BitFields.end() && "Unable to find bitfield info");
return it->second;
}
void print(raw_ostream &os) const;
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's very handy to have an additional method, dump() which prints to llvm::errs() and is marked with LLVM_DUMP_METHOD. This lets you print the object from a debugger.

LLVM_DUMP_METHOD void dump() const;
};

} // namespace clang::CIRGen
Expand Down
44 changes: 43 additions & 1 deletion clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,13 +760,55 @@ CIRGenTypes::computeRecordLayout(const RecordDecl *D, cir::RecordType *Ty) {

// Dump the layout, if requested.
if (getContext().getLangOpts().DumpRecordLayouts) {
llvm_unreachable("NYI");
llvm::outs() << "\n*** Dumping CIRgen Record Layout\n";
llvm::outs() << "Record: ";
D->dump(llvm::outs());
llvm::outs() << "\nLayout: ";
RL->print(llvm::outs());
}

// TODO: implement verification
return RL;
}

void CIRGenRecordLayout::print(raw_ostream &os) const {
os << "<CIRecordLayout\n";
os << " CIR Type:" << CompleteObjectType << "\n";
if (BaseSubobjectType)
os << " NonVirtualBaseCIRType:" << BaseSubobjectType << "\n";
os << " IsZeroInitializable:" << IsZeroInitializable << "\n";
os << " BitFields:[\n";
std::vector<std::pair<unsigned, const CIRGenBitFieldInfo *>> bitInfo;
for (auto &[decl, info] : BitFields) {
const RecordDecl *rd = decl->getParent();
unsigned index = 0;
for (RecordDecl::field_iterator it = rd->field_begin(); *it != decl; ++it)
++index;
bitInfo.push_back(std::make_pair(index, &info));
}
llvm::array_pod_sort(bitInfo.begin(), bitInfo.end());
for (auto &info : bitInfo) {
os.indent(4);
info.second->print(os);
os << "\n";
}
os << " ]>\n";
}

void CIRGenRecordLayout::dump() const { print(llvm::errs()); }

void CIRGenBitFieldInfo::print(raw_ostream &os) const {
os << "<CIRBitFieldInfo" << " name:" << Name << " offset:" << Offset
<< " size:" << Size << " isSigned:" << IsSigned
<< " storageSize:" << StorageSize
<< " storageOffset:" << StorageOffset.getQuantity()
<< " volatileOffset:" << VolatileOffset
<< " volatileStorageSize:" << VolatileStorageSize
<< " volatileStorageOffset:" << VolatileStorageOffset.getQuantity() << ">";
}

void CIRGenBitFieldInfo::dump() const { print(llvm::errs()); }

CIRGenBitFieldInfo CIRGenBitFieldInfo::MakeInfo(CIRGenTypes &Types,
const FieldDecl *FD,
uint64_t Offset, uint64_t Size,
Expand Down
64 changes: 64 additions & 0 deletions clang/test/CIR/CodeGen/dumb-record.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -fdump-record-layouts %s -o - | FileCheck %s

struct SimpleStruct {
int a;
float b;
} simple;
// CHECK: Layout: <CIRecordLayout
// CHECK: CIR Type:!cir.record<struct "SimpleStruct" {!cir.int<s, 32>, !cir.float} #cir.record.decl.ast>
// CHECK: NonVirtualBaseCIRType:!cir.record<struct "SimpleStruct" {!cir.int<s, 32>, !cir.float} #cir.record.decl.ast>
// CHECK: IsZeroInitializable:1
// CHECK: BitFields:[
// CHECK: ]>

struct Empty {
} empty;

// CHECK: Layout: <CIRecordLayout
// CHECK: CIR Type:!cir.record<struct "Empty" padded {!cir.int<u, 8>} #cir.record.decl.ast>
// CHECK: NonVirtualBaseCIRType:!cir.record<struct "Empty" padded {!cir.int<u, 8>} #cir.record.decl.ast>
// CHECK: IsZeroInitializable:1
// CHECK: BitFields:[
// CHECK: ]>

struct BitfieldsInOrder {
char a;
unsigned bit: 8;
unsigned should : 20;
unsigned have: 3;
unsigned order: 1;
} bitfield_order;

// CHECK: Layout: <CIRecordLayout
// CHECK: CIR Type:!cir.record<struct "BitfieldsInOrder" padded {!cir.int<s, 8>, !cir.int<u, 8>, !cir.array<!cir.int<u, 8> x 2>, !cir.array<!cir.int<u, 8> x 3>, !cir.int<u, 8>} #cir.record.decl.ast>
// CHECK: NonVirtualBaseCIRType:!cir.record<struct "BitfieldsInOrder" padded {!cir.int<s, 8>, !cir.int<u, 8>, !cir.array<!cir.int<u, 8> x 2>, !cir.array<!cir.int<u, 8> x 3>, !cir.int<u, 8>} #cir.record.decl.ast>
// CHECK: IsZeroInitializable:1
// CHECK: BitFields:[
// CHECK-NEXT: <CIRBitFieldInfo name:bit offset:0 size:8 isSigned:0 storageSize:8 storageOffset:1 volatileOffset:0 volatileStorageSize:0 volatileStorageOffset:0>
// CHECK-NEXT: <CIRBitFieldInfo name:should offset:0 size:20 isSigned:0 storageSize:24 storageOffset:4 volatileOffset:0 volatileStorageSize:0 volatileStorageOffset:0>
// CHECK-NEXT: <CIRBitFieldInfo name:have offset:20 size:3 isSigned:0 storageSize:24 storageOffset:4 volatileOffset:0 volatileStorageSize:0 volatileStorageOffset:0>
// CHECK-NEXT: <CIRBitFieldInfo name:order offset:23 size:1 isSigned:0 storageSize:24 storageOffset:4 volatileOffset:0 volatileStorageSize:0 volatileStorageOffset:0>
// CHECK:]>

struct Inner {
int x;
} in;

//CHECK: Layout: <CIRecordLayout
//CHECK: CIR Type:!cir.record<struct "Inner" {!cir.int<s, 32>} #cir.record.decl.ast>
//CHECK: NonVirtualBaseCIRType:!cir.record<struct "Inner" {!cir.int<s, 32>} #cir.record.decl.ast>
//CHECK: IsZeroInitializable:1
//CHECK: BitFields:[
//CHECK: ]>

struct Outer {
Inner i;
int y = 6;
} ou;

//CHECK: Layout: <CIRecordLayout
//CHECK: CIR Type:!cir.record<struct "Outer" {!cir.record<struct "Inner" {!cir.int<s, 32>} #cir.record.decl.ast>, !cir.int<s, 32>} #cir.record.decl.ast>
//CHECK: NonVirtualBaseCIRType:!cir.record<struct "Outer" {!cir.record<struct "Inner" {!cir.int<s, 32>} #cir.record.decl.ast>, !cir.int<s, 32>} #cir.record.decl.ast>
//CHECK: IsZeroInitializable:1
//CHECK: BitFields:[
//CHECK: ]>
Loading