-
Notifications
You must be signed in to change notification settings - Fork 13.4k
first stage of implementing LLVM code coverage #73011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a6127e3
d139a72
395256a
5068ae1
088037a
2c5c2a6
d2cd59a
e4df7e7
7e49a9e
46ebd57
20aba8f
163e585
98685a4
1db44af
c338729
b9f0304
36c9014
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -389,6 +389,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | |||||
); | ||||||
self.copy_op(self.operand_index(args[0], index)?, dest)?; | ||||||
} | ||||||
// FIXME(#73156): Handle source code coverage in const eval | ||||||
sym::count_code_region => (), | ||||||
richkadel marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please always ping @rust-lang/wg-const-eval for new intrinsics in CTFE. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, note that as per this comment, T-lang should have been involved as well: rust/src/libcore/intrinsics.rs Lines 8 to 9 in 394e1b4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. amusingly this intrinsic is not marked as const at all. The static checks preventing non-const intrinsics from being invoked happen before mir optimizations. Since instrumentation is a MIR transformation... this isn't caught. Maybe instrumentation should happen before const checking and such? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, so this intrinsic is not called by users, but the call is inserted by an instrumentation pass later? Interesting. That reduces my T-lang concern.
Maybe -- I am not sure to what extend it would be useful to check the instrumented code as if it was user-written. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I write const fn a() -> u8 {
42
}
const A: u8 = a();
fn main() {
println!("{}", A);
} Then currently There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't see that issue, thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry about that @RalfJung! That was my fault; I didn't realize we should be pinging that group. I will try to keep that in mind in the future. Do you think we still need to get the lang team involved? This intrinsic is used by an unstable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt they will have any objections, but sending T-lang an "FYI this is what we did here" would still be a good idea I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for flagging the issue @RalfJung ... Note also, the next PR increment is under review, and adds some additional Rust intrinsics that I believe we need for coverage map generation. See this related note: Since the MIR analysis is minimal at the moment (injecting at the function level only) I'm not generating calls to these intrinsics yet, but when I do, I suspect I may need to add these new intrinsics to src/librustc_mir/interpret/intrinsics.rs as well, similarly stubbed out. |
||||||
_ => return Ok(false), | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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, | ||
fn_span: span, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed new merge error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested locally, including the mir-opt test. This should fix the failure in the rollup log. |
||
}, | ||
); | ||
|
||
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, | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.