Skip to content

Commit 3fd6b5b

Browse files
authored
Unrolled build for #156746
Rollup merge of #156746 - Zalathar:llvm-debuginfo, r=jieyouxu cg_llvm: Use `LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision` - Part of #134001 - Follow-up to #146763 --- This PR replaces our custom `LLVMRustDIBuilderCreateEnumerator` FFI binding with an equivalent LLVM-C binding to `LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision`, which was introduced in LLVM 21. I have also added comments to the remaining `LLVMRustDIBuilder` functions explaining why they currently can't use an LLVM-C binding, and noted some other functions that can use LLVM-C bindings in the future.
2 parents b5e038d + d59cae8 commit 3fd6b5b

3 files changed

Lines changed: 50 additions & 33 deletions

File tree

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::debuginfo::metadata::{
2121
file_metadata_from_def_id, type_di_node, unknown_file_metadata,
2222
};
2323
use crate::debuginfo::utils::{DIB, create_DIArray, get_namespace_for_item};
24-
use crate::llvm;
2524
use crate::llvm::debuginfo::{DIFlags, DIType};
25+
use crate::llvm::{self, ToLlvmBool};
2626

2727
mod cpp_like;
2828
mod native;
@@ -111,16 +111,23 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
111111
let (size, align) = cx.size_and_align_of(base_type);
112112

