Skip to content

Commit

Permalink
Avoid depending on enable_multi_ret_implicit_sret for libcalls
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Dec 5, 2024
1 parent 0974099 commit 17365e9
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
args: &[Value],
) -> Cow<'_, [Value]> {
if self.tcx.sess.target.is_like_windows {
let (mut params, mut args): (Vec<_>, Vec<_>) = params
let (params, args): (Vec<_>, Vec<_>) = params
.into_iter()
.zip(args)
.map(|(param, &arg)| {
Expand All @@ -140,29 +140,45 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
})
.unzip();

let indirect_ret_val = returns.len() == 1 && returns[0].value_type == types::I128;

if indirect_ret_val {
params.insert(0, AbiParam::new(self.pointer_type));
let ret_ptr = self.create_stack_slot(16, 16);
args.insert(0, ret_ptr.get_addr(self));
self.lib_call_unadjusted(name, params, vec![], &args);
return Cow::Owned(vec![ret_ptr.load(self, types::I128, MemFlags::trusted())]);
self.lib_call_unadjusted(name, params, returns, &args)
} else {
return self.lib_call_unadjusted(name, params, returns, &args);
}
self.lib_call_unadjusted(name, params, returns, args)
}

self.lib_call_unadjusted(name, params, returns, args)
}

pub(crate) fn lib_call_unadjusted(
&mut self,
name: &str,
params: Vec<AbiParam>,
mut params: Vec<AbiParam>,
returns: Vec<AbiParam>,
args: &[Value],
) -> Cow<'_, [Value]> {
let adjust_ret_param =
if self.tcx.sess.target.is_like_windows || self.tcx.sess.target.arch == "s390x" {
returns.len() == 1 && returns[0].value_type == types::I128
} else {
false
};

if adjust_ret_param {
params.insert(0, AbiParam::new(self.pointer_type));
let ret_ptr = self.create_stack_slot(16, 16);
let mut args = args.to_vec();
args.insert(0, ret_ptr.get_addr(self));
self.lib_call_inner(name, params, vec![], &args);
Cow::Owned(vec![ret_ptr.load(self, types::I128, MemFlags::trusted())])
} else {
Cow::Borrowed(self.lib_call_inner(name, params, returns, &args))
}
}

fn lib_call_inner(
&mut self,
name: &str,
params: Vec<AbiParam>,
returns: Vec<AbiParam>,
args: &[Value],
) -> &[Value] {
let sig = Signature { params, returns, call_conv: self.target_config.default_call_conv };
let func_id = self.module.declare_function(name, Linkage::Import, &sig).unwrap();
let func_ref = self.module.declare_func_in_func(func_id, &mut self.bcx.func);
Expand All @@ -175,7 +191,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
}
let results = self.bcx.inst_results(call_inst);
assert!(results.len() <= 2, "{}", results.len());
Cow::Borrowed(results)
results
}
}

Expand Down

0 comments on commit 17365e9

Please sign in to comment.