Skip to content

Commit f7feedd

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

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/dwarf_const.rs

+8
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ pub(crate) const DW_ATE_float: c_uint = 0x04;
1616
pub(crate) const DW_ATE_signed: c_uint = 0x05;
1717
pub(crate) const DW_ATE_unsigned: c_uint = 0x07;
1818
pub(crate) const DW_ATE_UTF: c_uint = 0x10;
19+
20+
// DWARF expression operators
21+
22+
pub(crate) const DW_OP_deref: u64 = 0x06;
23+
pub(crate) const DW_OP_plus_uconst: u64 = 0x23;
24+
/// Defined by LLVM in `llvm/include/llvm/BinaryFormat/Dwarf.h`.
25+
/// Double-checked by a static assertion in `RustWrapper.cpp`.
26+
pub(crate) const DW_OP_LLVM_fragment: u64 = 0x1000;

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
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-3
Original file line numberDiff line numberDiff line change
@@ -2171,9 +2171,6 @@ unsafe extern "C" {
21712171
Location: &'a DILocation,
21722172
BD: c_uint,
21732173
) -> Option<&'a DILocation>;
2174-
pub fn LLVMRustDIBuilderCreateOpDeref() -> u64;
2175-
pub fn LLVMRustDIBuilderCreateOpPlusUconst() -> u64;
2176-
pub fn LLVMRustDIBuilderCreateOpLLVMFragment() -> u64;
21772174

21782175
pub fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
21792176
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+6-12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ 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 verify
58+
// that the hard-coded value in `dwarf_const.rs` still agrees with LLVM.
59+
static_assert(
60+
dwarf::DW_OP_LLVM_fragment == 0x1000,
61+
"DW_OP_LLVM_fragment has changed from the value used by Rust-side code");
62+
5763
// LLVMAtomicOrdering is already an enum - don't create another
5864
// one.
5965
static AtomicOrdering fromRust(LLVMAtomicOrdering Ordering) {
@@ -1390,18 +1396,6 @@ LLVMRustDILocationCloneWithBaseDiscriminator(LLVMMetadataRef Location,
13901396
return wrap(NewLoc.has_value() ? NewLoc.value() : nullptr);
13911397
}
13921398

1393-
extern "C" uint64_t LLVMRustDIBuilderCreateOpDeref() {
1394-
return dwarf::DW_OP_deref;
1395-
}
1396-
1397-
extern "C" uint64_t LLVMRustDIBuilderCreateOpPlusUconst() {
1398-
return dwarf::DW_OP_plus_uconst;
1399-
}
1400-
1401-
extern "C" uint64_t LLVMRustDIBuilderCreateOpLLVMFragment() {
1402-
return dwarf::DW_OP_LLVM_fragment;
1403-
}
1404-
14051399
extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
14061400
auto OS = RawRustStringOstream(Str);
14071401
unwrap<llvm::Type>(Ty)->print(OS);

0 commit comments

Comments
 (0)