Skip to content

Commit 540f0e4

Browse files
authored
Unrolled build for #146763
Rollup merge of #146763 - Zalathar:di-builder, r=jdonszelmann cg_llvm: Replace some DIBuilder wrappers with LLVM-C API bindings (part 5) - Part of #134001 - Follow-up to #146673 --- This is another batch of LLVMDIBuilder binding migrations, replacing some our own LLVMRust bindings with bindings to upstream LLVM-C APIs. Some of these are a little more complex than most of the previous migrations, because they split one LLVMRust binding into multiple LLVM bindings, but nothing too fancy. This appears to be the last of the low-hanging fruit. As noted in #134001 (comment), the remaining bindings are difficult or impossible to migrate at present.
2 parents 4ffeda1 + 741e1e2 commit 540f0e4

File tree

5 files changed

+102
-133
lines changed

5 files changed

+102
-133
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn build_fixed_size_array_di_node<'ll, 'tcx>(
117117
.try_to_target_usize(cx.tcx)
118118
.expect("expected monomorphic const in codegen") as c_longlong;
119119

120-
let subrange = unsafe { llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) };
120+
let subrange = unsafe { llvm::LLVMDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) };
121121
let subscripts = &[subrange];
122122

123123
let di_node = unsafe {

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ mod utils;
5252
use self::create_scope_map::compute_mir_scopes;
5353
pub(crate) use self::metadata::build_global_var_di_node;
5454

55-
// FIXME(Zalathar): These `DW_TAG_*` constants are fake values that were
56-
// removed from LLVM in 2015, and are only used by our own `RustWrapper.cpp`
57-
// to decide which C++ API to call. Instead, we should just have two separate
58-
// FFI functions and choose the correct one on the Rust side.
59-
#[allow(non_upper_case_globals)]
60-
const DW_TAG_auto_variable: c_uint = 0x100;
61-
#[allow(non_upper_case_globals)]
62-
const DW_TAG_arg_variable: c_uint = 0x101;
63-
6455
/// A context object for maintaining all state needed by the debuginfo module.
6556
pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> {
6657
llmod: &'ll llvm::Module,
@@ -174,35 +165,38 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
174165

175166
if direct_offset.bytes() > 0 {
176167
addr_ops.push(DW_OP_plus_uconst);
177-
addr_ops.push(direct_offset.bytes() as u64);
168+
addr_ops.push(direct_offset.bytes());
178169
}
179170
for &offset in indirect_offsets {
180171
addr_ops.push(DW_OP_deref);
181172
if offset.bytes() > 0 {
182173
addr_ops.push(DW_OP_plus_uconst);
183-
addr_ops.push(offset.bytes() as u64);
174+
addr_ops.push(offset.bytes());
184175
}
185176
}
186177
if let Some(fragment) = fragment {
187178
// `DW_OP_LLVM_fragment` takes as arguments the fragment's
188179
// offset and size, both of them in bits.
189180
addr_ops.push(DW_OP_LLVM_fragment);
190-
addr_ops.push(fragment.start.bits() as u64);
191-
addr_ops.push((fragment.end - fragment.start).bits() as u64);
181+
addr_ops.push(fragment.start.bits());
182+
addr_ops.push((fragment.end - fragment.start).bits());
192183
}
193184

185+
let di_builder = DIB(self.cx());
186+
let addr_expr = unsafe {
187+
llvm::LLVMDIBuilderCreateExpression(di_builder, addr_ops.as_ptr(), addr_ops.len())
188+
};
194189
unsafe {
195190
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
196-
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
197-
DIB(self.cx()),
191+
llvm::LLVMDIBuilderInsertDeclareRecordAtEnd(
192+
di_builder,
198193
variable_alloca,
199194
dbg_var,
200-
addr_ops.as_ptr(),
201-
addr_ops.len() as c_uint,
195+
addr_expr,
202196
dbg_loc,
203197
self.llbb(),
204-
);
205-
}
198+
)
199+
};
206200
}
207201

