2020-07-26 17:11:30 +00:00
|
|
|
#include "LLVMWrapper.h"
|
2024-04-03 23:55:20 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2020-07-02 18:27:15 +00:00
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
|
|
|
|
#include "llvm/ProfileData/Coverage/CoverageMappingWriter.h"
|
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2023-05-08 03:57:20 +00:00
|
|
|
// FFI equivalent of enum `llvm::coverage::Counter::CounterKind`
|
|
|
|
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L97-L99
|
|
|
|
enum class LLVMRustCounterKind {
|
|
|
|
Zero = 0,
|
|
|
|
CounterValueReference = 1,
|
|
|
|
Expression = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
// FFI equivalent of struct `llvm::coverage::Counter`
|
|
|
|
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L94-L149
|
|
|
|
struct LLVMRustCounter {
|
|
|
|
LLVMRustCounterKind CounterKind;
|
|
|
|
uint32_t ID;
|
|
|
|
};
|
|
|
|
|
|
|
|
static coverage::Counter fromRust(LLVMRustCounter Counter) {
|
|
|
|
switch (Counter.CounterKind) {
|
|
|
|
case LLVMRustCounterKind::Zero:
|
|
|
|
return coverage::Counter::getZero();
|
|
|
|
case LLVMRustCounterKind::CounterValueReference:
|
|
|
|
return coverage::Counter::getCounter(Counter.ID);
|
|
|
|
case LLVMRustCounterKind::Expression:
|
|
|
|
return coverage::Counter::getExpression(Counter.ID);
|
|
|
|
}
|
|
|
|
report_fatal_error("Bad LLVMRustCounterKind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// FFI equivalent of enum `llvm::coverage::CounterMappingRegion::RegionKind`
|
|
|
|
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L213-L234
|
|
|
|
enum class LLVMRustCounterMappingRegionKind {
|
|
|
|
CodeRegion = 0,
|
|
|
|
ExpansionRegion = 1,
|
|
|
|
SkippedRegion = 2,
|
|
|
|
GapRegion = 3,
|
|
|
|
BranchRegion = 4,
|
2024-04-19 02:53:04 +00:00
|
|
|
MCDCDecisionRegion = 5,
|
|
|
|
MCDCBranchRegion = 6
|
2023-05-08 03:57:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static coverage::CounterMappingRegion::RegionKind
|
|
|
|
fromRust(LLVMRustCounterMappingRegionKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case LLVMRustCounterMappingRegionKind::CodeRegion:
|
|
|
|
return coverage::CounterMappingRegion::CodeRegion;
|
|
|
|
case LLVMRustCounterMappingRegionKind::ExpansionRegion:
|
|
|
|
return coverage::CounterMappingRegion::ExpansionRegion;
|
|
|
|
case LLVMRustCounterMappingRegionKind::SkippedRegion:
|
|
|
|
return coverage::CounterMappingRegion::SkippedRegion;
|
|
|
|
case LLVMRustCounterMappingRegionKind::GapRegion:
|
|
|
|
return coverage::CounterMappingRegion::GapRegion;
|
|
|
|
case LLVMRustCounterMappingRegionKind::BranchRegion:
|
|
|
|
return coverage::CounterMappingRegion::BranchRegion;
|
2024-04-19 02:53:04 +00:00
|
|
|
#if LLVM_VERSION_GE(18, 0)
|
|
|
|
case LLVMRustCounterMappingRegionKind::MCDCDecisionRegion:
|
|
|
|
return coverage::CounterMappingRegion::MCDCDecisionRegion;
|
|
|
|
case LLVMRustCounterMappingRegionKind::MCDCBranchRegion:
|
|
|
|
return coverage::CounterMappingRegion::MCDCBranchRegion;
|
|
|
|
#else
|
|
|
|
case LLVMRustCounterMappingRegionKind::MCDCDecisionRegion:
|
|
|
|
break;
|
|
|
|
case LLVMRustCounterMappingRegionKind::MCDCBranchRegion:
|
|
|
|
break;
|
|
|
|
#endif
|
2023-05-08 03:57:20 +00:00
|
|
|
}
|
|
|
|
report_fatal_error("Bad LLVMRustCounterMappingRegionKind!");
|
|
|
|
}
|
|
|
|
|
2024-04-19 02:53:04 +00:00
|
|
|
enum LLVMRustMCDCParametersTag {
|
|
|
|
None = 0,
|
|
|
|
Decision = 1,
|
|
|
|
Branch = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LLVMRustMCDCDecisionParameters {
|
|
|
|
uint32_t BitmapIdx;
|
|
|
|
uint16_t NumConditions;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LLVMRustMCDCBranchParameters {
|
|
|
|
int16_t ConditionID;
|
|
|
|
int16_t ConditionIDs[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LLVMRustMCDCParameters {
|
|
|
|
LLVMRustMCDCParametersTag Tag;
|
|
|
|
LLVMRustMCDCDecisionParameters DecisionParameters;
|
|
|
|
LLVMRustMCDCBranchParameters BranchParameters;
|
|
|
|
};
|
|
|
|
|
|
|
|
// LLVM representations for `MCDCParameters` evolved from LLVM 18 to 19.
|
|
|
|
// Look at representations in 18
|
|
|
|
// https://github.com/rust-lang/llvm-project/blob/66a2881a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L253-L263
|
|
|
|
// and representations in 19
|
|
|
|
// https://github.com/llvm/llvm-project/blob/843cc474faefad1d639f4c44c1cf3ad7dbda76c8/llvm/include/llvm/ProfileData/Coverage/MCDCTypes.h
|
|
|
|
#if LLVM_VERSION_GE(18, 0) && LLVM_VERSION_LT(19, 0)
|
|
|
|
static coverage::CounterMappingRegion::MCDCParameters
|
|
|
|
fromRust(LLVMRustMCDCParameters Params) {
|
|
|
|
auto parameter = coverage::CounterMappingRegion::MCDCParameters{};
|
|
|
|
switch (Params.Tag) {
|
|
|
|
case LLVMRustMCDCParametersTag::None:
|
|
|
|
return parameter;
|
|
|
|
case LLVMRustMCDCParametersTag::Decision:
|
|
|
|
parameter.BitmapIdx =
|
|
|
|
static_cast<unsigned>(Params.DecisionParameters.BitmapIdx),
|
|
|
|
parameter.NumConditions =
|
|
|
|
static_cast<unsigned>(Params.DecisionParameters.NumConditions);
|
|
|
|
return parameter;
|
|
|
|
case LLVMRustMCDCParametersTag::Branch:
|
|
|
|
parameter.ID = static_cast<coverage::CounterMappingRegion::MCDCConditionID>(
|
|
|
|
Params.BranchParameters.ConditionID),
|
|
|
|
parameter.FalseID =
|
|
|
|
static_cast<coverage::CounterMappingRegion::MCDCConditionID>(
|
|
|
|
Params.BranchParameters.ConditionIDs[0]),
|
|
|
|
parameter.TrueID =
|
|
|
|
static_cast<coverage::CounterMappingRegion::MCDCConditionID>(
|
|
|
|
Params.BranchParameters.ConditionIDs[1]);
|
|
|
|
return parameter;
|
|
|
|
}
|
|
|
|
report_fatal_error("Bad LLVMRustMCDCParametersTag!");
|
|
|
|
}
|
|
|
|
#elif LLVM_VERSION_GE(19, 0)
|
|
|
|
static coverage::mcdc::Parameters fromRust(LLVMRustMCDCParameters Params) {
|
|
|
|
switch (Params.Tag) {
|
|
|
|
case LLVMRustMCDCParametersTag::None:
|
|
|
|
return std::monostate();
|
|
|
|
case LLVMRustMCDCParametersTag::Decision:
|
|
|
|
return coverage::mcdc::DecisionParameters(
|
|
|
|
Params.DecisionParameters.BitmapIdx,
|
|
|
|
Params.DecisionParameters.NumConditions);
|
|
|
|
case LLVMRustMCDCParametersTag::Branch:
|
|
|
|
return coverage::mcdc::BranchParameters(
|
|
|
|
static_cast<coverage::mcdc::ConditionID>(
|
|
|
|
Params.BranchParameters.ConditionID),
|
|
|
|
{static_cast<coverage::mcdc::ConditionID>(
|
|
|
|
Params.BranchParameters.ConditionIDs[0]),
|
|
|
|
static_cast<coverage::mcdc::ConditionID>(
|
|
|
|
Params.BranchParameters.ConditionIDs[1])});
|
|
|
|
}
|
|
|
|
report_fatal_error("Bad LLVMRustMCDCParametersTag!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-05-08 03:57:20 +00:00
|
|
|
// FFI equivalent of struct `llvm::coverage::CounterMappingRegion`
|
|
|
|
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L211-L304
|
2021-02-20 15:43:05 +00:00
|
|
|
struct LLVMRustCounterMappingRegion {
|
2023-05-08 03:57:20 +00:00
|
|
|
LLVMRustCounter Count;
|
|
|
|
LLVMRustCounter FalseCount;
|
2024-04-19 02:53:04 +00:00
|
|
|
LLVMRustMCDCParameters MCDCParameters;
|
2021-02-20 15:43:05 +00:00
|
|
|
uint32_t FileID;
|
|
|
|
uint32_t ExpandedFileID;
|
|
|
|
uint32_t LineStart;
|
|
|
|
uint32_t ColumnStart;
|
|
|
|
uint32_t LineEnd;
|
|
|
|
uint32_t ColumnEnd;
|
2023-05-08 03:57:20 +00:00
|
|
|
LLVMRustCounterMappingRegionKind Kind;
|
|
|
|
};
|
|
|
|
|
|
|
|
// FFI equivalent of enum `llvm::coverage::CounterExpression::ExprKind`
|
|
|
|
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L154
|
|
|
|
enum class LLVMRustCounterExprKind {
|
|
|
|
Subtract = 0,
|
|
|
|
Add = 1,
|
2021-02-20 15:43:05 +00:00
|
|
|
};
|
|
|
|
|
2023-05-08 03:57:20 +00:00
|
|
|
// FFI equivalent of struct `llvm::coverage::CounterExpression`
|
|
|
|
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L151-L160
|
|
|
|
struct LLVMRustCounterExpression {
|
|
|
|
LLVMRustCounterExprKind Kind;
|
|
|
|
LLVMRustCounter LHS;
|
|
|
|
LLVMRustCounter RHS;
|
|
|
|
};
|
|
|
|
|
|
|
|
static coverage::CounterExpression::ExprKind
|
|
|
|
fromRust(LLVMRustCounterExprKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case LLVMRustCounterExprKind::Subtract:
|
|
|
|
return coverage::CounterExpression::Subtract;
|
|
|
|
case LLVMRustCounterExprKind::Add:
|
|
|
|
return coverage::CounterExpression::Add;
|
|
|
|
}
|
|
|
|
report_fatal_error("Bad LLVMRustCounterExprKind!");
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
|
2024-04-03 23:55:20 +00:00
|
|
|
const char *const Filenames[], size_t FilenamesLen, // String start pointers
|
|
|
|
const size_t *const Lengths, size_t LengthsLen, // Corresponding lengths
|
2020-07-02 18:27:15 +00:00
|
|
|
RustStringRef BufferOut) {
|
2023-07-24 07:27:29 +00:00
|
|
|
if (FilenamesLen != LengthsLen) {
|
|
|
|
report_fatal_error(
|
|
|
|
"Mismatched lengths in LLVMRustCoverageWriteFilenamesSectionToBuffer");
|
|
|
|
}
|
|
|
|
|
2024-04-03 23:55:20 +00:00
|
|
|
SmallVector<std::string, 32> FilenameRefs;
|
2023-07-24 07:27:29 +00:00
|
|
|
FilenameRefs.reserve(FilenamesLen);
|
2021-03-16 20:32:00 +00:00
|
|
|
for (size_t i = 0; i < FilenamesLen; i++) {
|
2023-07-24 07:27:29 +00:00
|
|
|
FilenameRefs.emplace_back(Filenames[i], Lengths[i]);
|
2021-03-16 20:32:00 +00:00
|
|
|
}
|
2024-04-03 23:55:20 +00:00
|
|
|
auto FilenamesWriter = coverage::CoverageFilenamesSectionWriter(
|
|
|
|
ArrayRef<std::string>(FilenameRefs));
|
2024-03-06 04:04:24 +00:00
|
|
|
auto OS = RawRustStringOstream(BufferOut);
|
2020-11-23 20:56:07 +00:00
|
|
|
FilenamesWriter.write(OS);
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void LLVMRustCoverageWriteMappingToBuffer(
|
2024-04-03 23:55:20 +00:00
|
|
|
const unsigned *VirtualFileMappingIDs, unsigned NumVirtualFileMappingIDs,
|
|
|
|
const LLVMRustCounterExpression *RustExpressions, unsigned NumExpressions,
|
2023-05-09 08:38:47 +00:00
|
|
|
const LLVMRustCounterMappingRegion *RustMappingRegions,
|
2024-04-03 23:55:20 +00:00
|
|
|
unsigned NumMappingRegions, RustStringRef BufferOut) {
|
2021-02-20 15:43:05 +00:00
|
|
|
// Convert from FFI representation to LLVM representation.
|
|
|
|
SmallVector<coverage::CounterMappingRegion, 0> MappingRegions;
|
|
|
|
MappingRegions.reserve(NumMappingRegions);
|
2023-01-11 15:39:01 +00:00
|
|
|
for (const auto &Region : ArrayRef<LLVMRustCounterMappingRegion>(
|
|
|
|
RustMappingRegions, NumMappingRegions)) {
|
2021-02-20 15:43:05 +00:00
|
|
|
MappingRegions.emplace_back(
|
2023-05-08 03:57:20 +00:00
|
|
|
fromRust(Region.Count), fromRust(Region.FalseCount),
|
2024-02-13 14:15:14 +00:00
|
|
|
#if LLVM_VERSION_GE(18, 0) && LLVM_VERSION_LT(19, 0)
|
2024-04-19 02:53:04 +00:00
|
|
|
// LLVM 19 may move this argument to last.
|
|
|
|
fromRust(Region.MCDCParameters),
|
2023-12-14 15:02:23 +00:00
|
|
|
#endif
|
2024-04-03 23:55:20 +00:00
|
|
|
Region.FileID, Region.ExpandedFileID, // File IDs, then region info.
|
2021-02-20 15:43:05 +00:00
|
|
|
Region.LineStart, Region.ColumnStart, Region.LineEnd, Region.ColumnEnd,
|
2023-05-08 03:57:20 +00:00
|
|
|
fromRust(Region.Kind));
|
2021-02-20 15:43:05 +00:00
|
|
|
}
|
2023-05-08 03:57:20 +00:00
|
|
|
|
|
|
|
std::vector<coverage::CounterExpression> Expressions;
|
|
|
|
Expressions.reserve(NumExpressions);
|
|
|
|
for (const auto &Expression :
|
|
|
|
ArrayRef<LLVMRustCounterExpression>(RustExpressions, NumExpressions)) {
|
|
|
|
Expressions.emplace_back(fromRust(Expression.Kind),
|
|
|
|
fromRust(Expression.LHS),
|
|
|
|
fromRust(Expression.RHS));
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
auto CoverageMappingWriter = coverage::CoverageMappingWriter(
|
2023-01-11 15:39:01 +00:00
|
|
|
ArrayRef<unsigned>(VirtualFileMappingIDs, NumVirtualFileMappingIDs),
|
2024-04-03 23:55:20 +00:00
|
|
|
Expressions, MappingRegions);
|
2024-03-06 04:04:24 +00:00
|
|
|
auto OS = RawRustStringOstream(BufferOut);
|
2020-07-02 18:27:15 +00:00
|
|
|
CoverageMappingWriter.write(OS);
|
|
|
|
}
|
|
|
|
|
2024-04-03 23:55:20 +00:00
|
|
|
extern "C" LLVMValueRef
|
|
|
|
LLVMRustCoverageCreatePGOFuncNameVar(LLVMValueRef F, const char *FuncName,
|
|
|
|
size_t FuncNameLen) {
|
2024-03-06 04:04:24 +00:00
|
|
|
auto FuncNameRef = StringRef(FuncName, FuncNameLen);
|
LLVM IR coverage encoding aligns closer to Clang's
I found some areas for improvement while attempting to debug the
SegFault issue when running rust programs compiled using MSVC, with
coverage instrumentation.
I discovered that LLVM's coverage writer was generating incomplete
function name variable names (that's not a typo: the name of the
variable that holds a function name).
The existing implementation used one-up numbers to distinguish
variables, and correcting the names did not fix the MSVC coverage bug,
but the fix in this PR makes the names and resulting LLVM IR easier to
follow and more consistent with Clang's implementation.
I also changed the way the `-Zinstrument-coverage` option is supported
in symbol_export.rs. The original implementation was incorrect, and the
corrected version matches the handling for `-Zprofile-generate`, as it
turns out.
(An argument could be made that maybe `-Zinstrument-coverage` should
automatically enable `-Cprofile-generate`. In fact, if
`-Cprofile-generate` is analagous to Clang's `-fprofile-generate`, as
some documentation implies, Clang always requires this flag for its
implementation of source-based code coverage. This would require a
little more validation, and if implemented, would probably require
updating some of the user-facing messages related to
`-Cprofile-generate` to not be so specific to the PGO use case.)
None of these changes fixed the MSVC coverage problems, but they should
still be welcome improvements.
Lastly, I added some additional FIXME comments in instrument_coverage.rs
describing issues I found with the generated LLVM IR that would be
resolved if the coverage instrumentation is injected with a `Statement`
instead of as a new `BasicBlock`. I describe seven advantages of this
change, but it requires some discussion before making a change like
this.
2020-08-06 05:53:11 +00:00
|
|
|
return wrap(createPGOFuncNameVar(*cast<Function>(unwrap(F)), FuncNameRef));
|
|
|
|
}
|
|
|
|
|
2024-04-03 23:55:20 +00:00
|
|
|
extern "C" uint64_t LLVMRustCoverageHashByteArray(const char *Bytes,
|
|
|
|
size_t NumBytes) {
|
2024-03-06 04:04:24 +00:00
|
|
|
auto StrRef = StringRef(Bytes, NumBytes);
|
2020-11-23 20:56:07 +00:00
|
|
|
return IndexedInstrProf::ComputeHash(StrRef);
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 23:55:20 +00:00
|
|
|
static void WriteSectionNameToString(LLVMModuleRef M, InstrProfSectKind SK,
|
2020-11-23 20:56:07 +00:00
|
|
|
RustStringRef Str) {
|
2024-03-06 04:04:24 +00:00
|
|
|
auto TargetTriple = Triple(unwrap(M)->getTargetTriple());
|
2020-11-23 20:56:07 +00:00
|
|
|
auto name = getInstrProfSectionName(SK, TargetTriple.getObjectFormat());
|
2024-03-06 04:04:24 +00:00
|
|
|
auto OS = RawRustStringOstream(Str);
|
2020-07-02 18:27:15 +00:00
|
|
|
OS << name;
|
|
|
|
}
|
|
|
|
|
2020-11-23 20:56:07 +00:00
|
|
|
extern "C" void LLVMRustCoverageWriteMapSectionNameToString(LLVMModuleRef M,
|
|
|
|
RustStringRef Str) {
|
|
|
|
WriteSectionNameToString(M, IPSK_covmap, Str);
|
|
|
|
}
|
|
|
|
|
2024-04-03 23:55:20 +00:00
|
|
|
extern "C" void
|
|
|
|
LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M,
|
|
|
|
RustStringRef Str) {
|
2020-11-23 20:56:07 +00:00
|
|
|
WriteSectionNameToString(M, IPSK_covfun, Str);
|
|
|
|
}
|
|
|
|
|
2020-07-02 18:27:15 +00:00
|
|
|
extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
|
|
|
|
auto name = getCoverageMappingVarName();
|
2024-03-06 04:04:24 +00:00
|
|
|
auto OS = RawRustStringOstream(Str);
|
2020-07-02 18:27:15 +00:00
|
|
|
OS << name;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" uint32_t LLVMRustCoverageMappingVersion() {
|
2024-02-13 12:00:49 +00:00
|
|
|
// This should always be `CurrentVersion`, because that's the version LLVM
|
|
|
|
// will use when encoding the data we give it. If for some reason we ever
|
|
|
|
// want to override the version number we _emit_, do it on the Rust side.
|
|
|
|
return coverage::CovMapVersion::CurrentVersion;
|
2020-07-02 18:27:15 +00:00
|
|
|
}
|