[clang] [clang][CodeGen] Identify virtual methods by their override chain root in the call graph section (PR #230831)
Prabhu Rajasekaran via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 14:56:28 PST 2026
https://github.com/Prabhuk updated https://github.com/llvm/llvm-project/pull/230831
>From e100bf1da0f1eae90b53900f6ebc40d46545e55b Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Sat, 10 Oct 2026 12:24:22 -0700
Subject: [PATCH 1/2] [clang][CodeGen] Identify virtual methods by their
override chain root in the call graph section
A virtual call site is typed after the method named at the call site, but it
can reach any overrider of that method, and overriders may differ from it in
their covariant return type. In that case the `!callee_type` identifier of a
call through a base class pointer did not match the `!callgraph` identifier
of the overrider, and the call graph section missed the edge.
Identify every method of an override chain, at definitions, call sites and
thunks, by the type of the method that introduced the virtual function, so
that virtual calls through a pointer to any class of the hierarchy match the
overriders they can reach.
Stacked on #230838.
Assisted-by: Claude
---
clang/lib/CodeGen/CGCall.cpp | 4 +-
clang/lib/CodeGen/CGVTables.cpp | 9 ++
clang/lib/CodeGen/CodeGenModule.cpp | 25 +++++-
clang/lib/CodeGen/CodeGenModule.h | 8 ++
.../call-graph-section-covariant-return.cpp | 87 +++++++++++++++++++
5 files changed, 131 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CodeGen/call-graph-section-covariant-return.cpp
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 1d7f9f742b87e9..d9be1270fcd96c 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -6439,7 +6439,9 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
*callOrInvoke = CI;
if (CGM.getCodeGenOpts().CallGraphSection) {
QualType CST;
- if (TargetDecl && TargetDecl->getFunctionType())
+ if (const auto *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl))
+ CST = CGM.GetCallGraphFunctionType(FD);
+ else if (TargetDecl && TargetDecl->getFunctionType())
CST = QualType(TargetDecl->getFunctionType(), 0);
else if (const auto *FPT =
Callee.getAbstractInfo().getCalleeFunctionProtoType())
diff --git a/clang/lib/CodeGen/CGVTables.cpp b/clang/lib/CodeGen/CGVTables.cpp
index c5e16e8fe03fd8..b147fb371b6351 100644
--- a/clang/lib/CodeGen/CGVTables.cpp
+++ b/clang/lib/CodeGen/CGVTables.cpp
@@ -647,6 +647,15 @@ llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD,
CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, TI, IsUnprototyped);
}
+ // A return adjusting thunk fills the vtable slot created by the overridden
+ // method recorded in the thunk info, so it is reached by virtual calls typed
+ // after that method rather than after the thunk's target. Identify it in the
+ // call graph section accordingly, replacing the identifier of its target
+ // attached when the thunk was created (see
+ // CodeGenModule::GetCallGraphFunctionType).
+ if (CGM.getCodeGenOpts().CallGraphSection && TI.Method)
+ CGM.createIndirectFunctionTypeMD(TI.Method, ThunkFn);
+
setThunkProperties(CGM, TI, ThunkFn, ForVTable, GD);
return ThunkFn;
}
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 88129acfe2dabc..9bc276eebd3179 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3689,6 +3689,29 @@ static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
}
+/// Returns the method that introduced the virtual function that \p MD
+/// overrides, i.e., the root of its (first) override chain.
+static const CXXMethodDecl *GetRootOverriddenMethod(const CXXMethodDecl *MD) {
+ while (MD->size_overridden_methods() > 0)
+ MD = *MD->begin_overridden_methods();
+ return MD;
+}
+
+QualType CodeGenModule::GetCallGraphFunctionType(const FunctionDecl *FD) const {
+ // A virtual call is typed after the method named at the call site, but it
+ // can reach any overrider of that method, whose type may differ from it only
+ // in a covariant return type. Identify every method of an override chain by
+ // the type of the method that introduced the virtual function, so that calls
+ // through a pointer to any class of the hierarchy match the overriders they
+ // can reach. Methods that override several virtual functions with different
+ // (covariant) return types are identified after the first one; the thunks
+ // that fill the vtable slots of the others are identified after those (see
+ // CodeGenVTables::maybeEmitThunk).
+ if (const auto *MD = dyn_cast<CXXMethodDecl>(FD); MD && MD->isVirtual())
+ return GetRootOverriddenMethod(MD)->getType();
+ return FD->getType();
+}
+
void CodeGenModule::createIndirectFunctionTypeMD(const FunctionDecl *FD,
llvm::Function *F) {
// All functions which are not internal linkage could be indirect targets.
@@ -3702,7 +3725,7 @@ void CodeGenModule::createIndirectFunctionTypeMD(const FunctionDecl *FD,
if (!HasBody || !Def)
Def = FD;
- QualType QT = Def->getType();
+ QualType QT = GetCallGraphFunctionType(Def);
if (const auto *FNPT = QT->getAs<FunctionNoProtoType>()) {
// If there is no definition available in this TU for an unprototyped
// function declaration, skip generating incomplete callgraph metadata.
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index e2bd6bc4701fda..d106d4a7167c02 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -1786,6 +1786,14 @@ class CodeGenModule : public CodeGenTypeCache {
void createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
llvm::Function *F);
+ /// Returns the function type that identifies \p FD as an indirect call
+ /// target or callee in the call graph section. Virtual methods are
+ /// identified by the type of the method that introduced the virtual function
+ /// (the root of their override chain) so that calls through pointers to any
+ /// class of the hierarchy match the overriders they can reach, even if those
+ /// have a covariant return type.
+ QualType GetCallGraphFunctionType(const FunctionDecl *FD) const;
+
/// Create and attach callgraph metadata if the function is a potential
/// indirect call target to support call graph section.
void createIndirectFunctionTypeMD(const FunctionDecl *FD, llvm::Function *F);
diff --git a/clang/test/CodeGen/call-graph-section-covariant-return.cpp b/clang/test/CodeGen/call-graph-section-covariant-return.cpp
new file mode 100644
index 00000000000000..27ebac8a71b142
--- /dev/null
+++ b/clang/test/CodeGen/call-graph-section-covariant-return.cpp
@@ -0,0 +1,87 @@
+// Tests that virtual calls through a pointer to any class of a hierarchy get
+// the same call graph section type identifier as the overriders they can reach,
+// even when the overriders have a covariant return type.
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section \
+// RUN: -std=c++17 -emit-llvm -o - %s | FileCheck %s
+
+////////////////////////////////////////////////////////////////////////////////
+// A chain of covariant overrides: all methods are identified by the type of
+// Base::clone, the method that introduced the virtual function.
+
+struct Base {
+ virtual ~Base() {}
+ virtual Base *clone() { return this; }
+};
+
+struct Mid : Base {
+ Mid *clone() override { return this; }
+};
+
+struct Derived : Mid {
+ Derived *clone() override { return this; }
+};
+
+////////////////////////////////////////////////////////////////////////////////
+// A method overriding two virtual functions with different covariant return
+// types: the method is identified after the first one (A::f), and the return
+// adjusting thunk that fills the vtable slot of B::f after the second one.
+
+struct A {
+ virtual A *f() { return this; }
+};
+struct B {
+ virtual B *f() { return this; }
+};
+struct C : A, B {
+ C *f() override { return this; }
+};
+
+////////////////////////////////////////////////////////////////////////////////
+// Call sites.
+
+// CHECK-LABEL: define {{.*}} @_Z12call_clone_bP4Base(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[CLONE_CT:![0-9]+]]
+Base *call_clone_b(Base *b) { return b->clone(); }
+
+// CHECK-LABEL: define {{.*}} @_Z12call_clone_mP3Mid(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[CLONE_CT]]
+Mid *call_clone_m(Mid *m) { return m->clone(); }
+
+// CHECK-LABEL: define {{.*}} @_Z12call_clone_dP7Derived(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[CLONE_CT]]
+Derived *call_clone_d(Derived *d) { return d->clone(); }
+
+// CHECK-LABEL: define {{.*}} @_Z8call_f_aP1A(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[A_F_CT:![0-9]+]]
+A *call_f_a(A *a) { return a->f(); }
+
+// CHECK-LABEL: define {{.*}} @_Z8call_f_bP1B(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[B_F_CT:![0-9]+]]
+B *call_f_b(B *b) { return b->f(); }
+
+// CHECK-LABEL: define {{.*}} @_Z8call_f_cP1C(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[A_F_CT]]
+C *call_f_c(C *c) { return c->f(); }
+
+// Instantiate the vtables (and thereby the methods and thunks below).
+Base *make() { return new Derived(); }
+C *make_c() { return new C(); }
+
+////////////////////////////////////////////////////////////////////////////////
+// Indirect call targets (emitted after the functions above).
+
+// CHECK-DAG: define {{.*}} @_ZN7Derived5cloneEv({{.*}} !callgraph [[CLONE:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZN3Mid5cloneEv({{.*}} !callgraph [[CLONE]] {
+// CHECK-DAG: define {{.*}} @_ZN4Base5cloneEv({{.*}} !callgraph [[CLONE]] {
+// CHECK-DAG: define {{.*}} @_ZN1C1fEv({{.*}} !callgraph [[A_F:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZTchn8_h8_N1C1fEv({{.*}} !callgraph [[B_F:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZN1A1fEv({{.*}} !callgraph [[A_F]] {
+// CHECK-DAG: define {{.*}} @_ZN1B1fEv({{.*}} !callgraph [[B_F]] {
+
+// CHECK-DAG: [[CLONE]] = !{!"_ZTSFP4BasevE"}
+// CHECK-DAG: [[CLONE_CT]] = !{[[CLONE]]}
+// CHECK-DAG: [[A_F]] = !{!"_ZTSFP1AvE"}
+// CHECK-DAG: [[A_F_CT]] = !{[[A_F]]}
+// CHECK-DAG: [[B_F]] = !{!"_ZTSFP1BvE"}
+// CHECK-DAG: [[B_F_CT]] = !{[[B_F]]}
>From d1ccff9675b702750727ce29d8e1a41bfa88d243 Mon Sep 17 00:00:00 2001
From: prabhukr <prabhukr at google.com>
Date: Tue, 13 Oct 2026 16:44:34 -0700
Subject: [PATCH 2/2] [clang][CodeGen] Use the primary base chain for virtual
method IDs
Identify a virtual method in the call graph section after the method it
overrides along the primary base chain of its class, as given by the record
layout. That is the vtable entry the method is placed in, which is not always
the first method it overrides.
Assisted-by: Claude
---
clang/lib/CodeGen/CGVTables.cpp | 12 +-
clang/lib/CodeGen/CodeGenModule.cpp | 68 +++++--
clang/lib/CodeGen/CodeGenModule.h | 9 +-
.../call-graph-section-covariant-return.cpp | 181 +++++++++++++++---
4 files changed, 218 insertions(+), 52 deletions(-)
diff --git a/clang/lib/CodeGen/CGVTables.cpp b/clang/lib/CodeGen/CGVTables.cpp
index b147fb371b6351..5aa12f8b20f2f5 100644
--- a/clang/lib/CodeGen/CGVTables.cpp
+++ b/clang/lib/CodeGen/CGVTables.cpp
@@ -647,12 +647,12 @@ llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD,
CodeGenFunction(CGM).generateThunk(ThunkFn, FnInfo, GD, TI, IsUnprototyped);
}
- // A return adjusting thunk fills the vtable slot created by the overridden
- // method recorded in the thunk info, so it is reached by virtual calls typed
- // after that method rather than after the thunk's target. Identify it in the
- // call graph section accordingly, replacing the identifier of its target
- // attached when the thunk was created (see
- // CodeGenModule::GetCallGraphFunctionType).
+ // A thunk fills the vtable entry of the overridden method recorded in the
+ // thunk info (an entry of a non-primary base, or one whose return type
+ // requires an adjustment), so it is reached by virtual calls typed after that
+ // method rather than after the thunk's target. Identify it in the call graph
+ // section accordingly, replacing the identifier of its target attached when
+ // the thunk was created (see CodeGenModule::GetCallGraphFunctionType).
if (CGM.getCodeGenOpts().CallGraphSection && TI.Method)
CGM.createIndirectFunctionTypeMD(TI.Method, ThunkFn);
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 9bc276eebd3179..6276f42c414470 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -36,6 +36,7 @@
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Mangle.h"
+#include "clang/AST/RecordLayout.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Basic/Builtins.h"
@@ -54,6 +55,7 @@
#include "llvm/ABI/TargetInfo.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
@@ -3689,27 +3691,57 @@ static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
}
-/// Returns the method that introduced the virtual function that \p MD
-/// overrides, i.e., the root of its (first) override chain.
-static const CXXMethodDecl *GetRootOverriddenMethod(const CXXMethodDecl *MD) {
- while (MD->size_overridden_methods() > 0)
- MD = *MD->begin_overridden_methods();
- return MD;
+/// Returns the method that \p MD overrides in the nearest class of the primary
+/// base chain of its class, or null if there is none.
+static const CXXMethodDecl *
+FindOverriddenMethodInPrimaryBases(const ASTContext &Context,
+ const CXXMethodDecl *MD) {
+ // Collect the methods MD overrides, directly or indirectly: the overridden
+ // method in the primary base chain may be hidden from the direct list by a
+ // dominating declaration on another path (e.g., through a virtual base).
+ llvm::SmallSetVector<const CXXMethodDecl *, 8> Overridden;
+ SmallVector<const CXXMethodDecl *, 8> Worklist(MD->overridden_methods());
+ while (!Worklist.empty()) {
+ const CXXMethodDecl *OM = Worklist.pop_back_val();
+ if (Overridden.insert(OM))
+ llvm::append_range(Worklist, OM->overridden_methods());
+ }
+ if (Overridden.empty())
+ return nullptr;
+
+ // Walk the primary base chain, nearest base first.
+ for (const CXXRecordDecl *RD = MD->getParent()->getDefinition(); RD;) {
+ const CXXRecordDecl *PrimaryBase =
+ Context.getASTRecordLayout(RD).getPrimaryBase();
+ if (!PrimaryBase)
+ return nullptr;
+ for (const CXXMethodDecl *OM : Overridden)
+ if (OM->getParent()->getCanonicalDecl() ==
+ PrimaryBase->getCanonicalDecl())
+ return OM;
+ RD = PrimaryBase->getDefinition();
+ }
+ return nullptr;
}
QualType CodeGenModule::GetCallGraphFunctionType(const FunctionDecl *FD) const {
- // A virtual call is typed after the method named at the call site, but it
- // can reach any overrider of that method, whose type may differ from it only
- // in a covariant return type. Identify every method of an override chain by
- // the type of the method that introduced the virtual function, so that calls
- // through a pointer to any class of the hierarchy match the overriders they
- // can reach. Methods that override several virtual functions with different
- // (covariant) return types are identified after the first one; the thunks
- // that fill the vtable slots of the others are identified after those (see
- // CodeGenVTables::maybeEmitThunk).
- if (const auto *MD = dyn_cast<CXXMethodDecl>(FD); MD && MD->isVirtual())
- return GetRootOverriddenMethod(MD)->getType();
- return FD->getType();
+ // A virtual call is typed after the method named at the call site, so every
+ // function that the vtable entry of that method can hold must carry the same
+ // identifier. A virtual method is placed in the entry of the method it
+ // overrides in the primary base chain of its class (Itanium C++ ABI 2.5.2),
+ // and otherwise gets an entry of its own. Identify it after the method that
+ // introduced that entry, which also covers covariant overriders that get an
+ // entry of their own because their return type requires an adjustment, as
+ // the calls typed after them reach them through that entry. The entries of
+ // the other overridden methods are filled by thunks, which are identified
+ // after those methods (see CodeGenVTables::maybeEmitThunk).
+ const auto *MD = dyn_cast<CXXMethodDecl>(FD);
+ if (!MD || !MD->isVirtual())
+ return FD->getType();
+ while (const CXXMethodDecl *Overridden =
+ FindOverriddenMethodInPrimaryBases(getContext(), MD))
+ MD = Overridden;
+ return MD->getType();
}
void CodeGenModule::createIndirectFunctionTypeMD(const FunctionDecl *FD,
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index d106d4a7167c02..27f6c14af33e97 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -1788,10 +1788,11 @@ class CodeGenModule : public CodeGenTypeCache {
/// Returns the function type that identifies \p FD as an indirect call
/// target or callee in the call graph section. Virtual methods are
- /// identified by the type of the method that introduced the virtual function
- /// (the root of their override chain) so that calls through pointers to any
- /// class of the hierarchy match the overriders they can reach, even if those
- /// have a covariant return type.
+ /// identified by the type of the method that introduced the vtable entry
+ /// they are placed in (the last method overridden along the primary base
+ /// chain of their class), so that calls through pointers to any class of the
+ /// hierarchy match the functions they reach through that entry, even if the
+ /// overriders have a covariant return type.
QualType GetCallGraphFunctionType(const FunctionDecl *FD) const;
/// Create and attach callgraph metadata if the function is a potential
diff --git a/clang/test/CodeGen/call-graph-section-covariant-return.cpp b/clang/test/CodeGen/call-graph-section-covariant-return.cpp
index 27ebac8a71b142..1019990e73cdd0 100644
--- a/clang/test/CodeGen/call-graph-section-covariant-return.cpp
+++ b/clang/test/CodeGen/call-graph-section-covariant-return.cpp
@@ -1,13 +1,14 @@
// Tests that virtual calls through a pointer to any class of a hierarchy get
-// the same call graph section type identifier as the overriders they can reach,
-// even when the overriders have a covariant return type.
+// the same call graph section type identifier as the functions they can reach
+// through the vtable entry they use, even when overriders have a covariant
+// return type.
// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section \
// RUN: -std=c++17 -emit-llvm -o - %s | FileCheck %s
////////////////////////////////////////////////////////////////////////////////
-// A chain of covariant overrides: all methods are identified by the type of
-// Base::clone, the method that introduced the virtual function.
+// A chain of covariant overrides: every method is placed in the vtable entry of
+// Base::clone and identified after it.
struct Base {
virtual ~Base() {}
@@ -22,24 +23,6 @@ struct Derived : Mid {
Derived *clone() override { return this; }
};
-////////////////////////////////////////////////////////////////////////////////
-// A method overriding two virtual functions with different covariant return
-// types: the method is identified after the first one (A::f), and the return
-// adjusting thunk that fills the vtable slot of B::f after the second one.
-
-struct A {
- virtual A *f() { return this; }
-};
-struct B {
- virtual B *f() { return this; }
-};
-struct C : A, B {
- C *f() override { return this; }
-};
-
-////////////////////////////////////////////////////////////////////////////////
-// Call sites.
-
// CHECK-LABEL: define {{.*}} @_Z12call_clone_bP4Base(
// CHECK: call noundef ptr %{{.*}}, !callee_type [[CLONE_CT:![0-9]+]]
Base *call_clone_b(Base *b) { return b->clone(); }
@@ -52,6 +35,25 @@ Mid *call_clone_m(Mid *m) { return m->clone(); }
// CHECK: call noundef ptr %{{.*}}, !callee_type [[CLONE_CT]]
Derived *call_clone_d(Derived *d) { return d->clone(); }
+Base *make() { return new Derived(); }
+
+
+////////////////////////////////////////////////////////////////////////////////
+// A method overriding two virtual functions: C::f is placed in the entry of
+// A::f, the method it overrides in its primary base, and identified after it.
+// The entry of B::f, in the vtable of the non-primary base B, is filled by a
+// thunk identified after B::f.
+
+struct A {
+ virtual A *f() { return this; }
+};
+struct B {
+ virtual B *f() { return this; }
+};
+struct C : A, B {
+ C *f() override { return this; }
+};
+
// CHECK-LABEL: define {{.*}} @_Z8call_f_aP1A(
// CHECK: call noundef ptr %{{.*}}, !callee_type [[A_F_CT:![0-9]+]]
A *call_f_a(A *a) { return a->f(); }
@@ -64,10 +66,122 @@ B *call_f_b(B *b) { return b->f(); }
// CHECK: call noundef ptr %{{.*}}, !callee_type [[A_F_CT]]
C *call_f_c(C *c) { return c->f(); }
-// Instantiate the vtables (and thereby the methods and thunks below).
-Base *make() { return new Derived(); }
C *make_c() { return new C(); }
+
+////////////////////////////////////////////////////////////////////////////////
+// The same, without any return adjustment: A1 and B1 are both at offset zero
+// in C1. The entry of B2::f is still filled by a (this adjusting) thunk.
+
+namespace no_return_adjustment {
+struct A1 {};
+struct B1 {};
+struct C1 : A1, B1 {};
+struct A2 {
+ virtual A1 *f() { return nullptr; }
+};
+struct B2 {
+ virtual B1 *f() { return nullptr; }
+};
+struct C2 : A2, B2 {
+ C1 *f() override { return nullptr; }
+};
+
+// CHECK-LABEL: define {{.*}} @_ZN20no_return_adjustment9call_f_a2EPNS_2A2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[NRA_A_CT:![0-9]+]]
+A1 *call_f_a2(A2 *a) { return a->f(); }
+
+// CHECK-LABEL: define {{.*}} @_ZN20no_return_adjustment9call_f_b2EPNS_2B2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[NRA_B_CT:![0-9]+]]
+B1 *call_f_b2(B2 *b) { return b->f(); }
+
+// CHECK-LABEL: define {{.*}} @_ZN20no_return_adjustment9call_f_c2EPNS_2C2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[NRA_A_CT]]
+C1 *call_f_c2(C2 *c) { return c->f(); }
+
+C2 c;
+
+} // namespace no_return_adjustment
+
+////////////////////////////////////////////////////////////////////////////////
+// A return adjustment in the primary base: C2::f gets an entry of its own in
+// the vtable of C2, and the entry of A2::f is filled by a return adjusting
+// thunk. Both are identified after A2::f, like the calls through C2 and A2
+// pointers that reach them.
+
+namespace primary_return_adjustment {
+struct A1 {
+ int a1;
+};
+struct B1 {
+ int b1;
+};
+struct C1 : B1, A1 {};
+struct A2 {
+ virtual A1 *f() { return nullptr; }
+};
+struct B2 {
+ virtual B1 *f() { return nullptr; }
+};
+struct C2 : A2, B2 {
+ C1 *f() override { return nullptr; }
+};
+
+// CHECK-LABEL: define {{.*}} @_ZN25primary_return_adjustment9call_f_a2EPNS_2A2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[PRA_A_CT:![0-9]+]]
+A1 *call_f_a2(A2 *a) { return a->f(); }
+
+// CHECK-LABEL: define {{.*}} @_ZN25primary_return_adjustment9call_f_b2EPNS_2B2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[PRA_B_CT:![0-9]+]]
+B1 *call_f_b2(B2 *b) { return b->f(); }
+
+// CHECK-LABEL: define {{.*}} @_ZN25primary_return_adjustment9call_f_c2EPNS_2C2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[PRA_A_CT]]
+C1 *call_f_c2(C2 *c) { return c->f(); }
+
+C2 c;
+
+} // namespace primary_return_adjustment
+
+////////////////////////////////////////////////////////////////////////////////
+// The primary base is not the first base: B2 is the first non-virtual dynamic
+// base of C2, so C2::f is placed in the entry of B2::f and identified after it,
+// although A2::f comes first among the methods it overrides.
+
+namespace virtual_base_first {
+struct A1 {
+ int a1;
+};
+struct B1 {
+ int b1;
+};
+struct C1 : B1, A1 {};
+struct A2 {
+ virtual A1 *f() { return nullptr; }
+};
+struct B2 {
+ virtual B1 *f() { return nullptr; }
+};
+struct C2 : virtual A2, B2 {
+ C1 *f() override { return nullptr; }
+};
+
+// CHECK-LABEL: define {{.*}} @_ZN18virtual_base_first9call_f_a2EPNS_2A2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[VBF_A_CT:![0-9]+]]
+A1 *call_f_a2(A2 *a) { return a->f(); }
+
+// CHECK-LABEL: define {{.*}} @_ZN18virtual_base_first9call_f_b2EPNS_2B2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[VBF_B_CT:![0-9]+]]
+B1 *call_f_b2(B2 *b) { return b->f(); }
+
+// CHECK-LABEL: define {{.*}} @_ZN18virtual_base_first9call_f_c2EPNS_2C2E(
+// CHECK: call noundef ptr %{{.*}}, !callee_type [[VBF_B_CT]]
+C1 *call_f_c2(C2 *c) { return c->f(); }
+
+C2 c;
+
+} // namespace virtual_base_first
+
////////////////////////////////////////////////////////////////////////////////
// Indirect call targets (emitted after the functions above).
@@ -78,6 +192,13 @@ C *make_c() { return new C(); }
// CHECK-DAG: define {{.*}} @_ZTchn8_h8_N1C1fEv({{.*}} !callgraph [[B_F:![0-9]+]] {
// CHECK-DAG: define {{.*}} @_ZN1A1fEv({{.*}} !callgraph [[A_F]] {
// CHECK-DAG: define {{.*}} @_ZN1B1fEv({{.*}} !callgraph [[B_F]] {
+// CHECK-DAG: define {{.*}} @_ZN20no_return_adjustment2C21fEv({{.*}} !callgraph [[NRA_A:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZThn8_N20no_return_adjustment2C21fEv({{.*}} !callgraph [[NRA_B:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZN25primary_return_adjustment2C21fEv({{.*}} !callgraph [[PRA_A:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZTch0_h4_N25primary_return_adjustment2C21fEv({{.*}} !callgraph [[PRA_A]] {
+// CHECK-DAG: define {{.*}} @_ZThn8_N25primary_return_adjustment2C21fEv({{.*}} !callgraph [[PRA_B:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZN18virtual_base_first2C21fEv({{.*}} !callgraph [[VBF_B:![0-9]+]] {
+// CHECK-DAG: define {{.*}} @_ZTcv0_n24_h4_N18virtual_base_first2C21fEv({{.*}} !callgraph [[VBF_A:![0-9]+]] {
// CHECK-DAG: [[CLONE]] = !{!"_ZTSFP4BasevE"}
// CHECK-DAG: [[CLONE_CT]] = !{[[CLONE]]}
@@ -85,3 +206,15 @@ C *make_c() { return new C(); }
// CHECK-DAG: [[A_F_CT]] = !{[[A_F]]}
// CHECK-DAG: [[B_F]] = !{!"_ZTSFP1BvE"}
// CHECK-DAG: [[B_F_CT]] = !{[[B_F]]}
+// CHECK-DAG: [[NRA_A]] = !{!"_ZTSFPN20no_return_adjustment2A1EvE"}
+// CHECK-DAG: [[NRA_A_CT]] = !{[[NRA_A]]}
+// CHECK-DAG: [[NRA_B]] = !{!"_ZTSFPN20no_return_adjustment2B1EvE"}
+// CHECK-DAG: [[NRA_B_CT]] = !{[[NRA_B]]}
+// CHECK-DAG: [[PRA_A]] = !{!"_ZTSFPN25primary_return_adjustment2A1EvE"}
+// CHECK-DAG: [[PRA_A_CT]] = !{[[PRA_A]]}
+// CHECK-DAG: [[PRA_B]] = !{!"_ZTSFPN25primary_return_adjustment2B1EvE"}
+// CHECK-DAG: [[PRA_B_CT]] = !{[[PRA_B]]}
+// CHECK-DAG: [[VBF_A]] = !{!"_ZTSFPN18virtual_base_first2A1EvE"}
+// CHECK-DAG: [[VBF_A_CT]] = !{[[VBF_A]]}
+// CHECK-DAG: [[VBF_B]] = !{!"_ZTSFPN18virtual_base_first2B1EvE"}
+// CHECK-DAG: [[VBF_B_CT]] = !{[[VBF_B]]}
More information about the cfe-commits
mailing list