Skip to content

Commit ba94414

Browse files
committed
Use LLVMDIBuilderCreateGlobalVariableExpression
Note that the code in `LLVMRustDIBuilderCreateStaticVariable` that tried to downcast `InitVal` was actually dead, because `llvm::ConstantInt` and `llvm::ConstantFP` are not subclasses of `llvm::GlobalVariable`.
1 parent 1db7d41 commit ba94414

File tree

3 files changed

+37
-60
lines changed

3 files changed

+37
-60
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use libc::c_uint;
22
use rustc_abi::Align;
33

4-
use crate::common::AsCCharPtr;
5-
use crate::llvm;
64
use crate::llvm::debuginfo::DIBuilder;
5+
use crate::llvm::{self, ToLlvmBool};
76

87
/// Extension trait for defining safe wrappers and helper methods on
98
/// `&DIBuilder<'ll>`, without requiring it to be defined in the same crate.
@@ -31,23 +30,33 @@ pub(crate) trait DIBuilderExt<'ll> {
3130
let this = self.as_di_builder();
3231
let align_in_bits = align.map_or(0, |align| align.bits() as u32);
3332

34-
unsafe {
35-
llvm::LLVMRustDIBuilderCreateStaticVariable(
33+
// `LLVMDIBuilderCreateGlobalVariableExpression` would assert if we
34+
// gave it a null `Expr` pointer, so give it an empty expression
35+
// instead, which is what the C++ `createGlobalVariableExpression`
36+
// method would do if given a null `DIExpression` pointer.
37+
let expr = self.create_expression(&[]);
38+
39+
let global_var_expr = unsafe {
40+
llvm::LLVMDIBuilderCreateGlobalVariableExpression(
3641
this,
3742
scope,
38-
name.as_c_char_ptr(),
43+
name.as_ptr(),
3944
name.len(),
40-
linkage_name.as_c_char_ptr(),
45+
linkage_name.as_ptr(),
4146
linkage_name.len(),
4247
file,
4348
line_number,
4449
ty,
45-
is_local_to_unit,
46-
val,
50+
is_local_to_unit.to_llvm_bool(),
51+
expr,
4752
decl,
4853
align_in_bits,
4954
)
50-
}
55+
};
56+
57+
unsafe { llvm::LLVMGlobalSetMetadata(val, llvm::MD_dbg, global_var_expr) };
58+
59+
global_var_expr
5160
}
5261
}
5362

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use libc::{c_char, c_int, c_uchar, c_uint, c_ulonglong, c_void, size_t};
2222

2323
use super::RustString;
2424
use super::debuginfo::{
25-
DIArray, DIBuilder, DIDerivedType, DIDescriptor, DIEnumerator, DIFile, DIFlags,
26-
DIGlobalVariableExpression, DILocation, DISPFlags, DIScope, DISubprogram,
27-
DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind,
25+
DIArray, DIBuilder, DIDerivedType, DIDescriptor, DIEnumerator, DIFile, DIFlags, DILocation,
26+
DISPFlags, DIScope, DISubprogram, DITemplateTypeParameter, DIType, DebugEmissionKind,
27+
DebugNameTableKind,
2828
};
2929
use crate::llvm::MetadataKindId;
3030
use crate::{TryFromU32, llvm};
@@ -781,7 +781,6 @@ pub(crate) mod debuginfo {
781781
pub(crate) type DIDerivedType = DIType;
782782
pub(crate) type DICompositeType = DIDerivedType;
783783
pub(crate) type DIVariable = DIDescriptor;
784-
pub(crate) type DIGlobalVariableExpression = DIDescriptor;
785784
pub(crate) type DIArray = DIDescriptor;
786785
pub(crate) type DIEnumerator = DIDescriptor;
787786
pub(crate) type DITemplateTypeParameter = DIDescriptor;
@@ -1875,6 +1874,22 @@ unsafe extern "C" {
18751874
Length: size_t,
18761875
) -> &'ll Metadata;
18771876

1877+
pub(crate) fn LLVMDIBuilderCreateGlobalVariableExpression<'ll>(
1878+
Builder: &DIBuilder<'ll>,
1879+
Scope: Option<&'ll Metadata>,
1880+
Name: *const c_uchar, // See "PTR_LEN_STR".
1881+
NameLen: size_t,
1882+
Linkage: *const c_uchar, // See "PTR_LEN_STR".
1883+
LinkLen: size_t,
1884+
File: &'ll Metadata,
1885+
LineNo: c_uint,
1886+
Ty: &'ll Metadata,
1887+
LocalToUnit: llvm::Bool,
1888+
Expr: &'ll Metadata,
1889+
Decl: Option<&'ll Metadata>,
1890+
AlignInBits: u32,
1891+
) -> &'ll Metadata;
1892+
18781893
pub(crate) fn LLVMDIBuilderInsertDeclareRecordAtEnd<'ll>(
18791894
Builder: &DIBuilder<'ll>,
18801895
Storage: &'ll Value,
@@ -2214,22 +2229,6 @@ unsafe extern "C" {
22142229
Ty: &'a DIType,
22152230
) -> &'a DIType;
22162231

