Don't CHECK-fail when emitting IR with cross-file locations. (#5447)

If a function contains instructions whose locations are in another file,
skip providing debug locations for those instructions rather than
CHECK-failing.

This happens when emitting a thunk where the signature is declared in
one file and the call target is in another file: some parts of the thunk
use the original signature as their locations, whereas other parts of it
use the location of the call target.
This commit is contained in:
Richard Smith
2025-05-08 20:16:51 +00:00
committed by GitHub
parent e6a6624ec6
commit 04505f4a64
2 changed files with 102 additions and 7 deletions
+10 -3
View File
@@ -153,9 +153,16 @@ auto FunctionContext::GetDebugLoc(SemIR::InstId inst_id) -> llvm::DebugLoc {
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");
if (loc.filename != di_subprogram_->getFile()->getFilename()) {
// Location is from a different file. We can't represent that directly
// within the scope of this function's subprogram, and we don't want to
// generate a new subprogram, so just discard the location information. This
// happens for thunks when emitting the portion of the thunk that is
// duplicated from the original signature.
//
// TODO: Handle this case better.
return llvm::DebugLoc();
}
return llvm::DILocation::get(builder_.getContext(), loc.line_number,
loc.column_number, di_subprogram_);
}