From 2dfd4d0669665d7b174fb157c3458d58d7c591f2 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 | 15 +++- .../rust-abi-arch-specific-adjustment.rs | 77 ++++++++++++------- .../cfi/emit-type-checks-attr-no-sanitize.rs | 2 +- .../codegen/sanitizer/cfi/emit-type-checks.rs | 2 +- 6 files changed, 93 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_target/src/callconv/loongarch.rs b/compiler/rustc_target/src/callconv/loongarch.rs index 4a21935623b92..ffec76370d026 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 @@ 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); + } +} diff --git a/compiler/rustc_target/src/callconv/mod.rs b/compiler/rustc_target/src/callconv/mod.rs index 6e101d72c5a8b..e2e86ca6763a0 100644 --- a/compiler/rustc_target/src/callconv/mod.rs +++ b/compiler/rustc_target/src/callconv/mod.rs @@ -729,6 +729,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), _ => {} }; diff --git a/tests/assembly/rust-abi-arg-attr.rs b/tests/assembly/rust-abi-arg-attr.rs index bd27d6c183cd6..30381ccd4dada 100644 --- a/tests/assembly/rust-abi-arg-attr.rs +++ b/tests/assembly/rust-abi-arg-attr.rs @@ -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 @@ -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 #![crate_type = "lib"] @@ -22,6 +25,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 u32::max(a, b) } @@ -37,6 +45,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 i32::max(a, b) } diff --git a/tests/codegen/rust-abi-arch-specific-adjustment.rs b/tests/codegen/rust-abi-arch-specific-adjustment.rs index 2f3c516aa17c2..46c600f108054 100644 --- a/tests/codegen/rust-abi-arch-specific-adjustment.rs +++ b/tests/codegen/rust-abi-arch-specific-adjustment.rs @@ -1,109 +1,128 @@ //@ compile-flags: -O -C no-prepopulate-passes -//@ revisions: others riscv64 +//@ revisions: others riscv64 loongarch64 //@[others] ignore-riscv64 +//@[others] ignore-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] -// others: define noundef i8 @arg_attr_u8(i8 noundef %x) -// riscv64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x) +// others: define noundef i8 @arg_attr_u8(i8 noundef %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] -// others: define noundef i16 @arg_attr_u16(i16 noundef %x) -// riscv64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x) +// others: define noundef i16 @arg_attr_u16(i16 noundef %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] -// others: define noundef i32 @arg_attr_u32(i32 noundef %x) -// riscv64: define noundef i32 @arg_attr_u32(i32 noundef signext %x) +// others: define noundef i32 @arg_attr_u32(i32 noundef %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] -// others: define noundef i64 @arg_attr_u64(i64 noundef %x) -// riscv64: define noundef i64 @arg_attr_u64(i64 noundef %x) +// others: 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] -// others: define noundef i128 @arg_attr_u128(i128 noundef %x) -// riscv64: define noundef i128 @arg_attr_u128(i128 noundef %x) +// others: 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] -// others: define noundef i8 @arg_attr_i8(i8 noundef %x) -// riscv64: define noundef i8 @arg_attr_i8(i8 noundef signext %x) +// others: define noundef i8 @arg_attr_i8(i8 noundef %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] -// others: define noundef i16 @arg_attr_i16(i16 noundef %x) -// riscv64: define noundef i16 @arg_attr_i16(i16 noundef signext %x) +// others: define noundef i16 @arg_attr_i16(i16 noundef %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] -// others: define noundef i32 @arg_attr_i32(i32 noundef %x) -// riscv64: define noundef i32 @arg_attr_i32(i32 noundef signext %x) +// others: define noundef i32 @arg_attr_i32(i32 noundef %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] -// others: define noundef i64 @arg_attr_i64(i64 noundef %x) -// riscv64: define noundef i64 @arg_attr_i64(i64 noundef %x) +// others: 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] -// others: define noundef i128 @arg_attr_i128(i128 noundef %x) -// riscv64: define noundef i128 @arg_attr_i128(i128 noundef %x) +// others: 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] -// others: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x) -// riscv64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x) +// others: 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 -// others: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef{{( range\(i32 0, 1114112\))?}} %x) -// riscv64: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x) +// others: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef{{( 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] -// others: define noundef float @arg_attr_f32(float noundef %x) -// riscv64: define noundef float @arg_attr_f32(float noundef %x) +// others: 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] -// others: define noundef double @arg_attr_f64(double noundef %x) -// riscv64: define noundef double @arg_attr_f64(double noundef %x) +// others: 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 259967e89181a..71ccdc8ca624f 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 37edbefee56ad..ebc66a015df69 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