Skip to content

Commit 90a40a6

Browse files
committed
Auto merge of #131955 - SpriteOvO:riscv-int-arg-attr, r=<try>
Set `signext` or `zeroext` for integer arguments on RISC-V and LoongArch64 Fixes #114508. This PR contains 3 commits: - the first one introduces a new function `adjust_for_rust_abi` in `rustc_target`, and moves the x86 specific adjustment code into it; - the second one adds RISC-V specific adjustment code into it, which sets `signext` or `zeroext` attribute for integer arguments. - **UPDATE**: added the 3rd commit for apply the same adjustment for LoongArch64. r? `@workingjubilee` CC `@coastalwhite` `@Urgau` `@topperc` `@michaelmaitland` try-job: dist-loongarch64-linux try-job: dist-riscv64-linux try-job: test-various try-job: i686-gnu-nopt try-job: i686-gnu
2 parents 4392847 + 2dfd4d0 commit 90a40a6

20 files changed

+434
-173
lines changed

compiler/rustc_target/src/callconv/loongarch.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::abi::call::{ArgAbi, ArgExtension, CastTarget, FnAbi, PassMode, Reg, RegKind, Uniform};
22
use crate::abi::{self, Abi, FieldsShape, HasDataLayout, Size, TyAbiInterface, TyAndLayout};
33
use crate::spec::HasTargetSpec;
4+
use crate::spec::abi::Abi as SpecAbi;
45

56
#[derive(Copy, Clone)]
67
enum RegPassKind {
@@ -359,3 +360,30 @@ where
359360
);
360361
}
361362
}
363+
364+
pub(crate) fn compute_rust_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, abi: SpecAbi)
365+
where
366+
Ty: TyAbiInterface<'a, C> + Copy,
367+
C: HasDataLayout + HasTargetSpec,
368+
{
369+
if abi == SpecAbi::RustIntrinsic {
370+
return;
371+
}
372+
373+
let grlen = cx.data_layout().pointer_size.bits();
374+
375+
for arg in fn_abi.args.iter_mut() {
376+
if arg.is_ignore() {
377+
continue;
378+
}
379+
380+
// LLVM integers types do not differentiate between signed or unsigned integers.
381+
// Some LoongArch instructions do not have a `.w` suffix version, they use all the
382+
// GRLEN bits. By explicitly setting the `signext` or `zeroext` attribute
383+
// according to signedness to avoid unnecessary integer extending instructions.
384+
//
385+
// This is similar to the RISC-V case, see
386+
// https://github.com/rust-lang/rust/issues/114508 for details.
387+
extend_integer_width(arg, grlen);
388+
}
389+
}

compiler/rustc_target/src/callconv/mod.rs

+116-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use std::fmt;
21
use std::str::FromStr;
2+
use std::{fmt, iter};
33

4+
use rustc_abi::AddressSpace;
5+
use rustc_abi::Primitive::Pointer;
46
pub use rustc_abi::{Reg, RegKind};
57
use rustc_macros::HashStable_Generic;
68
use rustc_span::Symbol;
79

810
use crate::abi::{self, Abi, Align, HasDataLayout, Size, TyAbiInterface, TyAndLayout};
11+
use crate::spec::abi::Abi as SpecAbi;
912
use crate::spec::{self, HasTargetSpec, HasWasmCAbiOpt, WasmCAbi};
1013