2217-
pub(crate) fn LLVMRustDIBuilderCreateStaticVariable<'a>(
2218-
Builder: &DIBuilder<'a>,
2219-
Context: Option<&'a DIScope>,
2220-
Name: *const c_char,
2221-
NameLen: size_t,
2222-
LinkageName: *const c_char,
2223-
LinkageNameLen: size_t,
2224-
File: &'a DIFile,
2225-
LineNo: c_uint,
2226-
Ty: &'a DIType,
2227-
isLocalToUnit: bool,
2228-
Val: &'a Value,
2229-
Decl: Option<&'a DIDescriptor>,
2230-
AlignInBits: u32,
2231-
) -> &'a DIGlobalVariableExpression;
2232-
22332232
pub(crate) fn LLVMRustDIBuilderCreateEnumerator<'a>(
22342233
Builder: &DIBuilder<'a>,
22352234
Name: *const c_char,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,37 +1044,6 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariantMemberType(
10441044
fromRust(Flags), unwrapDI<DIType>(Ty)));
10451045
}
10461046

1047-
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateStaticVariable(
1048-
LLVMDIBuilderRef Builder, LLVMMetadataRef Context, const char *Name,
1049-
size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
1050-
LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1051-
bool IsLocalToUnit, LLVMValueRef V, LLVMMetadataRef Decl = nullptr,
1052-
uint32_t AlignInBits = 0) {
1053-
llvm::GlobalVariable *InitVal = cast<llvm::GlobalVariable>(unwrap(V));
1054-
1055-
llvm::DIExpression *InitExpr = nullptr;
1056-
if (llvm::ConstantInt *IntVal = llvm::dyn_cast<llvm::ConstantInt>(InitVal)) {
1057-
InitExpr = unwrap(Builder)->createConstantValueExpression(
1058-
IntVal->getValue().getSExtValue());
1059-
} else if (llvm::ConstantFP *FPVal =
1060-
llvm::dyn_cast<llvm::ConstantFP>(InitVal)) {
1061-
InitExpr = unwrap(Builder)->createConstantValueExpression(
1062-
FPVal->getValueAPF().bitcastToAPInt().getZExtValue());
1063-
}
1064-
1065-
llvm::DIGlobalVariableExpression *VarExpr =
1066-
unwrap(Builder)->createGlobalVariableExpression(
1067-
unwrapDI<DIDescriptor>(Context), StringRef(Name, NameLen),
1068-
StringRef(LinkageName, LinkageNameLen), unwrapDI<DIFile>(File),
1069-
LineNo, unwrapDI<DIType>(Ty), IsLocalToUnit,
1070-
/* isDefined */ true, InitExpr, unwrapDIPtr<MDNode>(Decl),
1071-
/* templateParams */ nullptr, AlignInBits);
1072-
1073-
InitVal->setMetadata("dbg", VarExpr);
1074-
1075-
return wrap(VarExpr);
1076-
}
1077-
10781047
extern "C" LLVMMetadataRef
10791048
LLVMRustDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, const char *Name,
10801049
size_t NameLen, const uint64_t Value[2],

0 commit comments

Comments
 (0)