mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 08:18:34 +00:00
Define exported C++ thunks in the LLVM function Clang uses for them (#8060)
Carbon gives the C++ declaration of an exported Carbon thunk an asm label that includes the target's global symbol prefix (`_` on Mach-O). Clang names a declaration with an asm label `\01<label>` in the IR, so that LLVM uses the label verbatim as the symbol name, while Carbon lowered the thunk definition under its unprefixed name. Both name the same symbol, but they were distinct globals in the IR, so LLVM couldn't see that the C++ calls refer to the Carbon definition. That only works while the definition is never discarded. Mark the Carbon thunk as a `CppThunk` (as described in `toolchain/docs/check/cpp/thunks.md`), record its C++ declaration in `clang_decls`, and when lowering the thunk, ask Clang for its function (creating it if necessary) and define the thunk in that function. This means that the thunk and the C++ calls to it always use the same IR global, with Clang's name and signature. The thunk takes all of its parameters by reference, so Clang lowers its parameters the same way Carbon does, but some C++ ABIs (such as Apple arm64) return `this` from constructors. In that case, return the parameter that Clang marks `returned`. On targets without a global prefix, the IR names already matched, so the only change is that the thunk definitions now carry Clang's parameter and function attributes.
This commit is contained in:
1 parent
9192ee0032
commit
4abb6100e7
17 files changed
+577
-91
No files matched your search
@@ -1607,8 +1607,10 @@ static auto BuildCarbonToCarbonThunk(Context& context, SemIR::LocId loc_id,
|
||||
// If this thunk will be exposed as a C++ constructor, we put the output
|
||||
// parameter first to match the Itanium constructor ABI.
|
||||
//
|
||||
// TODO: use `clang::CodeGen::CGCXXABI::HasThisReturn` to determine if the
|
||||
// constructor's `this` should be a return value instead of an output param.
|
||||
// Some C++ ABIs also return `this` from constructors. We don't model that
|
||||
// here; instead, lowering defines the thunk in the LLVM function Clang
|
||||
// creates for its C++ declaration, and returns `this` if Clang's signature
|
||||
// does. See `FileContext::GetCppThunkLLVMFunction`.
|
||||
if (target.export_as_constructor) {
|
||||
CARBON_CHECK(target.return_type_id != SemIR::TypeId::None);
|
||||
std::rotate(thunk_param_type_ids.begin(), thunk_param_type_ids.end() - 1,
|
||||
@@ -1626,6 +1628,9 @@ static auto BuildCarbonToCarbonThunk(Context& context, SemIR::LocId loc_id,
|
||||
.param_type_ids = thunk_param_type_ids,
|
||||
.param_kinds = thunk_param_kinds})
|
||||
.second;
|
||||
context.functions()
|
||||
.Get(carbon_thunk_function_id)
|
||||
.SetCppThunk(target.function.first_decl_id());
|
||||
|
||||
BuildThunkDefinitionForExport(
|
||||
context, carbon_thunk_function_id, target.function_id,
|
||||
@@ -1673,6 +1678,20 @@ static auto BuildCppToCarbonThunk(Context& context, SemIR::LocId loc_id,
|
||||
return;
|
||||
}
|
||||
|
||||
// Map the Carbon thunk to its C++ declaration, so that lowering can define
|
||||
// the thunk in the same LLVM function that Clang uses to call it.
|
||||
SemIR::ClangDeclSignature carbon_function_signature{
|
||||
.kind = SemIR::ClangDeclSignature::Normal,
|
||||
.num_params = static_cast<int32_t>(carbon_function_decl->getNumParams())};
|
||||
carbon_function_signature.passing_modes.assign(
|
||||
carbon_function_signature.num_params,
|
||||
SemIR::ClangDeclSignature::PassingMode::ByValue);
|
||||
context.clang_decls().Add(
|
||||
{.key = SemIR::ClangDeclKey::ForFunctionDecl(
|
||||
carbon_function_decl, context.clang_decl_signatures().Add(
|
||||
std::move(carbon_function_signature))),
|
||||
.inst_id = carbon_thunk_target.function.first_decl_id()});
|
||||
|
||||
// Build the thunk function body.
|
||||
clang::Sema& sema = context.clang_sema();
|
||||
clang::Sema::ContextRAII context_raii(sema, thunk_function_decl);
|
||||
|
||||
Reference in new issue
Block a user