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][CIRGen] virtual table pointer initialization without ctor #1283

Merged
merged 2 commits into from
Jan 14, 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
23 changes: 19 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenExprConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,12 +736,27 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
const CXXRecordDecl *VTableClass,
CharUnits Offset) {
const ASTRecordLayout &Layout = CGM.getASTContext().getASTRecordLayout(RD);

if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
// Add a vtable pointer, if we need one and it hasn't already been added.
if (Layout.hasOwnVFPtr())
llvm_unreachable("NYI");

if (Layout.hasOwnVFPtr()) {
CIRGenBuilderTy &builder = CGM.getBuilder();
cir::GlobalOp vtable =
CGM.getCXXABI().getAddrOfVTable(VTableClass, CharUnits());
clang::VTableLayout::AddressPointLocation addressPoint =
CGM.getItaniumVTableContext()
.getVTableLayout(VTableClass)
.getAddressPoint(BaseSubobject(CD, Offset));
assert(!cir::MissingFeatures::ptrAuth());
mlir::ArrayAttr indices = builder.getArrayAttr({
builder.getI32IntegerAttr(0),
builder.getI32IntegerAttr(addressPoint.VTableIndex),
builder.getI32IntegerAttr(addressPoint.AddressPointIndex),
});
cir::GlobalViewAttr vtableInit =
CGM.getBuilder().getGlobalViewAttr(vtable, indices);
if (!AppendBytes(Offset, vtableInit))
return false;
}
// Accumulate and sort bases, in order to visit them in address order, which
// may not be the same as declaration order.
SmallVector<BaseInfo, 8> Bases;
Expand Down
16 changes: 15 additions & 1 deletion clang/test/CIR/CodeGen/vtable-emission.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -o - %s \
// RUN: | opt -S -passes=instcombine,mem2reg,simplifycfg -o %t.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s

struct S {
virtual void key();
virtual void nonKey() {}
};
} sobj;

void S::key() {}

// CHECK-DAG: !ty_anon_struct1 = !cir.struct<struct {!cir.array<!cir.ptr<!u8i> x 4>}>
// CHECK-DAG: !ty_anon_struct2 = !cir.struct<struct {!cir.ptr<!ty_anon_struct1>}>

// The definition of the key function should result in the vtable being emitted.
// CHECK: cir.global external @_ZTV1S = #cir.vtable
// LLVM: @_ZTV1S = global { [4 x ptr] } { [4 x ptr]
// LLVM-SAME: [ptr null, ptr @_ZTI1S, ptr @_ZN1S3keyEv, ptr @_ZN1S6nonKeyEv] }, align 8

// CHECK: cir.global external @sobj = #cir.const_struct
// CHECK-SAME: <{#cir.global_view<@_ZTV1S, [0 : i32, 0 : i32, 2 : i32]> :
// CHECK-SAME: !cir.ptr<!ty_anon_struct1>}> : !ty_anon_struct2 {alignment = 8 : i64}
// LLVM: @sobj = global { ptr } { ptr getelementptr inbounds
// LLVM-SAME: ({ [4 x ptr] }, ptr @_ZTV1S, i32 0, i32 0, i32 2) }, align 8

// The reference from the vtable should result in nonKey being emitted.
// CHECK: cir.func linkonce_odr @_ZN1S6nonKeyEv({{.*}} {
Loading