Refactor whether a function is lowered into InstKind::Define (#4117)

This is to remove all the FatalIfEncountered handlers in handle.cpp.
They just feel like noise when reading the file. Plus it's one less bit
of boilerplate to add for instructions that don't lower.

Note that I left HandleParam/HandleAddrPattern. I'd be happy to change
those to just set lowered=false too, but was hesitant to given the
separate logic.

Also, I'm separately considering migrating the macro logic into similar
constexpr things. If I do, I might switch Define to take in a struct.
But for how this particular parameter works, the overload felt
reasonable, particularly since is_lowered is not used in combination
with TerminatorKind.
This commit is contained in:
Jon Ross-Perkins
2024-07-11 19:54:09 +00:00
committed by GitHub
parent a81d67c629
commit 6682241ea0
5 changed files with 63 additions and 86 deletions
+19 -5
View File
@@ -51,6 +51,15 @@ auto FunctionContext::LowerBlock(SemIR::InstBlockId block_id) -> void {
}
}
template <typename InstT>
static auto FatalErrorIfEncountered(InstT inst) -> void {
CARBON_FATAL()
<< "Encountered an instruction that isn't expected to lower. It's "
"possible that logic needs to be changed in order to stop "
"showing this instruction in lowered contexts. Instruction: "
<< inst;
}
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()) {
@@ -62,10 +71,15 @@ auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
builder_.getInserter().SetCurrentInstId(inst_id);
CARBON_KIND_SWITCH(inst) {
#define CARBON_SEM_IR_INST_KIND_CONSTANT_ALWAYS(Name)
#define CARBON_SEM_IR_INST_KIND(Name) \
case CARBON_KIND(SemIR::Name typed_inst): \
Handle##Name(*this, inst_id, typed_inst); \
break;
#define CARBON_SEM_IR_INST_KIND(Name) \
case CARBON_KIND(SemIR::Name typed_inst): { \
if constexpr (SemIR::Name::Kind.is_lowered()) { \
Handle##Name(*this, inst_id, typed_inst); \
} else { \
FatalErrorIfEncountered(typed_inst); \
} \
break; \
}
#include "toolchain/sem_ir/inst_kind.def"
default:
@@ -73,7 +87,7 @@ auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
<< inst;
}
builder_.getInserter().SetCurrentInstId(SemIR::InstId::Invalid);
}
} // namespace Carbon::Lower
auto FunctionContext::GetBlockArg(SemIR::InstBlockId block_id,
SemIR::TypeId type_id) -> llvm::PHINode* {