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:
Nick Fitzgerald
2019-11-05 14:36:03 -08:00
committed by GitHub
parent 45fb377457
commit a49483408c
29 changed files with 3206 additions and 69 deletions

View File

@@ -6,6 +6,7 @@ use crate::abi::{legalize_args, ArgAction, ArgAssigner, ValueConversion};
use crate::ir::{self, AbiParam, ArgumentExtension, ArgumentLoc, Type};
use crate::isa::RegClass;
use crate::regalloc::RegisterSet;
use alloc::borrow::Cow;
use core::i32;
use target_lexicon::Triple;
@@ -78,11 +79,13 @@ impl ArgAssigner for Args {
}
/// Legalize `sig`.
pub fn legalize_signature(sig: &mut ir::Signature, triple: &Triple, _current: bool) {
pub fn legalize_signature(sig: &mut Cow<ir::Signature>, triple: &Triple, _current: bool) {
let bits = triple.pointer_width().unwrap().bits();
let mut args = Args::new(bits);
legalize_args(&mut sig.params, &mut args);
if let Some(new_params) = legalize_args(&sig.params, &mut args) {
sig.to_mut().params = new_params;
}
}
/// Get register class for a type appearing in a legalized signature.

View File

@@ -15,6 +15,7 @@ use crate::isa::enc_tables::{self as shared_enc_tables, lookup_enclist, Encoding
use crate::isa::Builder as IsaBuilder;
use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa};
use crate::regalloc;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::fmt;
use target_lexicon::{Architecture, Triple};
@@ -100,7 +101,7 @@ impl TargetIsa for Isa {
)
}
fn legalize_signature(&self, sig: &mut ir::Signature, current: bool) {
fn legalize_signature(&self, sig: &mut Cow<ir::Signature>, current: bool) {
abi::legalize_signature(sig, &self.triple, current)
}

View File

@@ -5,10 +5,11 @@ use crate::ir;
use crate::isa::RegClass;
use crate::regalloc::RegisterSet;
use crate::settings as shared_settings;
use alloc::borrow::Cow;
/// Legalize `sig`.
pub fn legalize_signature(
_sig: &mut ir::Signature,
_sig: &mut Cow<ir::Signature>,
_flags: &shared_settings::Flags,
_current: bool,
) {

View File

@@ -15,6 +15,7 @@ use crate::isa::enc_tables::{lookup_enclist, Encodings};
use crate::isa::Builder as IsaBuilder;
use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa};
use crate::regalloc;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::fmt;
use target_lexicon::Triple;
@@ -88,7 +89,7 @@ impl TargetIsa for Isa {
)
}
fn legalize_signature(&self, sig: &mut ir::Signature, current: bool) {
fn legalize_signature(&self, sig: &mut Cow<ir::Signature>, current: bool) {
abi::legalize_signature(sig, &self.shared_flags, current)
}

View File

@@ -63,6 +63,7 @@ use crate::result::CodegenResult;
use crate::settings;
use crate::settings::SetResult;
use crate::timing;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::fmt;
@@ -315,7 +316,7 @@ pub trait TargetIsa: fmt::Display + Sync {
/// Arguments and return values for the caller's frame pointer and other callee-saved registers
/// should not be added by this function. These arguments are not added until after register
/// allocation.
fn legalize_signature(&self, sig: &mut ir::Signature, current: bool);
fn legalize_signature(&self, sig: &mut Cow<ir::Signature>, current: bool);
/// Get the register class that should be used to represent an ABI argument or return value of
/// type `ty`. This should be the top-level register class that contains the argument

View File

@@ -11,6 +11,7 @@ use crate::abi::{legalize_args, ArgAction, ArgAssigner, ValueConversion};
use crate::ir::{self, AbiParam, ArgumentExtension, ArgumentLoc, ArgumentPurpose, Type};
use crate::isa::RegClass;
use crate::regalloc::RegisterSet;
use alloc::borrow::Cow;
use core::i32;
use target_lexicon::Triple;
@@ -88,7 +89,7 @@ impl ArgAssigner for Args {
/// Legalize `sig` for RISC-V.
pub fn legalize_signature(
sig: &mut ir::Signature,
sig: &mut Cow<ir::Signature>,
triple: &Triple,
isa_flags: &settings::Flags,
current: bool,
@@ -96,10 +97,14 @@ pub fn legalize_signature(
let bits = triple.pointer_width().unwrap().bits();
let mut args = Args::new(bits, isa_flags.enable_e());
legalize_args(&mut sig.params, &mut args);
if let Some(new_params) = legalize_args(&sig.params, &mut args) {
sig.to_mut().params = new_params;
}
let mut rets = Args::new(bits, isa_flags.enable_e());
legalize_args(&mut sig.returns, &mut rets);
if let Some(new_returns) = legalize_args(&sig.returns, &mut rets) {
sig.to_mut().returns = new_returns;
}
if current {
let ptr = Type::int(u16::from(bits)).unwrap();
@@ -110,8 +115,8 @@ pub fn legalize_signature(
// in any register, but a micro-architecture with a return address predictor will only
// recognize it as a return if the address is in `x1`.
let link = AbiParam::special_reg(ptr, ArgumentPurpose::Link, GPR.unit(1));
sig.params.push(link);
sig.returns.push(link);
sig.to_mut().params.push(link);
sig.to_mut().returns.push(link);
}
}

View File

@@ -15,6 +15,7 @@ use crate::isa::enc_tables::{self as shared_enc_tables, lookup_enclist, Encoding
use crate::isa::Builder as IsaBuilder;
use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa};
use crate::regalloc;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use core::fmt;
use target_lexicon::{PointerWidth, Triple};
@@ -95,7 +96,7 @@ impl TargetIsa for Isa {
)
}
fn legalize_signature(&self, sig: &mut ir::Signature, current: bool) {
fn legalize_signature(&self, sig: &mut Cow<ir::Signature>, current: bool) {
abi::legalize_signature(sig, &self.triple, &self.isa_flags, current)
}

View File

@@ -17,6 +17,7 @@ use crate::isa::{CallConv, RegClass, RegUnit, TargetIsa};
use crate::regalloc::RegisterSet;
use crate::result::CodegenResult;
use crate::stack_layout::layout_stack;
use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::i32;
use target_lexicon::{PointerWidth, Triple};
@@ -166,9 +167,117 @@ impl ArgAssigner for Args {
}
}
/// Get the number of general-purpose and floating-point registers required to
/// hold the given `AbiParam` returns.
fn num_return_registers_required<'a>(
word_bit_size: u8,
call_conv: CallConv,
shared_flags: &shared_settings::Flags,
isa_flags: &isa_settings::Flags,
return_params: impl IntoIterator<Item = &'a AbiParam>,
) -> (usize, usize) {
// Pretend we have "infinite" registers to give out, since we aren't
// actually assigning `AbiParam`s to registers yet, just seeing how many
// registers we would need in order to fit all the `AbiParam`s in registers.
let gprs = &[RU::rax; 128];
let fpr_limit = std::usize::MAX;
let mut assigner = Args::new(
word_bit_size,
gprs,
fpr_limit,
call_conv,
shared_flags,
isa_flags,
);
let mut gprs_required = 0;
let mut fprs_required = 0;
for param in return_params {
match param.location {
ArgumentLoc::Unassigned => {
// Let this fall through so that we assign it a location and
// account for how many registers it ends up requiring below...
}
ArgumentLoc::Reg(_) => {
// This is already assigned to a register. Count it.
if param.value_type.is_float() {
fprs_required += 1;
} else {
gprs_required += 1;
}
continue;
}
_ => {
// It is already assigned, but not to a register. Skip it.
continue;
}
}
// We're going to mutate the type as it gets converted, so make our own
// copy that isn't visible to the outside world.
let mut param = param.clone();
let mut split_factor = 1;
loop {
match assigner.assign(&param) {
ArgAction::Convert(ValueConversion::IntSplit) => {
split_factor *= 2;
param.value_type = param.value_type.half_width().unwrap();
}
ArgAction::Convert(ValueConversion::VectorSplit) => {
split_factor *= 2;
param.value_type = param.value_type.half_vector().unwrap();
}
ArgAction::Assign(ArgumentLoc::Reg(_))
| ArgAction::Convert(ValueConversion::IntBits)
| ArgAction::Convert(ValueConversion::Sext(_))
| ArgAction::Convert(ValueConversion::Uext(_)) => {
// Ok! We can fit this (potentially split) value into a
// register! Add the number of params we split the parameter
// into to our current counts.
if param.value_type.is_float() {
fprs_required += split_factor;
} else {
gprs_required += split_factor;
}
// But we also have to call `assign` once for each split value, to
// update `assigner`'s internal state.
for _ in 1..split_factor {
match assigner.assign(&param) {
ArgAction::Assign(_)
| ArgAction::Convert(ValueConversion::IntBits)
| ArgAction::Convert(ValueConversion::Sext(_))
| ArgAction::Convert(ValueConversion::Uext(_)) => {
continue;
}
otherwise => panic!(
"unexpected action after first split succeeded: {:?}",
otherwise
),
}
}
// Continue to the next param.
break;
}
ArgAction::Assign(loc) => panic!(
"unexpected location assignment, should have had enough registers: {:?}",
loc
),
}
}
}
(gprs_required, fprs_required)
}
/// Legalize `sig`.
pub fn legalize_signature(
sig: &mut ir::Signature,
sig: &mut Cow<ir::Signature>,
triple: &Triple,
_current: bool,
shared_flags: &shared_settings::Flags,
@@ -207,9 +316,7 @@ pub fn legalize_signature(
}
}
legalize_args(&mut sig.params, &mut args);
let (regs, fpr_limit) = if sig.call_conv.extends_windows_fastcall() {
let (ret_regs, ret_fpr_limit) = if sig.call_conv.extends_windows_fastcall() {
// windows-x64 calling convention only uses XMM0 or RAX for return values
(&RET_GPRS_WIN_FASTCALL_X64[..], 1)
} else {
@@ -218,13 +325,77 @@ pub fn legalize_signature(
let mut rets = Args::new(
bits,
regs,
fpr_limit,
ret_regs,
ret_fpr_limit,
sig.call_conv,
shared_flags,
isa_flags,
);
legalize_args(&mut sig.returns, &mut rets);
if sig.is_multi_return() && {
// Even if it is multi-return, see if the return values will fit into
// our available return registers.
let (gprs_required, fprs_required) = num_return_registers_required(
bits,
sig.call_conv,
shared_flags,
isa_flags,
&sig.returns,
);
gprs_required > ret_regs.len() || fprs_required > ret_fpr_limit
} {
debug_assert!(!sig.uses_struct_return_param());
// We're using the first register for the return pointer parameter.
let mut ret_ptr_param = AbiParam {
value_type: args.pointer_type,
purpose: ArgumentPurpose::StructReturn,
extension: ArgumentExtension::None,
location: ArgumentLoc::Unassigned,
};
match args.assign(&ret_ptr_param) {
ArgAction::Assign(ArgumentLoc::Reg(reg)) => {
ret_ptr_param.location = ArgumentLoc::Reg(reg);
sig.to_mut().params.push(ret_ptr_param);
}
_ => unreachable!("return pointer should always get a register assignment"),
}
// We're using the first return register for the return pointer (like
// sys v does).
let mut ret_ptr_return = AbiParam {
value_type: args.pointer_type,
purpose: ArgumentPurpose::StructReturn,
extension: ArgumentExtension::None,
location: ArgumentLoc::Unassigned,
};
match rets.assign(&ret_ptr_return) {
ArgAction::Assign(ArgumentLoc::Reg(reg)) => {
ret_ptr_return.location = ArgumentLoc::Reg(reg);
sig.to_mut().returns.push(ret_ptr_return);
}
_ => unreachable!("return pointer should always get a register assignment"),
}
sig.to_mut().returns.retain(|ret| {
// Either this is the return pointer, in which case we want to keep
// it, or else assume that it is assigned for a reason and doesn't
// conflict with our return pointering legalization.
debug_assert_eq!(
ret.location.is_assigned(),
ret.purpose != ArgumentPurpose::Normal
);
ret.location.is_assigned()
});
}
if let Some(new_params) = legalize_args(&sig.params, &mut args) {
sig.to_mut().params = new_params;
}
if let Some(new_returns) = legalize_args(&sig.returns, &mut rets) {
sig.to_mut().returns = new_returns;
}
}
/// Get register class for a type appearing in a legalized signature.

View File

@@ -18,6 +18,7 @@ use crate::isa::{EncInfo, RegClass, RegInfo, TargetIsa};
use crate::regalloc;
use crate::result::CodegenResult;
use crate::timing;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::fmt;
@@ -107,7 +108,7 @@ impl TargetIsa for Isa {
)
}
fn legalize_signature(&self, sig: &mut ir::Signature, current: bool) {
fn legalize_signature(&self, sig: &mut Cow<ir::Signature>, current: bool) {
abi::legalize_signature(
sig,
&self.triple,