Skip to content

Commit 2354fad

Browse files
committed
Set signext or zeroext for integer arguments on LoongArch64
1 parent 440a526 commit 2354fad

File tree

6 files changed

+93
-32
lines changed

6 files changed

+93
-32
lines changed

Diff for: 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+
}

Diff for: compiler/rustc_target/src/callconv/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ impl<'a, Ty> FnAbi<'a, Ty> {
729729
match &spec.arch[..] {
730730
"x86" => x86::compute_rust_abi_info(cx, self, abi),
731731
"riscv32" | "riscv64" => riscv::compute_rust_abi_info(cx, self, abi),
732+
"loongarch64" => loongarch::compute_rust_abi_info(cx, self, abi),
732733
_ => {}
733734
};
734735

Diff for: 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
#![crate_type = "lib"]
1316

@@ -22,6 +25,11 @@ pub fn issue_114508_u32(a: u32, b: u32) -> u32 {
2225

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

28+
// loongarch64-NEXT: sltu $a2, $a1, $a0
29+
// loongarch64-NEXT: masknez $a1, $a1, $a2
30+
// loongarch64-NEXT: maskeqz $a0, $a0, $a2
31+
// loongarch64-NEXT: or $a0, $a0, $a1
32+
2533
// CHECK-NEXT: ret
2634
u32::max(a, b)
2735
}
@@ -37,6 +45,11 @@ pub fn issue_114508_i32(a: i32, b: i32) -> i32 {
3745

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

48+
// loongarch64-NEXT: slt $a2, $a1, $a0
49+
// loongarch64-NEXT: masknez $a1, $a1, $a2
50+
// loongarch64-NEXT: maskeqz $a0, $a0, $a2
51+
// loongarch64-NEXT: or $a0, $a0, $a1
52+
4053
// CHECK-NEXT: ret
4154
i32::max(a, b)
4255
}

Diff for: tests/codegen/rust-abi-arch-specific-adjustment.rs

+48-29
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,128 @@
11
//@ compile-flags: -O -C no-prepopulate-passes
2-
//@ revisions: others riscv64
2+
//@ revisions: others riscv64 loongarch64
33

44
//@[others] ignore-riscv64
5+
//@[others] ignore-loongarch64
56

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

11+
//@[loongarch64] only-loongarch64
12+
//@[loongarch64] compile-flags: --target loongarch64-unknown-linux-gnu
13+
//@[loongarch64] needs-llvm-components: loongarch
14+
1015
#![crate_type = "lib"]
1116

1217
#[no_mangle]
13-
// others: define noundef i8 @arg_attr_u8(i8 noundef %x)
14-
// riscv64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
18+
// others: define noundef i8 @arg_attr_u8(i8 noundef %x)
19+
// riscv64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
20+
// loongarch64: define noundef i8 @arg_attr_u8(i8 noundef zeroext %x)
1521
pub fn arg_attr_u8(x: u8) -> u8 {
1622
x
1723
}
1824

1925
#[no_mangle]
20-
// others: define noundef i16 @arg_attr_u16(i16 noundef %x)
21-
// riscv64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
26+
// others: define noundef i16 @arg_attr_u16(i16 noundef %x)
27+
// riscv64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
28+
// loongarch64: define noundef i16 @arg_attr_u16(i16 noundef zeroext %x)
2229
pub fn arg_attr_u16(x: u16) -> u16 {
2330
x
2431
}
2532

2633
#[no_mangle]
27-
// others: define noundef i32 @arg_attr_u32(i32 noundef %x)
28-
// riscv64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
34+
// others: define noundef i32 @arg_attr_u32(i32 noundef %x)
35+
// riscv64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
36+
// loongarch64: define noundef i32 @arg_attr_u32(i32 noundef signext %x)
2937
pub fn arg_attr_u32(x: u32) -> u32 {
3038
x
3139
}
3240

3341
#[no_mangle]
34-
// others: define noundef i64 @arg_attr_u64(i64 noundef %x)
35-
// riscv64: define noundef i64 @arg_attr_u64(i64 noundef %x)
42+
// others: define noundef i64 @arg_attr_u64(i64 noundef %x)
43+
// riscv64: define noundef i64 @arg_attr_u64(i64 noundef %x)
44+
// loongarch64: define noundef i64 @arg_attr_u64(i64 noundef %x)
3645
pub fn arg_attr_u64(x: u64) -> u64 {
3746
x
3847
}
3948

4049
#[no_mangle]
41-
// others: define noundef i128 @arg_attr_u128(i128 noundef %x)
42-
// riscv64: define noundef i128 @arg_attr_u128(i128 noundef %x)
50+
// others: define noundef i128 @arg_attr_u128(i128 noundef %x)
51+
// riscv64: define noundef i128 @arg_attr_u128(i128 noundef %x)
52+
// loongarch64: define noundef i128 @arg_attr_u128(i128 noundef %x)
4353
pub fn arg_attr_u128(x: u128) -> u128 {
4454
x
4555
}
4656

4757
#[no_mangle]
48-
// others: define noundef i8 @arg_attr_i8(i8 noundef %x)
49-
// riscv64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
58+
// others: define noundef i8 @arg_attr_i8(i8 noundef %x)
59+
// riscv64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
60+
// loongarch64: define noundef i8 @arg_attr_i8(i8 noundef signext %x)
5061
pub fn arg_attr_i8(x: i8) -> i8 {
5162
x
5263
}
5364

5465
#[no_mangle]
55-
// others: define noundef i16 @arg_attr_i16(i16 noundef %x)
56-
// riscv64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
66+
// others: define noundef i16 @arg_attr_i16(i16 noundef %x)
67+
// riscv64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
68+
// loongarch64: define noundef i16 @arg_attr_i16(i16 noundef signext %x)
5769
pub fn arg_attr_i16(x: i16) -> i16 {
5870
x
5971
}
6072

6173
#[no_mangle]
62-
// others: define noundef i32 @arg_attr_i32(i32 noundef %x)
63-
// riscv64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
74+
// others: define noundef i32 @arg_attr_i32(i32 noundef %x)
75+
// riscv64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
76+
// loongarch64: define noundef i32 @arg_attr_i32(i32 noundef signext %x)
6477
pub fn arg_attr_i32(x: i32) -> i32 {
6578
x
6679
}
6780

6881
#[no_mangle]
69-
// others: define noundef i64 @arg_attr_i64(i64 noundef %x)
70-
// riscv64: define noundef i64 @arg_attr_i64(i64 noundef %x)
82+
// others: define noundef i64 @arg_attr_i64(i64 noundef %x)
83+
// riscv64: define noundef i64 @arg_attr_i64(i64 noundef %x)
84+
// loongarch64: define noundef i64 @arg_attr_i64(i64 noundef %x)
7185
pub fn arg_attr_i64(x: i64) -> i64 {
7286
x
7387
}
7488

7589
#[no_mangle]
76-
// others: define noundef i128 @arg_attr_i128(i128 noundef %x)
77-
// riscv64: define noundef i128 @arg_attr_i128(i128 noundef %x)
90+
// others: define noundef i128 @arg_attr_i128(i128 noundef %x)
91+
// riscv64: define noundef i128 @arg_attr_i128(i128 noundef %x)
92+
// loongarch64: define noundef i128 @arg_attr_i128(i128 noundef %x)
7893
pub fn arg_attr_i128(x: i128) -> i128 {
7994
x
8095
}
8196

8297
#[no_mangle]
83-
// others: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
84-
// riscv64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
98+
// others: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
99+
// riscv64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
100+
// loongarch64: define noundef zeroext i1 @arg_attr_bool(i1 noundef zeroext %x)
85101
pub fn arg_attr_bool(x: bool) -> bool {
86102
x
87103
}
88104

89105
#[no_mangle]
90106
// ignore-tidy-linelength
91-
// others: define noundef range(i32 0, 1114112) i32 @arg_attr_char(i32 noundef{{( range\(i32 0, 1114112\))?}} %x)
92-
// riscv64: define noundef range(i32 0, 1114112) i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
107+
// others: define noundef range(i32 0, 1114112) i32 @arg_attr_char(i32 noundef{{( range\(i32 0, 1114112\))?}} %x)
108+
// riscv64: define noundef range(i32 0, 1114112) i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
109+
// loongarch64: define noundef range(i32 0, 1114112) i32 @arg_attr_char(i32 noundef signext{{( range\(i32 0, 1114112\))?}} %x)
93110
pub fn arg_attr_char(x: char) -> char {
94111
x
95112
}
96113

97114
#[no_mangle]
98-
// others: define noundef float @arg_attr_f32(float noundef %x)
99-
// riscv64: define noundef float @arg_attr_f32(float noundef %x)
115+
// others: define noundef float @arg_attr_f32(float noundef %x)
116+
// riscv64: define noundef float @arg_attr_f32(float noundef %x)
117+
// loongarch64: define noundef float @arg_attr_f32(float noundef %x)
100118
pub fn arg_attr_f32(x: f32) -> f32 {
101119
x
102120
}
103121

104122
#[no_mangle]
105-
// others: define noundef double @arg_attr_f64(double noundef %x)
106-
// riscv64: define noundef double @arg_attr_f64(double noundef %x)
123+
// others: define noundef double @arg_attr_f64(double noundef %x)
124+
// riscv64: define noundef double @arg_attr_f64(double noundef %x)
125+
// loongarch64: define noundef double @arg_attr_f64(double noundef %x)
107126
pub fn arg_attr_f64(x: f64) -> f64 {
108127
x
109128
}

Diff for: 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
}

Diff for: 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)