Skip to content

Commit

Permalink
Set signext or zeroext for integer arguments on LoongArch64
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Oct 23, 2024
1 parent 7f064d3 commit f8cdf3c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 18 deletions.
28 changes: 28 additions & 0 deletions compiler/rustc_target/src/callconv/loongarch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::abi::call::{ArgAbi, ArgExtension, CastTarget, FnAbi, PassMode, Reg, RegKind, Uniform};
use crate::abi::{self, Abi, FieldsShape, HasDataLayout, Size, TyAbiInterface, TyAndLayout};
use crate::spec::HasTargetSpec;
use crate::spec::abi::Abi as SpecAbi;

#[derive(Copy, Clone)]
enum RegPassKind {
Expand Down Expand Up @@ -359,3 +360,30 @@ where
);
}
}

pub(crate) fn compute_rust_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>, abi: SpecAbi)
where
Ty: TyAbiInterface<'a, C> + Copy,
C: HasDataLayout + HasTargetSpec,
{
if abi == SpecAbi::RustIntrinsic {
return;
}

let grlen = cx.data_layout().pointer_size.bits();

for arg in fn_abi.args.iter_mut() {
if arg.is_ignore() {
continue;
}

// LLVM integers types do not differentiate between signed or unsigned integers.
// Some LoongArch instructions do not have a `.w` suffix version, they use all the
// GRLEN bits. By explicitly setting the `signext` or `zeroext` attribute
// according to signedness to avoid unnecessary integer extending instructions.
//
// This is similar to the RISC-V case, see
// https://github.com/rust-lang/rust/issues/114508 for details.
extend_integer_width(arg, grlen);
}
}
1 change: 1 addition & 0 deletions compiler/rustc_target/src/callconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
match &spec.arch[..] {
"x86" => x86::compute_rust_abi_info(cx, self, abi),
"riscv32" | "riscv64" => riscv::compute_rust_abi_info(cx, self, abi),
"loongarch64" => loongarch::compute_rust_abi_info(cx, self, abi),
_ => {}
};

Expand Down
15 changes: 14 additions & 1 deletion tests/assembly/rust-abi-arg-attr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ assembly-output: emit-asm
//@ revisions: riscv64 riscv64-zbb
//@ revisions: riscv64 riscv64-zbb loongarch64
//@ compile-flags: -C opt-level=3
//@ [riscv64] only-riscv64
//@ [riscv64] compile-flags: --target riscv64gc-unknown-linux-gnu
Expand All @@ -8,6 +8,9 @@
//@ [riscv64-zbb] compile-flags: --target riscv64gc-unknown-linux-gnu
//@ [riscv64-zbb] compile-flags: -C target-feature=+zbb
//@ [riscv64-zbb] needs-llvm-components: riscv
//@ [loongarch64] only-loongarch64
//@ [loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu
//@ [loongarch64] needs-llvm-components: loongarch

#![feature(no_core, lang_items, intrinsics, rustc_attrs)]
#![crate_type = "lib"]
Expand Down Expand Up @@ -78,6 +81,11 @@ pub fn issue_114508_u32(a: u32, b: u32) -> u32 {

// riscv64-zbb-NEXT: maxu a0, a0, a1

// loongarch64-NEXT: sltu $a2, $a1, $a0
// loongarch64-NEXT: masknez $a1, $a1, $a2
// loongarch64-NEXT: maskeqz $a0, $a0, $a2
// loongarch64-NEXT: or $a0, $a0, $a1

// CHECK-NEXT: ret
max!(a, b)
}
Expand All @@ -93,6 +101,11 @@ pub fn issue_114508_i32(a: i32, b: i32) -> i32 {

// riscv64-zbb-NEXT: max a0, a0, a1

// loongarch64-NEXT: slt $a2, $a1, $a0
// loongarch64-NEXT: masknez $a1, $a1, $a2
// loongarch64-NEXT: maskeqz $a0, $a0, $a2
// loongarch64-NEXT: or $a0, $a0, $a1

// CHECK-NEXT: ret
max!(a, b)
}
48 changes: 33 additions & 15 deletions tests/codegen/rust-abi-arch-specific-adjustment.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,111 @@
//@ compile-flags: -O -C no-prepopulate-passes
//@ revisions: riscv64
//@ revisions: riscv64 loongarch64

//@[riscv64] only-riscv64
//@[riscv64] compile-flags: --target riscv64gc-unknown-linux-gnu
//@[riscv64] needs-llvm-components: riscv