208202
fn set_dbg_loc(&mut self, dbg_loc: &'ll DILocation) {
@@ -630,28 +624,39 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
630624

631625
let type_metadata = spanned_type_di_node(self, variable_type, span);
632626

633-
let (argument_index, dwarf_tag) = match variable_kind {
634-
ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable),
635-
LocalVariable => (0, DW_TAG_auto_variable),
636-
};
637627
let align = self.align_of(variable_type);
638628

639629
let name = variable_name.as_str();
640-
unsafe {
641-
llvm::LLVMRustDIBuilderCreateVariable(
642-
DIB(self),
643-
dwarf_tag,
644-
scope_metadata,
645-
name.as_c_char_ptr(),
646-
name.len(),
647-
file_metadata,
648-
loc.line,
649-
type_metadata,
650-
true,
651-
DIFlags::FlagZero,
652-
argument_index,
653-
align.bits() as u32,
654-
)
630+
631+
match variable_kind {
632+
ArgumentVariable(arg_index) => unsafe {
633+
llvm::LLVMDIBuilderCreateParameterVariable(
634+
DIB(self),
635+
scope_metadata,
636+
name.as_ptr(),
637+
name.len(),
638+
arg_index as c_uint,
639+
file_metadata,
640+
loc.line,
641+
type_metadata,
642+
llvm::Bool::TRUE, // (preserve descriptor during optimizations)
643+
DIFlags::FlagZero,
644+
)
645+
},
646+
LocalVariable => unsafe {
647+
llvm::LLVMDIBuilderCreateAutoVariable(
648+
DIB(self),
649+
scope_metadata,
650+
name.as_ptr(),
651+
name.len(),
652+
file_metadata,
653+
loc.line,
654+
type_metadata,
655+
llvm::Bool::TRUE, // (preserve descriptor during optimizations)
656+
DIFlags::FlagZero,
657+
align.bits() as u32,
658+
)
659+
},
655660
}
656661
}
657662
}

compiler/rustc_codegen_llvm/src/debuginfo/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn create_DIArray<'ll>(
2828
builder: &DIBuilder<'ll>,
2929
arr: &[Option<&'ll DIDescriptor>],
3030
) -> &'ll DIArray {
31-
unsafe { llvm::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32) }
31+
unsafe { llvm::LLVMDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len()) }
3232
}
3333

3434
#[inline]

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use rustc_target::spec::SymbolVisibility;
2525
use super::RustString;
2626
use super::debuginfo::{
2727
DIArray, DIBuilder, DIDerivedType, DIDescriptor, DIEnumerator, DIFile, DIFlags,
28-
DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram, DISubrange,
29-
DITemplateTypeParameter, DIType, DIVariable, DebugEmissionKind, DebugNameTableKind,
28+
DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
29+
DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind,
3030
};
3131
use crate::llvm;
3232

