Skip to content

Commit

Permalink
coverage: Tidy up coverage-specific FFI functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed Nov 5, 2024
1 parent 3357d50 commit 89ce0fb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
26 changes: 12 additions & 14 deletions compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@

use std::ffi::CString;

use libc::c_uint;

use crate::common::AsCCharPtr;
use crate::coverageinfo::ffi;
use crate::llvm;

pub(crate) fn covmap_var_name() -> CString {
CString::new(llvm::build_byte_buffer(|s| unsafe {
llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
llvm::LLVMRustCoverageWriteCovmapVarNameToString(s);
}))
.expect("covmap variable name should not contain NUL")
}

pub(crate) fn covmap_section_name(llmod: &llvm::Module) -> CString {
CString::new(llvm::build_byte_buffer(|s| unsafe {
llvm::LLVMRustCoverageWriteMapSectionNameToString(llmod, s);
llvm::LLVMRustCoverageWriteCovmapSectionNameToString(llmod, s);
}))
.expect("covmap section name should not contain NUL")
}

pub(crate) fn covfun_section_name(llmod: &llvm::Module) -> CString {
CString::new(llvm::build_byte_buffer(|s| unsafe {
llvm::LLVMRustCoverageWriteFuncSectionNameToString(llmod, s);
llvm::LLVMRustCoverageWriteCovfunSectionNameToString(llmod, s);
}))
.expect("covfun section name should not contain NUL")
}
Expand All @@ -51,7 +49,7 @@ pub(crate) fn write_filenames_to_buffer<'a>(
.unzip::<_, _, Vec<_>, Vec<_>>();

