Switch types to a SemanticsTypeId. (#2854)

This switches types to using SemanticsTypeId instead of SemanticsNodeId, and lowering pre-builds its list of types. The empty tuple type is special-cased because we don't want to emit it unless it's in-use, but as the implicit return for functions, it's frequently used. Callables use invalid to indicate the implicit return, and that seems undesirable to change due to the size increase.
This commit is contained in:
Jon Ross-Perkins
2023-05-26 11:19:49 -07:00
committed by GitHub
parent a2156f5a4b
commit 1497e1333d
71 changed files with 645 additions and 523 deletions
+7 -22
View File
@@ -23,6 +23,12 @@ LoweringContext::LoweringContext(llvm::LLVMContext& llvm_context,
lowered_callables_(semantics_ir_->callables_size(), nullptr) {
CARBON_CHECK(!semantics_ir.has_errors())
<< "Generating LLVM IR from invalid SemanticsIR is unsupported.";
auto types = semantics_ir_->types();
lowered_types_.resize_for_overwrite(types.size());
for (int i = 0; i < static_cast<int>(types.size()); ++i) {
lowered_types_[i] = BuildLoweredNodeAsType(types[i]);
}
}
auto LoweringContext::Run() -> std::unique_ptr<llvm::Module> {
@@ -86,7 +92,7 @@ auto LoweringContext::BuildLoweredNodeAsType(SemanticsNodeId node_id)
// recursion while still letting them cache.
CARBON_CHECK(type_id.index < SemanticsBuiltinKind::ValidCount)
<< type_id;
subtypes.push_back(GetLoweredNodeAsType(type_id));
subtypes.push_back(GetType(type_id));
}
return llvm::StructType::create(*llvm_context_, subtypes,
"StructLiteralType");
@@ -97,25 +103,4 @@ auto LoweringContext::BuildLoweredNodeAsType(SemanticsNodeId node_id)
}
}
auto LoweringContext::GetLoweredNodeAsType(SemanticsNodeId node_id)
-> llvm::Type* {
if (lowered_nodes_[node_id.index]) {
return lowered_nodes_[node_id.index].get<llvm::Type*>();
}
auto* type = BuildLoweredNodeAsType(node_id);
lowered_nodes_[node_id.index] = type;
return type;
}
auto LoweringContext::GetLoweredNodeAsValue(SemanticsNodeId node_id)
-> llvm::Value* {
auto& node = lowered_nodes_[node_id.index];
auto* value = node.dyn_cast<llvm::Value*>();
CARBON_CHECK(value != nullptr)
<< node_id << " doesn't have a Value (either Type or null); nullness: "
<< node.isNull();
return value;
}
} // namespace Carbon