Skip to content

Commit ffb9b61

Browse files
committed
Auto merge of rust-lang#113430 - Zalathar:hash, r=b-naber
Remove `LLVMRustCoverageHashCString` Coverage has two FFI functions for computing the hash of a byte string. One takes a ptr/len pair (`LLVMRustCoverageHashByteArray`), and the other takes a NUL-terminated C string (`LLVMRustCoverageHashCString`). But on closer inspection, the C string version is unnecessary. The calling-side code converts a Rust `&str` into a `CString`, and the C++ code then immediately turns it back into a ptr/len string before actually hashing it. So we can just call the ptr/len version directly instead. --- This PR also fixes a bug in the C++ declaration of `LLVMRustCoverageHashByteArray`. It should be `size_t`, since that's what is declared and passed on the Rust side, and it's what `StrRef`'s constructor expects to receive on the callee side.
2 parents 2c718d1 + 352d031 commit ffb9b61

File tree

4 files changed

+6
-17
lines changed

4 files changed

+6
-17
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
6363
let mut function_data = Vec::new();
6464
for (instance, function_coverage) in function_coverage_map {
6565
debug!("Generate function coverage for {}, {:?}", cx.codegen_unit.name(), instance);
66-
let mangled_function_name = tcx.symbol_name(instance).to_string();
66+
let mangled_function_name = tcx.symbol_name(instance).name;
6767
let source_hash = function_coverage.source_hash();
6868
let is_used = function_coverage.is_used();
6969
let (expressions, counter_regions) =
@@ -95,7 +95,7 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
9595

9696
let filenames_size = filenames_buffer.len();
9797
let filenames_val = cx.const_bytes(&filenames_buffer);
98-
let filenames_ref = coverageinfo::hash_bytes(filenames_buffer);
98+
let filenames_ref = coverageinfo::hash_bytes(&filenames_buffer);
9999

100100
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
101101
let cov_data_val = mapgen.generate_coverage_map(cx, version, filenames_size, filenames_val);
@@ -228,7 +228,7 @@ impl CoverageMapGenerator {
228228
/// specific, well-known section and name.
229229
fn save_function_record(
230230
cx: &CodegenCx<'_, '_>,
231-
mangled_function_name: String,
231+
mangled_function_name: &str,
232232
source_hash: u64,
233233
filenames_ref: u64,
234234
coverage_mapping_buffer: Vec<u8>,
@@ -238,7 +238,7 @@ fn save_function_record(
238238
let coverage_mapping_size = coverage_mapping_buffer.len();
239239
let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
240240

241-
let func_name_hash = coverageinfo::hash_str(&mangled_function_name);
241+
let func_name_hash = coverageinfo::hash_bytes(mangled_function_name.as_bytes());
242242
let func_name_hash_val = cx.const_u64(func_name_hash);
243243
let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
244244
let source_hash_val = cx.const_u64(source_hash);

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,7 @@ pub(crate) fn write_mapping_to_buffer(
373373
}
374374
}
375375

376-
pub(crate) fn hash_str(strval: &str) -> u64 {
377-
let strval = CString::new(strval).expect("null error converting hashable str to C string");
378-
unsafe { llvm::LLVMRustCoverageHashCString(strval.as_ptr()) }
379-
}
380-
381-
pub(crate) fn hash_bytes(bytes: Vec<u8>) -> u64 {
376+
pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
382377
unsafe { llvm::LLVMRustCoverageHashByteArray(bytes.as_ptr().cast(), bytes.len()) }
383378
}
384379

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,6 @@ extern "C" {
19161916
);
19171917

19181918
pub fn LLVMRustCoverageCreatePGOFuncNameVar(F: &Value, FuncName: *const c_char) -> &Value;
1919-
pub fn LLVMRustCoverageHashCString(StrVal: *const c_char) -> u64;
19201919
pub fn LLVMRustCoverageHashByteArray(Bytes: *const c_char, NumBytes: size_t) -> u64;
19211920

19221921
#[allow(improper_ctypes)]

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,9 @@ extern "C" LLVMValueRef LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, con
158158
return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
159159
}
160160

161-
extern "C" uint64_t LLVMRustCoverageHashCString(const char *StrVal) {
162-
StringRef StrRef(StrVal);
163-
return IndexedInstrProf::ComputeHash(StrRef);
164-
}
165-
166161
extern "C" uint64_t LLVMRustCoverageHashByteArray(
167162
const char *Bytes,
168-
unsigned NumBytes) {
163+
size_t NumBytes) {
169164
StringRef StrRef(Bytes, NumBytes);
170165
return IndexedInstrProf::ComputeHash(StrRef);
171166
}

0 commit comments

Comments
 (0)