Skip to content

Preserve whole-aggregate stack passing when x86_64 SysV argument registers are exhausted #670

Closed

Description

@LunaStev

Reproduced incorrect result

Audit baseline: master 0c32b946, Linux x86_64, wavec 0.2.1-pre-beta-dev, LLVM/Clang 21.1.8. Native execution reproduces at both -O0 and -O2 in both C-to-Wave and Wave-to-C calls:

C -> Wave: expected 650, observed 415
Wave -> C: expected 650, observed 415
exit: 42

Seven scalar float arguments leave one SSE argument register available. A following 16-byte four-float struct requires two SSE slots. Clang passes the whole aggregate in memory and uses the remaining register for the trailing scalar; Wave flattens the struct into two independent vector arguments.

Reproducer

Save as repro.wave:

struct Hfa {
    a: f32;
    b: f32;
    c: f32;
    d: f32;
}
extern(c) fun c_sum(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32, g: f32, value: Hfa, tail: f32) -> f32;
extern(c) fun c_check() -> i32;
export(c) fun wave_sum(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32, g: f32, value: Hfa, tail: f32) -> f32 {
    return a + b * 2.0 + c * 3.0 + d * 4.0 + e * 5.0 + f * 6.0 + g * 7.0 + value.a * 8.0 + value.b * 9.0 + value.c * 10.0 + value.d * 11.0 + tail * 12.0;
}
fun main() -> i32 {
    var reverse: i32 = c_check();
    var result: f32 = c_sum(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, Hfa { a: 8.0, b: 9.0, c: 10.0, d: 11.0 }, 12.0);
    if (result != 650.0) {
        return 42;
    }
    return reverse;
}

Save as peer.c:

#include <stdio.h>
typedef struct { float a, b, c, d; } Hfa;
extern float wave_sum(float, float, float, float, float, float, float, Hfa, float);
float c_sum(float a, float b, float c, float d, float e, float f, float g, Hfa value, float tail) {
    float result = a + b * 2 + c * 3 + d * 4 + e * 5 + f * 6 + g * 7
        + value.a * 8 + value.b * 9 + value.c * 10 + value.d * 11 + tail * 12;
    printf("Wave -> C: expected 650, observed %.9g\n", result);
    return result;
}
int c_check(void) {
    float value = wave_sum(1, 2, 3, 4, 5, 6, 7, (Hfa){8,9,10,11}, 12);
    printf("C -> Wave: expected 650, observed %.9g\n", value);
    return value == 650 ? 0 : 41;
}
clang-21 -O0 -c peer.c -o peer.o
target/debug/wavec build repro.wave peer.o --target x86_64-unknown-linux-gnu -O0 --emit=ir,obj,bin --out-dir out -o out/repro
./out/repro

Repeat with both compilations at -O2. Expected exit is zero and both printed results are 650.

Code and IR evidence

SysV parameter classification classifies parameters independently; signature construction has register accounting for LoongArch but no equivalent SysV argument-budget decision.

For this signature, Clang emits ptr byval(%struct.Hfa) align 8 after the first seven floats. Wave instead emits <2 x float>, <2 x float>, losing aggregate grouping at the register/stack boundary. This explains the observed mismatch; it is not an optimizer-only failure.

Scope and completion criteria

Difficulty: high. Correct SysV argument classification using the complete signature and register availability, including any hidden return argument. Preserve the whole-aggregate fallback and remaining-register accounting consistently for calls and exported wrappers.

  • The supplied executable passes in both directions at O0/O2 against a separately compiled Clang peer.
  • Add boundary cases around available SSE and integer argument registers, including mixed-class aggregates and trailing scalars, with independent expected results.
  • Compare representative LLVM signatures with Clang and execute Linux x64 regressions.
  • Check other targets sharing this classifier (Darwin/FreeBSD x64) without claiming their native failure has already been reproduced.
  • Preserve MSVC, ARM64 and LoongArch ABI behavior; do not work around the defect in the fixture or std library.

Related to the wider ABI validation work in #350. The MSVC fixture in #668 exposed this separate SysV defect during cross-target audit; native MSVC success does not cover it.

Activity

  1. added
    bug
    cross-platform
    on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcross-platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions