From 57bffe1d59cb6caa39c6ae5f5620c0cb371eea72 Mon Sep 17 00:00:00 2001 From: Asuna Date: Sun, 20 Oct 2024 23:17:04 +0200 Subject: [PATCH] Set `signext` or `zeroext` for integer arguments on LoongArch64 --- .../rustc_target/src/callconv/loongarch.rs | 28 +++++++++++ compiler/rustc_target/src/callconv/mod.rs | 1 + tests/assembly/rust-abi-arg-attr.rs | 14 +++++- .../rust-abi-arch-specific-adjustment.rs | 48 +++++++++++++------ .../cfi/emit-type-checks-attr-no-sanitize.rs | 2 +- .../codegen/sanitizer/cfi/emit-type-checks.rs | 2 +- 6 files changed, 77 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_target/src/callconv/loongarch.rs b/compiler/rustc_target/src/callconv/loongarch.rs index 4a21935623b..ffec76370d0 100644 --- a/compiler/rustc_target/src/callconv/loongarch.rs +++ b/compiler/rustc_target/src/callconv/loongarch.rs @@ -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 { @@ -359,3 +360,30 @@ pub(crate) fn compute_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>) ); } } + +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); + } +} diff --git a/compiler/rustc_target/src/callconv/mod.rs b/compiler/rustc_target/src/callconv/mod.rs index 811ef4e3069..25b001b57e8 100644 --- a/compiler/rustc_target/src/callconv/mod.rs +++ b/compiler/rustc_target/src/callconv/mod.rs @@ -733,6 +733,7 @@ pub fn adjust_for_rust_abi(&mut self, cx: &C, abi: SpecAbi) 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), _ => {} }; diff --git a/tests/assembly/rust-abi-arg-attr.rs b/tests/assembly/rust-abi-arg-attr.rs index 2226aa8fa5a..2a113eed4ba 100644 --- a/tests/assembly/rust-abi-arg-attr.rs +++ b/tests/assembly/rust-abi-arg-attr.rs @@ -1,11 +1,13 @@ //@ assembly-output: emit-asm -//@ revisions: riscv64 riscv64-zbb +//@ revisions: riscv64 riscv64-zbb loongarch64 //@ compile-flags: -C opt-level=3 //@ [riscv64] compile-flags: --target riscv64gc-unknown-linux-gnu //@ [riscv64] needs-llvm-components: riscv //@ [riscv64-zbb] compile-flags: --target riscv64gc-unknown-linux-gnu //@ [riscv64-zbb] compile-flags: -C target-feature=+zbb //@ [riscv64-zbb] needs-llvm-components: riscv +//@ [loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu +//@ [loongarch64] needs-llvm-components: loongarch #![feature(no_core, lang_items, intrinsics, rustc_attrs)] #![crate_type = "lib"] @@ -76,6 +78,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) } @@ -91,6 +98,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) } diff --git a/tests/codegen/rust-abi-arch-specific-adjustment.rs b/tests/codegen/rust-abi-arch-specific-adjustment.rs index 87a7cd8ae26..9da10f662b0 100644 --- a/tests/codegen/rust-abi-arch-specific-adjustment.rs +++ b/tests/codegen/rust-abi-arch-specific-adjustment.rs @@ -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 } diff --git a/tests/codegen/sanitizer/cfi/emit-type-checks-attr-no-sanitize.rs b/tests/codegen/sanitizer/cfi/emit-type-checks-attr-no-sanitize.rs index 259967e8918..71ccdc8ca62 100644 --- a/tests/codegen/sanitizer/cfi/emit-type-checks-attr-no-sanitize.rs +++ b/tests/codegen/sanitizer/cfi/emit-type-checks-attr-no-sanitize.rs @@ -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) } diff --git a/tests/codegen/sanitizer/cfi/emit-type-checks.rs b/tests/codegen/sanitizer/cfi/emit-type-checks.rs index 37edbefee56..ebc66a015df 100644 --- a/tests/codegen/sanitizer/cfi/emit-type-checks.rs +++ b/tests/codegen/sanitizer/cfi/emit-type-checks.rs @@ -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