//@[loongarch64] only-loongarch64
//@[loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu
//@[loongarch64] needs-llvm-components: loongarch

#![crate_type = "lib"]

#[no_mangle]
// riscv64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
// riscv64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
// loongarch64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
pub fn arg_attr_u8(x: u8) -> u8 {
x
}

#[no_mangle]
// riscv64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
// riscv64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
// loongarch64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
pub fn arg_attr_u16(x: u16) -> u16 {
x
}

#[no_mangle]
// riscv64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
// riscv64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
// loongarch64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
pub fn arg_attr_u32(x: u32) -> u32 {
x
}

#[no_mangle]
// riscv64: define noundef i64 @arg_attr_u64(i64 noundef %x)
// riscv64: define noundef i64 @arg_attr_u64(i64 noundef %x)
// loongarch64: define noundef i64 @arg_attr_u64(i64 noundef %x)
pub fn arg_attr_u64(x: u64) -> u64 {
x
}

#[no_mangle]
// riscv64: define noundef i128 @arg_attr_u128(i128 noundef %x)
// riscv64: define noundef i128 @arg_attr_u128(i128 noundef %x)
// loongarch64: define noundef i128 @arg_attr_u128(i128 noundef %x)
pub fn arg_attr_u128(x: u128) -> u128 {
x
}

#[no_mangle]
// riscv64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
// riscv64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
// loongarch64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
pub fn arg_attr_i8(x: i8) -> i8 {
x
}

#[no_mangle]
// riscv64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
// riscv64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
// loongarch64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
pub fn arg_attr_i16(x: i16) -> i16 {
x
}

#[no_mangle]
// riscv64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
// riscv64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
// loongarch64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
pub fn arg_attr_i32(x: i32) -> i32 {
x
}

#[no_mangle]
// riscv64: define noundef i64 @arg_attr_i64(i64 noundef %x)
// riscv64: define noundef i64 @arg_attr_i64(i64 noundef %x)
// loongarch64: define noundef i64 @arg_attr_i64(i64 noundef %x)
pub fn arg_attr_i64(x: i64) -> i64 {
x
}

#[no_mangle]
// riscv64: define noundef i128 @arg_attr_i128(i128 noundef %x)
// riscv64: define noundef i128 @arg_attr_i128(i128 noundef %x)
// loongarch64: define noundef i128 @arg_attr_i128(i128 noundef %x)
pub fn arg_attr_i128(x: i128) -> i128 {
x
}

#[no_mangle]
// riscv64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
// riscv64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
// loongarch64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
pub fn arg_attr_bool(x: bool) -> bool {
x
}

#[no_mangle]
// ignore-tidy-linelength
// riscv64: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
// riscv64: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
// loongarch64: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
pub fn arg_attr_char(x: char) -> char {
x
}

#[no_mangle]
// riscv64: define noundef float @arg_attr_f32(float noundef %x)
// riscv64: define noundef float @arg_attr_f32(float noundef %x)
// loongarch64: define noundef float @arg_attr_f32(float noundef %x)
pub fn arg_attr_f32(x: f32) -> f32 {
x
}

#[no_mangle]
// riscv64: define noundef double @arg_attr_f64(double noundef %x)
// riscv64: define noundef double @arg_attr_f64(double noundef %x)
// loongarch64: define noundef double @arg_attr_f64(double noundef %x)
pub fn arg_attr_f64(x: f64) -> f64 {
x
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
// CHECK: Function Attrs: {{.*}}
// CHECK-LABEL: define{{.*}}foo{{.*}}!type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
// CHECK: start:
// CHECK-NEXT: {{%.+}} = call i32 %f(i32 %arg)
// CHECK-NEXT: {{%.+}} = call i32 %f(i32{{.*}} %arg)
// CHECK-NEXT: ret i32 {{%.+}}
f(arg)
}
2 changes: 1 addition & 1 deletion tests/codegen/sanitizer/cfi/emit-type-checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
// CHECK: [[TT:%.+]] = call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"{{[[:print:]]+}}")
// CHECK-NEXT: br i1 [[TT]], label %type_test.pass, label %type_test.fail
// CHECK: type_test.pass:
// CHECK-NEXT: {{%.+}} = call i32 %f(i32 %arg)
// CHECK-NEXT: {{%.+}} = call i32 %f(i32{{.*}} %arg)
// CHECK: type_test.fail:
// CHECK-NEXT: call void @llvm.trap()
// CHECK-NEXT: unreachable
Expand Down

0 comments on commit f8cdf3c

Please sign in to comment.