Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute and cache the value representation of a type when it becomes complete. #3271

Merged
merged 16 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions toolchain/lower/handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,11 @@ static auto GetStructOrTupleElement(FunctionContext& context,
auto* elem_ptr =
context.builder().CreateStructGEP(aggr_type, aggr_value, idx, name);

// If this is a value access, load the element if necessary.
// If this is a value access, we were holding a pointer to the value, so load
// the element.
if (aggr_cat == SemIR::ExpressionCategory::Value) {
switch (
SemIR::GetValueRepresentation(context.semantics_ir(), result_type_id)
.kind) {
case SemIR::ValueRepresentation::None:
return llvm::PoisonValue::get(context.GetType(result_type_id));
case SemIR::ValueRepresentation::Copy:
return context.builder().CreateLoad(context.GetType(result_type_id),
elem_ptr, name + ".load");
case SemIR::ValueRepresentation::Pointer:
return elem_ptr;
case SemIR::ValueRepresentation::Custom:
CARBON_FATAL() << "TODO: Add support for custom value representation";
}
return context.builder().CreateLoad(context.GetType(result_type_id),
elem_ptr, name + ".load");
}
return elem_ptr;
}
Expand Down Expand Up @@ -360,12 +350,10 @@ auto EmitStructOrTupleValueRepresentation(FunctionContext& context,
SemIR::TypeId type_id,
SemIR::NodeBlockId refs_id,
llvm::Twine name) -> llvm::Value* {
auto* llvm_type = context.GetType(type_id);

switch (SemIR::GetValueRepresentation(context.semantics_ir(), type_id).kind) {
case SemIR::ValueRepresentation::None:
// TODO: Add a helper to get a "no value representation" value.
return llvm::PoisonValue::get(llvm_type);
return llvm::PoisonValue::get(context.GetType(type_id));

case SemIR::ValueRepresentation::Copy: {
auto refs = context.semantics_ir().GetNodeBlock(refs_id);
Expand All @@ -374,20 +362,35 @@ auto EmitStructOrTupleValueRepresentation(FunctionContext& context,
// TODO: Remove the LLVM StructType wrapper in this case, so we don't
// need this `insert_value` wrapping.
return context.builder().CreateInsertValue(
llvm::PoisonValue::get(llvm_type), context.GetLocal(refs[0]), {0});
llvm::PoisonValue::get(context.GetType(type_id)),
context.GetLocal(refs[0]), {0});
}

case SemIR::ValueRepresentation::Pointer: {
// Write the object representation to a local alloca so we can produce a
// pointer to it as the value representation.
auto* alloca = context.builder().CreateAlloca(
llvm_type, /*ArraySize=*/nullptr, name);
auto refs = context.semantics_ir().GetNodeBlock(refs_id);

// Compute the type of the value representation. Note that we don't use
// `context.GetType(type_id)` here, because that would give us the object
// representation rather than the value representation.
// TODO: Consider precomputing or caching this.
llvm::SmallVector<llvm::Type*> element_types;
element_types.reserve(refs.size());
for (auto [i, ref] : llvm::enumerate(refs)) {
element_types.push_back(context.GetLocal(ref)->getType());
}
llvm::Type* llvm_value_rep_type =
llvm::StructType::get(context.llvm_context(), element_types);

// Write the value representation to a local alloca so we can produce a
// pointer to it as the value representation of the struct or tuple.
auto* alloca =
context.builder().CreateAlloca(llvm_value_rep_type,
/*ArraySize=*/nullptr, name);
for (auto [i, ref] :
llvm::enumerate(context.semantics_ir().GetNodeBlock(refs_id))) {
auto* gep = context.builder().CreateStructGEP(llvm_type, alloca, i);
// TODO: We are loading a value representation here and storing an
// object representation!
context.builder().CreateStore(context.GetLocal(ref), gep);
context.builder().CreateStore(
context.GetLocal(ref),
context.builder().CreateStructGEP(llvm_value_rep_type, alloca, i));
}
return alloca;
}
Expand Down
13 changes: 7 additions & 6 deletions toolchain/lower/testdata/let/tuple.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ fn F() -> i32 {
// CHECK:STDOUT: store i32 %7, ptr %9, align 4
// CHECK:STDOUT: %10 = getelementptr inbounds { i32, i32 }, ptr %tuple10, i32 0, i32 1
// CHECK:STDOUT: store i32 %8, ptr %10, align 4
// CHECK:STDOUT: %tuple11 = alloca { { i32, i32, i32 }, { i32, i32 } }, align 8
// CHECK:STDOUT: %11 = getelementptr inbounds { { i32, i32, i32 }, { i32, i32 } }, ptr %tuple11, i32 0, i32 0
// CHECK:STDOUT: %tuple11 = alloca { ptr, ptr }, align 8
// CHECK:STDOUT: %11 = getelementptr inbounds { ptr, ptr }, ptr %tuple11, i32 0, i32 0
// CHECK:STDOUT: store ptr %tuple, ptr %11, align 8
// CHECK:STDOUT: %12 = getelementptr inbounds { { i32, i32, i32 }, { i32, i32 } }, ptr %tuple11, i32 0, i32 1
// CHECK:STDOUT: %12 = getelementptr inbounds { ptr, ptr }, ptr %tuple11, i32 0, i32 1
// CHECK:STDOUT: store ptr %tuple10, ptr %12, align 8
// CHECK:STDOUT: %tuple.index = getelementptr inbounds { { i32, i32, i32 }, { i32, i32 } }, ptr %tuple11, i32 0, i32 1
// CHECK:STDOUT: %tuple.index12 = getelementptr inbounds { i32, i32 }, ptr %tuple.index, i32 0, i32 1
// CHECK:STDOUT: %tuple.index.load = load i32, ptr %tuple.index12, align 4
// CHECK:STDOUT: ret i32 %tuple.index.load
// CHECK:STDOUT: %tuple.index.load = load { i32, i32 }, ptr %tuple.index, align 4
// CHECK:STDOUT: %tuple.index12 = getelementptr inbounds { i32, i32 }, { i32, i32 } %tuple.index.load, i32 0, i32 1
// CHECK:STDOUT: %tuple.index.load13 = load i32, { i32, i32 } %tuple.index12, align 4
// CHECK:STDOUT: ret i32 %tuple.index.load13
// CHECK:STDOUT: }
6 changes: 3 additions & 3 deletions toolchain/lower/testdata/tuple/value_formation.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ fn F() {
// CHECK:STDOUT: store i32 %8, ptr %11, align 4
// CHECK:STDOUT: %12 = getelementptr inbounds { i32, i32, i32 }, ptr %tuple6, i32 0, i32 2
// CHECK:STDOUT: store i32 %9, ptr %12, align 4
// CHECK:STDOUT: %tuple7 = alloca { { i32, i32, i32 }, { i32, i32, i32 } }, align 8
// CHECK:STDOUT: %13 = getelementptr inbounds { { i32, i32, i32 }, { i32, i32, i32 } }, ptr %tuple7, i32 0, i32 0
// CHECK:STDOUT: %tuple7 = alloca { ptr, ptr }, align 8
// CHECK:STDOUT: %13 = getelementptr inbounds { ptr, ptr }, ptr %tuple7, i32 0, i32 0
// CHECK:STDOUT: store ptr %tuple, ptr %13, align 8
// CHECK:STDOUT: %14 = getelementptr inbounds { { i32, i32, i32 }, { i32, i32, i32 } }, ptr %tuple7, i32 0, i32 1
// CHECK:STDOUT: %14 = getelementptr inbounds { ptr, ptr }, ptr %tuple7, i32 0, i32 1
// CHECK:STDOUT: store ptr %tuple6, ptr %14, align 8
// CHECK:STDOUT: call void @G(ptr %tuple7)
// CHECK:STDOUT: ret void
Expand Down
6 changes: 3 additions & 3 deletions toolchain/lower/testdata/tuple/value_forwarding.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ fn F(a: (i32, i32, i32), b: (i32, i32, i32)) {
// CHECK:STDOUT: declare void @G(ptr)
// CHECK:STDOUT:
// CHECK:STDOUT: define void @F(ptr %a, ptr %b) {
// CHECK:STDOUT: %tuple = alloca { { i32, i32, i32 }, { i32, i32, i32 } }, align 8
// CHECK:STDOUT: %1 = getelementptr inbounds { { i32, i32, i32 }, { i32, i32, i32 } }, ptr %tuple, i32 0, i32 0
// CHECK:STDOUT: %tuple = alloca { ptr, ptr }, align 8
// CHECK:STDOUT: %1 = getelementptr inbounds { ptr, ptr }, ptr %tuple, i32 0, i32 0
// CHECK:STDOUT: store ptr %a, ptr %1, align 8
// CHECK:STDOUT: %2 = getelementptr inbounds { { i32, i32, i32 }, { i32, i32, i32 } }, ptr %tuple, i32 0, i32 1
// CHECK:STDOUT: %2 = getelementptr inbounds { ptr, ptr }, ptr %tuple, i32 0, i32 1
// CHECK:STDOUT: store ptr %b, ptr %2, align 8
// CHECK:STDOUT: call void @G(ptr %tuple)
// CHECK:STDOUT: ret void
Expand Down