Skip to content

cg_llvm: Replace most of our DIBuilder wrappers with LLVM-C API bindings #134009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1d8fe72
Make our `DIFlags` match `LLVMDIFlags` in the LLVM-C API
Zalathar Dec 7, 2024
d759865
Introduce `DIBuilderBox`, an owning pointer to `DIBuilder`
Zalathar Dec 7, 2024
362b4aa
Use `LLVMDIBuilderFinalize`
Zalathar Dec 7, 2024
aa9417e
Use `LLVMDIBuilderCreateNameSpace`
Zalathar Dec 7, 2024
8f346a4
Use `LLVMDIBuilderCreateLexicalBlock`
Zalathar Dec 7, 2024
d4c0568
Use `LLVMDIBuilderCreateLexicalBlockFile`
Zalathar Dec 7, 2024
2d3b7b7
Use `LLVMDIBuilderCreateDebugLocation`
Zalathar Dec 7, 2024
ed9a4d2
Use `LLVMDIBuilderCreateSubroutineType`
Zalathar Dec 7, 2024
71e8169
Use `LLVMDIBuilderCreateUnionType`
Zalathar Dec 7, 2024
6230db2
Use `LLVMDIBuilderCreateArrayType`
Zalathar Dec 7, 2024
6596c94
Use `LLVMDIBuilderCreateBasicType`
Zalathar Dec 7, 2024
50194be
Use `LLVMDIBuilderCreatePointerType`
Zalathar Dec 7, 2024
3be1ccb
Use `LLVMDIBuilderCreateStructType`
Zalathar Dec 7, 2024
1629274
Use `LLVMDIBuilderCreateMemberType`
Zalathar Dec 7, 2024
86d7061
Use `LLVMDIBuilderCreateStaticMemberType`
Zalathar Dec 7, 2024
ef9cf38
Use `LLVMDIBuilderCreateTypedef`
Zalathar Dec 7, 2024
c04657f
Use `LLVMDIBuilderGetOrCreateSubrange`
Zalathar Dec 7, 2024
7e5ed45
Use `LLVMDIBuilderGetOrCreateArray`
Zalathar Dec 7, 2024
1a3ef56
Use `LLVMDIBuilderCreateExpression` and adjust our insert function
Zalathar Dec 7, 2024
a740567
Use constants for DWARF opcodes, instead of FFI calls
Zalathar Dec 8, 2024
b3fc61f
Properly unwrap `LLVMDIBuilderRef` in the remaining wrapper functions
Zalathar Dec 7, 2024
057f238
Remove `LLVMRustDIFlags`
Zalathar Dec 7, 2024
505665a
Add FIXME for auditing optional parameters passed to DIBuilder
Zalathar Dec 7, 2024
1837dbe
Explain why the remaining `LLVMRustDIBuilder*` wrappers exist
Zalathar Dec 8, 2024
d6297f8
Explain why (some) pointer/length strings are `*const c_uchar`
Zalathar Dec 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions compiler/rustc_codegen_llvm/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,17 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
addr_ops.push((fragment.end - fragment.start).bits() as u64);
}

let expr = unsafe {
llvm::LLVMDIBuilderCreateExpression(DIB(self.cx()), addr_ops.as_ptr(), addr_ops.len())
};

unsafe {
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
llvm::LLVMRustDIBuilderInsertDeclareRecordAtEnd(
DIB(self.cx()),
variable_alloca,
dbg_var,
addr_ops.as_ptr(),
addr_ops.len() as c_uint,
expr,
dbg_loc,
self.llbb(),
);
Expand Down
23 changes: 15 additions & 8 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,12 @@ unsafe extern "C" {
Data: *const Option<&'ll Metadata>,
NumElements: size_t,
) -> &'ll Metadata;

pub(crate) fn LLVMDIBuilderCreateExpression<'ll>(
Builder: &DIBuilder<'ll>,
Addr: *const u64,
Length: size_t,
) -> &'ll Metadata;
}

#[link(name = "llvm-wrapper", kind = "static")]
Expand Down Expand Up @@ -2162,14 +2168,15 @@ unsafe extern "C" {
AlignInBits: u32,
) -> &'a DIVariable;

pub fn LLVMRustDIBuilderInsertDeclareAtEnd<'a>(
Builder: &DIBuilder<'a>,
Val: &'a Value,
VarInfo: &'a DIVariable,
AddrOps: *const u64,
AddrOpsCount: c_uint,
DL: &'a DILocation,
InsertAtEnd: &'a BasicBlock,
/// Mostly equivalent to `LLVMDIBuilderInsertDeclareRecordAtEnd` in LLVM 19,
/// except that this works on LLVM 18 and also doesn't return a value.
pub(crate) fn LLVMRustDIBuilderInsertDeclareRecordAtEnd<'ll>(
Comment on lines +2198 to +2200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need fixme with note to change this when minimum llvm will be 19.

Builder: &DIBuilder<'ll>,
Storage: &'ll Value,
VarInfo: &'ll Metadata,
Expr: &'ll Metadata,
DebugLoc: &'ll Metadata,
Block: &'ll BasicBlock,
);

pub fn LLVMRustDIBuilderCreateEnumerator<'a>(
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1058,15 +1058,12 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariable(
}
}

extern "C" void LLVMRustDIBuilderInsertDeclareAtEnd(
LLVMRustDIBuilderRef Builder, LLVMValueRef V, LLVMMetadataRef VarInfo,
uint64_t *AddrOps, unsigned AddrOpsCount, LLVMMetadataRef DL,
LLVMBasicBlockRef InsertAtEnd) {
Builder->insertDeclare(unwrap(V), unwrap<DILocalVariable>(VarInfo),
Builder->createExpression(
llvm::ArrayRef<uint64_t>(AddrOps, AddrOpsCount)),
DebugLoc(cast<MDNode>(unwrap(DL))),
unwrap(InsertAtEnd));
extern "C" void LLVMRustDIBuilderInsertDeclareRecordAtEnd(
LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMBasicBlockRef Block) {
unwrap(Builder)->insertDeclare(
unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc), unwrap(Block));
}

extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateEnumerator(
Expand Down