]> git.lizzy.rs Git - rust.git/commitdiff
moved instrument_coverage pass, optimized scalar, added FIXME
authorRich Kadel <richkadel@google.com>
Mon, 8 Jun 2020 23:20:26 +0000 (16:20 -0700)
committerRich Kadel <richkadel@google.com>
Mon, 15 Jun 2020 23:50:10 +0000 (16:50 -0700)
src/librustc_codegen_llvm/intrinsic.rs
src/librustc_mir/transform/instrument_coverage.rs
src/librustc_mir/transform/mod.rs

index 7fddda99185b450aeff3134d2c4250cb47cf771a..95465939070a0e62a6e8a9749df0386fa67fc890 100644 (file)
@@ -148,6 +148,11 @@ fn codegen_intrinsic_call(
                         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);
index 0604caadaea38cfb235fde0dc3e5e4397088c3e2..27abe813b067dba96cbc99177759f4eb78707c05 100644 (file)
@@ -7,6 +7,7 @@
 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 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx
 }
 
 // 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),
     })
 }
 
index 956ddd2051bac39b6f4c216162c9e126f2d8b04b..846ed1f86d8d6ab996bd664edc191aaa460331f9 100644 (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"),
     ];