Skip to content

Commit 7f93af1

Browse files
Rollup merge of #132125 - Zalathar:coverage-intrinsics, r=jieyouxu
coverage: Emit LLVM intrinsics using the normal helper method Codegen already has convenient ways to declare and emit LLVM intrinsics, so there's no need for coverage instrumentation to jump through hoops to emit them manually.
2 parents 3c6d34f + 8f07514 commit 7f93af1

File tree

7 files changed

+26
-140
lines changed

7 files changed

+26
-140
lines changed

Diff for: compiler/rustc_codegen_gcc/src/builder.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1725,16 +1725,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
17251725
fn fptosi_sat(&mut self, val: RValue<'gcc>, dest_ty: Type<'gcc>) -> RValue<'gcc> {
17261726
self.fptoint_sat(true, val, dest_ty)
17271727
}
1728-
1729-
fn instrprof_increment(
1730-
&mut self,
1731-
_fn_name: RValue<'gcc>,
1732-
_hash: RValue<'gcc>,
1733-
_num_counters: RValue<'gcc>,
1734-
_index: RValue<'gcc>,
1735-
) {
1736-
unimplemented!();
1737-
}
17381728
}
17391729

17401730
impl<'a, 'gcc, 'tcx> Builder<'a, 'gcc, 'tcx> {

Diff for: compiler/rustc_codegen_llvm/src/builder.rs

+21-79
Original file line numberDiff line numberDiff line change
@@ -1165,39 +1165,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
11651165
self.call_lifetime_intrinsic("llvm.lifetime.end.p0i8", ptr, size);
11661166
}
11671167

1168-
fn instrprof_increment(
1169-
&mut self,
1170-
fn_name: &'ll Value,
1171-
hash: &'ll Value,
1172-
num_counters: &'ll Value,
1173-
index: &'ll Value,
1174-
) {
1175-
debug!(
1176-
"instrprof_increment() with args ({:?}, {:?}, {:?}, {:?})",
1177-
fn_name, hash, num_counters, index
1178-
);
1179-
1180-
let llfn = unsafe { llvm::LLVMRustGetInstrProfIncrementIntrinsic(self.cx().llmod) };
1181-
let llty = self.cx.type_func(
1182-
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_i32()],
1183-
self.cx.type_void(),
1184-
);
1185-
let args = &[fn_name, hash, num_counters, index];
1186-
let args = self.check_call("call", llty, llfn, args);
1187-
1188-
unsafe {
1189-
let _ = llvm::LLVMRustBuildCall(
1190-
self.llbuilder,
1191-
llty,
1192-
llfn,
1193-
args.as_ptr() as *const &llvm::Value,
1194-
args.len() as c_uint,
1195-
[].as_ptr(),
1196-
0 as c_uint,
1197-
);
1198-
}
1199-
}
1200-
12011168
fn call(
12021169
&mut self,
12031170
llty: &'ll Type,
@@ -1667,6 +1634,18 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
16671634
kcfi_bundle
16681635
}
16691636

1637+
/// Emits a call to `llvm.instrprof.increment`. Used by coverage instrumentation.
1638+
#[instrument(level = "debug", skip(self))]
1639+
pub(crate) fn instrprof_increment(
1640+
&mut self,
1641+
fn_name: &'ll Value,
1642+
hash: &'ll Value,
1643+
num_counters: &'ll Value,
1644+
index: &'ll Value,
1645+
) {
1646+
self.call_intrinsic("llvm.instrprof.increment", &[fn_name, hash, num_counters, index]);
1647+
}
1648+
16701649
/// Emits a call to `llvm.instrprof.mcdc.parameters`.
16711650
///
16721651
/// This doesn't produce any code directly, but is used as input by
@@ -1676,80 +1655,43 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
16761655
///
16771656
/// [`CodeGenPGO::emitMCDCParameters`]:
16781657
/// https://github.com/rust-lang/llvm-project/blob/5399a24/clang/lib/CodeGen/CodeGenPGO.cpp#L1124
1658+
#[instrument(level = "debug", skip(self))]
16791659
pub(crate) fn mcdc_parameters(
16801660
&mut self,
16811661
fn_name: &'ll Value,
16821662
hash: &'ll Value,
16831663
bitmap_bits: &'ll Value,
16841664
) {
1685-
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bits);
1686-
16871665
assert!(
16881666
crate::llvm_util::get_version() >= (19, 0, 0),
16891667
"MCDC intrinsics require LLVM 19 or later"
16901668
);
1691-
1692-
let llfn = unsafe { llvm::LLVMRustGetInstrProfMCDCParametersIntrinsic(self.cx().llmod) };
1693-
let llty = self.cx.type_func(
1694-
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32()],
1695-
self.cx.type_void(),
1696-
);
1697-
let args = &[fn_name, hash, bitmap_bits];
1698-
let args = self.check_call("call", llty, llfn, args);
1699-
1700-
unsafe {
1701-
let _ = llvm::LLVMRustBuildCall(
1702-
self.llbuilder,
1703-
llty,
1704-
llfn,
1705-
args.as_ptr() as *const &llvm::Value,
1706-
args.len() as c_uint,
1707-
[].as_ptr(),
1708-
0 as c_uint,
1709-
);
1710-
}
1669+
self.call_intrinsic("llvm.instrprof.mcdc.parameters", &[fn_name, hash, bitmap_bits]);
17111670
}
17121671

