Rollup merge of #147608 - Zalathar:debuginfo, r=nnethercote

cg_llvm: Use `LLVMDIBuilderCreateGlobalVariableExpression`

- Part of rust-lang/rust#134001
- Follow-up to rust-lang/rust#146763

---

This PR dismantles the somewhat complicated `LLVMRustDIBuilderCreateStaticVariable` function, and replaces it with equivalent calls to `LLVMDIBuilderCreateGlobalVariableExpression` and `LLVMGlobalSetMetadata`.

A key difference is that the new code does not replicate the attempted downcast of `InitVal`. As far as I can tell, those downcasts were actually dead, because `llvm::ConstantInt` and `llvm::ConstantFP` are not subclasses of `llvm::GlobalVariable`. I tried replacing those code paths with fatal errors, and was unable to induce failure in any of the relevant test suites I ran.

I have also confirmed that if the calls to `create_static_variable` are commented out, debuginfo tests will fail, demonstrating some amount of relevant test coverage.

The new `DIBuilder` methods have been added via an extension trait, not as inherent methods, to avoid impeding rust-lang/rust#142897.
This commit is contained in:
Guillaume Gomez 2025-10-13 11:25:23 +02:00 committed by GitHub
commit 3938f42bb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 150 additions and 93 deletions

View file

@ -1038,37 +1038,6 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariantMemberType(
fromRust(Flags), unwrapDI<DIType>(Ty)));
}
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateStaticVariable(
LLVMDIBuilderRef Builder, LLVMMetadataRef Context, const char *Name,
size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
bool IsLocalToUnit, LLVMValueRef V, LLVMMetadataRef Decl = nullptr,
uint32_t AlignInBits = 0) {
llvm::GlobalVariable *InitVal = cast<llvm::GlobalVariable>(unwrap(V));
llvm::DIExpression *InitExpr = nullptr;
if (llvm::ConstantInt *IntVal = llvm::dyn_cast<llvm::ConstantInt>(InitVal)) {
InitExpr = unwrap(Builder)->createConstantValueExpression(
IntVal->getValue().getSExtValue());
} else if (llvm::ConstantFP *FPVal =
llvm::dyn_cast<llvm::ConstantFP>(InitVal)) {
InitExpr = unwrap(Builder)->createConstantValueExpression(
FPVal->getValueAPF().bitcastToAPInt().getZExtValue());
}
llvm::DIGlobalVariableExpression *VarExpr =
unwrap(Builder)->createGlobalVariableExpression(
unwrapDI<DIDescriptor>(Context), StringRef(Name, NameLen),
StringRef(LinkageName, LinkageNameLen), unwrapDI<DIFile>(File),
LineNo, unwrapDI<DIType>(Ty), IsLocalToUnit,
/* isDefined */ true, InitExpr, unwrapDIPtr<MDNode>(Decl),
/* templateParams */ nullptr, AlignInBits);
InitVal->setMetadata("dbg", VarExpr);
return wrap(VarExpr);
}
extern "C" LLVMMetadataRef
LLVMRustDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, const char *Name,
size_t NameLen, const uint64_t Value[2],