Skip to content

Commit

Permalink
Get rid of skip monomorphizer hack (rust-lang#485) (rust-lang#624)
Browse files Browse the repository at this point in the history
Remove the hack we added to monomorphizer to skip functions that we were not able to handle.

The latest fixes that we added to RMC was enough to allow us to run the monomorphizer as is.
  • Loading branch information
celinval authored and tedinski committed Nov 4, 2021
1 parent b3f376d commit 37b4902
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 22 deletions.
6 changes: 2 additions & 4 deletions compiler/rustc_codegen_rmc/src/compiler_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

//! This file contains the code necessary to interface with the compiler backend
use crate::overrides::skip_monomorphize;
use crate::GotocCtx;

use bitflags::_core::any::Any;
use cbmc::goto_program::symtab_transformer;
use cbmc::goto_program::SymbolTable;
Expand Down Expand Up @@ -46,9 +46,7 @@ impl CodegenBackend for GotocCodegenBackend {
Box::new(rustc_codegen_ssa::back::metadata::DefaultMetadataLoader)
}

fn provide(&self, providers: &mut Providers) {
providers.skip_monomorphize = skip_monomorphize;
}
fn provide(&self, _providers: &mut Providers) {}

fn provide_extern(&self, _providers: &mut ty::query::ExternProviders) {}

Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_codegen_rmc/src/overrides/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ struct MemSwap;

impl<'tcx> GotocHook<'tcx> for MemSwap {
fn hook_applies(&self, tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
// We need to keep the old std / core functions here because we don't compile std yet.
let name = with_no_trimmed_paths(|| tcx.def_path_str(instance.def_id()));
name == "core::mem::swap"
|| name == "std::mem::swap"
Expand Down Expand Up @@ -688,7 +689,3 @@ impl<'tcx> GotocHooks<'tcx> {
None
}
}

pub fn skip_monomorphize<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> bool {
fn_hooks().hooks.iter().any(|hook| hook.hook_applies(tcx, instance))
}
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_rmc/src/overrides/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
mod hooks;

pub use hooks::{fn_hooks, skip_monomorphize, GotocHooks};
pub use hooks::{fn_hooks, GotocHooks};
1 change: 0 additions & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,6 @@ impl CrateInfo {
}

pub fn provide(providers: &mut Providers) {
providers.skip_monomorphize = |_, _| false;
providers.backend_optimization_level = |tcx, cratenum| {
let for_speed = match tcx.sess.opts.optimize {
// If globally no optimisation is done, #[optimize] has no effect.
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1862,11 +1862,4 @@ rustc_queries! {
no_hash
desc { "performing HIR wf-checking for predicate {:?} at item {:?}", key.0, key.1 }
}

query skip_monomorphize(key: ty::Instance<'tcx>)
-> bool {
eval_always
no_hash
desc { "Check if we should skip monomorphization for `{}`", key}
}
}
5 changes: 0 additions & 5 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ pub fn collect_crate_mono_items(
(visited.into_inner(), inlining_map.into_inner())
}

// Temporary function that allow us to skip monomorphizing lang_start.
// Find all non-generic items by walking the HIR. These items serve as roots to
// start monomorphizing from.
fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<'_>> {
Expand Down Expand Up @@ -922,10 +921,6 @@ fn visit_instance_use<'tcx>(
return;
}

if tcx.skip_monomorphize(instance) {
return;
}

match instance.def {
ty::InstanceDef::Virtual(..) | ty::InstanceDef::Intrinsic(_) => {
if !is_direct_call {
Expand Down
14 changes: 14 additions & 0 deletions src/test/rmc/FunctionAbstractions/mem_replace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::mem;

pub fn main() {
let mut var1 = rmc::nondet::<i32>();
let mut var2 = rmc::nondet::<i32>();
let old_var1 = var1;
unsafe {
assert_eq!(mem::replace(&mut var1, var2), old_var1);
}
assert_eq!(var1, var2);
}
16 changes: 16 additions & 0 deletions src/test/rmc/FunctionAbstractions/mem_swap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::mem;

pub fn main() {
let mut var1 = rmc::nondet::<i32>();
let mut var2 = rmc::nondet::<i32>();
let old_var1 = var1;
let old_var2 = var2;
unsafe {
mem::swap(&mut var1, &mut var2);
}
assert_eq!(var1, old_var2);
assert_eq!(var2, old_var1);
}
11 changes: 11 additions & 0 deletions src/test/rmc/FunctionAbstractions/ptr_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::ptr::read;

pub fn main() {
let var = 1;
unsafe {
assert_eq!(read(&var), var);
}
}
12 changes: 12 additions & 0 deletions src/test/rmc/FunctionAbstractions/ptr_write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::ptr::write;

pub fn main() {
let mut var = 1;
unsafe {
write(&mut var, 10);
}
assert_eq!(var, 10);
}
18 changes: 18 additions & 0 deletions src/test/rmc/Never/never_return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![feature(never_type)]

/// Test using the never type
pub fn err() -> ! {
panic!("EXPECTED FAIL: Function should always fail");
}

// Give an empty main to make rustc happy.
#[no_mangle]
pub fn main() {
let var = rmc::nondet::<i32>();
if var > 0 {
err();
}
}

0 comments on commit 37b4902

Please sign in to comment.