@@ -807,6 +807,8 @@ unsafe extern "C" {
807807
pub(crate) type Metadata;
808808
pub(crate) type BasicBlock;
809809
pub(crate) type Comdat;
810+
/// `&'ll DbgRecord` represents `LLVMDbgRecordRef`.
811+
pub(crate) type DbgRecord;
810812
}
811813
#[repr(C)]
812814
pub(crate) struct Builder<'a>(InvariantOpaque<'a>);
@@ -891,7 +893,6 @@ pub(crate) mod debuginfo {
891893
pub(crate) type DIVariable = DIDescriptor;
892894
pub(crate) type DIGlobalVariableExpression = DIDescriptor;
893895
pub(crate) type DIArray = DIDescriptor;
894-
pub(crate) type DISubrange = DIDescriptor;
895896
pub(crate) type DIEnumerator = DIDescriptor;
896897
pub(crate) type DITemplateTypeParameter = DIDescriptor;
897898

@@ -1992,6 +1993,59 @@ unsafe extern "C" {
19921993
Scope: Option<&'ll Metadata>,
19931994
AlignInBits: u32, // (optional; default is 0)
19941995
) -> &'ll Metadata;
1996+
1997+
pub(crate) fn LLVMDIBuilderGetOrCreateSubrange<'ll>(
1998+
Builder: &DIBuilder<'ll>,
1999+
LowerBound: i64,
2000+
Count: i64,
2001+
) -> &'ll Metadata;
2002+
2003+
pub(crate) fn LLVMDIBuilderGetOrCreateArray<'ll>(
2004+
Builder: &DIBuilder<'ll>,
2005+
Data: *const Option<&'ll Metadata>,
2006+
NumElements: size_t,
2007+
) -> &'ll Metadata;
2008+
2009+
pub(crate) fn LLVMDIBuilderCreateExpression<'ll>(
2010+
Builder: &DIBuilder<'ll>,
2011+
Addr: *const u64,
2012+
Length: size_t,
2013+
) -> &'ll Metadata;
2014+
2015+
pub(crate) fn LLVMDIBuilderInsertDeclareRecordAtEnd<'ll>(
2016+
Builder: &DIBuilder<'ll>,
2017+
Storage: &'ll Value,
2018+
VarInfo: &'ll Metadata,
2019+
Expr: &'ll Metadata,
2020+
DebugLoc: &'ll Metadata,
2021+
Block: &'ll BasicBlock,
2022+
) -> &'ll DbgRecord;
2023+
2024+
pub(crate) fn LLVMDIBuilderCreateAutoVariable<'ll>(
2025+
Builder: &DIBuilder<'ll>,
2026+
Scope: &'ll Metadata,
2027+
Name: *const c_uchar, // See "PTR_LEN_STR".
2028+
NameLen: size_t,
2029+
File: &'ll Metadata,
2030+
LineNo: c_uint,
2031+
Ty: &'ll Metadata,
2032+
AlwaysPreserve: llvm::Bool, // "If true, this descriptor will survive optimizations."
2033+
Flags: DIFlags,
2034+
AlignInBits: u32,
2035+
) -> &'ll Metadata;
2036+
2037+
pub(crate) fn LLVMDIBuilderCreateParameterVariable<'ll>(
2038+
Builder: &DIBuilder<'ll>,
2039+
Scope: &'ll Metadata,
2040+
Name: *const c_uchar, // See "PTR_LEN_STR".
2041+
NameLen: size_t,
2042+
ArgNo: c_uint,
2043+
File: &'ll Metadata,
2044+
LineNo: c_uint,
2045+
Ty: &'ll Metadata,
2046+
AlwaysPreserve: llvm::Bool, // "If true, this descriptor will survive optimizations."
2047+
Flags: DIFlags,
2048+
) -> &'ll Metadata;
19952049
}
19962050

19972051
#[link(name = "llvm-wrapper", kind = "static")]
@@ -2358,43 +2412,6 @@ unsafe extern "C" {
23582412
AlignInBits: u32,
23592413
) -> &'a DIGlobalVariableExpression;
23602414

2361-
pub(crate) fn LLVMRustDIBuilderCreateVariable<'a>(
2362-
Builder: &DIBuilder<'a>,
2363-
Tag: c_uint,
2364-
Scope: &'a DIDescriptor,
2365-
Name: *const c_char,
2366-
NameLen: size_t,
2367-
File: &'a DIFile,
2368-
LineNo: c_uint,
2369-
Ty: &'a DIType,
2370-
AlwaysPreserve: bool,
2371-
Flags: DIFlags,
2372-
ArgNo: c_uint,
2373-
AlignInBits: u32,
2374-
) -> &'a DIVariable;
2375-
2376-
pub(crate) fn LLVMRustDIBuilderGetOrCreateSubrange<'a>(
2377-
Builder: &DIBuilder<'a>,
2378-
Lo: i64,
2379-
Count: i64,
2380-
) -> &'a DISubrange;
2381-
2382-
pub(crate) fn LLVMRustDIBuilderGetOrCreateArray<'a>(
2383-
Builder: &DIBuilder<'a>,
2384-
Ptr: *const Option<&'a DIDescriptor>,
2385-
Count: c_uint,
2386-
) -> &'a DIArray;
2387-
2388-
pub(crate) fn LLVMRustDIBuilderInsertDeclareAtEnd<'a>(
2389-
Builder: &DIBuilder<'a>,
2390-
Val: &'a Value,
2391-
VarInfo: &'a DIVariable,
2392-
AddrOps: *const u64,
2393-
AddrOpsCount: c_uint,
2394-
DL: &'a DILocation,
2395-
InsertAtEnd: &'a BasicBlock,
2396-
);
2397-
23982415
pub(crate) fn LLVMRustDIBuilderCreateEnumerator<'a>(
23992416
Builder: &DIBuilder<'a>,
24002417
Name: *const c_char,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -990,14 +990,6 @@ extern "C" void LLVMRustGlobalAddMetadata(LLVMValueRef Global, unsigned Kind,
990990
unwrap<GlobalObject>(Global)->addMetadata(Kind, *unwrap<MDNode>(MD));
991991
}
992992

