-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustc_codegen_llvm: give names to non-alloca variable values. #64149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ use rustc_codegen_ssa::debuginfo::{FunctionDebugContext, MirDebugScope, Variable | |
|
||
use libc::c_uint; | ||
use std::cell::RefCell; | ||
use std::ffi::CString; | ||
use std::ffi::{CStr, CString}; | ||
|
||
use syntax_pos::{self, Span, Pos}; | ||
use syntax::ast; | ||
|
@@ -224,8 +224,37 @@ impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> { | |
gdb::insert_reference_to_gdb_debug_scripts_section_global(self) | ||
} | ||
|
||
fn set_value_name(&mut self, value: &'ll Value, name: &str) { | ||
let cname = SmallCStr::new(name); | ||
fn set_var_name(&mut self, value: &'ll Value, name: impl ToString) { | ||
// Avoid wasting time if LLVM value names aren't even enabled. | ||
if self.sess().fewer_names() { | ||
return; | ||
} | ||
|
||
// Only function parameters and instructions are local to a function, | ||
// don't change the name of anything else (e.g. globals). | ||
let param_or_inst = unsafe { | ||
llvm::LLVMIsAArgument(value).is_some() || | ||
llvm::LLVMIsAInstruction(value).is_some() | ||
}; | ||
if !param_or_inst { | ||
return; | ||
} | ||
|
||
let old_name = unsafe { | ||
CStr::from_ptr(llvm::LLVMGetValueName(value)) | ||
}; | ||
match old_name.to_str() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was gonna suggest writing this match as FYI while annoyed about this I also noticed that we can use better APIs for touching LLVM value names (see #64223) but that's unrelated to this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes I forgot to bring that up! I also noticed those functions. |
||
Ok("") => {} | ||
Ok(_) => { | ||
// Avoid replacing the name if it already exists. | ||
// While we could combine the names somehow, it'd | ||
// get noisy quick, and the usefulness is dubious. | ||
return; | ||
} | ||
Err(_) => return, | ||
} | ||
|
||
let cname = CString::new(name.to_string()).unwrap(); | ||
unsafe { | ||
llvm::LLVMSetValueName(value, cname.as_ptr()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// compile-flags: -O -C no-prepopulate-passes | ||
|
||
#![crate_type = "lib"] | ||
|
||
// CHECK-LABEL: define i32 @test(i32 %a, i32 %b) | ||
#[no_mangle] | ||
pub fn test(a: u32, b: u32) -> u32 { | ||
let c = a + b; | ||
// CHECK: %c = add i32 %a, %b | ||
let d = c; | ||
let e = d * a; | ||
// CHECK-NEXT: %e = mul i32 %c, %a | ||
e | ||
// CHECK-NEXT: ret i32 %e | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My first instinct would be to make this a
bug!()
, not a silent nop. Are there any callers (now or after more debuginfo work) that might good reason need to pass in something other than an instruction or argument?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, yeah,
Constant
s (which would be a nop), or, worse,GlobalValue
s (functions/statics), which would get their mangled names replaced.That is, I'm pretty sure
let x = foo as fn();
andlet y = &STATIC;
would both allow accidental renaming offoo
's orSTATIC
's symbol names without thisreturn
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense. Well, silent nop it is then.