Skip to content

Commit

Permalink
moved instrument_coverage pass, optimized scalar, added FIXME
Browse files Browse the repository at this point in the history
  • Loading branch information
richkadel committed Jun 15, 2020
1 parent 7e49a9e commit 46ebd57
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/librustc_codegen_llvm/intrinsic.rs
Expand Up @@ -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);
Expand Down
27 changes: 18 additions & 9 deletions src/librustc_mir/transform/instrument_coverage.rs
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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);

Expand Down Expand Up @@ -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),
})
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc_mir/transform/mod.rs
Expand Up @@ -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,
]],
);

Expand Down Expand Up @@ -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"),
];

Expand Down

0 comments on commit 46ebd57

Please sign in to comment.