Separate constant emission from function emission. (#3916)

Make constant emission non-recursive, and stop building a bogus
FunctionContext to emit constants.

To support this, move `InstConstantKind` from the typed instruction
definition into the `.def` file, and add more macros to allow us to
generate case labels based on whether an instruction is a constant.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
This commit is contained in:
Richard Smith
2024-04-26 23:27:07 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins Carbon Infra Bot
parent 5627f88c22
commit 79c0b65288
36 changed files with 625 additions and 469 deletions
+10 -4
View File
@@ -47,24 +47,30 @@ auto FunctionContext::TryToReuseBlock(SemIR::InstBlockId block_id,
auto FunctionContext::LowerBlock(SemIR::InstBlockId block_id) -> void {
for (auto inst_id : sem_ir().inst_blocks().Get(block_id)) {
// Skip over constants. `FileContext::GetGlobal` lowers them as needed.
if (sem_ir().constant_values().Get(inst_id).is_constant()) {
continue;
}
LowerInst(inst_id);
}
}
auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
// Skip over constants. `FileContext::GetGlobal` lowers them as needed.
if (sem_ir().constant_values().Get(inst_id).is_constant()) {
return;
}
auto inst = sem_ir().insts().Get(inst_id);
CARBON_VLOG() << "Lowering " << inst_id << ": " << inst << "\n";
builder_.getInserter().SetCurrentInstId(inst_id);
switch (inst.kind()) {
#define CARBON_SEM_IR_INST_KIND_CONSTANT_ALWAYS(Name)
#define CARBON_SEM_IR_INST_KIND(Name) \
case SemIR::Name::Kind: \
Handle##Name(*this, inst_id, inst.As<SemIR::Name>()); \
break;
#include "toolchain/sem_ir/inst_kind.def"
default:
CARBON_FATAL() << "Missing constant value for constant instruction "
<< inst;
}
builder_.getInserter().SetCurrentInstId(SemIR::InstId::Invalid);
}