Skip to content

Commit 7f18cf7

Browse files
committed
[HACK] rustc_codegen_llvm: use inline asm to work around llvm.dbg.value issues.
1 parent 531dda6 commit 7f18cf7

File tree

1 file changed

+59
-4
lines changed
  • src/librustc_codegen_llvm/debuginfo

1 file changed

+59
-4
lines changed

src/librustc_codegen_llvm/debuginfo/mod.rs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::value::Value;
2424
use rustc::mir;
2525
use rustc::session::config::{self, DebugInfo};
2626
use rustc::ty::{self, Instance, InstanceDef, ParamEnv, Ty};
27+
use rustc_codegen_ssa::common::TypeKind;
2728
use rustc_codegen_ssa::debuginfo::type_names;
2829
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind};
2930
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -188,14 +189,68 @@ impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
188189
unsafe {
189190
let debug_loc = llvm::LLVMGetCurrentDebugLocation(self.llbuilder);
190191
let instr = if is_by_val {
191-
llvm::LLVMRustDIBuilderInsertDbgValueAtEnd(
192+
let llval = variable_alloca;
193+
194+
let instr = llvm::LLVMRustDIBuilderInsertDbgValueAtEnd(
192195
DIB(cx),
193-
variable_alloca,
196+
llval,
194197
dbg_var,
195198
addr_ops.as_ptr(),
196199
addr_ops.len() as c_uint,
197200
debug_loc,
198-
self.llbb())
201+
self.llbb(),
202+
);
203+
204+
// HACK(eddyb) keep the value we passed to `llvm.dbg.value`
205+
// alive if we're not optimizing, otherwise LLVM will eagerly
206+
// destroy it (and debuggers will show `<optimized out>`),
207+
// if there are no other uses forcing the value to be computed.
208+
// FIXME(#68817) implement the correct behavior in LLVM.
209+
if self.sess().opts.optimize == config::OptLevel::No {
210+
let llty = cx.val_ty(llval);
211+
212+
// Avoid creating LLVM inline asm with unsupported types,
213+
// usually ZST structs.
214+
let asm_supported = match self.type_kind(llty) {
215+
// Floats.
216+
TypeKind::Half |
217+
TypeKind::Float |
218+
TypeKind::Double |
219+
TypeKind::X86_FP80 |
220+
TypeKind::FP128 |
221+
TypeKind::PPC_FP128 |
222+
// Integers.
223+
TypeKind::Integer |
224+
TypeKind::Pointer |
225+
// Vectors.
226+
TypeKind::Vector |
227+
TypeKind::X86_MMX => true,
228+
229+
TypeKind::Void |
230+
TypeKind::Label |
231+
TypeKind::Function |
232+
TypeKind::Struct |
233+
TypeKind::Array |
234+
TypeKind::Metadata |
235+
TypeKind::Token => false,
236+
};
237+
238+
if asm_supported {
239+
let asm = SmallCStr::new("");
240+
let constraints = SmallCStr::new("X");
241+
let asm = llvm::LLVMRustInlineAsm(
242+
cx.type_func(&[llty], cx.type_void()),
243+
asm.as_ptr(),
244+
constraints.as_ptr(),
245+
llvm::False,
246+
llvm::False,
247+
llvm::AsmDialect::Att,
248+
);
249+
self.call(asm, &[llval], None);
250+
}
251+
}
252+
253+
instr
199254
} else {
200255
// FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
201256
llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
@@ -583,7 +638,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
583638
file_metadata,
584639
loc.line as c_uint,
585640
type_metadata,
586-
self.sess().opts.optimize != config::OptLevel::No,
641+
true,
587642
DIFlags::FlagZero,
588643
argument_index,
589644
align.bytes() as u32,

0 commit comments

Comments
 (0)