Skip to content

Commit 717450a

Browse files
authored
Unrolled build for rust-lang#131962
Rollup merge of rust-lang#131962 - Zalathar:llvm-set-section, r=Swatinem,workingjubilee Make `llvm::set_section` take a `&CStr` There's no reason to convert the section name to an intermediate `String`, when the LLVM-C API wants a C string anyway. Follow-up to rust-lang#131876.
2 parents bfab34a + 3310419 commit 717450a

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ffi::CStr;
2+
13
use itertools::Itertools as _;
24
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods, ConstCodegenMethods};
35
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
@@ -305,7 +307,7 @@ fn generate_coverage_map<'ll>(
305307
/// specific, well-known section and name.
306308
fn save_function_record(
307309
cx: &CodegenCx<'_, '_>,
308-
covfun_section_name: &str,
310+
covfun_section_name: &CStr,
309311
mangled_function_name: &str,
310312
source_hash: u64,
311313
filenames_ref: u64,

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::cell::RefCell;
2-
use std::ffi::CString;
2+
use std::ffi::{CStr, CString};
33

44
use libc::c_uint;
55
use rustc_codegen_ssa::traits::{
@@ -292,10 +292,10 @@ pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>(
292292
.unwrap();
293293
debug!("covmap var name: {:?}", covmap_var_name);
294294

295-
let covmap_section_name = llvm::build_string(|s| unsafe {
295+
let covmap_section_name = CString::new(llvm::build_byte_buffer(|s| unsafe {
296296
llvm::LLVMRustCoverageWriteMapSectionNameToString(cx.llmod, s);
297-
})
298-
.expect("Rust Coverage section name failed UTF-8 conversion");
297+
}))
298+
.expect("covmap section name should not contain NUL");
299299
debug!("covmap section name: {:?}", covmap_section_name);
300300

301301
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
@@ -310,7 +310,7 @@ pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>(
310310

311311
pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
312312
cx: &CodegenCx<'ll, 'tcx>,
313-
covfun_section_name: &str,
313+
covfun_section_name: &CStr,
314314
func_name_hash: u64,
315315
func_record_val: &'ll llvm::Value,
316316
is_used: bool,
@@ -354,9 +354,9 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
354354
/// - `__llvm_covfun` on Linux
355355
/// - `__LLVM_COV,__llvm_covfun` on macOS (includes `__LLVM_COV,` segment prefix)
356356
/// - `.lcovfun$M` on Windows (includes `$M` sorting suffix)
357-
pub(crate) fn covfun_section_name(cx: &CodegenCx<'_, '_>) -> String {
358-
llvm::build_string(|s| unsafe {
357+
pub(crate) fn covfun_section_name(cx: &CodegenCx<'_, '_>) -> CString {
358+
CString::new(llvm::build_byte_buffer(|s| unsafe {
359359
llvm::LLVMRustCoverageWriteFuncSectionNameToString(cx.llmod, s);
360-
})
361-
.expect("Rust Coverage function record section name failed UTF-8 conversion")
360+
}))
361+
.expect("covfun section name should not contain NUL")
362362
}

compiler/rustc_codegen_llvm/src/llvm/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ impl MemoryEffects {
210210
}
211211
}
212212

213-
pub fn set_section(llglobal: &Value, section_name: &str) {
214-
let section_name_cstr = CString::new(section_name).expect("unexpected CString error");
213+
pub fn set_section(llglobal: &Value, section_name: &CStr) {
215214
unsafe {
216-
LLVMSetSection(llglobal, section_name_cstr.as_ptr());
215+
LLVMSetSection(llglobal, section_name.as_ptr());
217216
}
218217
}
219218

0 commit comments

Comments
 (0)