Skip to content

Commit 4b75494

Browse files
committed
moved instrument_coverage pass, optimized scalar, added FIXME
1 parent dea76a5 commit 4b75494

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
148148
caller_fn_path
149149
);
150150

151+
// FIXME(richkadel): (1) Replace raw function name with mangled function name;
152+
// (2) Replace hardcoded `1234` in `hash` with a computed hash (as discussed in)
153+
// the MCP (compiler-team/issues/278); and replace the hardcoded `1` for
154+
// `num_counters` with the actual number of counters per function (when the
155+
// changes are made to inject more than one counter per function).
151156
let (fn_name, _len_val) = self.const_str(Symbol::intern(&caller_fn_path));
152157
let index = args[0].immediate();
153158
let hash = self.const_u64(1234);

src/librustc_mir/transform/instrument_coverage.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_middle::ty::Ty;
77
use rustc_middle::ty::TyCtxt;
88
use rustc_span::def_id::DefId;
99
use rustc_span::Span;
10+
use rustc_target::abi;
1011

1112
pub struct InstrumentCoverage;
1213

@@ -25,7 +26,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
2526
}
2627

2728
// The first counter (start of the function) is index zero.
28-
const INIT_FUNCTION_COUNTER: u128 = 0;
29+
const INIT_FUNCTION_COUNTER: u32 = 0;
2930

3031
/// Injects calls to placeholder function `count_code_region()`.
3132
// 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>) {
3536

3637
let count_code_region_fn =
3738
function_handle(tcx, span, tcx.lang_items().count_code_region_fn().unwrap());
38-
let counter_index = const_int_operand(tcx, span, tcx.types.u32, INIT_FUNCTION_COUNTER);
39+
let counter_index =
40+
const_int_operand(tcx, span, tcx.types.u32, Scalar::from_u32(INIT_FUNCTION_COUNTER));
3941

4042
let mut patch = MirPatch::new(body);
4143

@@ -77,17 +79,24 @@ fn const_int_operand<'tcx>(
7779
tcx: TyCtxt<'tcx>,
7880
span: Span,
7981
ty: Ty<'tcx>,
80-
val: u128,
82+
val: Scalar,
8183
) -> Operand<'tcx> {
82-
let param_env_and_ty = ty::ParamEnv::empty().and(ty);
83-
let size = tcx
84-
.layout_of(param_env_and_ty)
85-
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
86-
.size;
84+
debug_assert!({
85+
let param_env_and_ty = ty::ParamEnv::empty().and(ty);
86+
let type_size = tcx
87+
.layout_of(param_env_and_ty)
88+
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
89+
.size;
90+
let scalar_size = abi::Size::from_bytes(match val {
91+
Scalar::Raw { size, .. } => size,
92+
_ => panic!("Invalid scalar type {:?}", val),
93+
});
94+
scalar_size == type_size
95+
});
8796
Operand::Constant(box Constant {
8897
span,
8998
user_ty: None,
90-
literal: ty::Const::from_scalar(tcx, Scalar::from_uint(val, size), ty),
99+
literal: ty::Const::from_scalar(tcx, val, ty),
91100
})
92101
}
93102

src/librustc_mir/transform/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ fn mir_validated(
288288
// What we need to run borrowck etc.
289289
&promote_pass,
290290
&simplify::SimplifyCfg::new("qualify-consts"),
291+
// If the `instrument-coverage` option is enabled, analyze the CFG, identify each
292+
// conditional branch, construct a coverage map to be passed to LLVM, and inject counters
293+
// where needed.
294+
&instrument_coverage::InstrumentCoverage,
291295
]],
292296
);
293297

@@ -318,10 +322,6 @@ fn run_optimization_passes<'tcx>(
318322
// `AddRetag` needs to run after `ElaborateDrops`. Otherwise it should run fairly late,
319323
// but before optimizations begin.
320324
&add_retag::AddRetag,
321-
// If the `instrument-coverage` option is enabled, analyze the CFG, identify each
322-
// conditional branch, construct a coverage map to be passed to LLVM, and inject counters
323-
// where needed.
324-
&instrument_coverage::InstrumentCoverage,
325325
&simplify::SimplifyCfg::new("elaborate-drops"),
326326
// No lifetime analysis based on borrowing can be done from here on out.
327327
];

0 commit comments

Comments
 (0)