Skip to content

Commit 0adeaf3

Browse files
committed
Set signext or zeroext for integer arguments on LoongArch64
1 parent 4338ab1 commit 0adeaf3

File tree

6 files changed

+78
-18
lines changed

6 files changed

+78
-18
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

+1
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
733733
match &spec.arch[..] {
734734
"x86" => x86::compute_rust_abi_info(cx, self, abi),
735735
"riscv32" | "riscv64" => riscv::compute_rust_abi_info(cx, self, abi),
736+
"loongarch64" => loongarch::compute_rust_abi_info(cx, self, abi),
736737
_ => {}
737738
};
738739

tests/assembly/rust-abi-arg-attr.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ assembly-output: emit-asm
2-
//@ revisions: riscv64 riscv64-zbb
2+
//@ revisions: riscv64 riscv64-zbb loongarch64
33
//@ compile-flags: -C opt-level=3
44
//@ [riscv64] only-riscv64
55
//@ [riscv64] compile-flags: --target riscv64gc-unknown-linux-gnu
@@ -8,6 +8,9 @@
88
//@ [riscv64-zbb] compile-flags: --target riscv64gc-unknown-linux-gnu
99
//@ [riscv64-zbb] compile-flags: -C target-feature=+zbb
1010
//@ [riscv64-zbb] needs-llvm-components: riscv
11+
//@ [loongarch64] only-loongarch64
12+
//@ [loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu
13+
//@ [loongarch64] needs-llvm-components: loongarch
1114

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

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

84+
// loongarch64-NEXT: sltu $a2, $a1, $a0
85+
// loongarch64-NEXT: masknez $a1, $a1, $a2
86+
// loongarch64-NEXT: maskeqz $a0, $a0, $a2
87+
// loongarch64-NEXT: or $a0, $a0, $a1
88+
8189
// CHECK-NEXT: ret
8290
max!(a, b)
8391
}
@@ -93,6 +101,11 @@ pub fn issue_114508_i32(a: i32, b: i32) -> i32 {
93101

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

104+
// loongarch64-NEXT: slt $a2, $a1, $a0
105+
// loongarch64-NEXT: masknez $a1, $a1, $a2
106+
// loongarch64-NEXT: maskeqz $a0, $a0, $a2
107+
// loongarch64-NEXT: or $a0, $a0, $a1
108+
96109
// CHECK-NEXT: ret
97110
max!(a, b)
98111
}
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,111 @@
11
//@ compile-flags: -O -C no-prepopulate-passes
2-
//@ revisions: riscv64
2+
//@ revisions: riscv64 loongarch64
33

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

8+
//@[loongarch64] only-loongarch64
9+
//@[loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu
10+
//@[loongarch64] needs-llvm-components: loongarch
11+
812
#![crate_type = "lib"]
913

1014
#[no_mangle]
11-
// riscv64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
15+
// riscv64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
16+
// loongarch64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
1217
pub fn arg_attr_u8(x: u8) -> u8 {
1318
x
1419
}
1520

1621
#[no_mangle]
17-
// riscv64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
22+
// riscv64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
23+
// loongarch64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
1824
pub fn arg_attr_u16(x: u16) -> u16 {
1925
x
2026
}
2127

2228
#[no_mangle]
23-
// riscv64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
29+
// riscv64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
30+
// loongarch64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
2431
pub fn arg_attr_u32(x: u32) -> u32 {
2532
x
2633
}
2734

2835
#[no_mangle]
29-
// riscv64: define noundef i64 @arg_attr_u64(i64 noundef %x)
36+
// riscv64: define noundef i64 @arg_attr_u64(i64 noundef %x)
37+
// loongarch64: define noundef i64 @arg_attr_u64(i64 noundef %x)
3038
pub fn arg_attr_u64(x: u64) -> u64 {
3139
x
3240
}
3341

3442
#[no_mangle]
35-
// riscv64: define noundef i128 @arg_attr_u128(i128 noundef %x)
43+
// riscv64: define noundef i128 @arg_attr_u128(i128 noundef %x)
44+
// loongarch64: define noundef i128 @arg_attr_u128(i128 noundef %x)
3645
pub fn arg_attr_u128(x: u128) -> u128 {
3746
x
3847
}
3948

