Skip to content

Commit ad34a0e

Browse files
Rollup merge of #165465 - beetrees:va-arg-m68k, r=folkertdev
Use `emit_ptr_va_arg` for `va_arg` on M68k This avoids using the LLVM `va_arg` instruction. AFAICT LLVM's implementation currently miscompiles the `va_arg` instruction for `f64` (and needlessly aligns the pointer that is guaranteed to be aligned for `i32` and `i64`) - this PR matches GCC (checked by comparing the assembly). Ping target maintainers: @glaubitz @ricky26 @knickish r? @folkertdev
2 parents a10a9da + 1c60721 commit ad34a0e

3 files changed

Lines changed: 76 additions & 11 deletions

File tree

‎compiler/rustc_codegen_llvm/src/builder.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,10 +1842,6 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
18421842

18431843
Cow::Owned(casted_args)
18441844
}
1845-
1846-
pub(crate) fn va_arg(&mut self, list: Value<'ll>, ty: Type<'ll>) -> Value<'ll> {
1847-
unsafe { llvm::LLVMBuildVAArg(self.llbuilder(), list, ty, UNNAMED) }
1848-
}
18491845
}
18501846

18511847
impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {

‎compiler/rustc_codegen_llvm/src/va_arg.rs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,17 +1313,19 @@ pub(super) fn emit_va_arg<'ll, 'tcx>(
13131313
AllowHigherAlign::No,
13141314
ForceRightAdjust::No,
13151315
),
1316+
Arch::M68k => emit_ptr_va_arg(
1317+
bx,
1318+
addr,
1319+
layout,
1320+
PassMode::Direct,
1321+
SlotSize::Bytes4,
1322+
AllowHigherAlign::No,
1323+
ForceRightAdjust::Yes,
1324+
),
13161325

13171326
Arch::Bpf => bug!("bpf does not support c-variadic functions"),
13181327
Arch::SpirV => bug!("spirv does not support c-variadic functions"),
13191328

1320-
Arch::M68k => {
1321-
std::assert_matches!(stability, CVariadicStatus::Unstable { .. });
1322-
1323-
// Clang uses the LLVM implementation for these architectures.
1324-
bx.va_arg(addr.immediate(), layout.llvm_type(bx.cx))
1325-
}
1326-
13271329
Arch::Other(ref arch) => {
13281330
std::assert_matches!(stability, CVariadicStatus::Unstable { .. });
13291331

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//@ add-minicore
2+
//@ assembly-output: emit-asm
3+
//
4+
//@ compile-flags: -Copt-level=3 --target=m68k-unknown-linux-gnu
5+
//@ needs-llvm-components: m68k
6+
#![feature(c_variadic_experimental_arch, no_core, lang_items, intrinsics, rustc_attrs)]
7+
#![no_core]
8+
#![crate_type = "lib"]
9+
10+
extern crate minicore;
11+
use minicore::ffi::VaList;
12+
use minicore::*;
13+
14+
// This checks `va_arg` does the same thing as GCC.
15+
16+
#[unsafe(no_mangle)]
17+
unsafe extern "C" fn read_f64(ap: &mut VaList<'_>) -> f64 {
18+
// CHECK-LABEL: read_f64
19+
//
20+
// CHECK: move.l (4,%sp), %a0
21+
// CHECK-NEXT: move.l (%a0), %d0
22+
// CHECK-NEXT: move.l %d0, %d1
23+
// CHECK-NEXT: add.l #8, %d1
24+
// CHECK-NEXT: move.l %d1, (%a0)
25+
// CHECK-NEXT: move.l %d0, %a0
26+
// CHECK-NEXT: move.l (%a0), %d0
27+
// CHECK-NEXT: move.l (4,%a0), %d1
28+
// CHECK-NEXT: rts
29+
ap.next_arg()
30+
}
31+
32+
#[unsafe(no_mangle)]
33+
unsafe extern "C" fn read_i32(ap: &mut VaList<'_>) -> i32 {
34+
// CHECK-LABEL: read_i32
35+
//
36+
// CHECK: move.l (4,%sp), %a0
37+
// CHECK-NEXT: move.l (%a0), %d0
38+
// CHECK-NEXT: move.l %d0, %d1
39+
// CHECK-NEXT: add.l #4, %d1
40+
// CHECK-NEXT: move.l %d1, (%a0)
41+
// CHECK-NEXT: move.l %d0, %a0
42+
// CHECK-NEXT: move.l (%a0), %d0
43+
// CHECK-NEXT: rts
44+
ap.next_arg()
45+
}
46+
47+
#[unsafe(no_mangle)]
48+
unsafe extern "C" fn read_i64(ap: &mut VaList<'_>) -> i64 {
49+
// CHECK-LABEL: read_i64
50+
//
51+
// CHECK: move.l (4,%sp), %a0
52+
// CHECK-NEXT: move.l (%a0), %d0
53+
// CHECK-NEXT: move.l %d0, %d1
54+
// CHECK-NEXT: add.l #8, %d1
55+
// CHECK-NEXT: move.l %d1, (%a0)
56+
// CHECK-NEXT: move.l %d0, %a0
57+
// CHECK-NEXT: move.l (%a0), %d0
58+
// CHECK-NEXT: move.l (4,%a0), %d1
59+
// CHECK-NEXT: rts
60+
ap.next_arg()
61+
}
62+
63+
#[unsafe(no_mangle)]
64+
unsafe extern "C" fn read_ptr(ap: &mut VaList<'_>) -> *const u8 {
65+
// CHECK: read_ptr = read_i32
66+
ap.next_arg()
67+
}

0 commit comments

Comments
 (0)