moved instrument_coverage pass, optimized scalar, added FIXME

This commit is contained in:
Rich Kadel 2020-06-08 16:20:26 -07:00
parent 7e49a9ec59
commit 46ebd57c42
3 changed files with 27 additions and 13 deletions

View File

@ -148,6 +148,11 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
caller_fn_path
);
// FIXME(richkadel): (1) Replace raw function name with mangled function name;
// (2) Replace hardcoded `1234` in `hash` with a computed hash (as discussed in)
// the MCP (compiler-team/issues/278); and replace the hardcoded `1` for
// `num_counters` with the actual number of counters per function (when the
// changes are made to inject more than one counter per function).
let (fn_name, _len_val) = self.const_str(Symbol::intern(&caller_fn_path));
let index = args[0].immediate();
let hash = self.const_u64(1234);

View File

@ -7,6 +7,7 @@ use rustc_middle::ty::Ty;
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::DefId;
use rustc_span::Span;
use rustc_target::abi;
pub struct InstrumentCoverage;
@ -25,7 +26,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
}
// The first counter (start of the function) is index zero.
const INIT_FUNCTION_COUNTER: u128 = 0;
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.
@ -35,7 +36,8 @@ pub fn instrument_coverage<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let count_code_region_fn =
function_handle(tcx, span, tcx.lang_items().count_code_region_fn().unwrap());
let counter_index = const_int_operand(tcx, span, tcx.types.u32, INIT_FUNCTION_COUNTER);
let counter_index =
const_int_operand(tcx, span, tcx.types.u32, Scalar::from_u32(INIT_FUNCTION_COUNTER));
let mut patch = MirPatch::new(body);
@ -77,17 +79,24 @@ fn const_int_operand<'tcx>(
tcx: TyCtxt<'tcx>,
span: Span,
ty: Ty<'tcx>,
val: u128,
val: Scalar,
) -> Operand<'tcx> {
let param_env_and_ty = ty::ParamEnv::empty().and(ty);
let size = tcx
.layout_of(param_env_and_ty)
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
.size;
debug_assert!({
let param_env_and_ty = ty::ParamEnv::empty().and(ty);
let type_size = tcx
.layout_of(param_env_and_ty)
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
.size;
let scalar_size = abi::Size::from_bytes(match val {
Scalar::Raw { size, .. } => size,
_ => panic!("Invalid scalar type {:?}", val),
});
scalar_size == type_size
});
Operand::Constant(box Constant {
span,
user_ty: None,
literal: ty::Const::from_scalar(tcx, Scalar::from_uint(val, size), ty),
literal: ty::Const::from_scalar(tcx, val, ty),
})
}

View File

@ -289,6 +289,10 @@ fn mir_validated(
// What we need to run borrowck etc.
&promote_pass,
&simplify::SimplifyCfg::new("qualify-consts"),
// If the `instrument-coverage` option is enabled, analyze the CFG, identify each
// conditional branch, construct a coverage map to be passed to LLVM, and inject counters
// where needed.
&instrument_coverage::InstrumentCoverage,
]],
);
@ -338,10 +342,6 @@ fn run_post_borrowck_cleanup_passes<'tcx>(
// `AddRetag` needs to run after `ElaborateDrops`. Otherwise it should run fairly late,
// but before optimizations begin.
&add_retag::AddRetag,
// If the `instrument-coverage` option is enabled, analyze the CFG, identify each
// conditional branch, construct a coverage map to be passed to LLVM, and inject counters
// where needed.
&instrument_coverage::InstrumentCoverage,
&simplify::SimplifyCfg::new("elaborate-drops"),
];