Skip to content

Commit 1b62645

Browse files
committed
Use constants for DWARF opcodes, instead of FFI calls
1 parent e267106 commit 1b62645

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/dwarf_const.rs

+8
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ declare_constant!(DW_ATE_float: c_uint);
2727
declare_constant!(DW_ATE_signed: c_uint);
2828
declare_constant!(DW_ATE_unsigned: c_uint);
2929
declare_constant!(DW_ATE_UTF: c_uint);
30+
31+
// DWARF expression operators.
32+
declare_constant!(DW_OP_deref: u64);
33+
declare_constant!(DW_OP_plus_uconst: u64);
34+
/// Defined by LLVM in `llvm/include/llvm/BinaryFormat/Dwarf.h`.
35+
/// Double-checked by a static assertion in `RustWrapper.cpp`.
36+
#[allow(non_upper_case_globals)]
37+
pub(crate) const DW_OP_LLVM_fragment: u64 = 0x1000;

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,26 @@ impl<'ll> DebugInfoBuilderMethods for Builder<'_, 'll, '_> {
153153
indirect_offsets: &[Size],
154154
fragment: Option<Range<Size>>,
155155
) {
156+
use dwarf_const::{DW_OP_LLVM_fragment, DW_OP_deref, DW_OP_plus_uconst};
157+
156158
// Convert the direct and indirect offsets and fragment byte range to address ops.
157-
// FIXME(eddyb) use `const`s instead of getting the values via FFI,
158-
// the values should match the ones in the DWARF standard anyway.
159-
let op_deref = || unsafe { llvm::LLVMRustDIBuilderCreateOpDeref() };
160-
let op_plus_uconst = || unsafe { llvm::LLVMRustDIBuilderCreateOpPlusUconst() };
161-
let op_llvm_fragment = || unsafe { llvm::LLVMRustDIBuilderCreateOpLLVMFragment() };
162159
let mut addr_ops = SmallVec::<[u64; 8]>::new();
163160

164161
if direct_offset.bytes() > 0 {
165-
addr_ops.push(op_plus_uconst());
162+
addr_ops.push(DW_OP_plus_uconst);
166163
addr_ops.push(direct_offset.bytes() as u64);
167164
}
168165
for &offset in indirect_offsets {
169-
addr_ops.push(op_deref());
166+
addr_ops.push(DW_OP_deref);
170167
if offset.bytes() > 0 {
171-
addr_ops.push(op_plus_uconst());
168+
addr_ops.push(DW_OP_plus_uconst);
172169
addr_ops.push(offset.bytes() as u64);
173170
}
174171
}
175172
if let Some(fragment) = fragment {
176173
// `DW_OP_LLVM_fragment` takes as arguments the fragment's
177174
// offset and size, both of them in bits.
178-
addr_ops.push(op_llvm_fragment());
175+
addr_ops.push(DW_OP_LLVM_fragment);
179176
addr_ops.push(fragment.start.bits() as u64);
180177
addr_ops.push((fragment.end - fragment.start).bits() as u64);
181178
}

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-3
Original file line numberDiff line numberDiff line change
@@ -2177,9 +2177,6 @@ unsafe extern "C" {
21772177
Location: &'a DILocation,
21782178
BD: c_uint,
21792179
) -> Option<&'a DILocation>;
2180-
pub fn LLVMRustDIBuilderCreateOpDeref() -> u64;
2181-
pub fn LLVMRustDIBuilderCreateOpPlusUconst() -> u64;
2182-
pub fn LLVMRustDIBuilderCreateOpLLVMFragment() -> u64;
21832180

21842181
pub fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
21852182
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);

Diff for: compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+4-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ using namespace llvm;
5454
using namespace llvm::sys;
5555
using namespace llvm::object;
5656

57+
// This opcode is an LLVM detail that could hypothetically change (?), so
58+
// verify that the hard-coded value in `dwarf_const.rs` still agrees with LLVM.
59+
static_assert(dwarf::DW_OP_LLVM_fragment == 0x1000);
60+
5761
// LLVMAtomicOrdering is already an enum - don't create another
5862
// one.
5963
static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) {
@@ -1397,18 +1401,6 @@ LLVMRustDILocationCloneWithBaseDiscriminator(LLVMMetadataRef Location,
13971401
return wrap(NewLoc.has_value() ? NewLoc.value() : nullptr);
13981402
}
13991403

1400-
extern "C" uint64_t LLVMRustDIBuilderCreateOpDeref() {
1401-
return dwarf::DW_OP_deref;
1402-
}
1403-
1404-
extern "C" uint64_t LLVMRustDIBuilderCreateOpPlusUconst() {
1405-
return dwarf::DW_OP_plus_uconst;
1406-
}
1407-
1408-
extern "C" uint64_t LLVMRustDIBuilderCreateOpLLVMFragment() {
1409-
return dwarf::DW_OP_LLVM_fragment;
1410-
}
1411-
14121404
extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
14131405
auto OS = RawRustStringOstream(Str);
14141406
unwrap<llvm::Type>(Ty)->print(OS);

0 commit comments

Comments
 (0)