Unify instruction naming between SemIR and LLVM IR (#3898)

Factor out `SemIR::InstNamer` and also use it when lowering to LLVM IR.
Automatically name all instructions created with our `IRBuilder` based
on the name computed by the `InstNamer`, and likewise name basic blocks
using the label generated by the `InstNamer`.

Move some of the existing naming logic out from lower into `InstNamer`
so that it's also used in SemIR. In particular, we now name call
instructions after their callee, or after the builtin name for calls to
builtins.

Computing and adding these names isn't completely free. This instruction
naming is designed to be optional, so that we can turn it off for builds
where the LLVM IR will only be converted to assembly and won't be seen
by a human, but so far it's enabled unconditionally. We can tune that
later as needed.
This commit is contained in:
Richard Smith authored and GitHub committed 2024-04-19 02:19:30 +00:00
1 parent 5964795f34
commit 3776c068de
225 files changed
+2835 -2455

No files matched your search

+28 -2
View File
@@ -14,14 +14,19 @@ FunctionContext::FunctionContext(FileContext& file_context,
llvm::raw_ostream* vlog_stream)
: file_context_(&file_context),
function_(function),
builder_(file_context.llvm_context()),
builder_(file_context.llvm_context(), llvm::ConstantFolder(),
Inserter(file_context.inst_namer())),
vlog_stream_(vlog_stream) {}
auto FunctionContext::GetBlock(SemIR::InstBlockId block_id)
-> llvm::BasicBlock* {
llvm::BasicBlock*& entry = blocks_[block_id];
if (!entry) {
entry = llvm::BasicBlock::Create(llvm_context(), "", function_);
llvm::StringRef label_name;
if (const auto* inst_namer = file_context_->inst_namer()) {
label_name = inst_namer->GetUnscopedLabelFor(block_id);
}
entry = llvm::BasicBlock::Create(llvm_context(), label_name, function_);
}
return entry;
}
@@ -34,6 +39,9 @@ auto FunctionContext::TryToReuseBlock(SemIR::InstBlockId block_id,
if (block == synthetic_block_) {
synthetic_block_ = nullptr;
}
if (const auto* inst_namer = file_context_->inst_namer()) {
block->setName(inst_namer->GetUnscopedLabelFor(block_id));
}
return true;
}
@@ -41,6 +49,7 @@ auto FunctionContext::LowerBlock(SemIR::InstBlockId block_id) -> void {
for (const auto& inst_id : sem_ir().inst_blocks().Get(block_id)) {
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(Name) \
case SemIR::Name::Kind: \
@@ -49,6 +58,7 @@ auto FunctionContext::LowerBlock(SemIR::InstBlockId block_id) -> void {
#include "toolchain/sem_ir/inst_kind.def"
}
}
builder_.getInserter().SetCurrentInstId(SemIR::InstId::Invalid);
}
auto FunctionContext::GetBlockArg(SemIR::InstBlockId block_id,
@@ -116,4 +126,20 @@ auto FunctionContext::CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
}
}
auto FunctionContext::Inserter::InsertHelper(
llvm::Instruction* inst, const llvm::Twine& name, llvm::BasicBlock* block,
llvm::BasicBlock::iterator insert_pt) const -> void {
llvm::StringRef base_name;
llvm::StringRef separator;
if (inst_namer_ && !inst->getType()->isVoidTy()) {
base_name = inst_namer_->GetUnscopedNameFor(inst_id_);
}
if (!base_name.empty() && !name.isTriviallyEmpty()) {
separator = ".";
}
IRBuilderDefaultInserter::InsertHelper(inst, base_name + separator + name,
block, insert_pt);
}
} // namespace Carbon::Lower