Many multi-value returns (#1147)
* Add x86 encodings for `bint` converting to `i8` and `i16`
* Introduce tests for many multi-value returns
* Support arbitrary numbers of return values
This commit implements support for returning an arbitrary number of return
values from a function. During legalization we transform multi-value signatures
to take a struct return ("sret") return pointer, instead of returning its values
in registers. Callers allocate the sret space in their stack frame and pass a
pointer to it into the caller, and once the caller returns to them, they load
the return values back out of the sret stack slot. The callee's return
operations are legalized to store the return values through the given sret
pointer.
* Keep track of old, pre-legalized signatures
When legalizing a call or return for its new legalized signature, we may need to
look at the old signature in order to figure out how to legalize the call or
return.
* Add test for multi-value returns and `call_indirect`
* Encode bool -> int x86 instructions in a loop
* Rename `Signature::uses_sret` to `Signature::uses_struct_return_param`
* Rename `p` to `param`
* Add a clarifiying comment in `num_registers_required`
* Rename `num_registers_required` to `num_return_registers_required`
* Re-add newline
* Handle already-assigned parameters in `num_return_registers_required`
* Document what some debug assertions are checking for
* Make "illegalizing" closure's control flow simpler
* Add unit tests and comments for our rounding-up-to-the-next-multiple-of-a-power-of-2 function
* Use `append_isnt_arg` instead of doing the same thing manually
* Fix grammar in comment
* Add `Signature::uses_special_{param,return}` helper functions
* Inline the definition of `legalize_type_for_sret_load` for readability
* Move sret legalization debug assertions out into their own function
* Add `round_up_to_multiple_of_type_align` helper for readability
* Add a debug assertion that we aren't removing the wrong return value
* Rename `RetPtr` stack slots to `StructReturnSlot`
* Make `legalize_type_for_sret_store` more symmetrical to `legalized_type_for_sret`
* rustfmt
* Remove unnecessary loop labels
* Do not pre-assign offsets to struct return stack slots
Instead, let the existing frame layout algorithm decide where they should go.
* Expand "sret" into explicit "struct return" in doc comment
* typo: "than" -> "then" in comment
* Fold test's debug message into the assertion itself
This commit is contained in:
@@ -62,6 +62,9 @@ pub struct DataFlowGraph {
|
||||
/// well as the external function references.
|
||||
pub signatures: PrimaryMap<SigRef, Signature>,
|
||||
|
||||
/// The pre-legalization signature for each entry in `signatures`, if any.
|
||||
pub old_signatures: SecondaryMap<SigRef, Option<Signature>>,
|
||||
|
||||
/// External function references. These are functions that can be called directly.
|
||||
pub ext_funcs: PrimaryMap<FuncRef, ExtFuncData>,
|
||||
|
||||
@@ -85,6 +88,7 @@ impl DataFlowGraph {
|
||||
value_lists: ValueListPool::new(),
|
||||
values: PrimaryMap::new(),
|
||||
signatures: PrimaryMap::new(),
|
||||
old_signatures: SecondaryMap::new(),
|
||||
ext_funcs: PrimaryMap::new(),
|
||||
values_labels: None,
|
||||
constants: ConstantPool::new(),
|
||||
|
||||
@@ -55,6 +55,53 @@ impl Signature {
|
||||
pub fn special_param_index(&self, purpose: ArgumentPurpose) -> Option<usize> {
|
||||
self.params.iter().rposition(|arg| arg.purpose == purpose)
|
||||
}
|
||||
|
||||
/// Find the index of a presumed unique special-purpose parameter.
|
||||
pub fn special_return_index(&self, purpose: ArgumentPurpose) -> Option<usize> {
|
||||
self.returns.iter().rposition(|arg| arg.purpose == purpose)
|
||||
}
|
||||
|
||||
/// Does this signature have a parameter whose `ArgumentPurpose` is
|
||||
/// `purpose`?
|
||||
pub fn uses_special_param(&self, purpose: ArgumentPurpose) -> bool {
|
||||
self.special_param_index(purpose).is_some()
|
||||
}
|
||||
|
||||
/// Does this signature have a return whose `ArgumentPurpose` is `purpose`?
|
||||
pub fn uses_special_return(&self, purpose: ArgumentPurpose) -> bool {
|
||||
self.special_return_index(purpose).is_some()
|
||||
}
|
||||
|
||||
/// How many special parameters does this function have?
|
||||
pub fn num_special_params(&self) -> usize {
|
||||
self.params
|
||||
.iter()
|
||||
.filter(|p| p.purpose != ArgumentPurpose::Normal)
|
||||
.count()
|
||||
}
|
||||
|
||||
/// How many special returns does this function have?
|
||||
pub fn num_special_returns(&self) -> usize {
|
||||
self.returns
|
||||
.iter()
|
||||
.filter(|r| r.purpose != ArgumentPurpose::Normal)
|
||||
.count()
|
||||
}
|
||||
|
||||
/// Does this signature take an struct return pointer parameter?
|
||||
pub fn uses_struct_return_param(&self) -> bool {
|
||||
self.uses_special_param(ArgumentPurpose::StructReturn)
|
||||
}
|
||||
|
||||
/// Does this return more than one normal value? (Pre-struct return
|
||||
/// legalization)
|
||||
pub fn is_multi_return(&self) -> bool {
|
||||
self.returns
|
||||
.iter()
|
||||
.filter(|r| r.purpose == ArgumentPurpose::Normal)
|
||||
.count()
|
||||
> 1
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper type capable of displaying a `Signature` with correct register names.
|
||||
|
||||
@@ -34,6 +34,10 @@ pub struct Function {
|
||||
/// Signature of this function.
|
||||
pub signature: Signature,
|
||||
|
||||
/// The old signature of this function, before the most recent legalization,
|
||||
/// if any.
|
||||
pub old_signature: Option<Signature>,
|
||||
|
||||
/// Stack slots allocated in this function.
|
||||
pub stack_slots: StackSlots,
|
||||
|
||||
@@ -96,6 +100,7 @@ impl Function {
|
||||
Self {
|
||||
name,
|
||||
signature: sig,
|
||||
old_signature: None,
|
||||
stack_slots: StackSlots::new(),
|
||||
global_values: PrimaryMap::new(),
|
||||
heaps: PrimaryMap::new(),
|
||||
|
||||
@@ -64,6 +64,15 @@ pub enum StackSlotKind {
|
||||
/// stack slots are only valid while setting up a call.
|
||||
OutgoingArg,
|
||||
|
||||
/// Space allocated in the caller's frame for the callee's return values
|
||||
/// that are passed out via return pointer.
|
||||
///
|
||||
/// If there are more return values than registers available for the callee's calling
|
||||
/// convention, or the return value is larger than the available registers' space, then we
|
||||
/// allocate stack space in this frame and pass a pointer to the callee, which then writes its
|
||||
/// return values into this space.
|
||||
StructReturnSlot,
|
||||
|
||||
/// An emergency spill slot.
|
||||
///
|
||||
/// Emergency slots are allocated late when the register's constraint solver needs extra space
|
||||
@@ -81,6 +90,7 @@ impl FromStr for StackSlotKind {
|
||||
"spill_slot" => Ok(SpillSlot),
|
||||
"incoming_arg" => Ok(IncomingArg),
|
||||
"outgoing_arg" => Ok(OutgoingArg),
|
||||
"sret_slot" => Ok(StructReturnSlot),
|
||||
"emergency_slot" => Ok(EmergencySlot),
|
||||
_ => Err(()),
|
||||
}
|
||||
@@ -95,6 +105,7 @@ impl fmt::Display for StackSlotKind {
|
||||
SpillSlot => "spill_slot",
|
||||
IncomingArg => "incoming_arg",
|
||||
OutgoingArg => "outgoing_arg",
|
||||
StructReturnSlot => "sret_slot",
|
||||
EmergencySlot => "emergency_slot",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user