Skip to content

Commit 8a2d68f

Browse files
authored
[clang][CodeGen] Switch declaration of vtable information to be [0 x ptr] (#65596)
Continuing the discussion in https://discourse.llvm.org/t/codegen-layout-of-si-class-type-info-doesnt-match-the-actual-size/73274 Before we had this code: @_ZTVN10__cxxabiv117__class_type_infoE = external global ptr now we'll produce: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr] This is because we may not know the exact size of this data, and clang issues gep inbounds with idx=2. Before, that gep would always result in poison.
1 parent 0a6f658 commit 8a2d68f

File tree

6 files changed

+11
-10
lines changed

6 files changed

+11
-10
lines changed

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3669,9 +3669,10 @@ void ItaniumRTTIBuilder::BuildVTablePointer(const Type *Ty) {
36693669
// Check if the alias exists. If it doesn't, then get or create the global.
36703670
if (CGM.getItaniumVTableContext().isRelativeLayout())
36713671
VTable = CGM.getModule().getNamedAlias(VTableName);
3672-
if (!VTable)
3673-
VTable =
3674-
CGM.getModule().getOrInsertGlobal(VTableName, CGM.GlobalsInt8PtrTy);
3672+
if (!VTable) {
3673+
llvm::Type *Ty = llvm::ArrayType::get(CGM.GlobalsInt8PtrTy, 0);
3674+
VTable = CGM.getModule().getOrInsertGlobal(VTableName, Ty);
3675+
}
36753676

36763677
CGM.setDSOLocal(cast<llvm::GlobalValue>(VTable->stripPointerCasts()));
36773678

clang/test/CodeGenCXX/RelativeVTablesABI/child-vtable-in-comdat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// CHECK-DAG: @_ZTV1B.local = linkonce_odr hidden unnamed_addr constant { [3 x i32] } { [3 x i32] [i32 0, i32 trunc (i64 sub (i64 ptrtoint (ptr @_ZTI1B.rtti_proxy to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1B.local, i32 0, i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @_ZN1B3fooEv to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1B.local, i32 0, i32 0, i32 2) to i64)) to i32)] }, comdat($_ZTV1B), align 4
1515

1616
// The RTTI objects aren’t that important, but it is good to know that they are emitted here since they are used in the vtable for B, and external references are used for RTTI stuff from A.
17-
// CHECK-DAG: @_ZTVN10__cxxabiv120__si_class_type_infoE = external global ptr
17+
// CHECK-DAG: @_ZTVN10__cxxabiv120__si_class_type_infoE = external global [0 x ptr]
1818
// CHECK-DAG: @_ZTS1B =
1919
// CHECK-DAG: @_ZTI1A =
2020
// CHECK-DAG: @_ZTI1B =

clang/test/CodeGenCXX/RelativeVTablesABI/parent-vtable-in-comdat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
// The VTable for A is emitted here and in a comdat section since it has no key function, and is used in this module when creating an instance of A.
1717
// CHECK: @_ZTV1A.local = linkonce_odr hidden unnamed_addr constant { [3 x i32] } { [3 x i32] [i32 0, i32 trunc (i64 sub (i64 ptrtoint (ptr @_ZTI1A.rtti_proxy to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1A.local, i32 0, i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @_ZN1A3fooEv to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1A.local, i32 0, i32 0, i32 2) to i64)) to i32)] }, comdat($_ZTV1A), align 4
18-
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external global ptr
18+
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr]
1919
// CHECK: @_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00", comdat, align 1
2020
// CHECK: @_ZTI1A = linkonce_odr constant { ptr, ptr } { ptr getelementptr inbounds (i8, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i32 8), ptr @_ZTS1A }, comdat, align 8
2121
// CHECK: @_ZTI1A.rtti_proxy = hidden unnamed_addr constant ptr @_ZTI1A, comdat

clang/test/CodeGenCXX/RelativeVTablesABI/simple-vtable-definition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// The vtable definition itself is private so we can take relative references to
1010
// it. The vtable symbol will be exposed through a public alias.
1111
// CHECK: @_ZTV1A.local = private unnamed_addr constant { [3 x i32] } { [3 x i32] [i32 0, i32 trunc (i64 sub (i64 ptrtoint (ptr @_ZTI1A.rtti_proxy to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1A.local, i32 0, i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr dso_local_equivalent @_ZN1A3fooEv to i64), i64 ptrtoint (ptr getelementptr inbounds ({ [3 x i32] }, ptr @_ZTV1A.local, i32 0, i32 0, i32 2) to i64)) to i32)] }, align 4
12-
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external global ptr
12+
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr]
1313
// CHECK: @_ZTS1A ={{.*}} constant [3 x i8] c"1A\00", align 1
1414
// CHECK: @_ZTI1A ={{.*}} constant { ptr, ptr } { ptr getelementptr inbounds (i8, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i32 8), ptr @_ZTS1A }, align 8
1515

clang/test/CodeGenCXX/RelativeVTablesABI/type-info.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
// CHECK: $_ZTI1A.rtti_proxy = comdat any
88
// CHECK: $_ZTI1B.rtti_proxy = comdat any
99

10-
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external global ptr
10+
// CHECK: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr]
1111
// CHECK: @_ZTS1A ={{.*}} constant [3 x i8] c"1A\00", align 1
1212
// CHECK: @_ZTI1A ={{.*}} constant { ptr, ptr } { ptr getelementptr inbounds (i8, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i32 8), ptr @_ZTS1A }, align 8
13-
// CHECK: @_ZTVN10__cxxabiv120__si_class_type_infoE = external global ptr
13+
// CHECK: @_ZTVN10__cxxabiv120__si_class_type_infoE = external global [0 x ptr]
1414
// CHECK: @_ZTS1B ={{.*}} constant [3 x i8] c"1B\00", align 1
1515
// CHECK: @_ZTI1B ={{.*}} constant { ptr, ptr, ptr } { ptr getelementptr inbounds (i8, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i32 8), ptr @_ZTS1B, ptr @_ZTI1A }, align 8
1616
// CHECK: @_ZTI1A.rtti_proxy = hidden unnamed_addr constant ptr @_ZTI1A, comdat

clang/test/CodeGenCXX/typeinfo-with-address-space.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class B : A {
1414
// NO-AS: @_ZTISt9type_info = external constant ptr
1515
// AS: @_ZTIi = external addrspace(1) constant ptr addrspace(1)
1616
// NO-AS: @_ZTIi = external constant ptr
17-
// AS: @_ZTVN10__cxxabiv117__class_type_infoE = external addrspace(1) global ptr addrspace(1)
18-
// NO-AS: @_ZTVN10__cxxabiv117__class_type_infoE = external global ptr
17+
// AS: @_ZTVN10__cxxabiv117__class_type_infoE = external addrspace(1) global [0 x ptr addrspace(1)]
18+
// NO-AS: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr]
1919
// AS: @_ZTS1A = linkonce_odr addrspace(1) constant [3 x i8] c"1A\00", comdat, align 1
2020
// NO-AS: @_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00", comdat, align 1
2121
// AS: @_ZTI1A = linkonce_odr addrspace(1) constant { ptr addrspace(1), ptr addrspace(1) } { ptr addrspace(1) getelementptr inbounds (ptr addrspace(1), ptr addrspace(1) @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr addrspace(1) @_ZTS1A }, comdat, align 8

0 commit comments

Comments
 (0)