Skip to content

Commit fc3dc84

Browse files
committed
Support clobber_abi and vector registers (clobber-only) in PowerPC inline assembly
1 parent 7caad69 commit fc3dc84

File tree

6 files changed

+116
-12
lines changed

6 files changed

+116
-12
lines changed

compiler/rustc_codegen_gcc/src/asm.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ fn reg_to_gcc(reg: InlineAsmRegOrRegClass) -> ConstraintOrRegister {
654654
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => "b",
655655
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::freg) => "f",
656656
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::cr)
657-
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer) => {
657+
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer)
658+
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::vreg) => {
658659
unreachable!("clobber-only")
659660
}
660661
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => "r",
@@ -729,7 +730,8 @@ fn dummy_output_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, reg: InlineAsmRegCl
729730
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => cx.type_i32(),
730731
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::freg) => cx.type_f64(),
731732
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::cr)
732-
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer) => {
733+
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer)
734+
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::vreg) => {
733735
unreachable!("clobber-only")
734736
}
735737
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => cx.type_i32(),

compiler/rustc_codegen_llvm/src/asm.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,9 @@ fn reg_to_llvm(reg: InlineAsmRegOrRegClass, layout: Option<&TyAndLayout<'_>>) ->
679679
PowerPC(PowerPCInlineAsmRegClass::reg) => "r",
680680
PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => "b",
681681
PowerPC(PowerPCInlineAsmRegClass::freg) => "f",
682-
PowerPC(PowerPCInlineAsmRegClass::cr) | PowerPC(PowerPCInlineAsmRegClass::xer) => {
682+
PowerPC(PowerPCInlineAsmRegClass::cr)
683+
| PowerPC(PowerPCInlineAsmRegClass::xer)
684+
| PowerPC(PowerPCInlineAsmRegClass::vreg) => {
683685
unreachable!("clobber-only")
684686
}
685687
RiscV(RiscVInlineAsmRegClass::reg) => "r",
@@ -841,7 +843,9 @@ fn dummy_output_type<'ll>(cx: &CodegenCx<'ll, '_>, reg: InlineAsmRegClass) -> &'
841843
PowerPC(PowerPCInlineAsmRegClass::reg) => cx.type_i32(),
842844
PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => cx.type_i32(),
843845
PowerPC(PowerPCInlineAsmRegClass::freg) => cx.type_f64(),
844-
PowerPC(PowerPCInlineAsmRegClass::cr) | PowerPC(PowerPCInlineAsmRegClass::xer) => {
846+
PowerPC(PowerPCInlineAsmRegClass::cr)
847+
| PowerPC(PowerPCInlineAsmRegClass::xer)
848+
| PowerPC(PowerPCInlineAsmRegClass::vreg) => {
845849
unreachable!("clobber-only")
846850
}
847851
RiscV(RiscVInlineAsmRegClass::reg) => cx.type_i32(),

compiler/rustc_target/src/asm/mod.rs

+30
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ pub enum InlineAsmClobberAbi {
892892
AArch64NoX18,
893893
RiscV,
894894
LoongArch,
895+
PowerPC,
895896
S390x,
896897
}
897898

@@ -942,6 +943,10 @@ impl InlineAsmClobberAbi {
942943
"C" | "system" => Ok(InlineAsmClobberAbi::LoongArch),
943944
_ => Err(&["C", "system"]),
944945
},
946+
InlineAsmArch::PowerPC | InlineAsmArch::PowerPC64 => match name {
947+
"C" | "system" => Ok(InlineAsmClobberAbi::PowerPC),
948+
_ => Err(&["C", "system"]),
949+
},
945950
InlineAsmArch::S390x => match name {
946951
"C" | "system" => Ok(InlineAsmClobberAbi::S390x),
947952
_ => Err(&["C", "system"]),
@@ -1103,6 +1108,31 @@ impl InlineAsmClobberAbi {
11031108
f16, f17, f18, f19, f20, f21, f22, f23,
11041109
}
11051110
},
1111+
InlineAsmClobberAbi::PowerPC => clobbered_regs! {
1112+
PowerPC PowerPCInlineAsmReg {
1113+
// r0, r3-r12
1114+
r0,
1115+
r3, r4, r5, r6, r7,
1116+
r8, r9, r10, r11, r12,
1117+
1118+
// f0-f13
1119+
f0, f1, f2, f3, f4, f5, f6, f7,
1120+
f8, f9, f10, f11, f12, f13,
1121+
1122+
// v0-v19
1123+
// FIXME: PPC32 SysV ABI does not mention vector registers processing.
1124+
// https://refspecs.linuxfoundation.org/elf/elfspec_ppc.pdf
1125+
v0, v1, v2, v3, v4, v5, v6, v7,
1126+
v8, v9, v10, v11, v12, v13, v14,
1127+
v15, v16, v17, v18, v19,
1128+
1129+
// cr0-cr1, cr5-cr7, xer
1130+
cr0, cr1,
1131+
cr5, cr6, cr7,
1132+
xer,
1133+
// lr and ctr are reserved
1134+
}
1135+
},
11061136
InlineAsmClobberAbi::S390x => clobbered_regs! {
11071137
S390x S390xInlineAsmReg {
11081138
r0, r1, r2, r3, r4, r5,

compiler/rustc_target/src/asm/powerpc.rs

+41
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def_reg_class! {
99
reg,
1010
reg_nonzero,
1111
freg,
12+
vreg,
1213
cr,
1314
xer,
1415
}
@@ -48,6 +49,7 @@ impl PowerPCInlineAsmRegClass {
4849
}
4950
}
5051
Self::freg => types! { _: F32, F64; },
52+
Self::vreg => &[],
5153
Self::cr | Self::xer => &[],
5254
}
5355
}
@@ -113,6 +115,40 @@ def_regs! {
113115
f29: freg = ["f29", "fr29"],
114116
f30: freg = ["f30", "fr30"],
115117
f31: freg = ["f31", "fr31"],
118+
v0: vreg = ["v0"],
119+
v1: vreg = ["v1"],
120+
v2: vreg = ["v2"],
121+
v3: vreg = ["v3"],
122+
v4: vreg = ["v4"],
123+
v5: vreg = ["v5"],
124+
v6: vreg = ["v6"],
125+
v7: vreg = ["v7"],
126+
v8: vreg = ["v8"],
127+
v9: vreg = ["v9"],
128+
v10: vreg = ["v10"],
129+
v11: vreg = ["v11"],
130+
v12: vreg = ["v12"],
131+
v13: vreg = ["v13"],
132+
v14: vreg = ["v14"],
133+
v15: vreg = ["v15"],
134+
v16: vreg = ["v16"],
135+
v17: vreg = ["v17"],
136+
v18: vreg = ["v18"],
137+
v19: vreg = ["v19"],
138+
// FIXME: In AIX, v20-v31 are reserved or nonvolatile depending on the mode.
139+
// https://www.ibm.com/docs/en/aix/7.3?topic=concepts-aix-vector-programming
140+
v20: vreg = ["v20"],
141+
v21: vreg = ["v21"],
142+
v22: vreg = ["v22"],
143+
v23: vreg = ["v23"],
144+
v24: vreg = ["v24"],
145+
v25: vreg = ["v25"],
146+
v26: vreg = ["v26"],
147+
v27: vreg = ["v27"],
148+
v28: vreg = ["v28"],
149+
v29: vreg = ["v29"],
150+
v30: vreg = ["v30"],
151+
v31: vreg = ["v31"],
116152
cr: cr = ["cr"],
117153
cr0: cr = ["cr0"],
118154
cr1: cr = ["cr1"],
@@ -170,6 +206,10 @@ impl PowerPCInlineAsmReg {
170206
(f8, "8"), (f9, "9"), (f10, "10"), (f11, "11"), (f12, "12"), (f13, "13"), (f14, "14"), (f15, "15");
171207
(f16, "16"), (f17, "17"), (f18, "18"), (f19, "19"), (f20, "20"), (f21, "21"), (f22, "22"), (f23, "23");
172208
(f24, "24"), (f25, "25"), (f26, "26"), (f27, "27"), (f28, "28"), (f29, "29"), (f30, "30"), (f31, "31");
209+
(v0, "0"), (v1, "1"), (v2, "2"), (v3, "3"), (v4, "4"), (v5, "5"), (v6, "6"), (v7, "7");
210+
(v8, "8"), (v9, "9"), (v10, "10"), (v11, "11"), (v12, "12"), (v13, "13"), (v14, "14"), (v15, "15");
211+
(v16, "16"), (v17, "17"), (v18, "18"), (v19, "19"), (v20, "20"), (v21, "21"), (v22, "22"), (v23, "23");
212+
(v24, "24"), (v25, "25"), (v26, "26"), (v27, "27"), (v28, "28"), (v29, "29"), (v30, "30"), (v31, "31");
173213
(cr, "cr");
174214
(cr0, "0"), (cr1, "1"), (cr2, "2"), (cr3, "3"), (cr4, "4"), (cr5, "5"), (cr6, "6"), (cr7, "7");
175215
(xer, "xer");
@@ -201,5 +241,6 @@ impl PowerPCInlineAsmReg {
201241
reg_conflicts! {
202242
cr : cr0 cr1 cr2 cr3 cr4 cr5 cr6 cr7;
203243
}
244+
// f0-f31 (vsr0-vsr31) and v0-v31 (vsr32-vsr63) do not conflict.
204245
}
205246
}

src/doc/unstable-book/src/language-features/asm-experimental-arch.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ This feature tracks `asm!` and `global_asm!` support for the following architect
3131
| NVPTX | `reg32` | None\* | `r` |
3232
| NVPTX | `reg64` | None\* | `l` |
3333
| Hexagon | `reg` | `r[0-28]` | `r` |
34-
| PowerPC | `reg` | `r[0-31]` | `r` |
35-
| PowerPC | `reg_nonzero` | `r[1-31]` | `b` |
34+
| PowerPC | `reg` | `r0`, `r[3-12]`, `r[14-28]` | `r` |
35+
| PowerPC | `reg_nonzero` | `r[3-12]`, `r[14-28]` | `b` |
3636
| PowerPC | `freg` | `f[0-31]` | `f` |
37+
| PowerPC | `vreg` | `v[0-31]` | Only clobbers |
3738
| PowerPC | `cr` | `cr[0-7]`, `cr` | Only clobbers |
3839
| PowerPC | `xer` | `xer` | Only clobbers |
3940
| wasm32 | `local` | None\* | `r` |
@@ -79,6 +80,7 @@ This feature tracks `asm!` and `global_asm!` support for the following architect
7980
| PowerPC | `reg` | None | `i8`, `i16`, `i32` |
8081
| PowerPC | `reg_nonzero` | None | `i8`, `i16`, `i32` |
8182
| PowerPC | `freg` | None | `f32`, `f64` |
83+
| PowerPC | `vreg` | N/A | Only clobbers |
8284
| PowerPC | `cr` | N/A | Only clobbers |
8385
| PowerPC | `xer` | N/A | Only clobbers |
8486
| wasm32 | `local` | None | `i8` `i16` `i32` `i64` `f32` `f64` |
@@ -105,6 +107,10 @@ This feature tracks `asm!` and `global_asm!` support for the following architect
105107
| Hexagon | `r29` | `sp` |
106108
| Hexagon | `r30` | `fr` |
107109
| Hexagon | `r31` | `lr` |
110+
| PowerPC | `r1` | `sp` |
111+
| PowerPC | `r31` | `fp` |
112+
| PowerPC | `r[0-31]` | `[0-31]` |
113+
| PowerPC | `f[0-31]` | `fr[0-31]`|
108114
| BPF | `r[0-10]` | `w[0-10]` |
109115
| AVR | `XH` | `r27` |
110116
| AVR | `XL` | `r26` |
@@ -145,14 +151,19 @@ This feature tracks `asm!` and `global_asm!` support for the following architect
145151
| Architecture | Unsupported register | Reason |
146152
| ------------ | --------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
147153
| All | `sp`, `r15` (s390x) | The stack pointer must be restored to its original value at the end of an asm code block. |
148-
| All | `fr` (Hexagon), `$fp` (MIPS), `Y` (AVR), `r4` (MSP430), `a6` (M68k), `r11` (s390x), `x29` (Arm64EC) | The frame pointer cannot be used as an input or output. |
154+
| All | `fr` (Hexagon), `fp` (PowerPC), `$fp` (MIPS), `Y` (AVR), `r4` (MSP430), `a6` (M68k), `r11` (s390x), `x29` (Arm64EC) | The frame pointer cannot be used as an input or output. |
149155
| All | `r19` (Hexagon), `x19` (Arm64EC) | This is used internally by LLVM as a "base pointer" for functions with complex stack frames. |
150156
| MIPS | `$0` or `$zero` | This is a constant zero register which can't be modified. |
151157
| MIPS | `$1` or `$at` | Reserved for assembler. |
152158
| MIPS | `$26`/`$k0`, `$27`/`$k1` | OS-reserved registers. |
153159
| MIPS | `$28`/`$gp` | Global pointer cannot be used as inputs or outputs. |
154160
| MIPS | `$ra` | Return address cannot be used as inputs or outputs. |
155161
| Hexagon | `lr` | This is the link register which cannot be used as an input or output. |
162+
| PowerPC | `$r2`, `$r13` | These are system reserved registers. |
163+
| PowerPC | `$r29`, `$r30` | These are used internally by LLVM. |
164+
| PowerPC | `lr` | The link register cannot be used as an input or output. |
165+
| PowerPC | `ctr` | The counter register cannot be used as an input or output. |
166+
| PowerPC | `vrsave` | The vrsave register cannot be used as an input or output. |
156167
| AVR | `r0`, `r1`, `r1r0` | Due to an issue in LLVM, the `r0` and `r1` registers cannot be used as inputs or outputs. If modified, they must be restored to their original values before the end of the block. |
157168
|MSP430 | `r0`, `r2`, `r3` | These are the program counter, status register, and constant generator respectively. Neither the status register nor constant generator can be written to. |
158169
| M68k | `a4`, `a5` | Used internally by LLVM for the base pointer and global base pointer. |

tests/codegen/asm-powerpc-clobbers.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
//@ revisions: powerpc powerpc64 powerpc64le
1+
//@ revisions: powerpc powerpc64 powerpc64le aix
22
//@[powerpc] compile-flags: --target powerpc-unknown-linux-gnu
33
//@[powerpc] needs-llvm-components: powerpc
44
//@[powerpc64] compile-flags: --target powerpc64-unknown-linux-gnu
55
//@[powerpc64] needs-llvm-components: powerpc
66
//@[powerpc64le] compile-flags: --target powerpc64le-unknown-linux-gnu
77
//@[powerpc64le] needs-llvm-components: powerpc
8+
//@[aix] compile-flags: --target powerpc64-ibm-aix
9+
//@[aix] needs-llvm-components: powerpc
810

911
#![crate_type = "rlib"]
1012
#![feature(no_core, rustc_attrs, lang_items, asm_experimental_arch)]
@@ -22,26 +24,40 @@ macro_rules! asm {
2224
// CHECK: call void asm sideeffect "", "~{cr}"()
2325
#[no_mangle]
2426
pub unsafe fn cr_clobber() {
25-
asm!("", out("cr") _, options(nostack, nomem));
27+
asm!("", out("cr") _, options(nostack, nomem, preserves_flags));
2628
}
2729

2830
// CHECK-LABEL: @cr0_clobber
2931
// CHECK: call void asm sideeffect "", "~{cr0}"()
3032
#[no_mangle]
3133
pub unsafe fn cr0_clobber() {
32-
asm!("", out("cr0") _, options(nostack, nomem));
34+
asm!("", out("cr0") _, options(nostack, nomem, preserves_flags));
3335
}
3436

3537
// CHECK-LABEL: @cr5_clobber
3638
// CHECK: call void asm sideeffect "", "~{cr5}"()
3739
#[no_mangle]
3840
pub unsafe fn cr5_clobber() {
39-
asm!("", out("cr5") _, options(nostack, nomem));
41+
asm!("", out("cr5") _, options(nostack, nomem, preserves_flags));
4042
}
4143

4244
// CHECK-LABEL: @xer_clobber
4345
// CHECK: call void asm sideeffect "", "~{xer}"()
4446
#[no_mangle]
4547
pub unsafe fn xer_clobber() {
46-
asm!("", out("xer") _, options(nostack, nomem));
48+
asm!("", out("xer") _, options(nostack, nomem, preserves_flags));
49+
}
50+
51+
// CHECK-LABEL: @v0_clobber
52+
// CHECK: call void asm sideeffect "", "~{v0}"()
53+
#[no_mangle]
54+
pub unsafe fn v0_clobber() {
55+
asm!("", out("v0") _, options(nostack, nomem, preserves_flags));
56+
}
57+
58+
// CHECK-LABEL: @clobber_abi
59+
// CHECK: asm sideeffect "", "={r0},={r3},={r4},={r5},={r6},={r7},={r8},={r9},={r10},={r11},={r12},={f0},={f1},={f2},={f3},={f4},={f5},={f6},={f7},={f8},={f9},={f10},={f11},={f12},={f13},~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9},~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},~{v16},~{v17},~{v18},~{v19},~{cr0},~{cr1},~{cr5},~{cr6},~{cr7},~{xer}"()
60+
#[no_mangle]
61+
pub unsafe fn clobber_abi() {
62+
asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags));
4763
}

0 commit comments

Comments
 (0)