Author: Chandler Carruth Date: 2026-11-14T03:33:45Z New Revision: de574ff51d4fff318028c235f20d3eda52e03915
URL: https://github.com/llvm/llvm-project/commit/de574ff51d4fff318028c235f20d3eda52e03915 DIFF: https://github.com/llvm/llvm-project/commit/de574ff51d4fff318028c235f20d3eda52e03915.diff LOG: [Clang][CFI] Only normalize integer types `cfi-icall` metadata (#242466) When using `-fsanitize-cfi-icall-experimental-normalize-integers`, its normalization was applied unconditionally when creating metadata, appending `.normalized` and normalizing parts of C++ class vtable `!type` metadata, virtual member-pointer metadata, virtual call checks, and `-fexperimental-call-graph-section` metadata This would cause ThinLTO to see vtable `!type` identifiers that mismatch RTTI type-infos. Best case, this blocked whole-program devirtualization, but in the worst case (mixed use of normalization) it would cause WPD to make unsound transforms. =[ Restricting the normalization and `.normalized` suffix strictly to `cfi-icall` function type metadata fixes these issues. Added both direct tests and a CFI test that actually uses ThinLTO and would break without this. Assisted-by: Antigravity with Gemini Added: clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp compiler-rt/test/cfi/normalize-integers-wpd.cpp Modified: clang/docs/ReleaseNotes.md clang/lib/CodeGen/CGExpr.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/CodeGenModule.h Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index e805697aaee65..0d5aba4ea8c56 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1585,6 +1585,10 @@ The `alpha.cplusplus.UseAfterLifetimeEnd` checker was renamed to `alpha.core.Use modes for `-fsanitize=alloc-token`, selected with `-Xclang -falloc-token-mode=<mode>`. These modes derive token IDs from both the allocated type and the name of the function containing the allocation. +- Fixed `-fsanitize-cfi-icall-experimental-normalize-integers` to only + normalize `cfi-icall` and `kcfi` function type metadata, leaving C++ class + vtable, member-pointer, and `-fexperimental-call-graph-section` metadata + canonical so whole-program devirtualization works reliably. (#GH242466) ### Python Binding Changes diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 2c2c53a4d5e96..af09da8f72f9e 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -7226,7 +7226,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, EmitSanitizerStatReport(llvm::SanStat_CFI_ICall); llvm::Metadata *MD = - CGM.CreateMetadataIdentifierForFnType(QualType(FnType, 0)); + CGM.CreateMetadataIdentifierForCFIFnType(QualType(FnType, 0)); llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 3854fded4781d..94680cfee5820 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -3757,14 +3757,12 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD, if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) return; - QualType FnType = GeneralizeFunctionType(getContext(), FD->getType(), - /*GeneralizePointers=*/false); - llvm::Metadata *MD = CreateMetadataIdentifierForType(FnType); + llvm::Metadata *MD = CreateMetadataIdentifierForCFIFnType( + FD->getType(), /*GeneralizePointers=*/false); F->addTypeMetadata(0, MD); - QualType GenPtrFnType = GeneralizeFunctionType(getContext(), FD->getType(), - /*GeneralizePointers=*/true); - F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(GenPtrFnType)); + F->addTypeMetadata(0, CreateMetadataIdentifierForCFIFnType( + FD->getType(), /*GeneralizePointers=*/true)); // Emit a hash-based bit set entry for cross-DSO calls. if (CodeGenOpts.SanitizeCfiCrossDso) @@ -8729,8 +8727,10 @@ void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) { } } -llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl( - QualType T, MetadataTypeMap &Map, StringRef Suffix, bool ForceString) { +llvm::Metadata * +CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, + StringRef Suffix, bool ForceString, + bool NormalizeIntegers) { if (auto *FnType = T->getAs<FunctionProtoType>()) T = getContext().getFunctionType( FnType->getReturnType(), FnType->getParamTypes(), @@ -8743,10 +8743,10 @@ llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl( if (ForceString || isExternallyVisible(T->getLinkage())) { std::string OutName; llvm::raw_string_ostream Out(OutName); - getCXXABI().getMangleContext().mangleCanonicalTypeName( - T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers); + getCXXABI().getMangleContext().mangleCanonicalTypeName(T, Out, + NormalizeIntegers); - if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers) + if (NormalizeIntegers) Out << ".normalized"; Out << Suffix; @@ -8760,13 +8760,24 @@ llvm::Metadata *CodeGenModule::CreateMetadataIdentifierImpl( return InternalId; } -llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) { - assert(isa<FunctionType>(T)); - T = GeneralizeFunctionType( - getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers); - if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers) - return CreateMetadataIdentifierGeneralized(T); - return CreateMetadataIdentifierForType(T); +llvm::Metadata * +CodeGenModule::CreateMetadataIdentifierForCFIFnType(QualType T, + bool GeneralizePointers) { + assert(T->isFunctionType()); + T = GeneralizeFunctionType(getContext(), T, GeneralizePointers); + bool NormalizeIntegers = getCodeGenOpts().SanitizeCfiICallNormalizeIntegers; + if (GeneralizePointers) + return CreateMetadataIdentifierImpl(T, GeneralizedMetadataIdMap, + ".generalized", /*ForceString=*/false, + NormalizeIntegers); + return CreateMetadataIdentifierImpl(T, CfiICallMetadataIdMap, "", + /*ForceString=*/false, NormalizeIntegers); +} + +llvm::Metadata * +CodeGenModule::CreateMetadataIdentifierForCFIFnType(QualType T) { + return CreateMetadataIdentifierForCFIFnType( + T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers); } llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { @@ -8778,11 +8789,6 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) { return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual"); } -llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) { - return CreateMetadataIdentifierImpl(T, GeneralizedMetadataIdMap, - ".generalized", /*ForceString=*/false); -} - // Applies C default argument promotions to a parameter type. // // FIXME: The canonical source of truth for C default argument promotion is diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index 92afa8e708e2a..c6b13f5881784 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -708,6 +708,7 @@ class CodeGenModule : public CodeGenTypeCache { typedef llvm::DenseMap<QualType, llvm::Metadata *> MetadataTypeMap; MetadataTypeMap MetadataIdMap; MetadataTypeMap VirtualMetadataIdMap; + MetadataTypeMap CfiICallMetadataIdMap; MetadataTypeMap GeneralizedMetadataIdMap; MetadataTypeMap CallGraphMetadataIdMap; @@ -1753,8 +1754,10 @@ class CodeGenModule : public CodeGenTypeCache { /// Generate a KCFI type identifier for T. llvm::ConstantInt *CreateKCFITypeId(QualType T, StringRef Salt); - /// Create a metadata identifier for the given function type. - llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T); + /// Create a metadata identifier for the given function type for CFI icall. + llvm::Metadata *CreateMetadataIdentifierForCFIFnType(QualType T, + bool GeneralizePointers); + llvm::Metadata *CreateMetadataIdentifierForCFIFnType(QualType T); /// Create a metadata identifier for the given type. This may either be an /// MDString (for external identifiers) or a distinct unnamed MDNode (for @@ -1781,11 +1784,6 @@ class CodeGenModule : public CodeGenTypeCache { /// calls via a member function pointer. llvm::Metadata *CreateMetadataIdentifierForVirtualMemPtrType(QualType T); - /// Create a metadata identifier for the generalization of the given type. - /// This may either be an MDString (for external identifiers) or a distinct - /// unnamed MDNode (for internal identifiers). - llvm::Metadata *CreateMetadataIdentifierGeneralized(QualType T); - /// Create and attach type metadata to the given function. void createFunctionTypeMetadataForIcall(const FunctionDecl *FD, llvm::Function *F); @@ -2223,7 +2221,8 @@ class CodeGenModule : public CodeGenTypeCache { llvm::Metadata *CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, StringRef Suffix, - bool ForceString = false); + bool ForceString = false, + bool NormalizeIntegers = false); /// Emit deactivation symbols for any PFP fields whose offset is taken with /// offsetof. diff --git a/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp b/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp new file mode 100644 index 0000000000000..53ac0ebd481b5 --- /dev/null +++ b/clang/test/CodeGenCXX/cfi-icall-normalize-vtable.cpp @@ -0,0 +1,87 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=kcfi -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,KCFI --implicit-check-not=.normalized.virtual +// RUN: %clang_cc1 -triple x86_64-unknown-linux -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-NOCG,CFI-PUBLIC --implicit-check-not=.normalized.virtual +// RUN: %clang_cc1 -triple x86_64-unknown-linux -fvisibility=hidden -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-NOCG,CFI-HIDDEN --implicit-check-not=.normalized.virtual +// RUN: %clang_cc1 -triple x86_64-unknown-linux -fexperimental-call-graph-section -fsanitize=cfi-icall,cfi-vcall,cfi-mfcall -fsanitize-trap=cfi-icall,cfi-vcall,cfi-mfcall -fwhole-program-vtables -flto=thin -flto-unit -fsanitize-cfi-icall-experimental-normalize-integers -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,CFI,CFI-PUBLIC,CALLGRAPH --implicit-check-not=.normalized.virtual + +// Verify that -fsanitize-cfi-icall-experimental-normalize-integers only +// normalizes cfi-icall/kcfi function metadata and does not alter class vtable +// !type metadata, member-pointer metadata, or -fexperimental-call-graph-section +// metadata. + +struct Base { + virtual int get(int x); +}; + +struct Derived : Base { + int get(int x) override; +}; + +template <typename T> +struct Box { + virtual T val(T x); +}; + +int Base::get(int x) { return x; } +int Derived::get(int x) { return x + 1; } +template <typename T> +T Box<T>::val(T x) { return x; } +template struct Box<int>; + +void icall_target(int x) {} + +int test_calls(Base *b, int (Base::*mp)(int), Box<int> *box, void (*fp)(int), int x) { + int r = b->get(x); + r += (b->*mp)(x); + r += box->val(x); + fp(x); + return r; +} + +void take_addr() { + void (*fp)(int) = &icall_target; + fp(1); +} + +// CHECK-NOT: .normalized.virtual +// CHECK-NOT: _ZTS4Base.normalized +// CHECK-NOT: _ZTS7Derived.normalized +// CHECK-NOT: _ZTS3BoxIu3i32E +// CHECK-NOT: _ZTS3BoxIiE.normalized +// CHECK-NOT: _ZTSM4BaseFuu3i32E +// CHECK-NOT: _ZTSM4BaseFiiE.normalized.virtual + +// CHECK-DAG: @_ZTV4Base = {{.*}}!type ![[BASE_TYPE:[0-9]+]], !type ![[BASE_VMEMPTR:[0-9]+]] +// CHECK-DAG: @_ZTV7Derived = {{.*}}!type ![[BASE_TYPE]], !type ![[BASE_VMEMPTR]], !type ![[DERIVED_TYPE:[0-9]+]], !type ![[DERIVED_VMEMPTR:[0-9]+]] +// CHECK-DAG: @_ZTV3BoxIiE = {{.*}}!type ![[BOX_TYPE:[0-9]+]], !type ![[BOX_VMEMPTR:[0-9]+]] + +// CALLGRAPH-DAG: define{{.*}} i32 @_ZN4Base3getEi(ptr {{.*}}, i32 {{.*}}){{.*}} !callgraph ![[CG_METHOD_TYPE:[0-9]+]] +// CFI-NOCG-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !type ![[FN_TYPE:[0-9]+]] !type ![[FN_GEN_TYPE:[0-9]+]] +// CALLGRAPH-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !type ![[FN_TYPE:[0-9]+]] !type ![[FN_GEN_TYPE:[0-9]+]]{{.*}} !callgraph ![[CG_FN_TYPE:[0-9]+]] +// KCFI-DAG: define{{.*}} void @_Z12icall_targeti(i32 {{.*}}){{.*}} !kcfi_type ![[KCFI_FN_TYPE:[0-9]+]] + +// CHECK-LABEL: define{{.*}} i32 @_Z10test_callsP4BaseMS_FiiEP3BoxIiEPFviEi( +// KCFI-DAG: call i1 @llvm.public.type.test(ptr {{.*}}, metadata !"_ZTS4Base") +// KCFI-DAG: call i1 @llvm.public.type.test(ptr {{.*}}, metadata !"_ZTS3BoxIiE") +// KCFI-DAG: call void %{{.*}}(i32 {{.*}}) [ "kcfi"(i32 1162514891) ] +// CFI-PUBLIC-DAG: call i1 @llvm.public.type.test(ptr {{.*}}, metadata !"_ZTSM4BaseFiiE.virtual") +// CFI-HIDDEN-DAG: call { ptr, i1 } @llvm.type.checked.load(ptr {{.*}}, i32 0, metadata !"_ZTS4Base") +// CFI-HIDDEN-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSM4BaseFiiE.virtual") +// CFI-HIDDEN-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSM4BaseFiiE") +// CFI-HIDDEN-DAG: call { ptr, i1 } @llvm.type.checked.load(ptr {{.*}}, i32 0, metadata !"_ZTS3BoxIiE") +// CFI-DAG: call i1 @llvm.type.test(ptr {{.*}}, metadata !"_ZTSFvu3i32E.normalized") +// CALLGRAPH-DAG: call noundef i32 %{{.*}}(ptr {{.*}}, i32 {{.*}}), !callee_type ![[CG_METHOD_CALLEE:[0-9]+]] +// CALLGRAPH-DAG: call void %{{.*}}(i32 {{.*}}), !callee_type ![[CG_FN_CALLEE:[0-9]+]] + +// CHECK-DAG: ![[BASE_TYPE]] = !{i64 16, !"_ZTS4Base"} +// CHECK-DAG: ![[BASE_VMEMPTR]] = !{i64 16, !"_ZTSM4BaseFiiE.virtual"} +// CHECK-DAG: ![[DERIVED_TYPE]] = !{i64 16, !"_ZTS7Derived"} +// CHECK-DAG: ![[DERIVED_VMEMPTR]] = !{i64 16, !"_ZTSM7DerivedFiiE.virtual"} +// CHECK-DAG: ![[BOX_TYPE]] = !{i64 16, !"_ZTS3BoxIiE"} +// CHECK-DAG: ![[BOX_VMEMPTR]] = !{i64 16, !"_ZTSM3BoxIiEFiiE.virtual"} +// CFI-DAG: ![[FN_TYPE]] = !{i64 0, !"_ZTSFvu3i32E.normalized"} +// CFI-DAG: ![[FN_GEN_TYPE]] = !{i64 0, !"_ZTSFvu3i32E.normalized.generalized"} +// KCFI-DAG: ![[KCFI_FN_TYPE]] = !{i32 1162514891} +// CALLGRAPH-DAG: ![[CG_METHOD_TYPE]] = !{!"_ZTSFiiE"} +// CALLGRAPH-DAG: ![[CG_METHOD_CALLEE]] = !{![[CG_METHOD_TYPE]]} +// CALLGRAPH-DAG: ![[CG_FN_TYPE]] = !{!"_ZTSFviE"} +// CALLGRAPH-DAG: ![[CG_FN_CALLEE]] = !{![[CG_FN_TYPE]]} diff --git a/compiler-rt/test/cfi/normalize-integers-wpd.cpp b/compiler-rt/test/cfi/normalize-integers-wpd.cpp new file mode 100644 index 0000000000000..ff09a7d156262 --- /dev/null +++ b/compiler-rt/test/cfi/normalize-integers-wpd.cpp @@ -0,0 +1,157 @@ +// Test that -fsanitize-cfi-icall-experimental-normalize-integers does not +// normalize C++ class vtable !type metadata and break WholeProgramDevirt: +// 1. Uniform ThinLTO with --lto-validate-all-vtables-have-type-infos still +// matches vtable !type metadata against RTTI _ZTI symbols and +// devirtualizes single-implementation virtual calls. +// 2. Mixed-TU ThinLTO (where only some TUs pass +// -fsanitize-cfi-icall-experimental-normalize-integers) still sees +// overriding subclasses across TUs and does not unsoundly devirtualize. + +// REQUIRES: linux, lld-available, thinlto, cxxabi, (x86_64-target-arch || aarch64-target-arch) + +// --- KCFI: uniform + --lto-validate-all-vtables-have-type-infos --- +// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \ +// RUN: -fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers \ +// RUN: -DTU_A -c %s -o %t.kcfi.a.o +// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \ +// RUN: -fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers \ +// RUN: -DTU_MAIN -c %s -o %t.kcfi.main.o +// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld %t.kcfi.a.o %t.kcfi.main.o \ +// RUN: -o %t.kcfi.uniform -Wl,--lto-whole-program-visibility \ +// RUN: -Wl,--lto-validate-all-vtables-have-type-infos \ +// RUN: -Wl,-mllvm,-pass-remarks=wholeprogramdevirt 2>&1 | \ +// RUN: FileCheck %s --check-prefix=DEVIRT +// RUN: %run %t.kcfi.uniform + +// --- KCFI: mixed-TU (TU_B without normalize-integers defines overrides) --- +// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \ +// RUN: -DTU_B -c %s -o %t.plain.b.o +// RUN: %clangxx -O2 -flto=thin -fno-split-lto-unit -fwhole-program-vtables \ +// RUN: -fsanitize=kcfi -fsanitize-cfi-icall-experimental-normalize-integers \ +// RUN: -DTU_MAIN -DWITH_ALT -c %s -o %t.kcfi.main_alt.o +// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld %t.kcfi.a.o %t.plain.b.o \ +// RUN: %t.kcfi.main_alt.o -o %t.kcfi.mixed -Wl,--lto-whole-program-visibility +// RUN: %run %t.kcfi.mixed + +// --- cfi-icall: uniform + --lto-validate-all-vtables-have-type-infos --- +// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \ +// RUN: -fsanitize=cfi-icall -fsanitize-trap=cfi-icall \ +// RUN: -fsanitize-cfi-icall-experimental-normalize-integers \ +// RUN: -DTU_A -c %s -o %t.cfi.a.o +// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \ +// RUN: -fsanitize=cfi-icall -fsanitize-trap=cfi-icall \ +// RUN: -fsanitize-cfi-icall-experimental-normalize-integers \ +// RUN: -DTU_MAIN -c %s -o %t.cfi.main.o +// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld -fsanitize=cfi-icall \ +// RUN: -fsanitize-trap=cfi-icall %t.cfi.a.o %t.cfi.main.o \ +// RUN: -o %t.cfi.uniform -Wl,--lto-whole-program-visibility \ +// RUN: -Wl,--lto-validate-all-vtables-have-type-infos \ +// RUN: -Wl,-mllvm,-pass-remarks=wholeprogramdevirt 2>&1 | \ +// RUN: FileCheck %s --check-prefix=DEVIRT +// RUN: %run %t.cfi.uniform + +// --- cfi-icall: mixed-TU (TU_B without normalize-integers defines overrides) --- +// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \ +// RUN: -DTU_B -c %s -o %t.plain_split.b.o +// RUN: %clangxx -O2 -flto=thin -fwhole-program-vtables \ +// RUN: -fsanitize=cfi-icall -fsanitize-trap=cfi-icall \ +// RUN: -fsanitize-cfi-icall-experimental-normalize-integers \ +// RUN: -DTU_MAIN -DWITH_ALT -c %s -o %t.cfi.main_alt.o +// RUN: %clangxx -O2 -flto=thin -fuse-ld=lld -fsanitize=cfi-icall \ +// RUN: -fsanitize-trap=cfi-icall %t.cfi.a.o %t.plain_split.b.o \ +// RUN: %t.cfi.main_alt.o -o %t.cfi.mixed -Wl,--lto-whole-program-visibility +// RUN: %run %t.cfi.mixed + +// DEVIRT-DAG: single-impl: devirtualized a call to _ZNK12_GLOBAL__N_17Primary3tagEv +// DEVIRT-DAG: single-impl: devirtualized a call to _ZNK12_GLOBAL__N_17Primary3valEi +// DEVIRT-DAG: single-impl: devirtualized a call to _ZNK3BoxIiE3getEi + +struct Shape { + virtual int tag() const = 0; + virtual int val(int x) const = 0; + virtual ~Shape() = default; +}; + +template <typename T> struct Box { + virtual T get(T x) const { return x + 1; } + virtual ~Box() = default; +}; + +Shape *make_primary(); +Box<int> *make_primary_box(); +Shape *make_alt(); +Box<int> *make_alt_box(); +int add_ten(int x); +int apply_icall(int (*fn)(int), int x); +int eval_shape(const Shape *s, const Box<int> *b, int x); + +#if defined(TU_A) + +namespace { +struct Primary : Shape { + int tag() const override { return 10; } + int val(int x) const override { return x + 100; } +}; +} // namespace + +template struct Box<int>; + +Shape *make_primary() { + static Primary p; + return &p; +} + +Box<int> *make_primary_box() { + static Box<int> b; + return &b; +} + +int add_ten(int x) { return x + 10; } + +__attribute__((noinline)) int apply_icall(int (*fn)(int), int x) { + return fn(x); +} + +__attribute__((noinline)) int eval_shape(const Shape *s, const Box<int> *b, + int x) { + return s->tag() + s->val(x) + b->get(x); +} + +#elif defined(TU_B) + +namespace { +struct Alt : Shape { + int tag() const override { return 20; } + int val(int x) const override { return x + 200; } +}; + +struct AltBox : Box<int> { + int get(int x) const override { return x + 50; } +}; +} // namespace + +Shape *make_alt() { + static Alt a; + return &a; +} + +Box<int> *make_alt_box() { + static AltBox ab; + return &ab; +} + +#elif defined(TU_MAIN) + +int main() { + if (apply_icall(&add_ten, 5) != 15) + return 1; + if (eval_shape(make_primary(), make_primary_box(), 5) != 121) + return 2; +# if defined(WITH_ALT) + if (eval_shape(make_alt(), make_alt_box(), 5) != 280) + return 3; +# endif + return 0; +} + +#endif _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
