From df8b25522eb92d2b0fec3c1034007bcd157c0982 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Mon, 11 May 2026 10:33:08 -0700 Subject: [PATCH] Import C++ vtables (#7174) This correctly renders the vtable in SemIR, including allowing overrides in Carbon-derived-from-C++ classes. It doesn't work in lowering because clang walks the methods of the CXXRecordDecl - and we currently don't export anything into the CXXRecordDecl's methods (we do export the fields) - so that's next. This also doesn't teach Clang to affirmatively emit the vtable regardless of the types use in C++ code - or to have Carbon use the vtable in an object's initialization. --- toolchain/check/cpp/import.cpp | 33 ++ .../interop/cpp/class/import/abstract.carbon | 32 +- .../interop/cpp/class/import/class.carbon | 10 +- .../interop/cpp/class/import/dynamic.carbon | 112 ++++++ toolchain/lower/file_context.cpp | 3 + .../interop/cpp/class/import/dynamic.carbon | 335 ++++++++++++++++-- .../lower/testdata/interop/cpp/method.carbon | 22 +- toolchain/sem_ir/vtable.h | 7 + 8 files changed, 508 insertions(+), 46 deletions(-) create mode 100644 toolchain/check/testdata/interop/cpp/class/import/dynamic.carbon diff --git a/toolchain/check/cpp/import.cpp b/toolchain/check/cpp/import.cpp index 5be3f4b0b508..73221e581c65 100644 --- a/toolchain/check/cpp/import.cpp +++ b/toolchain/check/cpp/import.cpp @@ -861,6 +861,39 @@ static auto BuildClassDefinition(Context& context, context, SemIR::WitnessType::TypeInstId), .object_repr_type_inst_id = object_repr_id})); + if (class_info.is_dynamic) { + llvm::SmallVector vtable; + const auto& vtable_layout = dyn_cast( + context.ast_context().getVTableContext()) + ->getVTableLayout(clang_def); + auto vtable_components = vtable_layout.vtable_components(); + vtable.reserve(vtable_components.size()); + auto num_components = 0; + for (const auto& vtable_component : vtable_components) { + if (vtable_component.getKind() != + clang::VTableComponent::CK_FunctionPointer) { + continue; + } + ++num_components; + const auto* method_decl = vtable_component.getFunctionDecl(); + vtable.push_back(ImportCppFunctionDecl( + context, SemIR::LocId(import_ir_inst_id), + const_cast(method_decl), + {.num_params = static_cast(method_decl->getNumParams())})); + } + vtable.truncate(num_components); + auto vtable_id = context.vtables().Add( + {{.class_id = class_id, + .virtual_functions_id = context.inst_blocks().Add(vtable), + .carbon_native_vtable = false}}); + auto vptr_type_id = GetPointerType(context, SemIR::VtableType::TypeInstId); + class_info.vtable_decl_id = + AddInst(context, SemIR::LocIdAndInst::RuntimeVerified( + context.sem_ir(), import_ir_inst_id, + SemIR::VtableDecl{.type_id = vptr_type_id, + .vtable_id = vtable_id})); + } + class_info.body_block_id = context.inst_block_stack().Pop(); } diff --git a/toolchain/check/testdata/interop/cpp/class/import/abstract.carbon b/toolchain/check/testdata/interop/cpp/class/import/abstract.carbon index 82dd5220d20c..b6352693eb09 100644 --- a/toolchain/check/testdata/interop/cpp/class/import/abstract.carbon +++ b/toolchain/check/testdata/interop/cpp/class/import/abstract.carbon @@ -38,7 +38,7 @@ class C { extend base: Cpp.A; } -// --- fail_todo_impl_abstract_member.carbon +// --- impl_abstract_member.carbon library "[[@TEST_NAME]]"; @@ -46,13 +46,24 @@ import Cpp library "abstract.h"; class C { extend base: Cpp.A; - // CHECK:STDERR: fail_todo_impl_abstract_member.carbon:[[@LINE+4]]:3: error: override without compatible virtual in base class [OverrideWithoutVirtualInBase] - // CHECK:STDERR: override fn f[unused ref self: Self]() {} - // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - // CHECK:STDERR: override fn f[unused ref self: Self]() {} } +// --- fail_impl_mismatch_member.carbon + +library "[[@TEST_NAME]]"; + +import Cpp library "abstract.h"; + +class C { + extend base: Cpp.A; + // CHECK:STDERR: fail_impl_mismatch_member.carbon:[[@LINE+4]]:3: error: override without compatible virtual in base class [OverrideWithoutVirtualInBase] + // CHECK:STDERR: override fn invalid[unused ref self: Self]() {} + // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // CHECK:STDERR: + override fn invalid[unused ref self: Self]() {} +} + // --- abstract_final.h // C++ allows a class to be both abstract and final. @@ -98,6 +109,8 @@ fn F() -> Cpp.AbstractFinal { // CHECK:STDOUT: %pattern_type: type = pattern_type %AbstractFinal [concrete] // CHECK:STDOUT: %F.type: type = fn_type @F [concrete] // CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %AbstractFinal.f.type: type = fn_type @AbstractFinal.f [concrete] +// CHECK:STDOUT: %AbstractFinal.f: %AbstractFinal.f.type = struct_value () [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -106,6 +119,11 @@ fn F() -> Cpp.AbstractFinal { // CHECK:STDOUT: import Cpp//... // CHECK:STDOUT: } // CHECK:STDOUT: %AbstractFinal.decl: type = class_decl @AbstractFinal [concrete = constants.%AbstractFinal] {} {} +// CHECK:STDOUT: %AbstractFinal.f.decl: %AbstractFinal.f.type = fn_decl @AbstractFinal.f [concrete = constants.%AbstractFinal.f] { +// CHECK:STDOUT: +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: file { @@ -121,6 +139,10 @@ fn F() -> Cpp.AbstractFinal { // CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: vtable @AbstractFinal.vtable { +// CHECK:STDOUT: imports.%AbstractFinal.f.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: // CHECK:STDOUT: fn @F() -> out %return.param: %AbstractFinal { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F] diff --git a/toolchain/check/testdata/interop/cpp/class/import/class.carbon b/toolchain/check/testdata/interop/cpp/class/import/class.carbon index f66fad744048..556c79f82361 100644 --- a/toolchain/check/testdata/interop/cpp/class/import/class.carbon +++ b/toolchain/check/testdata/interop/cpp/class/import/class.carbon @@ -486,10 +486,10 @@ fn GenericUse(p2: Generic(Cpp.Class1)) { // CHECK:STDOUT: %MyF.type: type = fn_type @MyF [concrete] // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] // CHECK:STDOUT: %MyF: %MyF.type = struct_value () [concrete] -// CHECK:STDOUT: %Bar.f.cpp_overload_set.type: type = cpp_overload_set_type @Bar.f.cpp_overload_set [concrete] -// CHECK:STDOUT: %Bar.f.cpp_overload_set.value: %Bar.f.cpp_overload_set.type = cpp_overload_set_value @Bar.f.cpp_overload_set [concrete] // CHECK:STDOUT: %Bar.f.type: type = fn_type @Bar.f [concrete] // CHECK:STDOUT: %Bar.f: %Bar.f.type = struct_value () [concrete] +// CHECK:STDOUT: %Bar.f.cpp_overload_set.type: type = cpp_overload_set_type @Bar.f.cpp_overload_set [concrete] +// CHECK:STDOUT: %Bar.f.cpp_overload_set.value: %Bar.f.cpp_overload_set.type = cpp_overload_set_value @Bar.f.cpp_overload_set [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { @@ -498,12 +498,12 @@ fn GenericUse(p2: Generic(Cpp.Class1)) { // CHECK:STDOUT: import Cpp//... // CHECK:STDOUT: } // CHECK:STDOUT: %Bar.decl: type = class_decl @Bar [concrete = constants.%Bar] {} {} -// CHECK:STDOUT: %Bar.f.cpp_overload_set.value: %Bar.f.cpp_overload_set.type = cpp_overload_set_value @Bar.f.cpp_overload_set [concrete = constants.%Bar.f.cpp_overload_set.value] // CHECK:STDOUT: %Bar.f.decl: %Bar.f.type = fn_decl @Bar.f [concrete = constants.%Bar.f] { // CHECK:STDOUT: // CHECK:STDOUT: } { // CHECK:STDOUT: // CHECK:STDOUT: } +// CHECK:STDOUT: %Bar.f.cpp_overload_set.value: %Bar.f.cpp_overload_set.type = cpp_overload_set_value @Bar.f.cpp_overload_set [concrete = constants.%Bar.f.cpp_overload_set.value] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: file { @@ -521,6 +521,10 @@ fn GenericUse(p2: Generic(Cpp.Class1)) { // CHECK:STDOUT: } // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: vtable @Bar.vtable { +// CHECK:STDOUT: imports.%Bar.f.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: // CHECK:STDOUT: fn @MyF(%bar.param: %ptr.f68) { // CHECK:STDOUT: !entry: // CHECK:STDOUT: %bar.ref: %ptr.f68 = name_ref bar, %bar diff --git a/toolchain/check/testdata/interop/cpp/class/import/dynamic.carbon b/toolchain/check/testdata/interop/cpp/class/import/dynamic.carbon new file mode 100644 index 000000000000..5ec599da2d84 --- /dev/null +++ b/toolchain/check/testdata/interop/cpp/class/import/dynamic.carbon @@ -0,0 +1,112 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/import/dynamic.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/import/dynamic.carbon + +// --- dynamic_base_from_cpp.carbon + +library "[[@TEST_NAME]]"; + +import Cpp; + +inline Cpp ''' + +struct FurtherBase { + virtual auto further_base_func() -> int { + return 11; + } +}; + +struct Base: FurtherBase { + int i; + virtual auto func() -> int { + return 7; + } +}; + +auto Use(Base& b) -> int { + return b.func(); +} + +'''; + +base class Derived { + extend base: Cpp.Base; + virtual fn other_func[unused self: Self]() -> i32 { + return 3; + } +} + +//@dump-sem-ir-begin +inline Cpp ''' + +// TODO: Carbon lowering should request clang to generate the vtable. +void WorkaroundToEmitCtorAndVtable() { + Carbon::Derived d; +} + +// TODO: Carbon lowering should emit initialization that refers to the +// Clang-lowered vtable. +auto DoThing() -> int { + Carbon::Derived d; + return Use(d); +} + +'''; +//@dump-sem-ir-end + +// CHECK:STDOUT: --- dynamic_base_from_cpp.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %Base: type = class_type @Base [concrete] +// CHECK:STDOUT: %FurtherBase.further_base_func.type: type = fn_type @FurtherBase.further_base_func [concrete] +// CHECK:STDOUT: %FurtherBase.further_base_func: %FurtherBase.further_base_func.type = struct_value () [concrete] +// CHECK:STDOUT: %Base.func.type: type = fn_type @Base.func [concrete] +// CHECK:STDOUT: %Base.func: %Base.func.type = struct_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Cpp: = namespace file.%Cpp.import_cpp, [concrete] { +// CHECK:STDOUT: .Base = %Base.decl +// CHECK:STDOUT: import Cpp//... +// CHECK:STDOUT: } +// CHECK:STDOUT: %Base.decl: type = class_decl @Base [concrete = constants.%Base] {} {} +// CHECK:STDOUT: %FurtherBase.further_base_func.decl: %FurtherBase.further_base_func.type = fn_decl @FurtherBase.further_base_func [concrete = constants.%FurtherBase.further_base_func] { +// CHECK:STDOUT: +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: %Base.func.decl: %Base.func.type = fn_decl @Base.func [concrete = constants.%Base.func] { +// CHECK:STDOUT: +// CHECK:STDOUT: } { +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %Cpp.ref.loc35: = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp] +// CHECK:STDOUT: inline_cpp "\n// TODO: Carbon lowering should request clang to generate the vtable.\nvoid WorkaroundToEmitCtorAndVtable() {\n Carbon::Derived d;\n}\n\n// TODO: Carbon lowering should emit initialization that refers to the\n// Clang-lowered vtable.\nauto DoThing() -> int {\n Carbon::Derived d;\n return Use(d);\n}\n\n" +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: vtable @Base.vtable { +// CHECK:STDOUT: imports.%FurtherBase.further_base_func.decl +// CHECK:STDOUT: imports.%Base.func.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: vtable @FurtherBase.vtable { +// CHECK:STDOUT: imports.%FurtherBase.further_base_func.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: vtable @Derived.vtable { +// CHECK:STDOUT: constants.%FurtherBase.further_base_func +// CHECK:STDOUT: constants.%Base.func +// CHECK:STDOUT: @Derived.%Derived.other_func.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/lower/file_context.cpp b/toolchain/lower/file_context.cpp index aebc17f57e2a..a7108213b3d9 100644 --- a/toolchain/lower/file_context.cpp +++ b/toolchain/lower/file_context.cpp @@ -709,6 +709,9 @@ auto FileContext::GetLocForDI(SemIR::InstId inst_id) -> Context::LocForDI { auto FileContext::BuildVtable(const SemIR::Vtable& vtable, SemIR::SpecificId specific_id) -> llvm::GlobalVariable* { + if (!vtable.carbon_native_vtable) { + return nullptr; + } const auto& class_info = sem_ir().classes().Get(vtable.class_id); SemIR::Mangler m(sem_ir(), context().total_ir_count()); diff --git a/toolchain/lower/testdata/interop/cpp/class/import/dynamic.carbon b/toolchain/lower/testdata/interop/cpp/class/import/dynamic.carbon index 8ff9a82888b2..5aa0428755dd 100644 --- a/toolchain/lower/testdata/interop/cpp/class/import/dynamic.carbon +++ b/toolchain/lower/testdata/interop/cpp/class/import/dynamic.carbon @@ -18,49 +18,297 @@ import Cpp; inline Cpp ''' -struct Base { - int i; - virtual void func(); +struct FurtherBase { + virtual auto further_base_func() -> int { + return 11; + } }; +struct Base: FurtherBase { + int i; + virtual auto func() -> int { + return 7; + } +}; + +auto Use(Base& b) -> int { + return b.func(); +} + '''; base class Derived { extend base: Cpp.Base; - virtual fn other_func[self: Self](); + // TODO: `other_func` should appear in the vtable (it currently + // doesn't because the CXXRecordDecl we export has no CXXMethodDecl + // children - those need to be added, and in the right order) + virtual fn other_func[unused self: Self]() -> i32 { + return 3; + } } -fn Use(d: Derived) -> i32 { - d.other_func(); - return d.i; +inline Cpp ''' + +// TODO: Carbon lowering should request clang to generate the vtable. +void WorkaroundToEmitCtorAndVtable() { + Carbon::Derived d; } +// TODO: Carbon lowering should emit initialization that refers to the +// Clang-lowered vtable. +auto DoThing() -> int { + Carbon::Derived d; + return Use(d); +} + +'''; + // CHECK:STDOUT: ; ModuleID = 'dynamic_base_from_cpp.carbon' // CHECK:STDOUT: source_filename = "dynamic_base_from_cpp.carbon" // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu" // CHECK:STDOUT: -// CHECK:STDOUT: @"_CDerived.Main.$vtable" = unnamed_addr constant [1 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr @_Cother_func.Derived.Main to i64), i64 ptrtoint (ptr @"_CDerived.Main.$vtable" to i64)) to i32)] +// CHECK:STDOUT: %"class.Carbon::Derived" = type { %struct.Base.base, [4 x i8] } +// CHECK:STDOUT: %struct.Base.base = type <{ %struct.FurtherBase, i32 }> +// CHECK:STDOUT: %struct.FurtherBase = type { ptr } // CHECK:STDOUT: -// CHECK:STDOUT: declare void @_Cother_func.Derived.Main(ptr) +// CHECK:STDOUT: $_ZN6Carbon7DerivedC2Ev = comdat any // CHECK:STDOUT: -// CHECK:STDOUT: ; Function Attrs: nounwind -// CHECK:STDOUT: define i32 @_CUse.Main(ptr %d) #0 !dbg !11 { +// CHECK:STDOUT: $_ZN6Carbon7DerivedD2Ev = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $__clang_call_terminate = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZN11FurtherBase17further_base_funcEv = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZN4Base4funcEv = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZN4BaseC2Ev = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZN11FurtherBaseC2Ev = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTVN6Carbon7DerivedE = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTIN6Carbon7DerivedE = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTSN6Carbon7DerivedE = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTI4Base = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTS4Base = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTI11FurtherBase = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTS11FurtherBase = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTV4Base = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: $_ZTV11FurtherBase = comdat any +// CHECK:STDOUT: +// CHECK:STDOUT: @"_CDerived.Main.$vtable" = unnamed_addr constant [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint (ptr @_ZN11FurtherBase17further_base_funcEv to i64), i64 ptrtoint (ptr @"_CDerived.Main.$vtable" to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @_ZN4Base4funcEv to i64), i64 ptrtoint (ptr @"_CDerived.Main.$vtable" to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (ptr @_Cother_func.Derived.Main to i64), i64 ptrtoint (ptr @"_CDerived.Main.$vtable" to i64)) to i32)] +// CHECK:STDOUT: @_ZTVN6Carbon7DerivedE = linkonce_odr dso_local unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr @_ZTIN6Carbon7DerivedE, ptr @_ZN11FurtherBase17further_base_funcEv, ptr @_ZN4Base4funcEv] }, comdat, align 8 +// CHECK:STDOUT: @_ZTIN6Carbon7DerivedE = linkonce_odr dso_local constant { ptr, ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2), ptr @_ZTSN6Carbon7DerivedE, ptr @_ZTI4Base }, comdat, align 8 +// CHECK:STDOUT: @_ZTVN10__cxxabiv120__si_class_type_infoE = external global [0 x ptr] +// CHECK:STDOUT: @_ZTSN6Carbon7DerivedE = linkonce_odr dso_local constant [18 x i8] c"N6Carbon7DerivedE\00", comdat, align 1 +// CHECK:STDOUT: @_ZTI4Base = linkonce_odr dso_local constant { ptr, ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2), ptr @_ZTS4Base, ptr @_ZTI11FurtherBase }, comdat, align 8 +// CHECK:STDOUT: @_ZTS4Base = linkonce_odr dso_local constant [6 x i8] c"4Base\00", comdat, align 1 +// CHECK:STDOUT: @_ZTI11FurtherBase = linkonce_odr dso_local constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS11FurtherBase }, comdat, align 8 +// CHECK:STDOUT: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr] +// CHECK:STDOUT: @_ZTS11FurtherBase = linkonce_odr dso_local constant [14 x i8] c"11FurtherBase\00", comdat, align 1 +// CHECK:STDOUT: @_ZTV4Base = linkonce_odr dso_local unnamed_addr constant { [4 x ptr] } { [4 x ptr] [ptr null, ptr @_ZTI4Base, ptr @_ZN11FurtherBase17further_base_funcEv, ptr @_ZN4Base4funcEv] }, comdat, align 8 +// CHECK:STDOUT: @_ZTV11FurtherBase = linkonce_odr dso_local unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI11FurtherBase, ptr @_ZN11FurtherBase17further_base_funcEv] }, comdat, align 8 +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: mustprogress uwtable +// CHECK:STDOUT: define dso_local noundef i32 @_Z3UseR4Base(ptr noundef nonnull align 8 dereferenceable(12) %b) #0 { // CHECK:STDOUT: entry: -// CHECK:STDOUT: %Derived.other_func.call.vtable = load ptr, ptr %d, align 8, !dbg !18 -// CHECK:STDOUT: %Derived.other_func.call = call ptr @llvm.load.relative.i32(ptr %Derived.other_func.call.vtable, i32 0), !dbg !18 -// CHECK:STDOUT: call void %Derived.other_func.call(ptr %d), !dbg !18 -// CHECK:STDOUT: %.loc22_11.1.base = getelementptr inbounds nuw { [16 x i8] }, ptr %d, i32 0, i32 0, !dbg !19 -// CHECK:STDOUT: %.loc22_11.3.i = getelementptr inbounds nuw [16 x i8], ptr %.loc22_11.1.base, i32 0, i32 8, !dbg !19 -// CHECK:STDOUT: %.loc22_11.4 = load i32, ptr %.loc22_11.3.i, align 4, !dbg !19 -// CHECK:STDOUT: ret i32 %.loc22_11.4, !dbg !20 +// CHECK:STDOUT: %b.addr = alloca ptr, align 8 +// CHECK:STDOUT: store ptr %b, ptr %b.addr, align 8, !tbaa !11 +// CHECK:STDOUT: %0 = load ptr, ptr %b.addr, align 8, !tbaa !11, !nonnull !14, !align !15 +// CHECK:STDOUT: %vtable = load ptr, ptr %0, align 8, !tbaa !16 +// CHECK:STDOUT: %vfn = getelementptr inbounds ptr, ptr %vtable, i64 1 +// CHECK:STDOUT: %1 = load ptr, ptr %vfn, align 8 +// CHECK:STDOUT: %call = call noundef i32 %1(ptr noundef nonnull align 8 dereferenceable(12) %0) +// CHECK:STDOUT: ret i32 %call // CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: read) -// CHECK:STDOUT: declare ptr @llvm.load.relative.i32(ptr, i32) #1 +// CHECK:STDOUT: ; Function Attrs: mustprogress uwtable +// CHECK:STDOUT: define dso_local void @_Z29WorkaroundToEmitCtorAndVtablev() #0 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %d = alloca %"class.Carbon::Derived", align 8 +// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %d) #5 +// CHECK:STDOUT: call void @_ZN6Carbon7DerivedC2Ev(ptr noundef nonnull align 8 dereferenceable(12) %d) #5 +// CHECK:STDOUT: call void @_ZN6Carbon7DerivedD2Ev(ptr noundef nonnull align 8 dead_on_return(12) dereferenceable(12) %d) +// CHECK:STDOUT: call void @llvm.lifetime.end.p0(ptr %d) #5 +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } // CHECK:STDOUT: -// CHECK:STDOUT: attributes #0 = { nounwind } -// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: read) } +// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) +// CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1 +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: inlinehint mustprogress nounwind uwtable +// CHECK:STDOUT: define linkonce_odr dso_local void @_ZN6Carbon7DerivedC2Ev(ptr noundef nonnull align 8 dereferenceable(12) %this) unnamed_addr #2 comdat align 2 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %this.addr = alloca ptr, align 8 +// CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !18 +// CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8 +// CHECK:STDOUT: call void @_ZN4BaseC2Ev(ptr noundef nonnull align 8 dereferenceable(12) %this1) #5 +// CHECK:STDOUT: store ptr getelementptr inbounds inrange(-16, 16) ({ [4 x ptr] }, ptr @_ZTVN6Carbon7DerivedE, i32 0, i32 0, i32 2), ptr %this1, align 8, !tbaa !16 +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: inlinehint mustprogress nounwind uwtable +// CHECK:STDOUT: define linkonce_odr dso_local void @_ZN6Carbon7DerivedD2Ev(ptr noundef nonnull align 8 dead_on_return(12) dereferenceable(12) %this) unnamed_addr #2 comdat align 2 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %this.addr = alloca ptr, align 8 +// CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !18 +// CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8 +// CHECK:STDOUT: store ptr getelementptr inbounds inrange(-16, 16) ({ [4 x ptr] }, ptr @_ZTVN6Carbon7DerivedE, i32 0, i32 0, i32 2), ptr %this1, align 8, !tbaa !16 +// CHECK:STDOUT: call void @"_C__destroy_thunk:thunk.Derived.Main"(ptr noundef nonnull align 8 dereferenceable(12) %this1) +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) +// CHECK:STDOUT: declare void @llvm.lifetime.end.p0(ptr captures(none)) #1 +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: mustprogress uwtable +// CHECK:STDOUT: define dso_local noundef i32 @_Z7DoThingv() #0 personality ptr @__gxx_personality_v0 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %d = alloca %"class.Carbon::Derived", align 8 +// CHECK:STDOUT: %exn.slot = alloca ptr, align 8 +// CHECK:STDOUT: %ehselector.slot = alloca i32, align 4 +// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %d) #5 +// CHECK:STDOUT: call void @_ZN6Carbon7DerivedC2Ev(ptr noundef nonnull align 8 dereferenceable(12) %d) #5 +// CHECK:STDOUT: %call = invoke noundef i32 @_Z3UseR4Base(ptr noundef nonnull align 8 dereferenceable(12) %d) +// CHECK:STDOUT: to label %invoke.cont unwind label %lpad +// CHECK:STDOUT: +// CHECK:STDOUT: invoke.cont: ; preds = %entry +// CHECK:STDOUT: call void @_ZN6Carbon7DerivedD2Ev(ptr noundef nonnull align 8 dead_on_return(12) dereferenceable(12) %d) +// CHECK:STDOUT: call void @llvm.lifetime.end.p0(ptr %d) #5 +// CHECK:STDOUT: ret i32 %call +// CHECK:STDOUT: +// CHECK:STDOUT: lpad: ; preds = %entry +// CHECK:STDOUT: %0 = landingpad { ptr, i32 } +// CHECK:STDOUT: cleanup +// CHECK:STDOUT: %1 = extractvalue { ptr, i32 } %0, 0 +// CHECK:STDOUT: store ptr %1, ptr %exn.slot, align 8 +// CHECK:STDOUT: %2 = extractvalue { ptr, i32 } %0, 1 +// CHECK:STDOUT: store i32 %2, ptr %ehselector.slot, align 4 +// CHECK:STDOUT: invoke void @_ZN6Carbon7DerivedD2Ev(ptr noundef nonnull align 8 dead_on_return(12) dereferenceable(12) %d) +// CHECK:STDOUT: to label %invoke.cont1 unwind label %terminate.lpad +// CHECK:STDOUT: +// CHECK:STDOUT: invoke.cont1: ; preds = %lpad +// CHECK:STDOUT: call void @llvm.lifetime.end.p0(ptr %d) #5 +// CHECK:STDOUT: br label %eh.resume +// CHECK:STDOUT: +// CHECK:STDOUT: eh.resume: ; preds = %invoke.cont1 +// CHECK:STDOUT: %exn = load ptr, ptr %exn.slot, align 8 +// CHECK:STDOUT: %sel = load i32, ptr %ehselector.slot, align 4 +// CHECK:STDOUT: %lpad.val = insertvalue { ptr, i32 } poison, ptr %exn, 0 +// CHECK:STDOUT: %lpad.val2 = insertvalue { ptr, i32 } %lpad.val, i32 %sel, 1 +// CHECK:STDOUT: resume { ptr, i32 } %lpad.val2 +// CHECK:STDOUT: +// CHECK:STDOUT: terminate.lpad: ; preds = %lpad +// CHECK:STDOUT: %3 = landingpad { ptr, i32 } +// CHECK:STDOUT: catch ptr null +// CHECK:STDOUT: %4 = extractvalue { ptr, i32 } %3, 0 +// CHECK:STDOUT: call void @__clang_call_terminate(ptr %4) #7 +// CHECK:STDOUT: unreachable +// CHECK:STDOUT: +// CHECK:STDOUT: ; uselistorder directives +// CHECK:STDOUT: uselistorder ptr %d, { 0, 2, 1, 3, 4, 5, 6 } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: declare i32 @__gxx_personality_v0(...) +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: noinline noreturn nounwind uwtable +// CHECK:STDOUT: define linkonce_odr hidden void @__clang_call_terminate(ptr noundef %0) #3 comdat { +// CHECK:STDOUT: %2 = call ptr @__cxa_begin_catch(ptr %0) #5 +// CHECK:STDOUT: call void @_ZSt9terminatev() #7 +// CHECK:STDOUT: unreachable +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: declare ptr @__cxa_begin_catch(ptr) +// CHECK:STDOUT: +// CHECK:STDOUT: declare void @_ZSt9terminatev() +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable +// CHECK:STDOUT: define linkonce_odr dso_local noundef i32 @_ZN11FurtherBase17further_base_funcEv(ptr noundef nonnull align 8 dereferenceable(8) %this) unnamed_addr #4 comdat align 2 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %this.addr = alloca ptr, align 8 +// CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !20 +// CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8 +// CHECK:STDOUT: ret i32 11 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable +// CHECK:STDOUT: define linkonce_odr dso_local noundef i32 @_ZN4Base4funcEv(ptr noundef nonnull align 8 dereferenceable(12) %this) unnamed_addr #4 comdat align 2 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %this.addr = alloca ptr, align 8 +// CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !11 +// CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8 +// CHECK:STDOUT: ret i32 7 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define i32 @_Cother_func.Derived.Main(ptr %self) #5 !dbg !22 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: ret i32 3, !dbg !29 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: alwaysinline nounwind +// CHECK:STDOUT: define void @"_C__destroy_thunk:thunk.Derived.Main"(ptr %self) #6 !dbg !30 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: call void @"_COp.12e0d0434542305a:core.Destroy.Core"(ptr %self), !dbg !35 +// CHECK:STDOUT: ret void, !dbg !35 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define weak_odr void @"_COp.bfe31446004e2b71:core.Destroy.Core"(ptr %self) #5 !dbg !36 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: ret void, !dbg !39 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: nounwind +// CHECK:STDOUT: define weak_odr void @"_COp.12e0d0434542305a:core.Destroy.Core"(ptr %self) #5 !dbg !40 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: ret void, !dbg !43 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: inlinehint mustprogress nounwind uwtable +// CHECK:STDOUT: define linkonce_odr dso_local void @_ZN4BaseC2Ev(ptr noundef nonnull align 8 dereferenceable(12) %this) unnamed_addr #2 comdat align 2 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %this.addr = alloca ptr, align 8 +// CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !11 +// CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8 +// CHECK:STDOUT: call void @_ZN11FurtherBaseC2Ev(ptr noundef nonnull align 8 dereferenceable(8) %this1) #5 +// CHECK:STDOUT: store ptr getelementptr inbounds inrange(-16, 16) ({ [4 x ptr] }, ptr @_ZTV4Base, i32 0, i32 0, i32 2), ptr %this1, align 8, !tbaa !16 +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; Function Attrs: inlinehint mustprogress nounwind uwtable +// CHECK:STDOUT: define linkonce_odr dso_local void @_ZN11FurtherBaseC2Ev(ptr noundef nonnull align 8 dereferenceable(8) %this) unnamed_addr #2 comdat align 2 { +// CHECK:STDOUT: entry: +// CHECK:STDOUT: %this.addr = alloca ptr, align 8 +// CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !20 +// CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8 +// CHECK:STDOUT: store ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTV11FurtherBase, i32 0, i32 0, i32 2), ptr %this1, align 8, !tbaa !16 +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: ; uselistorder directives +// CHECK:STDOUT: uselistorder ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2), { 1, 0 } +// CHECK:STDOUT: uselistorder ptr @_ZN6Carbon7DerivedC2Ev, { 1, 0 } +// CHECK:STDOUT: uselistorder ptr @_ZN6Carbon7DerivedD2Ev, { 2, 1, 0 } +// CHECK:STDOUT: uselistorder ptr @_ZN11FurtherBase17further_base_funcEv, { 3, 2, 1, 0 } +// CHECK:STDOUT: uselistorder ptr @_ZN4Base4funcEv, { 2, 1, 0 } +// CHECK:STDOUT: +// CHECK:STDOUT: attributes #0 = { mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) } +// CHECK:STDOUT: attributes #2 = { inlinehint mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #3 = { noinline noreturn nounwind uwtable "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #4 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #5 = { nounwind } +// CHECK:STDOUT: attributes #6 = { alwaysinline nounwind } +// CHECK:STDOUT: attributes #7 = { noreturn nounwind } // CHECK:STDOUT: // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4} // CHECK:STDOUT: !llvm.dbg.cu = !{!5} @@ -77,13 +325,36 @@ fn Use(d: Derived) -> i32 { // CHECK:STDOUT: !8 = !{!"int", !9, i64 0} // CHECK:STDOUT: !9 = !{!"omnipotent char", !10, i64 0} // CHECK:STDOUT: !10 = !{!"Simple C++ TBAA"} -// CHECK:STDOUT: !11 = distinct !DISubprogram(name: "Use", linkageName: "_CUse.Main", scope: null, file: !6, line: 20, type: !12, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !16) -// CHECK:STDOUT: !12 = !DISubroutineType(types: !13) -// CHECK:STDOUT: !13 = !{!14, !15} -// CHECK:STDOUT: !14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) -// CHECK:STDOUT: !15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64) -// CHECK:STDOUT: !16 = !{!17} -// CHECK:STDOUT: !17 = !DILocalVariable(arg: 1, scope: !11, type: !15) -// CHECK:STDOUT: !18 = !DILocation(line: 21, column: 3, scope: !11) -// CHECK:STDOUT: !19 = !DILocation(line: 22, column: 10, scope: !11) -// CHECK:STDOUT: !20 = !DILocation(line: 22, column: 3, scope: !11) +// CHECK:STDOUT: !11 = !{!12, !12, i64 0} +// CHECK:STDOUT: !12 = !{!"p1 _ZTS4Base", !13, i64 0} +// CHECK:STDOUT: !13 = !{!"any pointer", !9, i64 0} +// CHECK:STDOUT: !14 = !{} +// CHECK:STDOUT: !15 = !{i64 8} +// CHECK:STDOUT: !16 = !{!17, !17, i64 0} +// CHECK:STDOUT: !17 = !{!"vtable pointer", !10, i64 0} +// CHECK:STDOUT: !18 = !{!19, !19, i64 0} +// CHECK:STDOUT: !19 = !{!"p1 _ZTSN6Carbon7DerivedE", !13, i64 0} +// CHECK:STDOUT: !20 = !{!21, !21, i64 0} +// CHECK:STDOUT: !21 = !{!"p1 _ZTS11FurtherBase", !13, i64 0} +// CHECK:STDOUT: !22 = distinct !DISubprogram(name: "other_func", linkageName: "_Cother_func.Derived.Main", scope: null, file: !6, line: 32, type: !23, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !27) +// CHECK:STDOUT: !23 = !DISubroutineType(types: !24) +// CHECK:STDOUT: !24 = !{!25, !26} +// CHECK:STDOUT: !25 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +// CHECK:STDOUT: !26 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64) +// CHECK:STDOUT: !27 = !{!28} +// CHECK:STDOUT: !28 = !DILocalVariable(arg: 1, scope: !22, type: !26) +// CHECK:STDOUT: !29 = !DILocation(line: 33, column: 5, scope: !22) +// CHECK:STDOUT: !30 = distinct !DISubprogram(name: "__destroy_thunk", linkageName: "_C__destroy_thunk:thunk.Derived.Main", scope: null, file: !6, line: 27, type: !31, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !33) +// CHECK:STDOUT: !31 = !DISubroutineType(types: !32) +// CHECK:STDOUT: !32 = !{null, !26} +// CHECK:STDOUT: !33 = !{!34} +// CHECK:STDOUT: !34 = !DILocalVariable(arg: 1, scope: !30, type: !26) +// CHECK:STDOUT: !35 = !DILocation(line: 27, column: 1, scope: !30) +// CHECK:STDOUT: !36 = distinct !DISubprogram(name: "Op", linkageName: "_COp.bfe31446004e2b71:core.Destroy.Core", scope: null, file: !6, line: 27, type: !31, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !37) +// CHECK:STDOUT: !37 = !{!38} +// CHECK:STDOUT: !38 = !DILocalVariable(arg: 1, scope: !36, type: !26) +// CHECK:STDOUT: !39 = !DILocation(line: 27, column: 1, scope: !36) +// CHECK:STDOUT: !40 = distinct !DISubprogram(name: "Op", linkageName: "_COp.12e0d0434542305a:core.Destroy.Core", scope: null, file: !6, line: 27, type: !31, spFlags: DISPFlagDefinition, unit: !5, retainedNodes: !41) +// CHECK:STDOUT: !41 = !{!42} +// CHECK:STDOUT: !42 = !DILocalVariable(arg: 1, scope: !40, type: !26) +// CHECK:STDOUT: !43 = !DILocation(line: 27, column: 1, scope: !40) diff --git a/toolchain/lower/testdata/interop/cpp/method.carbon b/toolchain/lower/testdata/interop/cpp/method.carbon index 836d13a5037c..007daf64e52c 100644 --- a/toolchain/lower/testdata/interop/cpp/method.carbon +++ b/toolchain/lower/testdata/interop/cpp/method.carbon @@ -90,8 +90,12 @@ fn Call(n: Cpp.NeedThunk) { // CHECK:STDOUT: ret i32 %by_val__carbon_thunk.call, !dbg !19 // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: declare void @_ZN4Base5virt0Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #1 +// CHECK:STDOUT: +// CHECK:STDOUT: declare void @_ZN1A5virt1Ev(ptr noundef nonnull align 8 dereferenceable(12)) unnamed_addr #1 +// CHECK:STDOUT: // CHECK:STDOUT: ; Function Attrs: alwaysinline mustprogress uwtable -// CHECK:STDOUT: define internal noundef i32 @_ZNK1A6by_valEv.carbon_thunk(ptr noundef nonnull align 8 dereferenceable(12) %this) #1 { +// CHECK:STDOUT: define internal noundef i32 @_ZNK1A6by_valEv.carbon_thunk(ptr noundef nonnull align 8 dereferenceable(12) %this) #2 { // CHECK:STDOUT: entry: // CHECK:STDOUT: %this.addr = alloca ptr, align 8 // CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !20 @@ -101,7 +105,7 @@ fn Call(n: Cpp.NeedThunk) { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable -// CHECK:STDOUT: define linkonce_odr dso_local noundef i32 @_ZNK1A6by_valEv(ptr noundef nonnull align 8 dereferenceable(12) %this) #2 comdat align 2 { +// CHECK:STDOUT: define linkonce_odr dso_local noundef i32 @_ZNK1A6by_valEv(ptr noundef nonnull align 8 dereferenceable(12) %this) #3 comdat align 2 { // CHECK:STDOUT: entry: // CHECK:STDOUT: %this.addr = alloca ptr, align 8 // CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !20 @@ -112,8 +116,9 @@ fn Call(n: Cpp.NeedThunk) { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: attributes #0 = { nounwind } -// CHECK:STDOUT: attributes #1 = { alwaysinline mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } -// CHECK:STDOUT: attributes #2 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #2 = { alwaysinline mustprogress uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #3 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } // CHECK:STDOUT: // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4} // CHECK:STDOUT: !llvm.dbg.cu = !{!5} @@ -164,8 +169,12 @@ fn Call(n: Cpp.NeedThunk) { // CHECK:STDOUT: ret i32 %A.by_ref.call, !dbg !19 // CHECK:STDOUT: } // CHECK:STDOUT: +// CHECK:STDOUT: declare void @_ZN4Base5virt0Ev(ptr noundef nonnull align 8 dereferenceable(8)) unnamed_addr #1 +// CHECK:STDOUT: +// CHECK:STDOUT: declare void @_ZN1A5virt1Ev(ptr noundef nonnull align 8 dereferenceable(12)) unnamed_addr #1 +// CHECK:STDOUT: // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable -// CHECK:STDOUT: define linkonce_odr dso_local noundef i32 @_ZN1A6by_refEv(ptr noundef nonnull align 8 dereferenceable(12) %this) #1 comdat align 2 { +// CHECK:STDOUT: define linkonce_odr dso_local noundef i32 @_ZN1A6by_refEv(ptr noundef nonnull align 8 dereferenceable(12) %this) #2 comdat align 2 { // CHECK:STDOUT: entry: // CHECK:STDOUT: %this.addr = alloca ptr, align 8 // CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !20 @@ -176,7 +185,8 @@ fn Call(n: Cpp.NeedThunk) { // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: attributes #0 = { nounwind } -// CHECK:STDOUT: attributes #1 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +// CHECK:STDOUT: attributes #2 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } // CHECK:STDOUT: // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4} // CHECK:STDOUT: !llvm.dbg.cu = !{!5} diff --git a/toolchain/sem_ir/vtable.h b/toolchain/sem_ir/vtable.h index 96d99bb6f505..5fc40c457e3c 100644 --- a/toolchain/sem_ir/vtable.h +++ b/toolchain/sem_ir/vtable.h @@ -19,6 +19,13 @@ struct VtableFields { // non-overriden functions in base classes, forming the complete vtable for // the class. InstBlockId virtual_functions_id; + + // Specifies that this vtable uses Carbon's native vtable layout, rather than + // an Itanium-compatible vtable layout for C++ interop. + // TODO: This might change to an enum representing more diverse vtable layouts + // (eg: Carbon type with a C++ vtable distinct from a C++ type with a C++ + // vtable, maybe other language interop, etc) + bool carbon_native_vtable = true; }; struct Vtable : public VtableFields, public Printable {