Skip to content

Commit

Permalink
Auto merge of #79200 - Dylan-DPC:rollup-su689pq, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Rollup of 14 pull requests

Successful merges:

 - #78961 (Make bad "rust-call" arguments no longer ICE)
 - #79082 (Improve the diagnostic for when an `fn` contains qualifiers inside an `extern` block.)
 - #79090 (libary: Forward compiler-builtins "asm"  and "mangled-names" feature)
 - #79094 (Add //ignore-macos to pretty-std-collections.rs)
 - #79101 (Don't special case constant operands when lowering intrinsics)
 - #79102 (Add two regression tests)
 - #79110 (Remove redundant notes in E0275)
 - #79116 (compiletest: Fix a warning in debuginfo tests on windows-gnu)
 - #79117 (add optimization fuel checks to some mir passes)
 - #79147 (Highlight MIR as Rust on GitHub)
 - #79149 (Move capture lowering from THIR to MIR)
 - #79155 (fix handling the default config for profiler and sanitizers)
 - #79156 (Allow using `download-ci-llvm` from directories other than the root)
 - #79164 (Permit standalone generic parameters as const generic arguments in macros)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Nov 19, 2020
2 parents 3d3c8c5 + b5fffdc commit fe98231
Show file tree
Hide file tree
Showing 52 changed files with 632 additions and 729 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.h rust
*.rs rust diff=rust
*.fixed linguist-language=Rust
*.mir linguist-language=Rust
src/etc/installer/gfx/* binary
*.woff binary
src/vendor/** -text
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl<'a> AstValidator<'a> {
self.err_handler()
.struct_span_err(ident.span, "functions in `extern` blocks cannot have qualifiers")
.span_label(self.current_extern_span(), "in this `extern` block")
.span_suggestion(
.span_suggestion_verbose(
span.until(ident.span.shrink_to_lo()),
"remove the qualifiers",
"fn ".to_string(),
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_mir/src/transform/const_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
}
}

trace!("attepting to replace {:?} with {:?}", rval, value);
trace!("attempting to replace {:?} with {:?}", rval, value);
if let Err(e) = self.ecx.const_validate_operand(
value,
vec![],
Expand Down Expand Up @@ -890,6 +890,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
return false;
}

if !self.tcx.consider_optimizing(|| format!("ConstantPropagation - OpTy: {:?}", op)) {
return false;
}

match *op {
interpret::Operand::Immediate(Immediate::Scalar(ScalarMaybeUninit::Scalar(s))) => {
s.is_bits()
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir/src/transform/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
let should_cleanup = !opts_to_apply.is_empty();

for opt_to_apply in opts_to_apply {
if !tcx.consider_optimizing(|| format!("EarlyOtherwiseBranch {:?}", &opt_to_apply)) {
break;
}

trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply);

let statements_before =
Expand Down
28 changes: 21 additions & 7 deletions compiler/rustc_mir/src/transform/instcombine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl<'tcx> InstCombineVisitor<'tcx> {
fn should_combine(&self, rvalue: &Rvalue<'tcx>, location: Location) -> bool {
self.tcx.consider_optimizing(|| {
format!("InstCombine - Rvalue: {:?} Location: {:?}", rvalue, location)
})
}
}

impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
if self.optimizations.and_stars.remove(&location) {
if self.optimizations.and_stars.remove(&location) && self.should_combine(rvalue, location) {
debug!("replacing `&*`: {:?}", rvalue);
let new_place = match rvalue {
Rvalue::Ref(_, _, place) => {
Expand All @@ -67,18 +75,24 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
}

if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
debug!("replacing `Len([_; N])`: {:?}", rvalue);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
if self.should_combine(rvalue, location) {
debug!("replacing `Len([_; N])`: {:?}", rvalue);
*rvalue = Rvalue::Use(Operand::Constant(box constant));
}
}

if let Some(operand) = self.optimizations.unneeded_equality_comparison.remove(&location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
if self.should_combine(rvalue, location) {
debug!("replacing {:?} with {:?}", rvalue, operand);
*rvalue = Rvalue::Use(operand);
}
}

if let Some(place) = self.optimizations.unneeded_deref.remove(&location) {
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
*rvalue = Rvalue::Use(Operand::Copy(place));
if self.should_combine(rvalue, location) {
debug!("unneeded_deref: replacing {:?} with {:?}", rvalue, place);
*rvalue = Rvalue::Use(Operand::Copy(place));
}
}

self.super_rvalue(rvalue, location)
Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_mir/src/transform/lower_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ pub struct LowerIntrinsics;

impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
for block in body.basic_blocks_mut() {
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
for block in basic_blocks {
let terminator = block.terminator.as_mut().unwrap();
if let TerminatorKind::Call {
func: Operand::Constant(box Constant { literal: ty::Const { ty: func_ty, .. }, .. }),
args,
destination,
..
} = &mut terminator.kind
{
if let TerminatorKind::Call { func, args, destination, .. } = &mut terminator.kind {
let func_ty = func.ty(local_decls, tcx);
let (intrinsic_name, substs) = match resolve_rust_intrinsic(tcx, func_ty) {
None => continue,
Some(it) => it,
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_mir/src/transform/match_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
}

let param_env = tcx.param_env(body.source.def_id());
let def_id = body.source.def_id();
let (bbs, local_decls) = body.basic_blocks_and_local_decls_mut();
'outer: for bb_idx in bbs.indices() {
if !tcx.consider_optimizing(|| format!("MatchBranchSimplification {:?} ", def_id)) {
continue;
}

let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind {
TerminatorKind::SwitchInt {
discr: ref discr @ (Operand::Copy(_) | Operand::Move(_)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {

// find basic blocks with no statement and a return terminator
let mut bbs_simple_returns = BitSet::new_empty(body.basic_blocks().len());
let def_id = body.source.def_id();
let bbs = body.basic_blocks_mut();
for idx in bbs.indices() {
if bbs[idx].statements.is_empty()
Expand All @@ -26,6 +27,10 @@ impl<'tcx> MirPass<'tcx> for MultipleReturnTerminators {
}

for bb in bbs {
if !tcx.consider_optimizing(|| format!("MultipleReturnTerminators {:?} ", def_id)) {
break;
}

if let TerminatorKind::Goto { target } = bb.terminator().kind {
if bbs_simple_returns.contains(target) {
bb.terminator_mut().kind = TerminatorKind::Return;
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_mir/src/transform/nrvo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ impl<'tcx> MirPass<'tcx> for RenameReturnPlace {
return;
}

let def_id = body.source.def_id();
let returned_local = match local_eligible_for_nrvo(body) {
Some(l) => l,
None => {
debug!("`{:?}` was ineligible for NRVO", body.source.def_id());
debug!("`{:?}` was ineligible for NRVO", def_id);
return;
}
};

if !tcx.consider_optimizing(|| format!("RenameReturnPlace {:?}", def_id)) {
return;
}

debug!(
"`{:?}` was eligible for NRVO, making {:?} the return place",
body.source.def_id(),
returned_local
def_id, returned_local
);

RenameToReturnPlace { tcx, to_rename: returned_local }.visit_body(body);
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_mir/src/transform/remove_unneeded_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
opt_finder.visit_body(body);
let should_simplify = !opt_finder.optimizations.is_empty();
for (loc, target) in opt_finder.optimizations {
if !tcx
.consider_optimizing(|| format!("RemoveUnneededDrops {:?} ", body.source.def_id()))
{
break;
}

let terminator = body.basic_blocks_mut()[loc.block].terminator_mut();
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
terminator.kind = TerminatorKind::Goto { target };
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_mir/src/transform/unreachable_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ impl MirPass<'_> for UnreachablePropagation {

let replaced = !replacements.is_empty();
for (bb, terminator_kind) in replacements {
if !tcx.consider_optimizing(|| {
format!("UnreachablePropagation {:?} ", body.source.def_id())
}) {
break;
}

body.basic_blocks_mut()[bb].terminator_mut().kind = terminator_kind;
}

Expand Down
80 changes: 79 additions & 1 deletion compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr_span,
source_info,
),
ExprKind::SelfRef => block.and(PlaceBuilder::from(Local::new(1))),
ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
let capture = this
.hir
.typeck_results
.closure_captures
.get(&closure_def_id)
.and_then(|captures| captures.get_full(&var_hir_id));

if capture.is_none() {
if !this.hir.tcx().features().capture_disjoint_fields {
bug!(
"No associated capture found for {:?} even though \
capture_disjoint_fields isn't enabled",
expr.kind
)
}
// FIXME(project-rfc-2229#24): Handle this case properly
}

// Unwrap until the FIXME has been resolved
let (capture_index, _, upvar_id) = capture.unwrap();
this.lower_closure_capture(block, capture_index, *upvar_id)
}

ExprKind::VarRef { id } => {
let place_builder = if this.is_bound_var_in_guard(id) {
let index = this.var_local_id(id, RefWithinGuard);
Expand Down Expand Up @@ -270,6 +293,61 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

/// Lower a closure/generator capture by representing it as a field
/// access within the desugared closure/generator.
///
/// `capture_index` is the index of the capture within the desugared
/// closure/generator.
fn lower_closure_capture(
&mut self,
block: BasicBlock,
capture_index: usize,
upvar_id: ty::UpvarId,
) -> BlockAnd<PlaceBuilder<'tcx>> {
let closure_ty = self
.hir
.typeck_results()
.node_type(self.hir.tcx().hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));

// Captures are represented using fields inside a structure.
// This represents accessing self in the closure structure
let mut place_builder = PlaceBuilder::from(Local::new(1));

// In case of Fn/FnMut closures we must deref to access the fields
// Generators are considered FnOnce, so we ignore this step for them.
if let ty::Closure(_, closure_substs) = closure_ty.kind() {
match self.hir.infcx().closure_kind(closure_substs).unwrap() {
ty::ClosureKind::Fn | ty::ClosureKind::FnMut => {
place_builder = place_builder.deref();
}
ty::ClosureKind::FnOnce => {}
}
}

let substs = match closure_ty.kind() {
ty::Closure(_, substs) => ty::UpvarSubsts::Closure(substs),
ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs),
_ => bug!("Lowering capture for non-closure type {:?}", closure_ty)
};

// Access the capture by accessing the field within the Closure struct.
//
// We must have inferred the capture types since we are building MIR, therefore
// it's safe to call `upvar_tys` and we can unwrap here because
// we know that the capture exists and is the `capture_index`-th capture.
let var_ty = substs.upvar_tys().nth(capture_index).unwrap();
place_builder = place_builder.field(Field::new(capture_index), var_ty);

// If the variable is captured via ByRef(Immutable/Mutable) Borrow,
// we need to deref it
match self.hir.typeck_results.upvar_capture(upvar_id) {
ty::UpvarCapture::ByRef(_) => {
block.and(place_builder.deref())
}
ty::UpvarCapture::ByValue(_) => block.and(place_builder),
}
}

/// Lower an index expression
///
/// This has two complications;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::Deref { .. }
| ExprKind::Index { .. }
| ExprKind::VarRef { .. }
| ExprKind::SelfRef
| ExprKind::UpvarRef { .. }
| ExprKind::Break { .. }
| ExprKind::Continue { .. }
| ExprKind::Return { .. }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/expr/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Category {
ExprKind::Field { .. }
| ExprKind::Deref { .. }
| ExprKind::Index { .. }
| ExprKind::SelfRef
| ExprKind::UpvarRef { .. }
| ExprKind::VarRef { .. }
| ExprKind::PlaceTypeAscription { .. }
| ExprKind::ValueTypeAscription { .. } => Some(Category::Place),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

// Avoid creating a temporary
ExprKind::VarRef { .. }
| ExprKind::SelfRef
| ExprKind::UpvarRef { .. }
| ExprKind::PlaceTypeAscription { .. }
| ExprKind::ValueTypeAscription { .. } => {
debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
Expand Down
Loading

0 comments on commit fe98231

Please sign in to comment.