Fix debug location for variable allocas and lifetime markers. (#5432)

`IRBuilderBase::SetInsertPoint` weirdly replaces our debug location with
one copied from the new insertion point, so undo its damage after
calling it.

Also included: a couple of cleanups I made while tracking this down.
This commit is contained in:
Richard Smith
2025-05-06 21:26:59 +00:00
committed by GitHub
parent 7b9ec95118
commit 32e68cfb5b
33 changed files with 1511 additions and 1452 deletions
+19 -10
View File
@@ -101,14 +101,10 @@ auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
auto inst = sem_ir().insts().Get(inst_id);
CARBON_VLOG("Lowering {0}: {1}\n", inst_id, inst);
builder_.getInserter().SetCurrentInstId(inst_id);
if (di_subprogram_) {
auto loc = file_context_->GetLocForDI(inst_id);
CARBON_CHECK(loc.filename == di_subprogram_->getFile()->getFilename(),
"Instructions located in a different file from their "
"enclosing function aren't handled yet");
builder_.SetCurrentDebugLocation(
llvm::DILocation::get(builder_.getContext(), loc.line_number,
loc.column_number, di_subprogram_));
auto debug_loc = GetDebugLoc(inst_id);
if (debug_loc) {
builder_.SetCurrentDebugLocation(debug_loc);
}
CARBON_KIND_SWITCH(inst) {
@@ -120,10 +116,11 @@ auto FunctionContext::LowerInst(SemIR::InstId inst_id) -> void {
#include "toolchain/sem_ir/inst_kind.def"
}
builder_.getInserter().SetCurrentInstId(SemIR::InstId::None);
if (di_subprogram_) {
if (debug_loc) {
builder_.SetCurrentDebugLocation(llvm::DebugLoc());
}
builder_.getInserter().SetCurrentInstId(SemIR::InstId::None);
}
auto FunctionContext::GetBlockArg(SemIR::InstBlockId block_id,
@@ -151,6 +148,18 @@ auto FunctionContext::MakeSyntheticBlock() -> llvm::BasicBlock* {
return synthetic_block_;
}
auto FunctionContext::GetDebugLoc(SemIR::InstId inst_id) -> llvm::DebugLoc {
if (!di_subprogram_) {
return llvm::DebugLoc();
}
auto loc = file_context_->GetLocForDI(inst_id);
CARBON_CHECK(loc.filename == di_subprogram_->getFile()->getFilename(),
"Instructions located in a different file from their "
"enclosing function aren't handled yet");
return llvm::DILocation::get(builder_.getContext(), loc.line_number,
loc.column_number, di_subprogram_);
}
auto FunctionContext::FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
SemIR::InstId source_id) -> void {
switch (SemIR::InitRepr::ForType(sem_ir(), type_id).kind) {