Skip to content

Commit 46be365

Browse files
authored
Rollup merge of rust-lang#146831 - taiki-e:powerpc-clobber, r=Amanieu
Support ctr and lr as clobber-only registers in PowerPC inline assembly Follow-up to rust-lang#131341. CTR and LR are marked as volatile in all ABIs, but I skipped them in rust-lang#131341 due to they are currently marked as reserved. https://github.com/rust-lang/rust/blob/dd7fda570040e8a736f7d8bc28ddd1b444aabc82/compiler/rustc_target/src/asm/powerpc.rs#L209-L212 However, they are actually only unusable as input/output of inline assembly, and should be fine to support as clobber-only registers as discussed in [#t-compiler > ppc/ppc64 inline asm support](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/ppc.2Fppc64.20inline.20asm.20support/with/540413845). r? ````@Amanieu```` or ````@workingjubilee```` cc ````@programmerjake```` ````@rustbot```` label +O-PowerPC +A-inline-assembly
2 parents 40db498 + f4b8768 commit 46be365

File tree

12 files changed

+572
-189
lines changed

12 files changed

+572
-189
lines changed

compiler/rustc_codegen_gcc/src/asm.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,12 @@ fn reg_class_to_gcc(reg_class: InlineAsmRegClass) -> &'static str {
698698
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => "b",
699699
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::freg) => "f",
700700
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::vreg) => "v",
701-
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::cr)
702-
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer) => {
701+
InlineAsmRegClass::PowerPC(
702+
PowerPCInlineAsmRegClass::cr
703+
| PowerPCInlineAsmRegClass::ctr
704+
| PowerPCInlineAsmRegClass::lr
705+
| PowerPCInlineAsmRegClass::xer,
706+
) => {
703707
unreachable!("clobber-only")
704708
}
705709
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => "r",
@@ -777,8 +781,12 @@ fn dummy_output_type<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, reg: InlineAsmRegCl
777781
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::vreg) => {
778782
cx.type_vector(cx.type_i32(), 4)
779783
}
780-
InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::cr)
781-
| InlineAsmRegClass::PowerPC(PowerPCInlineAsmRegClass::xer) => {
784+
InlineAsmRegClass::PowerPC(
785+
PowerPCInlineAsmRegClass::cr
786+
| PowerPCInlineAsmRegClass::ctr
787+
| PowerPCInlineAsmRegClass::lr
788+
| PowerPCInlineAsmRegClass::xer,
789+
) => {
782790
unreachable!("clobber-only")
783791
}
784792
InlineAsmRegClass::RiscV(RiscVInlineAsmRegClass::reg) => cx.type_i32(),

compiler/rustc_codegen_llvm/src/asm.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,12 @@ fn reg_to_llvm(reg: InlineAsmRegOrRegClass, layout: Option<&TyAndLayout<'_>>) ->
662662
PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => "b",
663663
PowerPC(PowerPCInlineAsmRegClass::freg) => "f",
664664
PowerPC(PowerPCInlineAsmRegClass::vreg) => "v",
665-
PowerPC(PowerPCInlineAsmRegClass::cr) | PowerPC(PowerPCInlineAsmRegClass::xer) => {
665+
PowerPC(
666+
PowerPCInlineAsmRegClass::cr
667+
| PowerPCInlineAsmRegClass::ctr
668+
| PowerPCInlineAsmRegClass::lr
669+
| PowerPCInlineAsmRegClass::xer,
670+
) => {
666671
unreachable!("clobber-only")
667672
}
668673
RiscV(RiscVInlineAsmRegClass::reg) => "r",
@@ -830,7 +835,12 @@ fn dummy_output_type<'ll>(cx: &CodegenCx<'ll, '_>, reg: InlineAsmRegClass) -> &'
830835
PowerPC(PowerPCInlineAsmRegClass::reg_nonzero) => cx.type_i32(),
831836
PowerPC(PowerPCInlineAsmRegClass::freg) => cx.type_f64(),
832837
PowerPC(PowerPCInlineAsmRegClass::vreg) => cx.type_vector(cx.type_i32(), 4),
833-
PowerPC(PowerPCInlineAsmRegClass::cr) | PowerPC(PowerPCInlineAsmRegClass::xer) => {
838+
PowerPC(
839+
PowerPCInlineAsmRegClass::cr
840+
| PowerPCInlineAsmRegClass::ctr
841+
| PowerPCInlineAsmRegClass::lr
842+
| PowerPCInlineAsmRegClass::xer,
843+
) => {
834844
unreachable!("clobber-only")
835845
}
836846
RiscV(RiscVInlineAsmRegClass::reg) => cx.type_i32(),

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ symbols! {
794794
ctlz,
795795
ctlz_nonzero,
796796
ctpop,
797+
ctr,
797798
cttz,
798799
cttz_nonzero,
799800
custom_attribute,
@@ -1333,6 +1334,7 @@ symbols! {
13331334
loongarch_target_feature,
13341335
loop_break_value,
13351336
loop_match,
1337+
lr,
13361338
lt,
13371339
m68k_target_feature,
13381340
macro_at_most_once_rep,

compiler/rustc_target/src/asm/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,11 +1260,12 @@ impl InlineAsmClobberAbi {
12601260
v8, v9, v10, v11, v12, v13, v14,
12611261
v15, v16, v17, v18, v19,
12621262

1263-
// cr0-cr1, cr5-cr7, xer
1263+
// cr0-cr1, cr5-cr7, ctr, lr, xer
12641264
cr0, cr1,
12651265
cr5, cr6, cr7,
1266+
ctr,
1267+
lr,
12661268
xer,
1267-
// lr and ctr are reserved
12681269
}
12691270
},
12701271
InlineAsmClobberAbi::S390x => clobbered_regs! {

compiler/rustc_target/src/asm/powerpc.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ def_reg_class! {
1313
freg,
1414
vreg,
1515
cr,
16+
ctr,
17+
lr,
1618
xer,
1719
}
1820
}
@@ -56,7 +58,7 @@ impl PowerPCInlineAsmRegClass {
5658
altivec: VecI8(16), VecI16(8), VecI32(4), VecF32(4);
5759
vsx: F32, F64, VecI64(2), VecF64(2);
5860
},
59-
Self::cr | Self::xer => &[],
61+
Self::cr | Self::ctr | Self::lr | Self::xer => &[],
6062
}
6163
}
6264
}
@@ -195,6 +197,8 @@ def_regs! {
195197
cr5: cr = ["cr5"],
196198
cr6: cr = ["cr6"],
197199
cr7: cr = ["cr7"],
200+
ctr: ctr = ["ctr"],
201+
lr: lr = ["lr"],
198202
xer: xer = ["xer"],
199203
#error = ["r1", "1", "sp"] =>
200204
"the stack pointer cannot be used as an operand for inline asm",
@@ -206,10 +210,6 @@ def_regs! {
206210
"r30 is used internally by LLVM and cannot be used as an operand for inline asm",
207211
#error = ["r31", "31", "fp"] =>
208212
"the frame pointer cannot be used as an operand for inline asm",
209-
#error = ["lr"] =>
210-
"the link register cannot be used as an operand for inline asm",
211-
#error = ["ctr"] =>
212-
"the counter register cannot be used as an operand for inline asm",
213213
#error = ["vrsave"] =>
214214
"the vrsave register cannot be used as an operand for inline asm",
215215
}
@@ -247,6 +247,8 @@ impl PowerPCInlineAsmReg {
247247
(v24, "24"), (v25, "25"), (v26, "26"), (v27, "27"), (v28, "28"), (v29, "29"), (v30, "30"), (v31, "31");
248248
(cr, "cr");
249249
(cr0, "0"), (cr1, "1"), (cr2, "2"), (cr3, "3"), (cr4, "4"), (cr5, "5"), (cr6, "6"), (cr7, "7");
250+
(ctr, "ctr");
251+
(lr, "lr");
250252
(xer, "xer");
251253
}
252254
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ This feature tracks `asm!` and `global_asm!` support for the following architect
3636
| PowerPC | `freg` | `f[0-31]` | `f` |
3737
| PowerPC | `vreg` | `v[0-31]` | `v` |
3838
| PowerPC | `cr` | `cr[0-7]`, `cr` | Only clobbers |
39+
| PowerPC | `ctr` | `ctr` | Only clobbers |
40+
| PowerPC | `lr` | `lr` | Only clobbers |
3941
| PowerPC | `xer` | `xer` | Only clobbers |
4042
| wasm32 | `local` | None\* | `r` |
4143
| BPF | `reg` | `r[0-10]` | `r` |
@@ -78,6 +80,8 @@ This feature tracks `asm!` and `global_asm!` support for the following architect
7880
| PowerPC | `vreg` | `altivec` | `i8x16`, `i16x8`, `i32x4`, `f32x4` |
7981
| PowerPC | `vreg` | `vsx` | `f32`, `f64`, `i64x2`, `f64x2` |
8082
| PowerPC | `cr` | N/A | Only clobbers |
83+
| PowerPC | `ctr` | N/A | Only clobbers |
84+
| PowerPC | `lr` | N/A | Only clobbers |
8185
| PowerPC | `xer` | N/A | Only clobbers |
8286
| wasm32 | `local` | None | `i8` `i16` `i32` `i64` `f32` `f64` |
8387
| BPF | `reg` | None | `i8` `i16` `i32` `i64` |
@@ -150,8 +154,6 @@ This feature tracks `asm!` and `global_asm!` support for the following architect
150154
| MIPS | `$ra` | Return address cannot be used as inputs or outputs. |
151155
| Hexagon | `lr` | This is the link register which cannot be used as an input or output. |
152156
| PowerPC | `r2`, `r13` | These are system reserved registers. |
153-
| PowerPC | `lr` | The link register cannot be used as an input or output. |
154-
| PowerPC | `ctr` | The counter register cannot be used as an input or output. |
155157
| PowerPC | `vrsave` | The vrsave register cannot be used as an input or output. |
156158
| 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. |
157159
|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. |

tests/codegen-llvm/asm/powerpc-clobbers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ pub unsafe fn v0_clobber() {
5858

5959
// Output format depends on the availability of altivec.
6060
// CHECK-LABEL: @clobber_abi
61-
// powerpc: 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}"()
62-
// powerpc64: 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}"()
63-
// powerpc64le: 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}"()
64-
// aix64: 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}"()
61+
// powerpc: 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},~{ctr},~{lr},~{xer}"()
62+
// powerpc64: 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},~{ctr},~{lr},~{xer}"()
63+
// powerpc64le: 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},~{ctr},~{lr},~{xer}"()
64+
// aix64: 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},~{ctr},~{lr},~{xer}"()
6565
#[no_mangle]
6666
pub unsafe fn clobber_abi() {
6767
asm!("", clobber_abi("C"), options(nostack, nomem, preserves_flags));

0 commit comments

Comments
 (0)