1114
mod aarch64;
@@ -716,6 +719,118 @@ impl<'a, Ty> FnAbi<'a, Ty> {
716719

717720
Ok(())
718721
}
722+
723+
pub fn adjust_for_rust_abi<C>(&mut self, cx: &C, abi: SpecAbi)
724+
where
725+
Ty: TyAbiInterface<'a, C> + Copy,
726+
C: HasDataLayout + HasTargetSpec,
727+
{
728+
let spec = cx.target_spec();
729+
match &spec.arch[..] {
730+
"x86" => x86::compute_rust_abi_info(cx, self, abi),
731+
"riscv32" | "riscv64" => riscv::compute_rust_abi_info(cx, self, abi),
732+
"loongarch64" => loongarch::compute_rust_abi_info(cx, self, abi),
733+
_ => {}
734+
};
735+
736+
for (arg_idx, arg) in self
737+
.args
738+
.iter_mut()
739+
.enumerate()
740+
.map(|(idx, arg)| (Some(idx), arg))
741+
.chain(iter::once((None, &mut self.ret)))
742+
{
743+
if arg.is_ignore() {
744+
continue;
745+
}
746+
747+
if arg_idx.is_none() && arg.layout.size > Pointer(AddressSpace::DATA).size(cx) * 2 {
748+
// Return values larger than 2 registers using a return area
749+
// pointer. LLVM and Cranelift disagree about how to return
750+
// values that don't fit in the registers designated for return
751+
// values. LLVM will force the entire return value to be passed
752+
// by return area pointer, while Cranelift will look at each IR level
753+
// return value independently and decide to pass it in a
754+
// register or not, which would result in the return value
755+
// being passed partially in registers and partially through a
756+
// return area pointer.
757+
//
758+
// While Cranelift may need to be fixed as the LLVM behavior is
759+
// generally more correct with respect to the surface language,
760+
// forcing this behavior in rustc itself makes it easier for
761+
// other backends to conform to the Rust ABI and for the C ABI
762+
// rustc already handles this behavior anyway.
763+
//
764+
// In addition LLVM's decision to pass the return value in
765+
// registers or using a return area pointer depends on how
766+
// exactly the return type is lowered to an LLVM IR type. For
767+
// example `Option<u128>` can be lowered as `{ i128, i128 }`
768+
// in which case the x86_64 backend would use a return area
769+
// pointer, or it could be passed as `{ i32, i128 }` in which
770+
// case the x86_64 backend would pass it in registers by taking
771+
// advantage of an LLVM ABI extension that allows using 3
772+
// registers for the x86_64 sysv call conv rather than the
773+
// officially specified 2 registers.
774+
//
775+
// FIXME: Technically we should look at the amount of available
776+
// return registers rather than guessing that there are 2
777+
// registers for return values. In practice only a couple of
778+
// architectures have less than 2 return registers. None of
779+
// which supported by Cranelift.
780+
//
781+
// NOTE: This adjustment is only necessary for the Rust ABI as
782+
// for other ABI's the calling convention implementations in
783+
// rustc_target already ensure any return value which doesn't
784+
// fit in the available amount of return registers is passed in
785+
// the right way for the current target.
786+
arg.make_indirect();
787+
continue;
788+
}
789+
790+
match arg.layout.abi {
791+
Abi::Aggregate { .. } => {}
792+
793+
// This is a fun case! The gist of what this is doing is
794+
// that we want callers and callees to always agree on the
795+
// ABI of how they pass SIMD arguments. If we were to *not*
796+
// make these arguments indirect then they'd be immediates
797+
// in LLVM, which means that they'd used whatever the
798+
// appropriate ABI is for the callee and the caller. That
799+
// means, for example, if the caller doesn't have AVX
800+
// enabled but the callee does, then passing an AVX argument
801+
// across this boundary would cause corrupt data to show up.
802+
//
803+
// This problem is fixed by unconditionally passing SIMD
804+
// arguments through memory between callers and callees
805+
// which should get them all to agree on ABI regardless of
806+
// target feature sets. Some more information about this
807+
// issue can be found in #44367.
808+
//
809+
// Note that the intrinsic ABI is exempt here as
810+
// that's how we connect up to LLVM and it's unstable
811+
// anyway, we control all calls to it in libstd.
812+
Abi::Vector { .. } if abi != SpecAbi::RustIntrinsic && spec.simd_types_indirect => {
813+
arg.make_indirect();
814+
continue;
815+
}
816+
817+
_ => continue,
818+
}
819+
// Compute `Aggregate` ABI.
820+
821+
let is_indirect_not_on_stack =
822+
matches!(arg.mode, PassMode::Indirect { on_stack: false, .. });
823+
assert!(is_indirect_not_on_stack);
824+
825+
let size = arg.layout.size;
826+
if !arg.layout.is_unsized() && size <= Pointer(AddressSpace::DATA).size(cx) {
827+
// We want to pass small aggregates as immediates, but using
828+
// an LLVM aggregate type for this leads to bad optimizations,
829+
// so we pick an appropriately sized integer type instead.
830+
arg.cast_to(Reg { kind: RegKind::Integer, size });
831+
}
832+
}
833+
}
719834
}
720835

