-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle operand temps for function calls #32738
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
use llvm::{self, BasicBlockRef, ValueRef, OperandBundleDef}; | ||
use rustc::ty; | ||
use rustc::mir::repr as mir; | ||
use abi::{Abi, FnType}; | ||
use abi::{Abi, FnType, ArgType}; | ||
use adt; | ||
use base; | ||
use build; | ||
|
@@ -25,7 +25,7 @@ use type_of; | |
use glue; | ||
use type_::Type; | ||
|
||
use super::{MirContext, drop}; | ||
use super::{MirContext, TempRef, drop}; | ||
use super::lvalue::{LvalueRef, load_fat_ptr}; | ||
use super::operand::OperandRef; | ||
use super::operand::OperandValue::{self, FatPtr, Immediate, Ref}; | ||
|
@@ -191,25 +191,10 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | |
|
||
if intrinsic == Some("transmute") { | ||
let &(ref dest, target) = destination.as_ref().unwrap(); | ||
let dst = self.trans_lvalue(&bcx, dest); | ||
let mut val = self.trans_operand(&bcx, &args[0]); | ||
if let ty::TyFnDef(def_id, substs, _) = val.ty.sty { | ||
let llouttype = type_of::type_of(bcx.ccx(), dst.ty.to_ty(bcx.tcx())); | ||
let out_type_size = llbitsize_of_real(bcx.ccx(), llouttype); | ||
if out_type_size != 0 { | ||
// FIXME #19925 Remove this hack after a release cycle. | ||
let f = Callee::def(bcx.ccx(), def_id, substs); | ||
let datum = f.reify(bcx.ccx()); | ||
val = OperandRef { | ||
val: OperandValue::Immediate(datum.val), | ||
ty: datum.ty | ||
}; | ||
} | ||
} | ||
self.with_lvalue_ref(&bcx, dest, |this, dest| { | ||
this.trans_transmute(&bcx, &args[0], dest); | ||
}); | ||
|
||
let llty = type_of::type_of(bcx.ccx(), val.ty); | ||
let cast_ptr = bcx.pointercast(dst.llval, llty.ptr_to()); | ||
self.store_operand(&bcx, cast_ptr, val); | ||
self.set_operand_dropped(&bcx, &args[0]); | ||
funclet_br(bcx, self.llblock(target)); | ||
return; | ||
|
@@ -227,17 +212,71 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | |
|
||
// Prepare the return value destination | ||
let ret_dest = if let Some((ref d, _)) = *destination { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. split this out to a function? |
||
let dest = self.trans_lvalue(&bcx, d); | ||
if fn_ty.ret.is_indirect() { | ||
llargs.push(dest.llval); | ||
None | ||
} else if fn_ty.ret.is_ignore() { | ||
None | ||
} else { | ||
Some(dest) | ||
match *d { | ||
// Handle temporary lvalues, specifically Operand ones, as | ||
// they don't have allocas | ||
mir::Lvalue::Temp(idx) => { | ||
let lvalue_ty = self.mir.lvalue_ty(bcx.tcx(), d); | ||
let ret_ty = lvalue_ty.to_ty(bcx.tcx()); | ||
match self.temps[idx as usize] { | ||
TempRef::Lvalue(dest) => { | ||
if fn_ty.ret.is_indirect() { | ||
llargs.push(dest.llval); | ||
ReturnDest::Nothing | ||
} else if fn_ty.ret.is_ignore() { | ||
ReturnDest::Nothing | ||
} else { | ||
ReturnDest::Store(dest.llval) | ||
} | ||
} | ||
TempRef::Operand(None) => { | ||
let is_intrinsic = if let Intrinsic = callee.data { | ||
true | ||
} else { | ||
false | ||
}; | ||
|
||
if fn_ty.ret.is_indirect() { | ||
// Odd, but possible, case, we have an operand temporary, | ||
// but the calling convention has an indirect return. | ||
let tmp = bcx.with_block(|bcx| { | ||
base::alloc_ty(bcx, ret_ty, "tmp_ret") | ||
}); | ||
llargs.push(tmp); | ||
ReturnDest::IndirectOperand(tmp, idx) | ||
} else if is_intrinsic { | ||
// Currently, intrinsics always need a location to store | ||
// the result. so we create a temporary alloca for the | ||
// result | ||
let tmp = bcx.with_block(|bcx| { | ||
base::alloc_ty(bcx, ret_ty, "tmp_ret") | ||
}); | ||
ReturnDest::IndirectOperand(tmp, idx) | ||
} else if fn_ty.ret.is_ignore() { | ||
ReturnDest::Nothing | ||
} else { | ||
ReturnDest::DirectOperand(idx) | ||
} | ||
} | ||
TempRef::Operand(Some(_)) => { | ||
bug!("lvalue temp already assigned to"); | ||
} | ||
} | ||
} | ||
_ => { | ||
let dest = self.trans_lvalue(&bcx, d); | ||
if fn_ty.ret.is_indirect() { | ||
llargs.push(dest.llval); | ||
ReturnDest::Nothing | ||
} else if fn_ty.ret.is_ignore() { | ||
ReturnDest::Nothing | ||
} else { | ||
ReturnDest::Store(dest.llval) | ||
} | ||
} | ||
} | ||
} else { | ||
None | ||
ReturnDest::Nothing | ||
}; | ||
|
||
// Split the rust-call tupled arguments off. | ||
|
@@ -269,12 +308,15 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | |
use expr::{Ignore, SaveIn}; | ||
use intrinsic::trans_intrinsic_call; | ||
|
||
let (dest, llargs) = if fn_ty.ret.is_indirect() { | ||
(SaveIn(llargs[0]), &llargs[1..]) | ||
} else if let Some(dest) = ret_dest { | ||
(SaveIn(dest.llval), &llargs[..]) | ||
} else { | ||
(Ignore, &llargs[..]) | ||
let (dest, llargs) = match ret_dest { | ||
_ if fn_ty.ret.is_indirect() => { | ||
(SaveIn(llargs[0]), &llargs[1..]) | ||
} | ||
ReturnDest::Nothing => (Ignore, &llargs[..]), | ||
ReturnDest::IndirectOperand(dst, _) | | ||
ReturnDest::Store(dst) => (SaveIn(dst), &llargs[..]), | ||
ReturnDest::DirectOperand(_) => | ||
bug!("Cannot use direct operand with an intrinsic call") | ||
}; | ||
|
||
bcx.with_block(|bcx| { | ||
|
@@ -292,6 +334,16 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | |
// bcx.unreachable(); | ||
} | ||
}); | ||
|
||
if let ReturnDest::IndirectOperand(dst, _) = ret_dest { | ||
// Make a fake operand for store_return | ||
let op = OperandRef { | ||
val: OperandValue::Ref(dst), | ||
ty: sig.0.output.unwrap() | ||
}; | ||
self.store_return(&bcx, ret_dest, fn_ty.ret, op); | ||
} | ||
|
||
return; | ||
} | ||
Fn(f) => f, | ||
|
@@ -321,9 +373,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | |
if destination.is_some() { | ||
let ret_bcx = ret_bcx.build(); | ||
ret_bcx.at_start(|ret_bcx| { | ||
if let Some(ret_dest) = ret_dest { | ||
fn_ty.ret.store(&ret_bcx, invokeret, ret_dest.llval); | ||
} | ||
let op = OperandRef { | ||
val: OperandValue::Immediate(invokeret), | ||
ty: sig.0.output.unwrap() | ||
}; | ||
self.store_return(&ret_bcx, ret_dest, fn_ty.ret, op); | ||
for op in args { | ||
self.set_operand_dropped(&ret_bcx, op); | ||
} | ||
|
@@ -333,9 +387,11 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | |
let llret = bcx.call(fn_ptr, &llargs, cleanup_bundle.as_ref()); | ||
fn_ty.apply_attrs_callsite(llret); | ||
if let Some((_, target)) = *destination { | ||
if let Some(ret_dest) = ret_dest { | ||
fn_ty.ret.store(&bcx, llret, ret_dest.llval); | ||
} | ||
let op = OperandRef { | ||
val: OperandValue::Immediate(llret), | ||
ty: sig.0.output.unwrap() | ||
}; | ||
self.store_return(&bcx, ret_dest, fn_ty.ret, op); | ||
for op in args { | ||
self.set_operand_dropped(&bcx, op); | ||
} | ||
|
@@ -544,4 +600,58 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> { | |
pub fn llblock(&self, bb: mir::BasicBlock) -> BasicBlockRef { | ||
self.blocks[bb.index()].llbb | ||
} | ||
|
||
fn trans_transmute(&mut self, bcx: &BlockAndBuilder<'bcx, 'tcx>, | ||
src: &mir::Operand<'tcx>, dst: LvalueRef<'tcx>) { | ||
let mut val = self.trans_operand(bcx, src); | ||
if let ty::TyFnDef(def_id, substs, _) = val.ty.sty { | ||
let llouttype = type_of::type_of(bcx.ccx(), dst.ty.to_ty(bcx.tcx())); | ||
let out_type_size = llbitsize_of_real(bcx.ccx(), llouttype); | ||
if out_type_size != 0 { | ||
// FIXME #19925 Remove this hack after a release cycle. | ||
let f = Callee::def(bcx.ccx(), def_id, substs); | ||
let datum = f.reify(bcx.ccx()); | ||
val = OperandRef { | ||
val: OperandValue::Immediate(datum.val), | ||
ty: datum.ty | ||
}; | ||
} | ||
} | ||
|
||
let llty = type_of::type_of(bcx.ccx(), val.ty); | ||
let cast_ptr = bcx.pointercast(dst.llval, llty.ptr_to()); | ||
self.store_operand(bcx, cast_ptr, val); | ||
} | ||
|
||
// Stores the return value of a function call into it's final location. | ||
fn store_return(&mut self, | ||
bcx: &BlockAndBuilder<'bcx, 'tcx>, | ||
dest: ReturnDest, | ||
ret_ty: ArgType, | ||
op: OperandRef<'tcx>) { | ||
use self::ReturnDest::*; | ||
|
||
match dest { | ||
Nothing => (), | ||
Store(dst) => ret_ty.store(bcx, op.immediate(), dst), | ||
IndirectOperand(tmp, idx) => { | ||
let op = self.trans_load(bcx, tmp, op.ty); | ||
self.temps[idx as usize] = TempRef::Operand(Some(op)); | ||
} | ||
DirectOperand(idx) => { | ||
self.temps[idx as usize] = TempRef::Operand(Some(op)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
enum ReturnDest { | ||
// Do nothing, the return value is indirect or ignored | ||
Nothing, | ||
// Store the return value to the pointer | ||
Store(ValueRef), | ||
// Stores an indirect return value to an operand temporary lvalue | ||
IndirectOperand(ValueRef, u32), | ||
// Stores a direct return value to an operand temporary lvalue | ||
DirectOperand(u32) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
who is creating invalid regions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, I think it might be a cross-crate thing as the out-of-bounds index was much larger the size of the map and appeared while compiling a function from another crate.