1672+
#[instrument(level = "debug", skip(self))]
17131673
pub(crate) fn mcdc_tvbitmap_update(
17141674
&mut self,
17151675
fn_name: &'ll Value,
17161676
hash: &'ll Value,
17171677
bitmap_index: &'ll Value,
17181678
mcdc_temp: &'ll Value,
17191679
) {
1720-
debug!(
1721-
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?})",
1722-
fn_name, hash, bitmap_index, mcdc_temp
1723-
);
17241680
assert!(
17251681
crate::llvm_util::get_version() >= (19, 0, 0),
17261682
"MCDC intrinsics require LLVM 19 or later"
17271683
);
1728-
1729-
let llfn =
1730-
unsafe { llvm::LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(self.cx().llmod) };
1731-
let llty = self.cx.type_func(
1732-
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_ptr()],
1733-
self.cx.type_void(),
1734-
);
17351684
let args = &[fn_name, hash, bitmap_index, mcdc_temp];
1736-
let args = self.check_call("call", llty, llfn, args);
1737-
unsafe {
1738-
let _ = llvm::LLVMRustBuildCall(
1739-
self.llbuilder,
1740-
llty,
1741-
llfn,
1742-
args.as_ptr() as *const &llvm::Value,
1743-
args.len() as c_uint,
1744-
[].as_ptr(),
1745-
0 as c_uint,
1746-
);
1747-
}
1685+
self.call_intrinsic("llvm.instrprof.mcdc.tvbitmap.update", args);
1686+
}
1687+
1688+
#[instrument(level = "debug", skip(self))]
1689+
pub(crate) fn mcdc_condbitmap_reset(&mut self, mcdc_temp: &'ll Value) {
17481690
self.store(self.const_i32(0), mcdc_temp, self.tcx.data_layout.i32_align.abi);
17491691
}
17501692

