Files
carbon-lang/toolchain/lower/handle_expression_category.cpp
T
Richard SmithandChandler Carruth e4caf7d604 Compute and cache the value representation of a type when it becomes complete. (#3271)
Using the computed value representation, fix lowering of struct and
tuple values to use the value representation rather than the object
representation. Fixes an issue found in the review of #3257.

This currently causes us to compute value representations of all types
as they are created, which generates substantially more SemIR to
represent types. We can get some of that back by deferring computation
of the value representation until the type is required to be complete,
but some of the additional cost here will persist with this approach.

I also considered making the computation of the value representation
type be something that lives entirely within the lowering phase, but I
think that's not the right approach in the longer term, because the
value representation will be semantically visible and relevant once we
start allowing it to be customized.

We should consider moving the nodes that exist to compute canonical
non-local types, including value representations, out into a separate
global block. That will clean up the SemIR representation substantially,
and make the SemIR produced for a function not depend on which types we
happen to have encountered beforehand. But that's not being done in this
PR.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-10-13 22:27:02 +00:00

63 lines
2.6 KiB
C++

// 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 "toolchain/lower/function_context.h"
#include "toolchain/sem_ir/file.h"
namespace Carbon::Lower {
auto HandleBindValue(FunctionContext& context, SemIR::NodeId node_id,
SemIR::BindValue node) -> void {
switch (auto rep = SemIR::GetValueRepresentation(context.semantics_ir(),
node.type_id);
rep.kind) {
case SemIR::ValueRepresentation::Unknown:
CARBON_FATAL()
<< "Value binding for type with incomplete value representation";
case SemIR::ValueRepresentation::None:
// Nothing should use this value, but StubReference needs a value to
// propagate.
// TODO: Remove this now the StubReferences are gone.
context.SetLocal(node_id,
llvm::PoisonValue::get(context.GetType(node.type_id)));
break;
case SemIR::ValueRepresentation::Copy:
context.SetLocal(node_id, context.builder().CreateLoad(
context.GetType(node.type_id),
context.GetLocal(node.value_id)));
break;
case SemIR::ValueRepresentation::Pointer:
context.SetLocal(node_id, context.GetLocal(node.value_id));
break;
case SemIR::ValueRepresentation::Custom:
CARBON_FATAL() << "TODO: Add support for BindValue with custom value rep";
}
}
auto HandleTemporary(FunctionContext& context, SemIR::NodeId node_id,
SemIR::Temporary node) -> void {
context.FinishInitialization(node.type_id, node.storage_id, node.init_id);
context.SetLocal(node_id, context.GetLocal(node.storage_id));
}
auto HandleTemporaryStorage(FunctionContext& context, SemIR::NodeId node_id,
SemIR::TemporaryStorage node) -> void {
context.SetLocal(node_id,
context.builder().CreateAlloca(context.GetType(node.type_id),
nullptr, "temp"));
}
auto HandleValueAsReference(FunctionContext& context, SemIR::NodeId node_id,
SemIR::ValueAsReference node) -> void {
CARBON_CHECK(
SemIR::GetExpressionCategory(context.semantics_ir(), node.value_id) ==
SemIR::ExpressionCategory::Value);
CARBON_CHECK(
SemIR::GetValueRepresentation(context.semantics_ir(), node.type_id)
.kind == SemIR::ValueRepresentation::Pointer);
context.SetLocal(node_id, context.GetLocal(node.value_id));
}
} // namespace Carbon::Lower