113113
let enumerator_di_nodes: SmallVec<Option<&'ll DIType>> = enumerators
114-
.map(|(name, value)| unsafe {
115-
let value = [value as u64, (value >> 64) as u64];
116-
Some(llvm::LLVMRustDIBuilderCreateEnumerator(
117-
DIB(cx),
118-
name.as_c_char_ptr(),
119-
name.len(),
120-
value.as_ptr(),
121-
size.bits() as libc::c_uint,
122-
is_unsigned,
123-
))
114+
.map(|(name, value)| {
115+
let value_words = [value as u64, (value >> 64) as u64];
116+
let size_in_bits = size.bits();
117+
// LLVM computes `NumWords = (SizeInBits + 63) / 64`.
118+
assert!((size_in_bits + 63) / 64 <= value_words.len() as u64);
119+
120+
let enumerator = unsafe {
121+
llvm::LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision(
122+
DIB(cx),
123+
name.as_ptr(),
124+
name.len(),
125+
size_in_bits,
126+
value_words.as_ptr(),
127+
is_unsigned.to_llvm_bool(),
128+
)
129+
};
130+
Some(enumerator)
124131
})
125132
.collect();
126133

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ 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, DILocation,
26-
DISPFlags, DIScope, DISubprogram, DITemplateTypeParameter, DIType, DebugEmissionKind,
27-
DebugNameTableKind,
25+
DIArray, DIBuilder, DIDerivedType, DIDescriptor, DIFile, DIFlags, DILocation, DISPFlags,
26+
DIScope, DISubprogram, DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind,
2827
};
2928
use crate::llvm::MetadataKindId;
3029
use crate::{TryFromU32, llvm};
@@ -752,7 +751,6 @@ pub(crate) mod debuginfo {
752751
pub(crate) type DICompositeType = DIDerivedType;
753752
pub(crate) type DIVariable = DIDescriptor;
754753
pub(crate) type DIArray = DIDescriptor;
755-
pub(crate) type DIEnumerator = DIDescriptor;
756754
pub(crate) type DITemplateTypeParameter = DIDescriptor;
757755

758756
bitflags! {
@@ -1794,6 +1792,15 @@ unsafe extern "C" {
17941792
Flags: DIFlags, // (default is `DIFlags::DIFlagZero`)
17951793
) -> &'ll Metadata;
17961794

1795+
pub(crate) fn LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision<'ll>(
1796+
Builder: &DIBuilder<'ll>,
1797+
Name: *const c_uchar, // See "PTR_LEN_STR".
1798+
NameLen: size_t,
1799+
SizeInBits: u64,
1800+
Words: *const u64, // LLVM computes `NumWords = (SizeInBits + 63) / 64`.
1801+
IsUnsigned: llvm::Bool,
1802+
) -> &'ll Metadata;
1803+
17971804
pub(crate) fn LLVMDIBuilderCreateUnionType<'ll>(
17981805
Builder: &DIBuilder<'ll>,
17991806
Scope: Option<&'ll Metadata>,
@@ -1989,6 +1996,7 @@ unsafe extern "C" {
19891996
pub(crate) fn LLVMRustDisableSystemDialogsOnCrash();
19901997

19911998
// Operations on all values
1999+
/// FIXME: After dropping LLVM 21, migrate to LLVM-C's `LLVMGlobalAddMetadata`.
19922000
pub(crate) fn LLVMRustGlobalAddMetadata<'a>(
19932001
Val: &'a Value,
19942002
KindID: MetadataKindId,
@@ -2058,6 +2066,7 @@ unsafe extern "C" {
20582066
) -> &Attribute;
20592067

20602068
// Operations on functions
2069+
/// FIXME: After dropping LLVM 21, migrate to LLVM-C's `LLVMGetOrInsertFunction`.
20612070
pub(crate) fn LLVMRustGetOrInsertFunction<'a>(
20622071
M: &'a Module,
20632072
Name: *const c_char,
@@ -2208,6 +2217,9 @@ unsafe extern "C" {
22082217
ValueLen: size_t,
22092218
);
22102219

2220+
/// We can't use LLVM-C's `LLVMDIBuilderCreateCompileUnit` because it hardcodes
2221+
/// `DICompileUnit::DebugNameTableKind::Default`, but we want to be able to
2222+
/// pass other values.
22112223
pub(crate) fn LLVMRustDIBuilderCreateCompileUnit<'a>(
22122224
Builder: &DIBuilder<'a>,
22132225
Lang: c_uint,
@@ -2225,6 +2237,8 @@ unsafe extern "C" {
22252237
DebugNameTableKind: DebugNameTableKind,
22262238
) -> &'a DIDescriptor;
22272239

2240+
/// We can't use LLVM-C's `LLVMDIBuilderCreateFileWithChecksum` because it
2241+
/// _requires_ a checksum, but we sometimes don't provide one.
22282242
pub(crate) fn LLVMRustDIBuilderCreateFile<'a>(
22292243
Builder: &DIBuilder<'a>,
22302244
Filename: *const c_char,
@@ -2238,6 +2252,8 @@ unsafe extern "C" {
22382252
SourceLen: size_t,
22392253
) -> &'a DIFile;
22402254

2255+
/// We can't use LLVM-C's `LLVMDIBuilderCreateFunction` because it only
2256+
/// supports a subset of `DISubprogram::DISPFlags`.
22412257
pub(crate) fn LLVMRustDIBuilderCreateFunction<'a>(
22422258
Builder: &DIBuilder<'a>,
22432259
Scope: &'a DIDescriptor,
@@ -2256,6 +2272,7 @@ unsafe extern "C" {
22562272
Decl: Option<&'a DIDescriptor>,
22572273
) -> &'a DISubprogram;
22582274

2275+
/// As of LLVM 22 there is no corresponding LLVM-C function.
22592276
pub(crate) fn LLVMRustDIBuilderCreateMethod<'a>(
22602277
Builder: &DIBuilder<'a>,
22612278
Scope: &'a DIDescriptor,
@@ -2271,6 +2288,7 @@ unsafe extern "C" {
22712288
TParam: &'a DIArray,
22722289
) -> &'a DISubprogram;
22732290

2291+
/// As of LLVM 22 there is no corresponding LLVM-C function.
22742292
pub(crate) fn LLVMRustDIBuilderCreateVariantMemberType<'a>(
22752293
Builder: &DIBuilder<'a>,
22762294
Scope: &'a DIScope,
@@ -2286,15 +2304,7 @@ unsafe extern "C" {
22862304
Ty: &'a DIType,
22872305
) -> &'a DIType;
22882306

2289-
pub(crate) fn LLVMRustDIBuilderCreateEnumerator<'a>(
2290-
Builder: &DIBuilder<'a>,
2291-
Name: *const c_char,
2292-
NameLen: size_t,
2293-
Value: *const u64,
2294-
SizeInBits: c_uint,
2295-
IsUnsigned: bool,
2296-
) -> &'a DIEnumerator;
2297-
2307+
/// As of LLVM 22 there is no corresponding LLVM-C function.
22982308
pub(crate) fn LLVMRustDIBuilderCreateEnumerationType<'a>(
22992309
Builder: &DIBuilder<'a>,
23002310
Scope: &'a DIScope,
@@ -2309,6 +2319,7 @@ unsafe extern "C" {
23092319
IsScoped: bool,
23102320
) -> &'a DIType;
23112321

2322+
/// As of LLVM 22 there is no corresponding LLVM-C function.
23122323
pub(crate) fn LLVMRustDIBuilderCreateVariantPart<'a>(
23132324
Builder: &DIBuilder<'a>,
23142325
Scope: &'a DIScope,
@@ -2325,6 +2336,7 @@ unsafe extern "C" {
23252336
UniqueIdLen: size_t,
23262337
) -> &'a DIDerivedType;
23272338

2339+
/// As of LLVM 22 there is no corresponding LLVM-C function.
23282340
pub(crate) fn LLVMRustDIBuilderCreateTemplateTypeParameter<'a>(
23292341
Builder: &DIBuilder<'a>,
23302342
Scope: Option<&'a DIScope>,
@@ -2333,13 +2345,17 @@ unsafe extern "C" {
23332345
Ty: &'a DIType,
23342346
) -> &'a DITemplateTypeParameter;
23352347

2348+
/// We can't use LLVM-C's `LLVMReplaceArrays` because it doesn't take a
2349+
/// `Params` argument.
23362350
pub(crate) fn LLVMRustDICompositeTypeReplaceArrays<'a>(
23372351
Builder: &DIBuilder<'a>,
23382352
CompositeType: &'a DIType,
23392353
Elements: Option<&'a DIArray>,
23402354
Params: Option<&'a DIArray>,
23412355
);
23422356

2357+
/// We can't use LLVM-C's `LLVMDIBuilderGetOrCreateSubrange` because it doesn't
2358+
/// call the overload that takes a `Metadata` upper bound.
23432359
pub(crate) fn LLVMRustDIGetOrCreateSubrange<'a>(
23442360
Builder: &DIBuilder<'a>,
23452361
CountNode: Option<&'a Metadata>,
@@ -2348,6 +2364,8 @@ unsafe extern "C" {
23482364
Stride: Option<&'a Metadata>,
23492365
) -> &'a Metadata;
23502366

2367+
/// We can't use LLVM-C's `LLVMDIBuilderCreateVectorType` because it doesn't
2368+
/// take a `BitStride` argument.
23512369
pub(crate) fn LLVMRustDICreateVectorType<'a>(
23522370
Builder: &DIBuilder<'a>,
23532371
Size: u64,
@@ -2357,6 +2375,7 @@ unsafe extern "C" {
23572375
BitStride: Option<&'a Metadata>,
23582376
) -> &'a Metadata;
23592377

2378+
/// As of LLVM 22 there is no corresponding LLVM-C function.
23602379
pub(crate) fn LLVMRustDILocationCloneWithBaseDiscriminator<'a>(
23612380
Location: &'a DILocation,
23622381
BD: c_uint,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,15 +1198,6 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariantMemberType(
11981198
fromRust(Flags), unwrapDI<DIType>(Ty)));
11991199
}
12001200

1201-
extern "C" LLVMMetadataRef
1202-
LLVMRustDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder, const char *Name,
1203-
size_t NameLen, const uint64_t Value[2],
1204-
unsigned SizeInBits, bool IsUnsigned) {
1205-
return wrap(unwrap(Builder)->createEnumerator(
1206-
StringRef(Name, NameLen),
1207-
APSInt(APInt(SizeInBits, ArrayRef<uint64_t>(Value, 2)), IsUnsigned)));
1208-
}
1209-
12101201
extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateEnumerationType(
12111202
LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
12121203
size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,

0 commit comments

Comments
 (0)