721836
impl FromStr for Conv {

compiler/rustc_target/src/callconv/riscv.rs

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use crate::abi::call::{ArgAbi, ArgExtension, CastTarget, FnAbi, PassMode, Reg, RegKind, Uniform};
88
use crate::abi::{self, Abi, FieldsShape, HasDataLayout, Size, TyAbiInterface, TyAndLayout};
99
use crate::spec::HasTargetSpec;
10+
use crate::spec::abi::Abi as SpecAbi;
1011

1112
#[derive(Copy, Clone)]
1213
enum RegPassKind {
@@ -365,3 +366,29 @@ where
365366
);
366367
}
367368
}
369+
370+
pub(crate) fn compute_rust_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, abi: SpecAbi)
371+
where
372+
Ty: TyAbiInterface<'a, C> + Copy,
373+
C: HasDataLayout + HasTargetSpec,
374+
{
375+
if abi == SpecAbi::RustIntrinsic {
376+
return;
377+
}
378+
379+
let xlen = cx.data_layout().pointer_size.bits();
380+
381+
for arg in fn_abi.args.iter_mut() {
382+
if arg.is_ignore() {
383+
continue;
384+
}
385+
386+
// LLVM integers types do not differentiate between signed or unsigned integers.
387+
// Some RISC-V instructions do not have a `.w` suffix version, they use all the
388+
// XLEN bits. By explicitly setting the `signext` or `zeroext` attribute
389+
// according to signedness to avoid unnecessary integer extending instructions.
390+
//
391+
// See https://github.com/rust-lang/rust/issues/114508 for details.
392+
extend_integer_width(arg, xlen);
393+
}
394+
}

compiler/rustc_target/src/callconv/x86.rs

+39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
use rustc_abi::Float::*;
2+
use rustc_abi::Primitive::Float;
3+
14
use crate::abi::call::{ArgAttribute, FnAbi, PassMode, Reg, RegKind};
25
use crate::abi::{Abi, Align, HasDataLayout, TyAbiInterface, TyAndLayout};
36
use crate::spec::HasTargetSpec;
7+
use crate::spec::abi::Abi as SpecAbi;
48

59
#[derive(PartialEq)]
610
pub(crate) enum Flavor {
@@ -183,3 +187,38 @@ where
183187
}
184188
}
185189
}
190+
191+
pub(crate) fn compute_rust_abi_info<'a, Ty, C>(_cx: &C, fn_abi: &mut FnAbi<'a, Ty>, abi: SpecAbi)
192+
where
193+
Ty: TyAbiInterface<'a, C> + Copy,
194+
C: HasDataLayout + HasTargetSpec,
195+
{
196+
// Avoid returning floats in x87 registers on x86 as loading and storing from x87
197+
// registers will quiet signalling NaNs.
198+
if !fn_abi.ret.is_ignore()
199+
// Intrinsics themselves are not actual "real" functions, so theres no need to change their ABIs.
200+
&& abi != SpecAbi::RustIntrinsic
201+
{
202+
match fn_abi.ret.layout.abi {
203+
// Handle similar to the way arguments with an `Abi::Aggregate` abi are handled
204+
// in `adjust_for_rust_abi`, by returning arguments up to the size of a pointer (32 bits on x86)
205+
// cast to an appropriately sized integer.
206+
Abi::Scalar(s) if s.primitive() == Float(F32) => {
207+
// Same size as a pointer, return in a register.
208+
fn_abi.ret.cast_to(Reg::i32());
209+
}
210+
Abi::Scalar(s) if s.primitive() == Float(F64) => {
211+
// Larger than a pointer, return indirectly.
212+
fn_abi.ret.make_indirect();
213+
}
214+
Abi::ScalarPair(s1, s2)
215+
if matches!(s1.primitive(), Float(F32 | F64))
216+
|| matches!(s2.primitive(), Float(F32 | F64)) =>
217+
{
218+
// Larger than a pointer, return indirectly.
219+
fn_abi.ret.make_indirect();
220+
}
221+
_ => {}
222+
};
223+
}
224+
}

0 commit comments

Comments
 (0)