993-
extern "C" LLVMDIBuilderRef LLVMRustDIBuilderCreate(LLVMModuleRef M) {
994-
return wrap(new DIBuilder(*unwrap(M)));
995-
}
996-
997-
extern "C" void LLVMRustDIBuilderDispose(LLVMDIBuilderRef Builder) {
998-
delete unwrap(Builder);
999-
}
1000-
1001993
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateCompileUnit(
1002994
LLVMDIBuilderRef Builder, unsigned Lang, LLVMMetadataRef FileRef,
1003995
const char *Producer, size_t ProducerLen, bool isOptimized,
@@ -1129,51 +1121,6 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateStaticVariable(
11291121
return wrap(VarExpr);
11301122
}
11311123

1132-
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariable(
1133-
LLVMDIBuilderRef Builder, unsigned Tag, LLVMMetadataRef Scope,
1134-
const char *Name, size_t NameLen, LLVMMetadataRef File, unsigned LineNo,
1135-
LLVMMetadataRef Ty, bool AlwaysPreserve, LLVMDIFlags Flags, unsigned ArgNo,
1136-
uint32_t AlignInBits) {
1137-
if (Tag == 0x100) { // DW_TAG_auto_variable
1138-
return wrap(unwrap(Builder)->createAutoVariable(
1139-
unwrapDI<DIDescriptor>(Scope), StringRef(Name, NameLen),
1140-
unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), AlwaysPreserve,
1141-
fromRust(Flags), AlignInBits));
1142-
} else {
1143-
return wrap(unwrap(Builder)->createParameterVariable(
1144-
unwrapDI<DIDescriptor>(Scope), StringRef(Name, NameLen), ArgNo,
1145-
unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), AlwaysPreserve,
1146-
fromRust(Flags)));
1147-
}
1148-
}
1149-
1150-
extern "C" LLVMMetadataRef
1151-
LLVMRustDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder, int64_t Lo,
1152-
int64_t Count) {
1153-
return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
1154-
}
1155-
1156-
extern "C" LLVMMetadataRef
1157-
LLVMRustDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1158-
LLVMMetadataRef *Ptr, unsigned Count) {
1159-
Metadata **DataValue = unwrap(Ptr);
1160-
return wrap(unwrap(Builder)
1161-
->getOrCreateArray(ArrayRef<Metadata *>(DataValue, Count))
1162-
.get());
1163-
}
1164-
1165-
extern "C" void
1166-
LLVMRustDIBuilderInsertDeclareAtEnd(LLVMDIBuilderRef Builder, LLVMValueRef V,
1167-
LLVMMetadataRef VarInfo, uint64_t *AddrOps,
1168-
unsigned AddrOpsCount, LLVMMetadataRef DL,
1169-
LLVMBasicBlockRef InsertAtEnd) {
1170-
unwrap(Builder)->insertDeclare(
1171-
unwrap(V), unwrap<DILocalVariable>(VarInfo),
1172-
unwrap(Builder)->createExpression(
1173-
llvm::ArrayRef<uint64_t>(AddrOps, AddrOpsCount)),
1174-
DebugLoc(cast<MDNode>(unwrap(DL))), unwrap(InsertAtEnd));
1175-
}
1176-
11771124
extern "C" LLVMMetadataRef
11781125
LLVMRustDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, const char *Name,
11791126
size_t NameLen, const uint64_t Value[2],

0 commit comments

Comments
 (0)