4049
#[no_mangle]
41-
// riscv64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
50+
// riscv64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
51+
// loongarch64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
4252
pub fn arg_attr_i8(x: i8) -> i8 {
4353
x
4454
}
4555

4656
#[no_mangle]
47-
// riscv64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
57+
// riscv64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
58+
// loongarch64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
4859
pub fn arg_attr_i16(x: i16) -> i16 {
4960
x
5061
}
5162

5263
#[no_mangle]
53-
// riscv64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
64+
// riscv64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
65+
// loongarch64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
5466
pub fn arg_attr_i32(x: i32) -> i32 {
5567
x
5668
}
5769

5870
#[no_mangle]
59-
// riscv64: define noundef i64 @arg_attr_i64(i64 noundef %x)
71+
// riscv64: define noundef i64 @arg_attr_i64(i64 noundef %x)
72+
// loongarch64: define noundef i64 @arg_attr_i64(i64 noundef %x)
6073
pub fn arg_attr_i64(x: i64) -> i64 {
6174
x
6275
}
6376

6477
#[no_mangle]
65-
// riscv64: define noundef i128 @arg_attr_i128(i128 noundef %x)
78+
// riscv64: define noundef i128 @arg_attr_i128(i128 noundef %x)
79+
// loongarch64: define noundef i128 @arg_attr_i128(i128 noundef %x)
6680
pub fn arg_attr_i128(x: i128) -> i128 {
6781
x
6882
}
6983

7084
#[no_mangle]
71-
// riscv64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
85+
// riscv64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
86+
// loongarch64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
7287
pub fn arg_attr_bool(x: bool) -> bool {
7388
x
7489
}
7590

7691
#[no_mangle]
7792
// ignore-tidy-linelength
78-
// riscv64: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
93+
// riscv64: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
94+
// loongarch64: define noundef{{( range\(i32 0, 1114112\))?}} i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
7995
pub fn arg_attr_char(x: char) -> char {
8096
x
8197
}
8298

8399
#[no_mangle]
84-
// riscv64: define noundef float @arg_attr_f32(float noundef %x)
100+
// riscv64: define noundef float @arg_attr_f32(float noundef %x)
101+
// loongarch64: define noundef float @arg_attr_f32(float noundef %x)
85102
pub fn arg_attr_f32(x: f32) -> f32 {
86103
x
87104
}
88105

89106
#[no_mangle]
90-
// riscv64: define noundef double @arg_attr_f64(double noundef %x)
107+
// riscv64: define noundef double @arg_attr_f64(double noundef %x)
108+
// loongarch64: define noundef double @arg_attr_f64(double noundef %x)
91109
pub fn arg_attr_f64(x: f64) -> f64 {
92110
x
93111
}

tests/codegen/sanitizer/cfi/emit-type-checks-attr-no-sanitize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
1212
// CHECK: Function Attrs: {{.*}}
1313
// CHECK-LABEL: define{{.*}}foo{{.*}}!type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}} !type !{{[0-9]+}}
1414
// CHECK: start:
15-
// CHECK-NEXT: {{%.+}} = call i32 %f(i32 %arg)
15+
// CHECK-NEXT: {{%.+}} = call i32 %f(i32{{.*}} %arg)
1616
// CHECK-NEXT: ret i32 {{%.+}}
1717
f(arg)
1818
}

tests/codegen/sanitizer/cfi/emit-type-checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn foo(f: fn(i32) -> i32, arg: i32) -> i32 {
1111
// CHECK: [[TT:%.+]] = call i1 @llvm.type.test(ptr {{%f|%0}}, metadata !"{{[[:print:]]+}}")
1212
// CHECK-NEXT: br i1 [[TT]], label %type_test.pass, label %type_test.fail
1313
// CHECK: type_test.pass:
14-
// CHECK-NEXT: {{%.+}} = call i32 %f(i32 %arg)
14+
// CHECK-NEXT: {{%.+}} = call i32 %f(i32{{.*}} %arg)
1515
// CHECK: type_test.fail:
1616
// CHECK-NEXT: call void @llvm.trap()
1717
// CHECK-NEXT: unreachable

0 commit comments

Comments
 (0)