Skip to content

Commit ed3810b

Browse files
committed
lower move_val_init during MIR construction
Because of its "magic" order-of-evaluation semantics, `move_val_init` must be lowered during MIR construction in order to work.
1 parent 3e473b1 commit ed3810b

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

src/librustc_mir/build/expr/as_temp.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3838
debug!("expr_as_temp(block={:?}, expr={:?})", block, expr);
3939
let this = self;
4040

41-
if let ExprKind::Scope { .. } = expr.kind {
42-
span_bug!(expr.span, "unexpected scope expression in as_temp: {:?}",
43-
expr);
41+
if let ExprKind::Scope { extent, value } = expr.kind {
42+
return this.in_scope(extent, block, |this| {
43+
this.as_temp(block, temp_lifetime, value)
44+
});
4445
}
4546

4647
let expr_ty = expr.ty.clone();

src/librustc_mir/build/expr/into.rs

+43-17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use hair::*;
1616
use rustc::ty;
1717
use rustc::mir::*;
1818

19+
use syntax::abi::Abi;
20+
1921
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2022
/// Compile `expr`, storing the result into `destination`, which
2123
/// is assumed to be uninitialized.
@@ -206,25 +208,49 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
206208
}
207209
_ => false
208210
};
211+
let intrinsic = match ty.sty {
212+
ty::TyFnDef(def_id, _, ref f) if
213+
f.abi() == Abi::RustIntrinsic ||
214+
f.abi() == Abi::PlatformIntrinsic =>
215+
{
216+
Some(this.hir.tcx().item_name(def_id).as_str())
217+
}
218+
_ => None
219+
};
220+
let intrinsic = intrinsic.as_ref().map(|s| &s[..]);
209221
let fun = unpack!(block = this.as_local_operand(block, fun));
210-
let args: Vec<_> =
211-
args.into_iter()
212-
.map(|arg| unpack!(block = this.as_local_operand(block, arg)))
213-
.collect();
222+
if intrinsic == Some("move_val_init") {
223+
// `move_val_init` has "magic" semantics - the second argument is
224+
// always evaluated "directly" into the first one.
214225

215-
let success = this.cfg.start_new_block();
216-
let cleanup = this.diverge_cleanup();
217-
this.cfg.terminate(block, source_info, TerminatorKind::Call {
218-
func: fun,
219-
args: args,
220-
cleanup: cleanup,
221-
destination: if diverges {
222-
None
223-
} else {
224-
Some ((destination.clone(), success))
225-
}
226-
});
227-
success.unit()
226+
let mut args = args.into_iter();
227+
let ptr = args.next().expect("0 arguments to `move_val_init`");
228+
let val = args.next().expect("1 argument to `move_val_init`");
229+
assert!(args.next().is_none(), ">2 arguments to `move_val_init`");
230+
231+
let topmost_scope = this.topmost_scope();
232+
let ptr = unpack!(block = this.as_temp(block, Some(topmost_scope), ptr));
233+
this.into(&ptr.deref(), block, val)
234+
} else {
235+
let args: Vec<_> =
236+
args.into_iter()
237+
.map(|arg| unpack!(block = this.as_local_operand(block, arg)))
238+
.collect();
239+
240+
let success = this.cfg.start_new_block();
241+
let cleanup = this.diverge_cleanup();
242+
this.cfg.terminate(block, source_info, TerminatorKind::Call {
243+
func: fun,
244+
args: args,
245+
cleanup: cleanup,
246+
destination: if diverges {
247+
None
248+
} else {
249+
Some ((destination.clone(), success))
250+
}
251+
});
252+
success.unit()
253+
}
228254
}
229255

230256
// These cases don't actually need a destination

src/librustc_trans/mir/block.rs

-10
Original file line numberDiff line numberDiff line change
@@ -418,16 +418,6 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
418418
};
419419
let intrinsic = intrinsic.as_ref().map(|s| &s[..]);
420420

421-
if intrinsic == Some("move_val_init") {
422-
let &(_, target) = destination.as_ref().unwrap();
423-
// The first argument is a thin destination pointer.
424-
let llptr = self.trans_operand(&bcx, &args[0]).immediate();
425-
let val = self.trans_operand(&bcx, &args[1]);
426-
self.store_operand(&bcx, llptr, None, val);
427-
funclet_br(self, bcx, target);
428-
return;
429-
}
430-
431421
if intrinsic == Some("transmute") {
432422
let &(ref dest, target) = destination.as_ref().unwrap();
433423
self.trans_transmute(&bcx, &args[0], dest);

src/test/codegen/move-val-init.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -C no-prepopulate-passes
12+
13+
#![feature(core_intrinsics)]
14+
#![crate_type = "lib"]
15+
16+
// test that `move_val_init` actually avoids big allocas
17+
18+
use std::intrinsics::move_val_init;
19+
20+
pub struct Big {
21+
pub data: [u8; 65536]
22+
}
23+
24+
// CHECK-LABEL: @test_mvi
25+
#[no_mangle]
26+
pub unsafe fn test_mvi(target: *mut Big, make_big: fn() -> Big) {
27+
// CHECK: call void %1(%Big*{{[^%]*}} %0)
28+
move_val_init(target, make_big());
29+
}

0 commit comments

Comments
 (0)