Skip to content

Commit 5a484a1

Browse files
committed
gave unused_fn WeakAnyLinkage; moved create_pgo_func_name_var
The sample json5format tests produce coverage results again (and work with opt-level 3!)
1 parent bcf7555 commit 5a484a1

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,12 @@ impl CoverageInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
6767
let mut pgo_func_name_var_map = coverage_context.pgo_func_name_var_map.borrow_mut();
6868
pgo_func_name_var_map
6969
.entry(instance)
70-
.or_insert_with(|| self.create_pgo_func_name_var(instance))
70+
.or_insert_with(|| create_pgo_func_name_var(self, instance))
7171
} else {
7272
bug!("Could not get the `coverage_context`");
7373
}
7474
}
7575

76-
/// Calls llvm::createPGOFuncNameVar() with the given function instance's
77-
/// mangled function name. The LLVM API returns an llvm::GlobalVariable
78-
/// containing the function name, with the specific variable name and
79-
/// linkage required by LLVM InstrProf source-based coverage
80-
/// instrumentation. Use `bx.get_pgo_func_name_var()` to ensure the variable
81-
/// is only created once per `Instance`.
82-
fn create_pgo_func_name_var(&self, instance: Instance<'tcx>) -> &'ll llvm::Value {
83-
let mangled_fn_name = CString::new(self.tcx.symbol_name(instance).name)
84-
.expect("error converting function name to C string");
85-
let llfn = self.get_fn(instance);
86-
unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
87-
}
88-
8976
fn define_unused_fn(&self, def_id: DefId) {
9077
let instance = declare_unused_fn(self, &def_id);
9178
codegen_unused_fn_and_counter(self, instance);
@@ -210,10 +197,8 @@ fn declare_unused_fn(cx: &CodegenCx<'ll, 'tcx>, def_id: &DefId) -> Instance<'tcx
210197
),
211198
);
212199

213-
unsafe {
214-
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
215-
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
216-
}
200+
llvm::set_linkage(llfn, llvm::Linkage::WeakAnyLinkage);
201+
llvm::set_visibility(llfn, llvm::Visibility::Hidden);
217202

218203
cx.instances.borrow_mut().insert(instance, llfn);
219204

@@ -261,6 +246,22 @@ fn add_function_coverage(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>, de
261246
}
262247
}
263248

249+
/// Calls llvm::createPGOFuncNameVar() with the given function instance's
250+
/// mangled function name. The LLVM API returns an llvm::GlobalVariable
251+
/// containing the function name, with the specific variable name and linkage
252+
/// required by LLVM InstrProf source-based coverage instrumentation. Use
253+
/// `bx.get_pgo_func_name_var()` to ensure the variable is only created once per
254+
/// `Instance`.
255+
fn create_pgo_func_name_var(
256+
cx: &CodegenCx<'ll, 'tcx>,
257+
instance: Instance<'tcx>,
258+
) -> &'ll llvm::Value {
259+
let mangled_fn_name = CString::new(cx.tcx.symbol_name(instance).name)
260+
.expect("error converting function name to C string");
261+
let llfn = cx.get_fn(instance);
262+
unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
263+
}
264+
264265
pub(crate) fn write_filenames_section_to_buffer<'a>(
265266
filenames: impl IntoIterator<Item = &'a CString>,
266267
buffer: &RustString,

compiler/rustc_codegen_ssa/src/traits/coverageinfo.rs

-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ pub trait CoverageInfoMethods<'tcx>: BackendTypes {
2929
/// `instrprof.increment()`. The `Value` is only created once per instance.
3030
/// Multiple invocations with the same instance return the same `Value`.
3131
fn get_pgo_func_name_var(&self, instance: Instance<'tcx>) -> Self::Value;
32-
33-
/// Creates a new PGO function name variable. This should only be called
34-
/// to fill in the unused function names array.
35-
fn create_pgo_func_name_var(&self, instance: Instance<'tcx>) -> Self::Value;
3632
}
3733

3834
pub trait CoverageInfoBuilderMethods<'tcx>: BackendTypes {

0 commit comments

Comments
 (0)