1693+
#[instrument(level = "debug", skip(self))]
17511694
pub(crate) fn mcdc_condbitmap_update(&mut self, cond_index: &'ll Value, mcdc_temp: &'ll Value) {
1752-
debug!("mcdc_condbitmap_update() with args ({:?}, {:?})", cond_index, mcdc_temp);
17531695
assert!(
17541696
crate::llvm_util::get_version() >= (19, 0, 0),
17551697
"MCDC intrinsics require LLVM 19 or later"

Diff for: compiler/rustc_codegen_llvm/src/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,10 @@ impl<'ll> CodegenCx<'ll, '_> {
10991099

11001100
if self.sess().instrument_coverage() {
11011101
ifn!("llvm.instrprof.increment", fn(ptr, t_i64, t_i32, t_i32) -> void);
1102+
if crate::llvm_util::get_version() >= (19, 0, 0) {
1103+
ifn!("llvm.instrprof.mcdc.parameters", fn(ptr, t_i64, t_i32) -> void);
1104+
ifn!("llvm.instrprof.mcdc.tvbitmap.update", fn(ptr, t_i64, t_i32, ptr) -> void);
1105+
}
11021106
}
11031107

11041108
ifn!("llvm.type.test", fn(ptr, t_metadata) -> i1);

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
208208
let hash = bx.const_u64(function_coverage_info.function_source_hash);
209209
let bitmap_index = bx.const_u32(bitmap_idx);
210210
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_index, cond_bitmap);
211+
bx.mcdc_condbitmap_reset(cond_bitmap);
211212
}
212213
}
213214
}

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1615,10 +1615,6 @@ unsafe extern "C" {
16151615
pub fn LLVMRustSetAllowReassoc(Instr: &Value);
16161616

16171617
// Miscellaneous instructions
1618-
pub fn LLVMRustGetInstrProfIncrementIntrinsic(M: &Module) -> &Value;
1619-
pub fn LLVMRustGetInstrProfMCDCParametersIntrinsic(M: &Module) -> &Value;
1620-
pub fn LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(M: &Module) -> &Value;
1621-
16221618
pub fn LLVMRustBuildCall<'a>(
16231619
B: &Builder<'a>,
16241620
Ty: &'a Type,

Diff for: compiler/rustc_codegen_ssa/src/traits/builder.rs

-8
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,6 @@ pub trait BuilderMethods<'a, 'tcx>:
437437
/// Called for `StorageDead`
438438
fn lifetime_end(&mut self, ptr: Self::Value, size: Size);
439439

440-
fn instrprof_increment(
441-
&mut self,
442-
fn_name: Self::Value,
443-
hash: Self::Value,
444-
num_counters: Self::Value,
445-
index: Self::Value,
446-
);
447-
448440
fn call(
449441
&mut self,
450442
llty: Self::Type,

Diff for: compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

-39
Original file line numberDiff line numberDiff line change
@@ -1531,45 +1531,6 @@ extern "C" LLVMValueRef LLVMRustBuildCall(LLVMBuilderRef B, LLVMTypeRef Ty,
15311531
ArrayRef<OperandBundleDef>(OpBundles)));
15321532
}
15331533

1534-
extern "C" LLVMValueRef
1535-
LLVMRustGetInstrProfIncrementIntrinsic(LLVMModuleRef M) {
1536-
#if LLVM_VERSION_GE(20, 0)
1537-
return wrap(llvm::Intrinsic::getOrInsertDeclaration(
1538-
unwrap(M), llvm::Intrinsic::instrprof_increment));
1539-
#else
1540-
return wrap(llvm::Intrinsic::getDeclaration(
1541-
unwrap(M), llvm::Intrinsic::instrprof_increment));
1542-
#endif
1543-
}
1544-
1545-
extern "C" LLVMValueRef
1546-
LLVMRustGetInstrProfMCDCParametersIntrinsic(LLVMModuleRef M) {
1547-
#if LLVM_VERSION_LT(19, 0)
1548-
report_fatal_error("LLVM 19.0 is required for mcdc intrinsic functions");
1549-
#endif
1550-
#if LLVM_VERSION_GE(20, 0)
1551-
return wrap(llvm::Intrinsic::getOrInsertDeclaration(
1552-
unwrap(M), llvm::Intrinsic::instrprof_mcdc_parameters));
1553-
#else
1554-
return wrap(llvm::Intrinsic::getDeclaration(
1555-
unwrap(M), llvm::Intrinsic::instrprof_mcdc_parameters));
1556-
#endif
1557-
}
1558-
1559-
extern "C" LLVMValueRef
1560-
LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(LLVMModuleRef M) {
1561-
#if LLVM_VERSION_LT(19, 0)
1562-
report_fatal_error("LLVM 19.0 is required for mcdc intrinsic functions");
1563-
#endif
1564-
#if LLVM_VERSION_GE(20, 0)
1565-
return wrap(llvm::Intrinsic::getOrInsertDeclaration(
1566-
unwrap(M), llvm::Intrinsic::instrprof_mcdc_tvbitmap_update));
1567-
#else
1568-
return wrap(llvm::Intrinsic::getDeclaration(
1569-
unwrap(M), llvm::Intrinsic::instrprof_mcdc_tvbitmap_update));
1570-
#endif
1571-
}
1572-
15731534
extern "C" LLVMValueRef LLVMRustBuildMemCpy(LLVMBuilderRef B, LLVMValueRef Dst,
15741535
unsigned DstAlign, LLVMValueRef Src,
15751536
unsigned SrcAlign,

0 commit comments

Comments
 (0)