forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#73011 - richkadel:llvm-count-from-mir-pass,…
… r=tmandry first stage of implementing LLVM code coverage This PR replaces rust-lang#70680 (WIP toward LLVM Code Coverage for Rust) since I am re-implementing the Rust LLVM code coverage feature in a different part of the compiler (in MIR pass(es) vs AST). This PR updates rustc with `-Zinstrument-coverage` option that injects the llvm intrinsic `instrprof.increment()` for code generation. This initial version only injects counters at the top of each function, and does not yet implement the required coverage map. Upcoming PRs will add the coverage map, and add more counters and/or counter expressions for each conditional code branch. Rust compiler MCP rust-lang/compiler-team#278 Relevant issue: rust-lang#34701 - Implement support for LLVMs code coverage instrumentation ***[I put together some development notes here, under a separate branch.](https://github.com/richkadel/rust/blob/cfa0b21d34ee64e4ebee226101bd2ef0c6757865/src/test/codegen/coverage-experiments/README-THIS-IS-TEMPORARY.md)***
- Loading branch information
Showing
23 changed files
with
376 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::transform::{MirPass, MirSource}; | ||
use crate::util::patch::MirPatch; | ||
use rustc_hir::lang_items; | ||
use rustc_middle::mir::interpret::Scalar; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_span::def_id::DefId; | ||
use rustc_span::Span; | ||
|
||
/// Inserts call to count_code_region() as a placeholder to be replaced during code generation with | ||
/// the intrinsic llvm.instrprof.increment. | ||
pub struct InstrumentCoverage; | ||
|
||
impl<'tcx> MirPass<'tcx> for InstrumentCoverage { | ||
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) { | ||
if tcx.sess.opts.debugging_opts.instrument_coverage { | ||
debug!("instrumenting {:?}", src.def_id()); | ||
instrument_coverage(tcx, body); | ||
} | ||
} | ||
} | ||
|
||
// The first counter (start of the function) is index zero. | ||
const INIT_FUNCTION_COUNTER: u32 = 0; | ||
|
||
/// Injects calls to placeholder function `count_code_region()`. | ||
// FIXME(richkadel): As a first step, counters are only injected at the top of each function. | ||
// The complete solution will inject counters at each conditional code branch. | ||
pub fn instrument_coverage<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
let span = body.span.shrink_to_lo(); | ||
|
||
let count_code_region_fn = function_handle( | ||
tcx, | ||
tcx.require_lang_item(lang_items::CountCodeRegionFnLangItem, None), | ||
span, | ||
); | ||
let counter_index = Operand::const_from_scalar( | ||
tcx, | ||
tcx.types.u32, | ||
Scalar::from_u32(INIT_FUNCTION_COUNTER), | ||
span, | ||
); | ||
|
||
let mut patch = MirPatch::new(body); | ||
|
||
let new_block = patch.new_block(placeholder_block(SourceInfo::outermost(body.span))); | ||
let next_block = START_BLOCK; | ||
|
||
let temp = patch.new_temp(tcx.mk_unit(), body.span); | ||
patch.patch_terminator( | ||
new_block, | ||
TerminatorKind::Call { | ||
func: count_code_region_fn, | ||
args: vec![counter_index], | ||
// new_block will swapped with the next_block, after applying patch | ||
destination: Some((Place::from(temp), new_block)), | ||
cleanup: None, | ||
from_hir_call: false, | ||
}, | ||
); | ||
|
||
patch.add_statement(new_block.start_location(), StatementKind::StorageLive(temp)); | ||
patch.add_statement(next_block.start_location(), StatementKind::StorageDead(temp)); | ||
|
||
patch.apply(body); | ||
|
||
// To insert the `new_block` in front of the first block in the counted branch (for example, | ||
// the START_BLOCK, at the top of the function), just swap the indexes, leaving the rest of the | ||
// graph unchanged. | ||
body.basic_blocks_mut().swap(next_block, new_block); | ||
} | ||
|
||
fn function_handle<'tcx>(tcx: TyCtxt<'tcx>, fn_def_id: DefId, span: Span) -> Operand<'tcx> { | ||
let ret_ty = tcx.fn_sig(fn_def_id).output(); | ||
let ret_ty = ret_ty.no_bound_vars().unwrap(); | ||
let substs = tcx.mk_substs(::std::iter::once(ty::subst::GenericArg::from(ret_ty))); | ||
Operand::function_handle(tcx, fn_def_id, substs, span) | ||
} | ||
|
||
fn placeholder_block<'tcx>(source_info: SourceInfo) -> BasicBlockData<'tcx> { | ||
BasicBlockData { | ||
statements: vec![], | ||
terminator: Some(Terminator { | ||
source_info, | ||
// this gets overwritten by the counter Call | ||
kind: TerminatorKind::Unreachable, | ||
}), | ||
is_cleanup: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.