Lower return expr; for value return forms of class type (#7908)

A function with a value return form, such as `->? form(val C)`, is
lowered to return the value representation of `C`: a pointer for types
with a pointer value representation. However, lowering of `ReturnExpr`
re-derived the return convention independently of the function's
signature, and assumed that any returned value expression was a
`returned var`, taking the initializing-return path. For types with an
in-place initializing representation, that tried to initialize a
nonexistent return slot and crashed. (This only appeared to work for
types like `i32`, whose by-copy initializing representation happens to
match the value representation.)

Fix this by recording how the function returns its result -- `void`, by
copy, by reference, in place via an `sret` parameter, or as the entry
point's implicit `i32` -- as a `ReturnKind` in `FunctionTypeInfo` and
`FunctionInfo` when the signature is lowered, and having the `Return`
and `ReturnExpr` handlers switch on it. This keeps the signature and the
`return` lowering in agreement by construction, and replaces the
`IsEntryPoint` check in `Return` lowering.
This commit is contained in:
David Blaikie authored and GitHub committed 2026-10-08 19:52:12 +00:00
1 parent 73c247f1ef
commit 9fd28f7304
7 files changed
+197 -49

No files matched your search

+10
View File
@@ -136,6 +136,8 @@ class FunctionTypeInfoBuilder {
auto lowered_return_types = GetLoweredTypes(func_ctx, return_type_id);
return_type_ = lowered_return_types.llvm_ir_type;
param_di_types_.push_back(lowered_return_types.llvm_di_type);
return_kind_ =
return_type_id.has_value() ? ReturnKind::ByCopy : ReturnKind::None;
return true;
}
@@ -145,6 +147,7 @@ class FunctionTypeInfoBuilder {
return_type_ = llvm::Type::getInt32Ty(func_ctx.context->llvm_context());
param_di_types_.push_back(context_->di_builder().createBasicType(
"int", 32, llvm::dwarf::DW_ATE_signed));
return_kind_ = ReturnKind::EntryPointInt32;
return true;
}
@@ -156,6 +159,7 @@ class FunctionTypeInfoBuilder {
/*AddressSpace=*/0);
// TODO: replace this with a reference type.
param_di_types_.push_back(GetPointerDIType(nullptr));
return_kind_ = ReturnKind::ByReference;
return true;
}
@@ -167,6 +171,7 @@ class FunctionTypeInfoBuilder {
sret_type_ = func_ctx.context->GetType(return_type_id);
// We don't add to param_di_types_ because that will be handled by the
// loop over the SemIR parameters.
return_kind_ = ReturnKind::InPlace;
return true;
}
@@ -271,6 +276,9 @@ class FunctionTypeInfoBuilder {
// attribute with this type.
llvm::Type* sret_type_ = nullptr;
// How the LLVM function returns its result.
ReturnKind return_kind_ = ReturnKind::None;
// Whether we failed to form an exact description of the function type. This
// can happen if a parameter or return type is incomplete. In this case, we
// can still sometimes need to emit a declaration of the function, for example
@@ -508,6 +516,7 @@ auto FunctionTypeInfoBuilder::Finalize() -> FunctionTypeInfo {
.unused_param_indices = std::move(unused_param_indices_),
.param_name_ids = std::move(param_name_ids_),
.sret_type = sret_type_,
.return_kind = return_kind_,
.inexact = inexact_};
}
@@ -519,6 +528,7 @@ auto FunctionTypeInfoBuilder::Abort() -> FunctionTypeInfo {
param_types_.clear();
param_di_types_.clear();
return_type_ = llvm::Type::getVoidTy(context_->llvm_context());
return_kind_ = ReturnKind::None;
param_di_types_.push_back(nullptr);
inexact_ = true;
return Finalize();