llvm::build_byte_buffer(|buffer| unsafe {
llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
llvm::LLVMRustCoverageWriteFilenamesToBuffer(
pointers.as_ptr(),
pointers.len(),
lengths.as_ptr(),
Expand All @@ -70,19 +68,19 @@ pub(crate) fn write_function_mappings_to_buffer(
mcdc_decision_regions: &[ffi::MCDCDecisionRegion],
) -> Vec<u8> {
llvm::build_byte_buffer(|buffer| unsafe {
llvm::LLVMRustCoverageWriteMappingToBuffer(
llvm::LLVMRustCoverageWriteFunctionMappingsToBuffer(
virtual_file_mapping.as_ptr(),
virtual_file_mapping.len() as c_uint,
virtual_file_mapping.len(),
expressions.as_ptr(),
expressions.len() as c_uint,
expressions.len(),
code_regions.as_ptr(),
code_regions.len() as c_uint,
code_regions.len(),
branch_regions.as_ptr(),
branch_regions.len() as c_uint,
branch_regions.len(),
mcdc_branch_regions.as_ptr(),
mcdc_branch_regions.len() as c_uint,
mcdc_branch_regions.len(),
mcdc_decision_regions.as_ptr(),
mcdc_decision_regions.len() as c_uint,
mcdc_decision_regions.len(),
buffer,
)
})
Expand All @@ -91,7 +89,7 @@ pub(crate) fn write_function_mappings_to_buffer(
/// Hashes some bytes into a 64-bit hash, via LLVM's `IndexedInstrProf::ComputeHash`,
/// as required for parts of the LLVM coverage mapping format.
pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
unsafe { llvm::LLVMRustCoverageHashByteArray(bytes.as_c_char_ptr(), bytes.len()) }
unsafe { llvm::LLVMRustCoverageHashBytes(bytes.as_c_char_ptr(), bytes.len()) }
}

/// Returns LLVM's `coverage::CovMapVersion::CurrentVersion` (CoverageMapping.h)
Expand Down
24 changes: 12 additions & 12 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ unsafe extern "C" {
) -> bool;

#[allow(improper_ctypes)]
pub(crate) fn LLVMRustCoverageWriteFilenamesSectionToBuffer(
pub(crate) fn LLVMRustCoverageWriteFilenamesToBuffer(
Filenames: *const *const c_char,
FilenamesLen: size_t,
Lengths: *const size_t,
Expand All @@ -1799,19 +1799,19 @@ unsafe extern "C" {
);

#[allow(improper_ctypes)]
pub(crate) fn LLVMRustCoverageWriteMappingToBuffer(
pub(crate) fn LLVMRustCoverageWriteFunctionMappingsToBuffer(
VirtualFileMappingIDs: *const c_uint,
NumVirtualFileMappingIDs: c_uint,
NumVirtualFileMappingIDs: size_t,
Expressions: *const crate::coverageinfo::ffi::CounterExpression,
NumExpressions: c_uint,
NumExpressions: size_t,
CodeRegions: *const crate::coverageinfo::ffi::CodeRegion,
NumCodeRegions: c_uint,
NumCodeRegions: size_t,
BranchRegions: *const crate::coverageinfo::ffi::BranchRegion,
NumBranchRegions: c_uint,
NumBranchRegions: size_t,
MCDCBranchRegions: *const crate::coverageinfo::ffi::MCDCBranchRegion,
NumMCDCBranchRegions: c_uint,
NumMCDCBranchRegions: size_t,
MCDCDecisionRegions: *const crate::coverageinfo::ffi::MCDCDecisionRegion,
NumMCDCDecisionRegions: c_uint,
NumMCDCDecisionRegions: size_t,
BufferOut: &RustString,
);

Expand All @@ -1820,16 +1820,16 @@ unsafe extern "C" {
FuncName: *const c_char,
FuncNameLen: size_t,
) -> &Value;
pub(crate) fn LLVMRustCoverageHashByteArray(Bytes: *const c_char, NumBytes: size_t) -> u64;
pub(crate) fn LLVMRustCoverageHashBytes(Bytes: *const c_char, NumBytes: size_t) -> u64;

#[allow(improper_ctypes)]
pub(crate) fn LLVMRustCoverageWriteMapSectionNameToString(M: &Module, Str: &RustString);
pub(crate) fn LLVMRustCoverageWriteCovmapSectionNameToString(M: &Module, OutStr: &RustString);

#[allow(improper_ctypes)]
pub(crate) fn LLVMRustCoverageWriteFuncSectionNameToString(M: &Module, Str: &RustString);
pub(crate) fn LLVMRustCoverageWriteCovfunSectionNameToString(M: &Module, OutStr: &RustString);

#[allow(improper_ctypes)]
pub(crate) fn LLVMRustCoverageWriteMappingVarNameToString(Str: &RustString);
pub(crate) fn LLVMRustCoverageWriteCovmapVarNameToString(OutStr: &RustString);

pub(crate) fn LLVMRustCoverageMappingVersion() -> u32;
pub fn LLVMRustDebugMetadataVersion() -> u32;
Expand Down
54 changes: 28 additions & 26 deletions compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ fromRust(LLVMRustCounterExprKind Kind) {
report_fatal_error("Bad LLVMRustCounterExprKind!");
}

extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
extern "C" void LLVMRustCoverageWriteFilenamesToBuffer(
const char *const Filenames[], size_t FilenamesLen, // String start pointers
const size_t *const Lengths, size_t LengthsLen, // Corresponding lengths
RustStringRef BufferOut) {
if (FilenamesLen != LengthsLen) {
report_fatal_error(
"Mismatched lengths in LLVMRustCoverageWriteFilenamesSectionToBuffer");
"Mismatched lengths in LLVMRustCoverageWriteFilenamesToBuffer");
}

SmallVector<std::string, 32> FilenameRefs;
Expand All @@ -143,16 +143,15 @@ extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
FilenamesWriter.write(OS);
}

extern "C" void LLVMRustCoverageWriteMappingToBuffer(
const unsigned *VirtualFileMappingIDs, unsigned NumVirtualFileMappingIDs,
const LLVMRustCounterExpression *RustExpressions, unsigned NumExpressions,
const LLVMRustCoverageCodeRegion *CodeRegions, unsigned NumCodeRegions,
const LLVMRustCoverageBranchRegion *BranchRegions,
unsigned NumBranchRegions,
extern "C" void LLVMRustCoverageWriteFunctionMappingsToBuffer(
const unsigned *VirtualFileMappingIDs, size_t NumVirtualFileMappingIDs,
const LLVMRustCounterExpression *RustExpressions, size_t NumExpressions,
const LLVMRustCoverageCodeRegion *CodeRegions, size_t NumCodeRegions,
const LLVMRustCoverageBranchRegion *BranchRegions, size_t NumBranchRegions,
const LLVMRustCoverageMCDCBranchRegion *MCDCBranchRegions,
unsigned NumMCDCBranchRegions,
size_t NumMCDCBranchRegions,
const LLVMRustCoverageMCDCDecisionRegion *MCDCDecisionRegions,
unsigned NumMCDCDecisionRegions, RustStringRef BufferOut) {
size_t NumMCDCDecisionRegions, RustStringRef BufferOut) {
// Convert from FFI representation to LLVM representation.

// Expressions:
Expand Down Expand Up @@ -219,34 +218,37 @@ LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, const char *FuncName,
return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
}

extern "C" uint64_t LLVMRustCoverageHashByteArray(const char *Bytes,
size_t NumBytes) {
auto StrRef = StringRef(Bytes, NumBytes);
return IndexedInstrProf::ComputeHash(StrRef);
extern "C" uint64_t LLVMRustCoverageHashBytes(const char *Bytes,
size_t NumBytes) {
return IndexedInstrProf::ComputeHash(StringRef(Bytes, NumBytes));
}

static void WriteSectionNameToString(LLVMModuleRef M, InstrProfSectKind SK,
RustStringRef Str) {
// Private helper function for getting the covmap and covfun section names.
static void writeInstrProfSectionNameToString(LLVMModuleRef M,
InstrProfSectKind SectKind,
RustStringRef OutStr) {
auto TargetTriple = Triple(unwrap(M)->getTargetTriple());
auto name = getInstrProfSectionName(SK, TargetTriple.getObjectFormat());
auto OS = RawRustStringOstream(Str);
auto name = getInstrProfSectionName(SectKind, TargetTriple.getObjectFormat());
auto OS = RawRustStringOstream(OutStr);
OS << name;
}

extern "C" void LLVMRustCoverageWriteMapSectionNameToString(LLVMModuleRef M,
RustStringRef Str) {
WriteSectionNameToString(M, IPSK_covmap, Str);
extern "C" void
LLVMRustCoverageWriteCovmapSectionNameToString(LLVMModuleRef M,
RustStringRef OutStr) {
writeInstrProfSectionNameToString(M, IPSK_covmap, OutStr);
}

extern "C" void
LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M,
RustStringRef Str) {
WriteSectionNameToString(M, IPSK_covfun, Str);
LLVMRustCoverageWriteCovfunSectionNameToString(LLVMModuleRef M,
RustStringRef OutStr) {
writeInstrProfSectionNameToString(M, IPSK_covfun, OutStr);
}

extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
extern "C" void
LLVMRustCoverageWriteCovmapVarNameToString(RustStringRef OutStr) {
auto name = getCoverageMappingVarName();
auto OS = RawRustStringOstream(Str);
auto OS = RawRustStringOstream(OutStr);
OS << name;
}

Expand Down

0 comments on commit 89ce0fb

Please sign in to comment.